-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameManager.py
34 lines (28 loc) · 1.16 KB
/
GameManager.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
from ChessPiece import WHITE, BLACK
from ChessBoard import ChessBoard, Move, Square
# Currently, this class is hardly used, since most logic is in the ChessBoard. This may change as new functionality
# is added
class GameManager:
def __init__(self, chessboard: ChessBoard):
self.chessboard = chessboard
self.moves = []
def switch_turn(self):
self.chessboard.current_player = BLACK if self.chessboard.current_player == WHITE else WHITE
def make_move(self, start: str, end: str):
start_square = self.chessboard.square(start)
end_square = self.chessboard.square(end)
return self.exec_move(start_square, end_square)
# Hm. learning to use @singledispatch, @overload. None of these were particularly better than this on first try
def exec_move(self, start_square, end_square):
move = Move(start_square, end_square)
move.execute()
self.moves.append(move)
self.switch_turn()
return move
def take_back(self):
if self.moves:
last_move = self.moves.pop()
last_move.undo()
self.switch_turn()
return last_move
return None