2022-12-14 21:19:47 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
)
|
|
|
|
|
|
|
|
type chessGame struct {
|
|
|
|
id int
|
|
|
|
players [2]Player
|
|
|
|
currentTurnPlayer Player
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2022-12-18 00:05:23 +00:00
|
|
|
WhiteToMove = 0
|
|
|
|
BlackToMove = 1
|
|
|
|
CheckPlayerChange = 2
|
2022-12-14 21:19:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewChessGame(gameId int, players [2]Player) *chessGame {
|
|
|
|
var game chessGame = chessGame{
|
|
|
|
players: players,
|
|
|
|
id: gameId,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &game
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *chessGame) handle() {
|
|
|
|
|
2022-12-18 00:05:23 +00:00
|
|
|
gameState := WhiteToMove
|
2022-12-14 21:19:47 +00:00
|
|
|
game.currentTurnPlayer = game.players[0]
|
|
|
|
var move *chessMove
|
|
|
|
var messageType int
|
|
|
|
var receivedMessage []byte
|
|
|
|
|
2022-12-18 00:05:23 +00:00
|
|
|
WriteMessageToPlayer(&game.players[0], []byte("bd init"), 1)
|
|
|
|
WriteMessageToPlayer(&game.players[1], []byte("bd init"), 1)
|
|
|
|
|
|
|
|
WriteMessageToPlayer(&game.players[0], []byte("cl white"), 1)
|
|
|
|
WriteMessageToPlayer(&game.players[1], []byte("cl black"), 1)
|
2022-12-14 21:19:47 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
switch gameState {
|
2022-12-18 00:05:23 +00:00
|
|
|
case WhiteToMove:
|
2022-12-14 21:19:47 +00:00
|
|
|
var err error
|
2022-12-18 00:05:23 +00:00
|
|
|
messageType, receivedMessage, err = ReadMessageFromPlayer(&game.players[0])
|
2022-12-14 21:19:47 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Error during message reading:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
move, err = parseMove(string(receivedMessage))
|
2022-12-14 22:16:51 +00:00
|
|
|
log.Println("Player 0 moved: ", move)
|
|
|
|
|
2022-12-14 21:19:47 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Game: ", game.id, err)
|
|
|
|
return
|
|
|
|
}
|
2022-12-18 00:05:23 +00:00
|
|
|
gameState = CheckPlayerChange
|
2022-12-14 21:19:47 +00:00
|
|
|
|
2022-12-18 00:05:23 +00:00
|
|
|
case BlackToMove:
|
2022-12-14 21:19:47 +00:00
|
|
|
var err error
|
2022-12-18 00:05:23 +00:00
|
|
|
messageType, receivedMessage, err = ReadMessageFromPlayer(&game.players[1])
|
2022-12-14 21:19:47 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Error during message reading:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
move, err = parseMove(string(receivedMessage))
|
2022-12-14 22:16:51 +00:00
|
|
|
log.Println("Player 1 moved: ", move)
|
|
|
|
|
2022-12-14 21:19:47 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Game: ", game.id, err)
|
|
|
|
return
|
|
|
|
}
|
2022-12-18 00:05:23 +00:00
|
|
|
gameState = CheckPlayerChange
|
2022-12-14 21:19:47 +00:00
|
|
|
|
2022-12-18 00:05:23 +00:00
|
|
|
case CheckPlayerChange:
|
2022-12-14 21:19:47 +00:00
|
|
|
|
|
|
|
if move.realMove {
|
2022-12-14 22:16:51 +00:00
|
|
|
if game.currentTurnPlayer.id == game.players[0].id {
|
2022-12-14 21:19:47 +00:00
|
|
|
game.currentTurnPlayer = game.players[1]
|
2022-12-18 00:05:23 +00:00
|
|
|
gameState = BlackToMove
|
2022-12-14 21:19:47 +00:00
|
|
|
} else {
|
|
|
|
game.currentTurnPlayer = game.players[0]
|
2022-12-18 00:05:23 +00:00
|
|
|
gameState = WhiteToMove
|
2022-12-14 21:19:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-18 22:13:25 +00:00
|
|
|
err := WriteMessageToPlayer(&game.players[0], receivedMessage, messageType)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error during message writing:", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = WriteMessageToPlayer(&game.players[1], receivedMessage, messageType)
|
2022-12-14 21:19:47 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Error during message writing:", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} else {
|
2022-12-14 22:16:51 +00:00
|
|
|
if game.currentTurnPlayer.id == game.players[0].id {
|
2022-12-18 00:05:23 +00:00
|
|
|
gameState = WhiteToMove
|
2022-12-14 21:19:47 +00:00
|
|
|
} else {
|
2022-12-18 00:05:23 +00:00
|
|
|
gameState = BlackToMove
|
2022-12-14 21:19:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("GameState = ", gameState)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func addPlayersToGame(players [2]Player) {
|
2022-12-18 00:41:44 +00:00
|
|
|
log.Printf("Adding players %d and %d to new game", players[0].id, players[1].id)
|
2022-12-14 21:19:47 +00:00
|
|
|
game := NewChessGame(rand.Int(), players)
|
|
|
|
|
|
|
|
go game.handle()
|
|
|
|
}
|