Send all segments when expanding a move

This commit is contained in:
Trevor Slocum 2023-09-18 19:51:29 -07:00
parent 92a8adec01
commit d351bb7b88
2 changed files with 18 additions and 12 deletions

View file

@ -654,7 +654,8 @@ COMMANDS:
moves = append(moves, []int{from, to})
}
if !clientGame.AddMoves(moves) {
ok, expandedMoves := clientGame.AddMoves(moves)
if !ok {
cmd.client.sendEvent(&bgammon.EventFailedMove{
From: 0,
To: 0,
@ -675,7 +676,7 @@ COMMANDS:
clientGame.eachClient(func(client *serverClient) {
ev := &bgammon.EventMoved{
Moves: bgammon.FlipMoves(moves, client.playerNumber),
Moves: bgammon.FlipMoves(expandedMoves, client.playerNumber),
}
ev.Player = string(cmd.client.name)
client.sendEvent(ev)
@ -706,7 +707,8 @@ COMMANDS:
for i, move := range clientGame.Moves {
undoMoves[l-1-i] = []int{move[1], move[0]}
}
if !clientGame.AddMoves(undoMoves) {
ok, _ := clientGame.AddMoves(undoMoves)
if !ok {
cmd.client.sendNotice("Failed to undo move: invalid move.")
} else {
clientGame.eachClient(func(client *serverClient) {

22
game.go
View file

@ -179,9 +179,9 @@ func (g *Game) ExpandMove(move []int, currentSpace int, moves [][]int) ([][]int,
}
// AddMoves adds moves to the game state. Adding a backwards move will remove the equivalent existing move.
func (g *Game) AddMoves(moves [][]int) bool {
func (g *Game) AddMoves(moves [][]int) (bool, [][]int) {
if g.Player1.Name == "" || g.Player2.Name == "" || g.Winner != 0 {
return false
return false, nil
}
var addMoves [][]int
@ -203,7 +203,7 @@ VALIDATEMOVES:
if len(gameCopy.Moves) > 0 {
i := len(gameCopy.Moves) - 1 - validateOffset
if i < 0 {
return false
return false, nil
}
gameMove := gameCopy.Moves[i]
if move[0] == gameMove[1] && move[1] == gameMove[0] {
@ -221,11 +221,11 @@ VALIDATEMOVES:
continue VALIDATEMOVES
}
return false
return false, nil
}
if len(addMoves) != 0 && len(undoMoves) != 0 {
return false
return false, nil
}
var checkWin bool
@ -235,7 +235,7 @@ ADDMOVES:
for _, lm := range l {
if lm[0] == move[0] && lm[1] == move[1] {
if !gameCopy.addMove(move) {
return false
return false, nil
}
if move[1] == SpaceHomePlayer || move[1] == SpaceHomeOpponent {
@ -249,7 +249,7 @@ ADDMOVES:
if len(gameCopy.Moves) > 0 {
i := len(gameCopy.Moves) - 1
if i < 0 {
return false
return false, nil
}
gameMove := gameCopy.Moves[i]
if move[0] == gameMove[1] && move[1] == gameMove[0] {
@ -260,7 +260,7 @@ ADDMOVES:
continue
}
}
return false
return false, nil
}
g.Board = gameCopy.Board
@ -280,7 +280,11 @@ ADDMOVES:
}
}
return true
if len(addMoves) > 0 {
return true, addMoves
} else {
return true, undoMoves
}
}
func (g *Game) LegalMoves() [][]int {