-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
65 lines (58 loc) · 2.55 KB
/
board.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
from player import HumanPlayer, ComputerPlayer
from pieces import Rook, Knight, Bishop, Queen, King, Pawn
from view import GameView
from controller import GameManager
class GameBoard:
"""Class that handles the game board"""
def __init__(self):
self.board_state = [None] * 64
self.show_symbols = True
self.view = GameView()
self.game_manager = GameManager(self.view)
self.correlation = {f"{chr(65 + col)}{8 - row}": row * 8 + col for row in range(8) for col in range(8)}
self.pieces = []
self.currently_playing = 'White'
self.show_symbols = True
self.set_initial_pieces()
def set_initial_pieces(self):
"""Set the initial pieces on the board"""
positions = {'Rook': [0, 7, 56, 63], 'Knight': [1, 6, 57, 62], 'Bishop': [2, 5, 58, 61],
'Queen': [3, 59], 'King': [4, 60]}
for piece, places in positions.items():
for position in places:
color = 'White' if position > 7 else 'Black'
self.board_state[position] = globals()[piece](color, position, self)
for i in range(8):
self.board_state[8 + i] = Pawn('Black', 8 + i, self)
self.board_state[48 + i] = Pawn('White', 48 + i, self)
for pos in range(16, 48):
self.board_state[pos] = None
self.pieces = [piece for piece in self.board_state if piece is not None]
def update_positions(self, piece, start_pos, goal_pos, update):
"""Update the positions of the pieces on the board"""
killed_piece = self.board_state[goal_pos]
self.board_state[goal_pos], self.board_state[start_pos] = piece, None
piece.position = goal_pos
if isinstance(piece, Pawn) and piece.upgrade():
self.board_state[goal_pos] = Queen(self.currently_playing, goal_pos, self)
if killed_piece:
self.pieces.remove(killed_piece)
piece.moved = True
def toggle_player(self):
if self.currently_playing == 'White':
self.currently_playing = 'Black'
else:
self.currently_playing = 'White'
def check_for_king(self):
"""Check if the king is alive on the board"""
king_alive = False
for i in self.pieces:
if type(i) == King and i.colour == self.currently_playing:
king_alive = True
break
return king_alive
def get_copy_board_state(self, state=None):
"""Get a copy of the board state"""
if state is None:
state = self.board_state
return state.copy()