-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchessboard.py
127 lines (106 loc) · 3.22 KB
/
chessboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import copy as _copy
# Define the winning combinations for Tic Tac Toe
winning_combinations = [
[0, 1, 2], [3, 4, 5], [6, 7, 8], # Rows
[0, 3, 6], [1, 4, 7], [2, 5, 8], # Columns
[0, 4, 8], [2, 4, 6] # Diagonals
]
class Board:
"""Chess Board
position:
0 | 1 | 2
3 | 4 | 5
6 | 7 | 8
"""
def __init__(self, board=None, history=[]):
if board:
self.board = board
else:
self.board = [[None, None, None] for i in range(3)]
self.history = history
def __repr__(self):
board_str = [[' ', ' ', ' '] for i in range(3)]
for row in range(3):
for col in range(3):
if self.board[row][col] is True:
board_str[row][col] = 'O'
elif self.board[row][col] is False:
board_str[row][col] = 'X'
else:
pass
line = '-' * 9
board_lines = [" | ".join(row) for row in board_str]
board_lines.insert(2, line)
board_lines.insert(1, line)
return "\n" + "\n".join(board_lines)
def __getitem__(self, key):
return self.board[key]
# def __setitem__(self, key, value):
# self.board[key] = value
# def __delitem__(self, key):
# del self.data[key]
def show(self):
print(repr(self))
def check_win(self, player):
for combo in winning_combinations:
if all(self.board[i//3][i % 3] == player for i in combo):
return True
return False
def is_empty(self):
empty = []
for row in range(3):
for col in range(3):
if self.board[row][col] is None:
empty.append(row*3 + col)
return empty
def put(self, position: int, player: bool):
"""Put chess
Parameters
----------
position : int
0 | 1 | 2
3 | 4 | 5
6 | 7 | 8
player : bool
True: O
False: X
"""
if self.board[position//3][position % 3] is not None:
raise Exception(
"Position already occupied by another chess piece!")
self.board[position//3][position % 3] = player
self.history.append(position)
def copy(self):
return Board(_copy.deepcopy(self.board), history=self.history.copy())
def nospace(self):
for row in self.board:
for cell in row:
if cell is None:
return False
return True
if __name__ == "__main__":
test_board = [
[True, False, False],
[None, True, False],
[None, None, True],
]
board = Board(test_board)
board.show()
print("O Win:", board.check_win(True))
print("X Win:", board.check_win(False))
print("Empty:", board.is_empty())
board.put(7, False)
print("After X put at 7:")
board.show()
print("No space:", board.nospace())
board2 = board.copy()
board.put(3, False)
board.put(6, False)
print("After X put at 3, 6:")
board.show()
print("No space:", board.nospace())
board2.put(6, True)
print("A copy:")
board2.show()
print("Origin after copy:")
board.show()