diff --git a/chess/game.go b/chess/game.go index f082098..7ac3ceb 100644 --- a/chess/game.go +++ b/chess/game.go @@ -58,8 +58,8 @@ func (game Game) GetPlayer2() *Player { } func (game *Game) prepare() { - game.players[0].color = types.White - game.players[1].color = types.Black + game.players[0].SetColor(types.White) + game.players[1].SetColor(types.Black) game.currentTurnPlayer = game.GetPlayer1() @@ -99,7 +99,7 @@ func (game *Game) Handle() { game.gameState = PlayerToMove case PlayerToMove: - log.Println("with ", game.currentTurnPlayer.GetPlayerColor(), " to move") + log.Println("with ", game.currentTurnPlayer.GetColor(), " to move") receivedMove, err = game.currentTurnPlayer.ReadMove() if err != nil { log.Println("Error while reading message:", err) @@ -182,12 +182,12 @@ func (game Game) notifyPlayersAboutGameStart() error { } func (game Game) broadcastMove(move types.Move) error { - err := game.GetPlayer1().SendBoardState(move, game.board.PGN(), game.currentTurnPlayer.color) + err := game.GetPlayer1().SendBoardState(move, game.board.PGN(), game.currentTurnPlayer.GetColor()) if err != nil { return err } - err = game.GetPlayer2().SendBoardState(move, game.board.PGN(), game.currentTurnPlayer.color) + err = game.GetPlayer2().SendBoardState(move, game.board.PGN(), game.currentTurnPlayer.GetColor()) if err != nil { return err } diff --git a/chess/player.go b/chess/player.go index d674377..ea8d6b5 100644 --- a/chess/player.go +++ b/chess/player.go @@ -48,6 +48,14 @@ func (p *Player) SetWebsocketConnectionAndSendBoardState( p.SendBoardState(types.Move{}, boardPosition, turnColor) } +func (p *Player) SetColor(color types.ChessColor) { + p.color = color +} + +func (p *Player) GetColor() types.ChessColor { + return p.color +} + func (p *Player) SetDisconnectCallback(cb func(*Player)) { // Todo: Fucking complicated p.Conn.SetDisconnectCallback(p.PlayerDisconnectedCallback) @@ -63,8 +71,8 @@ func (p *Player) IsInGame() bool { } func (p *Player) SendBoardState(move types.Move, boardPosition string, turnColor types.ChessColor) error { - var pColor = p.color - if p.color == "" { // we default to white if we do not know the color yet + var pColor = p.GetColor() + if p.GetColor() == "" { // we default to white if we do not know the color yet pColor = types.White } @@ -154,7 +162,3 @@ func (p *Player) readMessage() ([]byte, error) { return msg, err } - -func (p Player) GetPlayerColor() string { - return string(p.color) -} diff --git a/types/common.go b/types/common.go index a085bd0..ecf14e3 100644 --- a/types/common.go +++ b/types/common.go @@ -16,6 +16,10 @@ func (c ChessColor) Opposite() ChessColor { } } +func (c ChessColor) String() string { + return string(c) +} + type AdditionalState struct { BlackKingMoved bool WhiteKingMoved bool