From ccc7004d9115a75a96818ad77c762a5d6132e95b Mon Sep 17 00:00:00 2001 From: Levi Eby Date: Sun, 8 Nov 2026 16:52:22 -0600 Subject: [PATCH 1/3] fixed TicTacToe display bug --- aima/games.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/aima/games.py b/aima/games.py index 533ebc277..64e759bbb 100644 --- a/aima/games.py +++ b/aima/games.py @@ -466,8 +466,8 @@ def terminal_test(self, state): def display(self, state): """Print the board as a grid, with empty squares shown as dots.""" board = state.board - for x in range(1, self.h + 1): - for y in range(1, self.v + 1): + for y in range(1, self.v + 1): + for x in range(1, self.h + 1): print(board.get((x, y), '.'), end=' ') print() @@ -508,7 +508,7 @@ def __init__(self, h=7, v=6, k=4): def actions(self, state): """Return legal moves: bottom-row squares or squares directly above an occupied one.""" return [(x, y) for (x, y) in state.moves - if x == self.h or (x + 1 , y ) in state.board] + if y == self.v or (x, y + 1) in state.board] class Gomoku(TicTacToe): """Also known as Five in a row.""" From 8579e6090f2c8ab36ea7b6d05f15fe085cf5e4fe Mon Sep 17 00:00:00 2001 From: Levi Eby Date: Sun, 8 Nov 2026 17:38:28 -0600 Subject: [PATCH 2/3] added TicTacToe display test --- tests/test_games.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/test_games.py b/tests/test_games.py index 81301a70c..e3bcdd3c3 100644 --- a/tests/test_games.py +++ b/tests/test_games.py @@ -91,6 +91,23 @@ def test_random_tests(): # The player 'X' (one who plays first) in TicTacToe never loses: assert ttt.play_game(alpha_beta_player, random_player) >= 0 +def test_ttt_display_dimensions(): + import io + from contextlib import redirect_stdout + + d = { + TicTacToe(): (3, 3), + TicTacToe(h=7, v=5): (7, 5), + TicTacToe(h=5, v=7): (5, 7), + ConnectFour(): (7, 6) + } + + for game, (h, v) in d.items(): + output = io.StringIO() + with redirect_stdout(output): + game.display() + + assert output.getvalue() == (". " * h + "\n") * v if __name__ == "__main__": pytest.main() From 27960212735bb1fddfb51f33f4b999b0a753b1b0 Mon Sep 17 00:00:00 2001 From: Donato Meoli Date: Fri, 20 Nov 2026 01:21:01 +0100 Subject: [PATCH 3/3] Pass the state to display in the test, check Connect Four gravity --- aima/games.py | 1 + tests/test_games.py | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/aima/games.py b/aima/games.py index 64e759bbb..fb69b2626 100644 --- a/aima/games.py +++ b/aima/games.py @@ -510,6 +510,7 @@ def actions(self, state): return [(x, y) for (x, y) in state.moves if y == self.v or (x, y + 1) in state.board] + class Gomoku(TicTacToe): """Also known as Five in a row.""" diff --git a/tests/test_games.py b/tests/test_games.py index e3bcdd3c3..74e5aff8c 100644 --- a/tests/test_games.py +++ b/tests/test_games.py @@ -91,23 +91,28 @@ def test_random_tests(): # The player 'X' (one who plays first) in TicTacToe never loses: assert ttt.play_game(alpha_beta_player, random_player) >= 0 + def test_ttt_display_dimensions(): + """display prints v rows of h squares, for square and rectangular boards.""" import io from contextlib import redirect_stdout - d = { - TicTacToe(): (3, 3), - TicTacToe(h=7, v=5): (7, 5), - TicTacToe(h=5, v=7): (5, 7), - ConnectFour(): (7, 6) - } - - for game, (h, v) in d.items(): + dimensions = {TicTacToe(): (3, 3), + TicTacToe(h=7, v=5): (7, 5), + TicTacToe(h=5, v=7): (5, 7), + ConnectFour(): (7, 6)} + for game, (h, v) in dimensions.items(): output = io.StringIO() with redirect_stdout(output): - game.display() + game.display(game.initial) + assert output.getvalue() == ('. ' * h + '\n') * v + # Connect Four drops pieces from the top: the first legal moves are the + # 7 squares of the bottom row, and a piece there opens the square above it + c4 = ConnectFour() + assert sorted(c4.actions(c4.initial)) == [(x, 6) for x in range(1, 8)] + state = c4.result(c4.initial, (4, 6)) + assert (4, 5) in c4.actions(state) and (4, 4) not in c4.actions(state) - assert output.getvalue() == (". " * h + "\n") * v if __name__ == "__main__": pytest.main()