Skip to content

Commit 176b4c5

Browse files
Add option to see the board visually
1 parent edf1317 commit 176b4c5

3 files changed

Lines changed: 63 additions & 1 deletion

File tree

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,26 @@ from tictactoe import Board
3636

3737
board = Board(dimensions=(10, 10, 10), x_in_a_row=8)
3838
```
39+
* See the board
40+
```python
41+
from tictactoe import Board
42+
43+
board = Board(dimensions=(2, 2, 2), x_in_a_row=2)
44+
board.push((0, 0, 0))
45+
board.push((0, 1, 0))
46+
print(board)
47+
48+
"""
49+
X |
50+
-------
51+
O |
52+
-------
53+
-------
54+
|
55+
-------
56+
|
57+
"""
58+
```
3959
* Generate endgame tablebases
4060
```python
4161
from tictactoe.egtb import Generator

test_tictactoe/test_init.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,31 @@ def test_x_and_o_won():
101101
assert True
102102

103103

104+
def test_repr():
105+
board = Board((2, 2, 2), 2)
106+
board.push((0, 0, 0))
107+
board.push((0, 1, 0))
108+
board.push((0, 0, 1))
109+
board.push((1, 1, 0))
110+
board.push((1, 0, 1))
111+
board.push((1, 0, 0))
112+
board.push((1, 1, 1))
113+
114+
correct_repr = """ X | O
115+
-------
116+
O | O
117+
-------
118+
-------
119+
X | X
120+
-------
121+
| X """
122+
assert correct_repr == str(board)
123+
124+
104125
if __name__ == "__main__":
105126
test_result()
106127
test_copy()
107128
test_inbound_outofbounds()
108129
test_move()
109130
test_illegal_move()
131+
test_repr()

tictactoe/__init__.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
__author__ = "AttackingOrDefending"
88
__copyright__ = "2022, " + __author__
9-
__version__ = "0.0.5"
9+
__version__ = "0.0.6"
1010

1111

1212
_all_numpy_int_types = Union[np.int8, np.int16, np.int32, np.int64]
@@ -165,6 +165,26 @@ def result(self) -> Optional[int]:
165165
return 0
166166
return None
167167

168+
def _get_dimension_repr(self, board_partition):
169+
if len(board_partition.shape) > 1:
170+
board_repr = ""
171+
divider = ((board_partition.shape[0] * 4 - 1) * "-" + "\n") * (len(board_partition.shape) - 1)
172+
for board_partition_index in range(board_partition.shape[-1]):
173+
board_repr += self._get_dimension_repr(board_partition[..., board_partition_index]) + "\n"
174+
board_repr += divider
175+
board_repr = board_repr[:-(len(divider) + 1)]
176+
return board_repr
177+
else:
178+
row = ""
179+
for item in board_partition:
180+
mark = "O" if item == O else ("X" if item == X else " ")
181+
row += f" {mark} |"
182+
row = row[:-1]
183+
return row
184+
185+
def __repr__(self):
186+
return self._get_dimension_repr(self.board)
187+
168188

169189
class Move:
170190
def __init__(self, coordinate_move: Optional[Iterable[int]] = None, str_move: Optional[str] = None) -> None:

0 commit comments

Comments
 (0)