From d07892344f78a3f76fb75616ed0c51275310cebe Mon Sep 17 00:00:00 2001 From: billybolton Date: Mon, 4 May 2026 15:59:52 -0400 Subject: [PATCH 1/2] fix: mandatory docker release --- .github/workflows/release.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c90a4f6..daa73cf 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -98,3 +98,15 @@ jobs: -f 'client_payload[tag]=${{ steps.version.outputs.version }}' env: GH_TOKEN: ${{ secrets.GATEWAY_PAT }} + + gate: + if: always() + needs: [release-please, docker] + runs-on: ubuntu-latest + steps: + - name: Verify docker job succeeded when required + run: | + if [[ "${{ needs.release-please.outputs.releases_created }}" == "true" && "${{ needs.docker.result }}" != "success" ]]; then + echo "::error::docker job did not succeed (result: ${{ needs.docker.result }})" + exit 1 + fi From b094c418620472ccb0ea40fdf69f8849e145024e Mon Sep 17 00:00:00 2001 From: billybolton Date: Mon, 4 May 2026 18:03:37 -0400 Subject: [PATCH 2/2] 0 based game --- internal/game/coverage_test.go | 18 ++-- internal/game/inorder_player.go | 15 +-- internal/game/menace_game.go | 18 ++-- internal/game/menace_game_test.go | 132 +++++++++++------------ internal/game/menace_integration_test.go | 14 +-- internal/game/menace_player.go | 14 +-- internal/game/menace_player_test.go | 14 +-- internal/game/perfect_game.go | 5 +- internal/game/perfect_game_test.go | 16 +-- internal/game/perfect_player.go | 7 +- internal/game/perfect_player_test.go | 62 +++++------ internal/game/tictactoe.go | 28 ++--- internal/game/tictactoe_test.go | 56 +++++----- 13 files changed, 196 insertions(+), 203 deletions(-) diff --git a/internal/game/coverage_test.go b/internal/game/coverage_test.go index de57dd1..a2f0211 100644 --- a/internal/game/coverage_test.go +++ b/internal/game/coverage_test.go @@ -15,22 +15,22 @@ func TestInOrderPlayer(t *testing.T) { if !p.Play(game) { t.Error("expected play to succeed on empty board") } - if game.LastPlayedPosition != 1 { - t.Errorf("got position %d, want 1", game.LastPlayedPosition) + if game.LastPlayedPosition != 0 { + t.Errorf("got position %d, want 0", game.LastPlayedPosition) } if !p.Play(game) { t.Error("expected play to succeed") } - if game.LastPlayedPosition != 2 { - t.Errorf("got position %d, want 2", game.LastPlayedPosition) + if game.LastPlayedPosition != 1 { + t.Errorf("got position %d, want 1", game.LastPlayedPosition) } } func TestInOrderPlayerFullBoard(t *testing.T) { p := NewInOrderPlayer() game := NewTicTacToe(3, 3, 3) - for i := 1; i <= 9; i++ { + for i := 0; i < 9; i++ { game.Play(i) } if p.Play(game) { @@ -148,8 +148,8 @@ func TestPlayerSetName(t *testing.T) { func TestTicTacToeString(t *testing.T) { game := NewTicTacToe(3, 3, 3) - game.Play(1) // X at 1 - game.Play(5) // O at 5 + game.Play(0) // X at 0 + game.Play(4) // O at 4 s := game.String() if !strings.Contains(s, "X") { @@ -165,8 +165,8 @@ func TestTicTacToeString(t *testing.T) { func TestTicTacToeBoardState(t *testing.T) { game := NewTicTacToe(3, 3, 3) - game.Play(1) // X - game.Play(2) // O + game.Play(0) // X + game.Play(1) // O state := game.BoardState() if state[0] != 1 { diff --git a/internal/game/inorder_player.go b/internal/game/inorder_player.go index 67388bf..5224021 100644 --- a/internal/game/inorder_player.go +++ b/internal/game/inorder_player.go @@ -9,16 +9,11 @@ func NewInOrderPlayer() *InOrderPlayer { } func (p *InOrderPlayer) Play(game *TicTacToe) bool { - nextPosition := 0 - for { - nextPosition++ - v := game.ValueAt(nextPosition) - if v == CellInvalid { - return false - } else if v == CellEmpty { - break + for pos := 0; pos < game.NumRows*game.NumColumns; pos++ { + if game.ValueAt(pos) == CellEmpty { + game.Play(pos) + return true } } - game.Play(nextPosition) - return true + return false } diff --git a/internal/game/menace_game.go b/internal/game/menace_game.go index dc53536..95916cb 100644 --- a/internal/game/menace_game.go +++ b/internal/game/menace_game.go @@ -24,24 +24,24 @@ func NewMenaceGame(game *TicTacToe) *MenaceGame { func (mg *MenaceGame) SetOutcome(outcome GameOutcome) { switch outcome { case OutcomeWin: - mg.BoardOdds[mg.CurrentPosition-1] += 3 + mg.BoardOdds[mg.CurrentPosition] += 3 mg.TotalOdds += 3 case OutcomeLose: if mg.TotalOdds == 1 { // Do not remove the last bead } else { - mg.BoardOdds[mg.CurrentPosition-1] -= 1 + mg.BoardOdds[mg.CurrentPosition] -= 1 mg.TotalOdds -= 1 } case OutcomeDraw: - mg.BoardOdds[mg.CurrentPosition-1] += 1 + mg.BoardOdds[mg.CurrentPosition] += 1 mg.TotalOdds += 1 } } func (mg *MenaceGame) PickCurrentPosition() int { if mg.fullBoard() { - return 0 + return -1 } mg.CurrentPosition = mg.CalculatePosition(mg.RollDice()) return mg.CurrentPosition @@ -49,23 +49,23 @@ func (mg *MenaceGame) PickCurrentPosition() int { func (mg *MenaceGame) RollDice() int { if mg.fullBoard() { - return 0 + return -1 } return rand.Intn(mg.TotalOdds) + 1 } func (mg *MenaceGame) CalculatePosition(diceRoll int) int { if mg.fullBoard() { - return 0 + return -1 } for i := 0; i < len(mg.BoardOdds); i++ { diceRoll -= mg.BoardOdds[i] if diceRoll <= 0 { - return i + 1 + return i } } - return 0 + return -1 } func (mg *MenaceGame) fullBoard() bool { @@ -75,7 +75,7 @@ func (mg *MenaceGame) fullBoard() bool { func (mg *MenaceGame) ResetOdds() { mg.BoardOdds = make([]int, 9) copy(mg.BoardOdds, initialBeads[:]) - mg.CurrentPosition = 0 + mg.CurrentPosition = -1 mg.updateOdds() } diff --git a/internal/game/menace_game_test.go b/internal/game/menace_game_test.go index 05d2515..e11ef80 100644 --- a/internal/game/menace_game_test.go +++ b/internal/game/menace_game_test.go @@ -29,9 +29,9 @@ func TestMenaceConstructorSetOddsEmptyGame(t *testing.T) { func TestMenaceConstructorSetOddsPartialGame(t *testing.T) { game := NewTicTacToe(3, 3, 3) - game.Play(1) - game.Play(3) - game.Play(5) + game.Play(0) + game.Play(2) + game.Play(4) menace := NewMenaceGame(game) @@ -50,16 +50,16 @@ func TestMenaceSetOutcomeWin(t *testing.T) { menace.BoardOdds[0] = 3 menace.BoardOdds[2] = 4 - menace.CurrentPosition = 1 + menace.CurrentPosition = 0 menace.SetOutcome(OutcomeWin) if menace.BoardOdds[0] != 6 { - t.Errorf("after win at 1: boardOdds[0] = %d, want 6", menace.BoardOdds[0]) + t.Errorf("after win at 0: boardOdds[0] = %d, want 6", menace.BoardOdds[0]) } - menace.CurrentPosition = 3 + menace.CurrentPosition = 2 menace.SetOutcome(OutcomeWin) if menace.BoardOdds[2] != 7 { - t.Errorf("after win at 3: boardOdds[2] = %d, want 7", menace.BoardOdds[2]) + t.Errorf("after win at 2: boardOdds[2] = %d, want 7", menace.BoardOdds[2]) } } @@ -70,16 +70,16 @@ func TestMenaceSetOutcomeDraw(t *testing.T) { menace.BoardOdds[0] = 3 menace.BoardOdds[2] = 4 - menace.CurrentPosition = 1 + menace.CurrentPosition = 0 menace.SetOutcome(OutcomeDraw) if menace.BoardOdds[0] != 4 { - t.Errorf("after draw at 1: boardOdds[0] = %d, want 4", menace.BoardOdds[0]) + t.Errorf("after draw at 0: boardOdds[0] = %d, want 4", menace.BoardOdds[0]) } - menace.CurrentPosition = 3 + menace.CurrentPosition = 2 menace.SetOutcome(OutcomeDraw) if menace.BoardOdds[2] != 5 { - t.Errorf("after draw at 3: boardOdds[2] = %d, want 5", menace.BoardOdds[2]) + t.Errorf("after draw at 2: boardOdds[2] = %d, want 5", menace.BoardOdds[2]) } } @@ -90,16 +90,16 @@ func TestMenaceSetOutcomeLose(t *testing.T) { menace.BoardOdds[0] = 3 menace.BoardOdds[2] = 4 - menace.CurrentPosition = 1 + menace.CurrentPosition = 0 menace.SetOutcome(OutcomeLose) if menace.BoardOdds[0] != 2 { - t.Errorf("after lose at 1: boardOdds[0] = %d, want 2", menace.BoardOdds[0]) + t.Errorf("after lose at 0: boardOdds[0] = %d, want 2", menace.BoardOdds[0]) } - menace.CurrentPosition = 3 + menace.CurrentPosition = 2 menace.SetOutcome(OutcomeLose) if menace.BoardOdds[2] != 3 { - t.Errorf("after lose at 3: boardOdds[2] = %d, want 3", menace.BoardOdds[2]) + t.Errorf("after lose at 2: boardOdds[2] = %d, want 3", menace.BoardOdds[2]) } } @@ -113,13 +113,13 @@ func TestMenaceSetOutcomeNeverGoToZero(t *testing.T) { menace.BoardOdds[i] = 0 } - menace.CurrentPosition = 1 + menace.CurrentPosition = 0 menace.SetOutcome(OutcomeLose) if menace.BoardOdds[0] != 1 { t.Errorf("should not go below 1 when totalOdds=1: boardOdds[0] = %d", menace.BoardOdds[0]) } - menace.CurrentPosition = 2 + menace.CurrentPosition = 1 menace.SetOutcome(OutcomeLose) if menace.BoardOdds[1] != 0 { t.Errorf("boardOdds[1] = %d, want 0", menace.BoardOdds[1]) @@ -131,7 +131,7 @@ func TestMenaceSetOutcomeUpdateOverallSum(t *testing.T) { menace := NewMenaceGame(game) menace.TotalOdds = 100 - menace.CurrentPosition = 1 + menace.CurrentPosition = 0 menace.SetOutcome(OutcomeWin) if menace.TotalOdds != 103 { @@ -151,9 +151,9 @@ func TestMenaceSetOutcomeUpdateOverallSum(t *testing.T) { func TestMenaceResetOdds(t *testing.T) { game := NewTicTacToe(3, 3, 3) - game.Play(1) - game.Play(3) - game.Play(5) + game.Play(0) + game.Play(2) + game.Play(4) menace := NewMenaceGame(game) @@ -169,7 +169,7 @@ func TestMenaceResetOdds(t *testing.T) { for i := 0; i < 9; i++ { menace.BoardOdds[i] = 10 + i } - menace.CurrentPosition = 1 + menace.CurrentPosition = 0 menace.ResetOdds() @@ -184,7 +184,7 @@ func TestMenaceToStringEmpty(t *testing.T) { game := NewTicTacToe(3, 3, 3) menace := NewMenaceGame(game) - expected := "POSITION: 0 (Odds 31)\n" + + expected := "POSITION: -1 (Odds 31)\n" + " 8 | 8 | 4\n" + "-----------\n" + " 4 | 2 | 2\n" + @@ -200,13 +200,13 @@ func TestMenaceToStringEmpty(t *testing.T) { func TestMenaceToStringShowXsAndOs(t *testing.T) { game := NewTicTacToe(3, 3, 3) + game.Play(0) game.Play(1) game.Play(2) - game.Play(3) menace := NewMenaceGame(game) - expected := "POSITION: 0 (Odds 11)\n" + + expected := "POSITION: -1 (Odds 11)\n" + " X | O | X\n" + "-----------\n" + " 4 | 2 | 2\n" + @@ -222,16 +222,16 @@ func TestMenaceToStringShowXsAndOs(t *testing.T) { func TestMenaceToStringShowRotated(t *testing.T) { game := NewTicTacToe(3, 3, 3) + game.Play(0) game.Play(1) game.Play(2) - game.Play(3) game.Next() game.Next() menace := NewMenaceGame(game) - expected := "POSITION: 0 (Odds 11)\n" + + expected := "POSITION: -1 (Odds 11)\n" + " 1 | 4 | X\n" + "-----------\n" + " 1 | 2 | O\n" + @@ -248,9 +248,9 @@ func TestMenaceToStringShowRotated(t *testing.T) { func TestMenaceToStringCurrentPosition(t *testing.T) { game := NewTicTacToe(3, 3, 3) menace := NewMenaceGame(game) - menace.CurrentPosition = 4 + menace.CurrentPosition = 3 - expected := "POSITION: 4 (Odds 31)\n" + + expected := "POSITION: 3 (Odds 31)\n" + " 8 | 8 | 4\n" + "-----------\n" + " 4 | 2 | 2\n" + @@ -287,6 +287,7 @@ func TestMenaceRollDiceBetween1AndTotalOdds(t *testing.T) { func TestMenaceRollDiceNoMovesLeft(t *testing.T) { game := NewTicTacToe(3, 3, 3) + game.Play(0) game.Play(1) game.Play(2) game.Play(3) @@ -295,13 +296,12 @@ func TestMenaceRollDiceNoMovesLeft(t *testing.T) { game.Play(6) game.Play(7) game.Play(8) - game.Play(9) menace := NewMenaceGame(game) for i := 0; i < 100; i++ { - if r := menace.RollDice(); r != 0 { - t.Errorf("rollDice on full board: got %d, want 0", r) + if r := menace.RollDice(); r != -1 { + t.Errorf("rollDice on full board: got %d, want -1", r) } } } @@ -328,6 +328,7 @@ func TestMenaceRollDiceCoverSpectrum(t *testing.T) { func TestMenaceCalculatePositionNoneLeft(t *testing.T) { game := NewTicTacToe(3, 3, 3) menace := NewMenaceGame(game) + game.Play(0) game.Play(1) game.Play(2) game.Play(3) @@ -336,18 +337,17 @@ func TestMenaceCalculatePositionNoneLeft(t *testing.T) { game.Play(6) game.Play(7) game.Play(8) - game.Play(9) - if got := menace.CalculatePosition(1); got != 0 { - t.Errorf("got %d, want 0", got) + if got := menace.CalculatePosition(1); got != -1 { + t.Errorf("got %d, want -1", got) } } func TestMenaceCalculatePositionInvalidRoll(t *testing.T) { game := NewTicTacToe(3, 3, 3) menace := NewMenaceGame(game) - if got := menace.CalculatePosition(999); got != 0 { - t.Errorf("got %d, want 0", got) + if got := menace.CalculatePosition(999); got != -1 { + t.Errorf("got %d, want -1", got) } } @@ -355,11 +355,11 @@ func TestMenaceCalculatePositionDoNotUpdateCurrentPosition(t *testing.T) { game := NewTicTacToe(3, 3, 3) menace := NewMenaceGame(game) - if got := menace.CalculatePosition(1); got != 1 { - t.Errorf("got %d, want 1", got) + if got := menace.CalculatePosition(1); got != 0 { + t.Errorf("got %d, want 0", got) } - if menace.CurrentPosition != 0 { - t.Errorf("currentPosition changed to %d, want 0", menace.CurrentPosition) + if menace.CurrentPosition != -1 { + t.Errorf("currentPosition changed to %d, want -1", menace.CurrentPosition) } } @@ -370,15 +370,15 @@ func TestMenaceCalculatePositionBasedOnRoll(t *testing.T) { cases := []struct { roll, want int }{ - {1, 1}, {8, 1}, - {9, 2}, {16, 2}, - {17, 3}, {20, 3}, - {21, 4}, {24, 4}, - {25, 5}, {26, 5}, - {27, 6}, {28, 6}, - {29, 7}, - {30, 8}, - {31, 9}, + {1, 0}, {8, 0}, + {9, 1}, {16, 1}, + {17, 2}, {20, 2}, + {21, 3}, {24, 3}, + {25, 4}, {26, 4}, + {27, 5}, {28, 5}, + {29, 6}, + {30, 7}, + {31, 8}, } for _, tc := range cases { @@ -390,9 +390,9 @@ func TestMenaceCalculatePositionBasedOnRoll(t *testing.T) { func TestMenaceCalculatePositionOnlyOpenSpots(t *testing.T) { game := NewTicTacToe(3, 3, 3) - game.Play(1) - game.Play(3) - game.Play(5) + game.Play(0) + game.Play(2) + game.Play(4) menace := NewMenaceGame(game) @@ -407,12 +407,12 @@ func TestMenaceCalculatePositionOnlyOpenSpots(t *testing.T) { cases := []struct { roll, want int }{ - {1, 2}, {8, 2}, - {9, 4}, {12, 4}, - {13, 6}, {14, 6}, - {15, 7}, - {16, 8}, - {17, 9}, + {1, 1}, {8, 1}, + {9, 3}, {12, 3}, + {13, 5}, {14, 5}, + {15, 6}, + {16, 7}, + {17, 8}, } for _, tc := range cases { @@ -424,15 +424,15 @@ func TestMenaceCalculatePositionOnlyOpenSpots(t *testing.T) { func TestMenacePickCurrentPositionRollAndFindNextPosition(t *testing.T) { game := NewTicTacToe(3, 3, 3) - game.Play(1) - game.Play(3) - game.Play(5) + game.Play(0) + game.Play(2) + game.Play(4) menace := NewMenaceGame(game) for i := 0; i < 100; i++ { pos := menace.PickCurrentPosition() - if pos == 1 || pos == 3 || pos == 5 { + if pos == 0 || pos == 2 || pos == 4 { t.Errorf("picked occupied position %d", pos) } if pos != menace.CurrentPosition { @@ -443,6 +443,7 @@ func TestMenacePickCurrentPositionRollAndFindNextPosition(t *testing.T) { func TestMenacePickCurrentPositionNoSpotsLeft(t *testing.T) { game := NewTicTacToe(3, 3, 3) + game.Play(0) game.Play(1) game.Play(2) game.Play(3) @@ -451,10 +452,9 @@ func TestMenacePickCurrentPositionNoSpotsLeft(t *testing.T) { game.Play(6) game.Play(7) game.Play(8) - game.Play(9) menace := NewMenaceGame(game) - if got := menace.PickCurrentPosition(); got != 0 { - t.Errorf("got %d, want 0", got) + if got := menace.PickCurrentPosition(); got != -1 { + t.Errorf("got %d, want -1", got) } } diff --git a/internal/game/menace_integration_test.go b/internal/game/menace_integration_test.go index d78e942..d6e6aa8 100644 --- a/internal/game/menace_integration_test.go +++ b/internal/game/menace_integration_test.go @@ -96,8 +96,8 @@ func TestMenaceReproduceScreenshotHang(t *testing.T) { game := NewDefaultTicTacToe() // X plays center - if err := game.Play(5); err != nil { - t.Fatalf("play 5: %v", err) + if err := game.Play(4); err != nil { + t.Fatalf("play 4: %v", err) } // O plays (MENACE first move) if !menace.Play(game) { @@ -107,7 +107,7 @@ func TestMenaceReproduceScreenshotHang(t *testing.T) { t.Logf("MENACE played O at position %d", firstOPos) // X plays corner (simulate various corners to cover rotations) - corners := []int{1, 3, 7, 9} + corners := []int{0, 2, 6, 8} for _, corner := range corners { if game.ValueAt(corner) != CellEmpty { continue @@ -128,7 +128,7 @@ func TestMenaceReproduceScreenshotHang(t *testing.T) { ok := menace.Play(testGame) if !ok || testGame.NumRounds == before { t.Errorf("MENACE failed to respond after X=%d, O=%d, X=%d. Board: %v", - 5, firstOPos, corner, testGame.Board) + 4, firstOPos, corner, testGame.Board) } else { t.Logf("MENACE responded with position %d", testGame.LastPlayedPosition) } @@ -167,9 +167,9 @@ func TestMenacePlaysMappedPosition(t *testing.T) { } // Verify the move was made at an actually empty cell pos := game.LastPlayedPosition - if boardBefore[pos-1] != CellEmpty { + if boardBefore[pos] != CellEmpty { t.Fatalf("game %d round %d: MENACE played pos %d which was %s in board %v", - i, round, pos, boardBefore[pos-1], boardBefore) + i, round, pos, boardBefore[pos], boardBefore) } if game.NumRounds != roundBefore+1 { t.Fatalf("game %d round %d: NumRounds did not increment after MENACE play", i, round) @@ -247,7 +247,7 @@ func TestServerMakeAIMoveIntegration(t *testing.T) { // Verify move was valid pos := game.LastPlayedPosition - if pos < 1 || pos > 9 { + if pos < 0 || pos > 8 { t.Fatalf("invalid position %d from AI", pos) } } diff --git a/internal/game/menace_player.go b/internal/game/menace_player.go index 6b969d9..4b4dba8 100644 --- a/internal/game/menace_player.go +++ b/internal/game/menace_player.go @@ -46,22 +46,22 @@ func (p *MenacePlayer) Play(game *TicTacToe) bool { for _, menaceGame := range p.winnableGames[game.NumRounds] { if menaceGame.Game.AlignAndEquals(game) { internalPos := menaceGame.PickCurrentPosition() - if internalPos == 0 { - log.Printf("[MENACE] PickCurrentPosition returned 0 at round %d", game.NumRounds) + if internalPos < 0 { + log.Printf("[MENACE] PickCurrentPosition returned -1 at round %d", game.NumRounds) return false } // Map internal position back to live game coordinates. // After AlignAndEquals, BoardIndexes[i] maps live index i → internal index. - // We need the inverse: find live index where BoardIndexes[live] == internalPos-1. - livePos := 0 + // We need the inverse: find live index where BoardIndexes[live] == internalPos. + livePos := -1 for i, idx := range menaceGame.Game.BoardIndexes { - if idx == internalPos-1 { - livePos = i + 1 + if idx == internalPos { + livePos = i break } } - if livePos == 0 { + if livePos < 0 { log.Printf("[MENACE] failed to map internal pos %d to live coordinates at round %d", internalPos, game.NumRounds) return false } diff --git a/internal/game/menace_player_test.go b/internal/game/menace_player_test.go index 54a5354..518cad1 100644 --- a/internal/game/menace_player_test.go +++ b/internal/game/menace_player_test.go @@ -7,6 +7,7 @@ import "testing" func TestMenacePlayerPlayNoSpotsLeft(t *testing.T) { p := NewMenacePlayer(3, 3, 3) game := NewTicTacToe(3, 3, 3) + game.Play(0) game.Play(1) game.Play(2) game.Play(3) @@ -15,35 +16,34 @@ func TestMenacePlayerPlayNoSpotsLeft(t *testing.T) { game.Play(6) game.Play(7) game.Play(8) - game.Play(9) p.StartGame(true) if p.Play(game) { t.Error("expected play to return false on full board") } - if game.LastPlayedPosition != 9 { - t.Errorf("lastPlayedPosition: got %d, want 9", game.LastPlayedPosition) + if game.LastPlayedPosition != 8 { + t.Errorf("lastPlayedPosition: got %d, want 8", game.LastPlayedPosition) } } func TestMenacePlayerPlayPickAvailableSpot(t *testing.T) { p := NewMenacePlayer(3, 3, 3) game := NewTicTacToe(3, 3, 3) + game.Play(0) game.Play(1) game.Play(2) game.Play(3) game.Play(4) game.Play(5) - game.Play(6) - game.Play(8) game.Play(7) + game.Play(6) p.StartGame(true) if !p.Play(game) { t.Error("expected play to return true") } - if game.LastPlayedPosition != 9 { - t.Errorf("lastPlayedPosition: got %d, want 9", game.LastPlayedPosition) + if game.LastPlayedPosition != 8 { + t.Errorf("lastPlayedPosition: got %d, want 8", game.LastPlayedPosition) } } diff --git a/internal/game/perfect_game.go b/internal/game/perfect_game.go index 7851d4f..9d41f1e 100644 --- a/internal/game/perfect_game.go +++ b/internal/game/perfect_game.go @@ -71,9 +71,8 @@ func (pg *PerfectGame) SetOutcome(nextMove *PerfectGame, position int) { shouldUpdate = true } - index := position - 1 - pg.MoveOutcomes[index] = currentOutcome - pg.MoveRemainings[index] = currentRemaining + pg.MoveOutcomes[position] = currentOutcome + pg.MoveRemainings[position] = currentRemaining if shouldUpdate { if pg.OverallRemaining == 0 || currentRemaining < pg.OverallRemaining { diff --git a/internal/game/perfect_game_test.go b/internal/game/perfect_game_test.go index d5e53ac..0eb2203 100644 --- a/internal/game/perfect_game_test.go +++ b/internal/game/perfect_game_test.go @@ -8,7 +8,7 @@ func TestPerfectGameSetOutcomeGameStateBasedOnNextPlayer(t *testing.T) { game := NewTicTacToe(3, 3, 3) perfect := NewPerfectGame(game) - game.Play(1) + game.Play(0) if game.NextPlayer() != CellO { t.Fatalf("expected next player O, got %s", game.NextPlayer()) } @@ -34,7 +34,7 @@ func TestPerfectGameSetOutcomeDrawIsADraw(t *testing.T) { game := NewTicTacToe(3, 3, 3) perfect := NewPerfectGame(game) - game.Play(1) + game.Play(0) perfect.SetOutcomeFromState(StateDraw) if perfect.OverallOutcome != OutcomeDraw { t.Errorf("got %s, want DRAW", perfect.OverallOutcome) @@ -43,7 +43,7 @@ func TestPerfectGameSetOutcomeDrawIsADraw(t *testing.T) { t.Errorf("remaining: got %d, want 0", perfect.OverallRemaining) } - game.Play(2) + game.Play(1) perfect.SetOutcomeFromState(StateDraw) if perfect.OverallOutcome != OutcomeDraw { t.Errorf("got %s, want DRAW", perfect.OverallOutcome) @@ -61,7 +61,7 @@ func TestPerfectGameSetOutcomeNextMove(t *testing.T) { thisGame := NewTicTacToe(3, 3, 3) thisPerfect := NewPerfectGame(thisGame) - thisPerfect.SetOutcome(nextMove, 3) + thisPerfect.SetOutcome(nextMove, 2) if thisPerfect.OverallOutcome != OutcomeLose { t.Errorf("overall: got %s, want LOSE", thisPerfect.OverallOutcome) } @@ -96,9 +96,9 @@ func TestPerfectGameToStringEmpty(t *testing.T) { func TestPerfectGameToStringShowXsAndOs(t *testing.T) { game := NewTicTacToe(3, 3, 3) + game.Play(0) game.Play(1) game.Play(2) - game.Play(3) perfect := NewPerfectGame(game) @@ -118,9 +118,9 @@ func TestPerfectGameToStringShowXsAndOs(t *testing.T) { func TestPerfectGameToStringShowRotated(t *testing.T) { game := NewTicTacToe(3, 3, 3) + game.Play(0) game.Play(1) game.Play(2) - game.Play(3) game.Next() // identity game.Next() // rotation @@ -143,9 +143,9 @@ func TestPerfectGameToStringShowRotated(t *testing.T) { func TestPerfectGameToStringShowWinLose(t *testing.T) { game := NewTicTacToe(3, 3, 3) + game.Play(0) game.Play(1) game.Play(2) - game.Play(3) game.Next() // identity game.Next() // rotation @@ -170,9 +170,9 @@ func TestPerfectGameToStringShowWinLossDrawInfo(t *testing.T) { game := NewTicTacToe(3, 3, 3) perfect := NewPerfectGame(game) + game.Play(0) game.Play(1) game.Play(2) - game.Play(3) perfect.OverallOutcome = OutcomeWin perfect.OverallRemaining = 0 diff --git a/internal/game/perfect_player.go b/internal/game/perfect_player.go index 61dfd9f..ef62ee2 100644 --- a/internal/game/perfect_player.go +++ b/internal/game/perfect_player.go @@ -62,13 +62,12 @@ func (p *PerfectPlayer) Play(game *TicTacToe) bool { return false } - bestPosition := 0 + bestPosition := -1 bestNumMoves := len(openSpots) bestOutcome := OutcomeUnknown for _, position := range openSpots { - index := position - 1 - transformIndex := perfectGame.Game.BoardIndexes[index] + transformIndex := perfectGame.Game.BoardIndexes[position] numMoves := perfectGame.MoveRemainings[transformIndex] outcome := perfectGame.MoveOutcomes[transformIndex] @@ -89,7 +88,7 @@ func (p *PerfectPlayer) Play(game *TicTacToe) bool { } } - if bestPosition > 0 { + if bestPosition >= 0 { game.Play(bestPosition) return true } diff --git a/internal/game/perfect_player_test.go b/internal/game/perfect_player_test.go index b133660..26d1084 100644 --- a/internal/game/perfect_player_test.go +++ b/internal/game/perfect_player_test.go @@ -7,16 +7,16 @@ import "testing" func TestPerfectPlayerPickWinnerWins(t *testing.T) { p := NewPerfectPlayer(3, 3, 3) game := NewTicTacToe(3, 3, 3) - game.Play(1) - game.Play(4) - game.Play(5) + game.Play(0) game.Play(3) + game.Play(4) + game.Play(2) p.StartGame(true) p.Play(game) - if game.LastPlayedPosition != 9 { - t.Errorf("lastPlayedPosition: got %d, want 9", game.LastPlayedPosition) + if game.LastPlayedPosition != 8 { + t.Errorf("lastPlayedPosition: got %d, want 8", game.LastPlayedPosition) } if game.GameState != StateXWin { t.Errorf("gameState: got %s, want XWIN", game.GameState) @@ -27,11 +27,11 @@ func TestPerfectPlayerPickWinnerRotated(t *testing.T) { p := NewPerfectPlayer(3, 3, 3) game := NewTicTacToe(3, 3, 3) - game.Play(4) - game.Play(7) + game.Play(3) game.Play(6) - game.Play(9) - game.Play(2) + game.Play(5) + game.Play(8) + game.Play(1) p.StartGame(false) if !p.Play(game) { @@ -40,16 +40,16 @@ func TestPerfectPlayerPickWinnerRotated(t *testing.T) { if game.GameState != StateOWin { t.Errorf("gameState: got %s, want OWIN", game.GameState) } - if game.LastPlayedPosition != 8 { - t.Errorf("lastPlayedPosition: got %d, want 8", game.LastPlayedPosition) + if game.LastPlayedPosition != 7 { + t.Errorf("lastPlayedPosition: got %d, want 7", game.LastPlayedPosition) } game = NewTicTacToe(3, 3, 3) - game.Play(4) - game.Play(1) - game.Play(6) game.Play(3) - game.Play(8) + game.Play(0) + game.Play(5) + game.Play(2) + game.Play(7) p.StartGame(false) if !p.Play(game) { @@ -58,8 +58,8 @@ func TestPerfectPlayerPickWinnerRotated(t *testing.T) { if game.GameState != StateOWin { t.Errorf("gameState: got %s, want OWIN", game.GameState) } - if game.LastPlayedPosition != 2 { - t.Errorf("lastPlayedPosition: got %d, want 2", game.LastPlayedPosition) + if game.LastPlayedPosition != 1 { + t.Errorf("lastPlayedPosition: got %d, want 1", game.LastPlayedPosition) } } @@ -67,13 +67,13 @@ func TestPerfectPlayerGoForDraw(t *testing.T) { p := NewPerfectPlayer(3, 3, 3) game := NewTicTacToe(3, 3, 3) + game.Play(0) + game.Play(2) game.Play(1) game.Play(3) - game.Play(2) game.Play(4) - game.Play(5) - game.Play(8) game.Play(7) + game.Play(6) p.StartGame(false) if !p.Play(game) { @@ -82,8 +82,8 @@ func TestPerfectPlayerGoForDraw(t *testing.T) { if game.GameState != StatePlaying { t.Errorf("gameState: got %s, want PLAYING", game.GameState) } - if game.LastPlayedPosition != 9 { - t.Errorf("lastPlayedPosition: got %d, want 9", game.LastPlayedPosition) + if game.LastPlayedPosition != 8 { + t.Errorf("lastPlayedPosition: got %d, want 8", game.LastPlayedPosition) } } @@ -91,9 +91,9 @@ func TestPerfectPlayerAvoidLosing(t *testing.T) { p := NewPerfectPlayer(3, 3, 3) game := NewTicTacToe(3, 3, 3) + game.Play(0) game.Play(1) - game.Play(2) - game.Play(4) + game.Play(3) p.StartGame(false) if !p.Play(game) { @@ -102,8 +102,8 @@ func TestPerfectPlayerAvoidLosing(t *testing.T) { if game.GameState != StatePlaying { t.Errorf("gameState: got %s, want PLAYING", game.GameState) } - if game.LastPlayedPosition != 7 { - t.Errorf("lastPlayedPosition: got %d, want 7", game.LastPlayedPosition) + if game.LastPlayedPosition != 6 { + t.Errorf("lastPlayedPosition: got %d, want 6", game.LastPlayedPosition) } } @@ -111,10 +111,10 @@ func TestPerfectPlayerChooseLongerDrawThanLosing(t *testing.T) { p := NewPerfectPlayer(3, 3, 3) game := NewTicTacToe(3, 3, 3) - game.Play(1) - game.Play(3) + game.Play(0) game.Play(2) - game.Play(5) + game.Play(1) + game.Play(4) p.StartGame(false) if !p.Play(game) { @@ -123,7 +123,7 @@ func TestPerfectPlayerChooseLongerDrawThanLosing(t *testing.T) { if game.GameState != StatePlaying { t.Errorf("gameState: got %s, want PLAYING", game.GameState) } - if game.LastPlayedPosition != 7 { - t.Errorf("lastPlayedPosition: got %d, want 7", game.LastPlayedPosition) + if game.LastPlayedPosition != 6 { + t.Errorf("lastPlayedPosition: got %d, want 6", game.LastPlayedPosition) } } diff --git a/internal/game/tictactoe.go b/internal/game/tictactoe.go index a5402d3..05fe5b3 100644 --- a/internal/game/tictactoe.go +++ b/internal/game/tictactoe.go @@ -36,7 +36,7 @@ func NewTicTacToe(numRows, numColumns, sizeToWin int) *TicTacToe { SizeToWin: sizeToWin, GameState: StatePlaying, CurrentPlayer: CellEmpty, - LastPlayedPosition: 0, + LastPlayedPosition: -1, Allowable: SymmetricTransformations(numRows, numColumns), } g.Reset() @@ -56,20 +56,20 @@ func (g *TicTacToe) NextPlayer() CellValue { func (g *TicTacToe) ValueAt(position int) CellValue { maxPosition := g.NumRows * g.NumColumns - if position < 1 || position > maxPosition { + if position < 0 || position >= maxPosition { return CellInvalid } - return g.Board[position-1] + return g.Board[position] } func (g *TicTacToe) ValueAtRC(row, column int) CellValue { - if row < 1 || row > g.NumRows { + if row < 0 || row >= g.NumRows { return CellInvalid } - if column < 1 || column > g.NumColumns { + if column < 0 || column >= g.NumColumns { return CellInvalid } - return g.ValueAt((row-1)*g.NumColumns + column) + return g.ValueAt(row*g.NumColumns + column) } func (g *TicTacToe) Play(position int) error { @@ -79,7 +79,7 @@ func (g *TicTacToe) Play(position int) error { playedBy := g.NextPlayer() g.CurrentPlayer = playedBy g.LastPlayedPosition = position - g.Board[position-1] = playedBy + g.Board[position] = playedBy g.NumRounds++ if g.GameState == StatePlaying { @@ -87,7 +87,7 @@ func (g *TicTacToe) Play(position int) error { } return nil case CellInvalid: - return fmt.Errorf("the value should be between 1 and %d", g.NumRows*g.NumColumns) + return fmt.Errorf("the value should be between 0 and %d", g.NumRows*g.NumColumns-1) default: return fmt.Errorf("cell %d has already been played with %s", position, cell) } @@ -104,8 +104,8 @@ func (g *TicTacToe) checkForWinner(position int) GameState { ifWon = StatePlaying } - currentRow := (position-1)/g.NumColumns + 1 - currentColumn := (position-1)%g.NumColumns + 1 + currentRow := position / g.NumColumns + currentColumn := position % g.NumColumns if g.checkSizeToWin(currentRow, currentColumn, 1, 0) { return ifWon @@ -184,7 +184,7 @@ func (g *TicTacToe) EmptyPositions() []int { answer := make([]int, 0, numOpenSpots) for i := 0; i < totalSpots; i++ { if g.Board[g.BoardIndexes[i]] == CellEmpty { - answer = append(answer, i+1) + answer = append(answer, i) } } return answer @@ -206,7 +206,7 @@ func (g *TicTacToe) CloneNextPlay(nextMove int) *TicTacToe { newGame := NewTicTacToe(g.NumRows, g.NumColumns, g.SizeToWin) CloneFromTo(g, newGame) - newGame.Board[nextMove-1] = newGame.CurrentPlayer + newGame.Board[nextMove] = newGame.CurrentPlayer newGame.GameState = newGame.checkForWinner(nextMove) return newGame } @@ -226,11 +226,11 @@ func (g *TicTacToe) CloneUndoPlay(lastMove int) *TicTacToe { newGame.CurrentPlayer = g.NextPlayer() newGame.NumRounds = g.NumRounds - 1 for i := 0; i < len(g.Board); i++ { - if i+1 != lastMove { + if i != lastMove { newGame.Board[i] = g.Board[i] } } - newGame.LastPlayedPosition = 0 + newGame.LastPlayedPosition = -1 newGame.CurrentPlayer = g.NextPlayer() newGame.GameState = StatePlaying return newGame diff --git a/internal/game/tictactoe_test.go b/internal/game/tictactoe_test.go index da2cc19..551a35b 100644 --- a/internal/game/tictactoe_test.go +++ b/internal/game/tictactoe_test.go @@ -9,7 +9,7 @@ import ( func TestEmptyPositionsNewGame(t *testing.T) { game := NewTicTacToe(2, 2, 2) - want := []int{1, 2, 3, 4} + want := []int{0, 1, 2, 3} got := game.EmptyPositions() if !reflect.DeepEqual(got, want) { t.Errorf("got %v, want %v", got, want) @@ -19,53 +19,53 @@ func TestEmptyPositionsNewGame(t *testing.T) { func TestEmptyPositionsGamePlayed(t *testing.T) { game := NewTicTacToe(2, 2, 2) - game.Play(2) - if got, want := game.EmptyPositions(), []int{1, 3, 4}; !reflect.DeepEqual(got, want) { - t.Errorf("after play(2): got %v, want %v", got, want) - } - game.Play(1) - if got, want := game.EmptyPositions(), []int{3, 4}; !reflect.DeepEqual(got, want) { + if got, want := game.EmptyPositions(), []int{0, 2, 3}; !reflect.DeepEqual(got, want) { t.Errorf("after play(1): got %v, want %v", got, want) } - game.Play(4) - if got, want := game.EmptyPositions(), []int{3}; !reflect.DeepEqual(got, want) { - t.Errorf("after play(4): got %v, want %v", got, want) + game.Play(0) + if got, want := game.EmptyPositions(), []int{2, 3}; !reflect.DeepEqual(got, want) { + t.Errorf("after play(0): got %v, want %v", got, want) } game.Play(3) + if got, want := game.EmptyPositions(), []int{2}; !reflect.DeepEqual(got, want) { + t.Errorf("after play(3): got %v, want %v", got, want) + } + + game.Play(2) got := game.EmptyPositions() if len(got) != 0 { - t.Errorf("after play(3): got %v, want empty", got) + t.Errorf("after play(2): got %v, want empty", got) } } func TestEmptyPositionsRotated(t *testing.T) { game := NewTicTacToe(2, 2, 2) - game.Play(2) + game.Play(1) - if got, want := game.EmptyPositions(), []int{1, 3, 4}; !reflect.DeepEqual(got, want) { + if got, want := game.EmptyPositions(), []int{0, 2, 3}; !reflect.DeepEqual(got, want) { t.Errorf("identity: got %v, want %v", got, want) } game.Next() - if got, want := game.EmptyPositions(), []int{1, 3, 4}; !reflect.DeepEqual(got, want) { + if got, want := game.EmptyPositions(), []int{0, 2, 3}; !reflect.DeepEqual(got, want) { t.Errorf("after next() 1: got %v, want %v", got, want) } game.Next() - if got, want := game.EmptyPositions(), []int{1, 2, 3}; !reflect.DeepEqual(got, want) { + if got, want := game.EmptyPositions(), []int{0, 1, 2}; !reflect.DeepEqual(got, want) { t.Errorf("after next() 2: got %v, want %v", got, want) } game.Next() - if got, want := game.EmptyPositions(), []int{1, 2, 4}; !reflect.DeepEqual(got, want) { + if got, want := game.EmptyPositions(), []int{0, 1, 3}; !reflect.DeepEqual(got, want) { t.Errorf("after next() 3: got %v, want %v", got, want) } game.Next() - if got, want := game.EmptyPositions(), []int{2, 3, 4}; !reflect.DeepEqual(got, want) { + if got, want := game.EmptyPositions(), []int{1, 2, 3}; !reflect.DeepEqual(got, want) { t.Errorf("after next() 4: got %v, want %v", got, want) } } @@ -81,37 +81,37 @@ func TestCloneUndoPlayInvalid(t *testing.T) { func TestCloneUndoEmptySpot(t *testing.T) { game := NewTicTacToe(2, 2, 2) - if game.CloneUndoPlay(1) != nil { + if game.CloneUndoPlay(0) != nil { t.Error("expected nil for empty spot") } } func TestCloneUndoWrongPiece(t *testing.T) { game := NewTicTacToe(2, 2, 2) + game.Play(0) game.Play(1) game.Play(2) - game.Play(3) if game.GameState != StateXWin { t.Fatalf("expected XWIN, got %s", game.GameState) } - if game.CloneUndoPlay(2) != nil { + if game.CloneUndoPlay(1) != nil { t.Error("expected nil for wrong piece") } } func TestCloneUndoLastMove(t *testing.T) { game := NewTicTacToe(2, 2, 2) + game.Play(0) game.Play(1) game.Play(2) - game.Play(3) if game.GameState != StateXWin { t.Fatalf("expected XWIN, got %s", game.GameState) } - cloned := game.CloneUndoPlay(3) + cloned := game.CloneUndoPlay(2) if cloned == nil { t.Fatal("expected non-nil clone") } @@ -121,8 +121,8 @@ func TestCloneUndoLastMove(t *testing.T) { if cloned.NumRounds != 2 { t.Errorf("cloned numRounds: got %d, want 2", cloned.NumRounds) } - if cloned.LastPlayedPosition != 0 { - t.Errorf("cloned lastPlayedPosition: got %d, want 0", cloned.LastPlayedPosition) + if cloned.LastPlayedPosition != -1 { + t.Errorf("cloned lastPlayedPosition: got %d, want -1", cloned.LastPlayedPosition) } if cloned.NextPlayer() != CellX { t.Errorf("cloned nextPlayer: got %s, want X", cloned.NextPlayer()) @@ -159,15 +159,15 @@ func TestCloneUndoLastMove(t *testing.T) { func TestCloneUndoAnyValidMove(t *testing.T) { game := NewTicTacToe(2, 2, 2) + game.Play(0) game.Play(1) game.Play(2) - game.Play(3) if game.GameState != StateXWin { t.Fatalf("expected XWIN, got %s", game.GameState) } - cloned := game.CloneUndoPlay(1) + cloned := game.CloneUndoPlay(0) if cloned == nil { t.Fatal("expected non-nil clone") } @@ -177,8 +177,8 @@ func TestCloneUndoAnyValidMove(t *testing.T) { if cloned.NumRounds != 2 { t.Errorf("cloned numRounds: got %d, want 2", cloned.NumRounds) } - if cloned.LastPlayedPosition != 0 { - t.Errorf("cloned lastPlayedPosition: got %d, want 0", cloned.LastPlayedPosition) + if cloned.LastPlayedPosition != -1 { + t.Errorf("cloned lastPlayedPosition: got %d, want -1", cloned.LastPlayedPosition) } if cloned.NextPlayer() != CellX { t.Errorf("cloned nextPlayer: got %s, want X", cloned.NextPlayer())