Flip second player's board. Use player's id for comparison.

This commit is contained in:
Marco 2022-12-14 23:16:51 +01:00
parent a169ff0b76
commit 3a2de183a1
4 changed files with 12 additions and 11 deletions

View File

@ -2,7 +2,6 @@ package server
import (
"errors"
"log"
"strings"
)
@ -21,7 +20,6 @@ func parseMove(received string) (*chessMove, error) {
var move chessMove
splitReceived := strings.Split(received, " ")
log.Println(splitReceived)
realMoveString := splitReceived[1]
startSquareString := splitReceived[2]

View File

@ -34,7 +34,7 @@ func (game *chessGame) handle() {
var messageType int
var receivedMessage []byte
game.players[0].conn.WriteMessage(1, []byte("fb"))
game.players[1].conn.WriteMessage(1, []byte("fb"))
for {
@ -47,6 +47,8 @@ func (game *chessGame) handle() {
return
}
move, err = parseMove(string(receivedMessage))
log.Println("Player 0 moved: ", move)
if err != nil {
log.Println("Game: ", game.id, err)
return
@ -61,6 +63,8 @@ func (game *chessGame) handle() {
return
}
move, err = parseMove(string(receivedMessage))
log.Println("Player 1 moved: ", move)
if err != nil {
log.Println("Game: ", game.id, err)
return
@ -70,7 +74,7 @@ func (game *chessGame) handle() {
case Check_Player_Change:
if move.realMove {
if game.currentTurnPlayer == game.players[0] {
if game.currentTurnPlayer.id == game.players[0].id {
game.currentTurnPlayer = game.players[1]
gameState = Player1_Move
} else {
@ -84,7 +88,7 @@ func (game *chessGame) handle() {
break
}
} else {
if game.currentTurnPlayer == game.players[0] {
if game.currentTurnPlayer.id == game.players[0].id {
gameState = Player0_Move
} else {
gameState = Player1_Move
@ -98,7 +102,7 @@ func (game *chessGame) handle() {
}
func addPlayersToGame(players [2]Player) {
log.Printf("Adding players \n%s\n%s\nto new game", players[0].name, players[1].name)
log.Printf("Adding players \n%d\n%d\nto new game", players[0].id, players[1].id)
game := NewChessGame(rand.Int(), players)
go game.handle()

View File

@ -3,13 +3,13 @@ package server
import "github.com/gorilla/websocket"
type Player struct {
name string
id int
conn *websocket.Conn
}
func NewPlayer(name string, conn *websocket.Conn) *Player {
func NewPlayer(id int, conn *websocket.Conn) *Player {
return &Player{
name: name,
id: id,
conn: conn,
}
}

View File

@ -1,7 +1,6 @@
package server
import (
"fmt"
"log"
"net/http"
@ -34,6 +33,6 @@ func SocketHandler(w http.ResponseWriter, r *http.Request) {
conn.WriteMessage(msg_type, []byte("Password correct. Let's play"))
}
GetLobby().registerPlayer(NewPlayer("Player"+fmt.Sprint(player_number), conn))
GetLobby().registerPlayer(NewPlayer(player_number, conn))
player_number = player_number + 1
}