forked from cis3296f23/Project-02-checkers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main_Board.py
224 lines (201 loc) · 7.84 KB
/
Main_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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"""
Main_Board.py
The Main_Board File holds the Main_Board class which is responsible for managing the board and the pieces.
"""
import pygame
from constants import BLACK, ROWS, RED, SQUARE_SIZE, COLS, WHITE, CHERRY
from pieces import Piece
class Main_Board:
"""
The Main_Board class is responsible for managing the board and the pieces, and contains functions to draw the board,
evaluate the board, get all pieces, get a single piece, move a piece (left or right), create the board, draw the board,
remove a piece, and check for a winner.
"""
def __init__(self, color):
"""
The init function initializes the Main_Board class with a color and creates the board.
"""
self.board = []
self.color = color
self.red_left = self.white_left = 12
self.red_kings = self.white_kings = 0
self.create_board()
def draw_squares(self, win):
"""
The draw squares function draws the squares on the board.
"""
win.fill(BLACK)
for row in range(ROWS):
for col in range(row % 2, COLS, 2):
pygame.draw.rect(win, self.color, (row*SQUARE_SIZE, col *SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE))
def evaluate(self):
"""
The evaluate function evaluates the board to see who is winning and returns the score.
"""
return self.white_left - self.red_left + (self.white_kings * 0.5 - self.red_kings * 0.5)
def get_all_pieces(self, color):
"""
The get all pieces function gets all the pieces for a given color and returns the pieces.
"""
pieces = []
for row in self.board:
for piece in row:
if piece != 0 and piece.color == color:
pieces.append(piece)
return pieces
def move(self, piece, row, col):
"""
The move function moves a piece to a given row and column. If the selected row is at the end of the board, the piece becomes a king piece.
"""
self.board[piece.row][piece.col], self.board[row][col] = self.board[row][col], self.board[piece.row][piece.col]
piece.move(row, col)
if row == ROWS - 1 or row == 0:
piece.make_king()
if piece.color == WHITE:
self.white_kings += 1
else:
self.red_kings += 1
def get_piece(self, row, col):
"""
The get piece function gets the piece at a given row and column and returns the piece.
"""
try:
return self.board[row][col]
except:
return None
def create_board(self):
"""
The create board function creates the board with the pieces.
"""
for row in range(ROWS):
self.board.append([])
for col in range(COLS):
if col % 2 == ((row + 1) % 2):
if row < 3:
self.board[row].append(Piece(row, col, WHITE))
elif row > 4:
self.board[row].append(Piece(row, col, CHERRY))
else:
self.board[row].append(0)
else:
self.board[row].append(0)
def draw(self, win):
"""
The draw function draws the board and the pieces.
"""
self.draw_squares(win)
for row in range(ROWS):
for col in range(COLS):
piece = self.board[row][col]
if piece != 0:
piece.draw(win)
def remove(self, pieces):
"""
The remove function removes a piece from the board in the event that a piece is jumped.
"""
for piece in pieces:
self.board[piece.row][piece.col] = 0
if piece != 0:
if piece.color == CHERRY:
self.red_left -= 1
else:
self.white_left -= 1
def winner(self):
"""
The winner function checks if a winner has been found and returns the winner. If no winner has been found, None is returned.
If a user has no pieces left or no moves left, the other user is the winner.
"""
if self.red_left <= 0 or self.no_moves(CHERRY):
return WHITE
elif self.white_left <= 0 or self.no_moves(WHITE):
return CHERRY
return None
def get_valid_moves(self, piece):
"""
The get valid moves function gets all the valid moves for a given piece and returns the moves.
"""
moves = {}
left = piece.col - 1
right = piece.col + 1
row = piece.row
if piece.color == CHERRY or piece.king:
moves.update(self.move_left(row -1, max(row-3, -1), -1, piece.color, left))
moves.update(self.move_right(row -1, max(row-3, -1), -1, piece.color, right))
if piece.color == WHITE or piece.king:
moves.update(self.move_left(row +1, min(row+3, ROWS), 1, piece.color, left))
moves.update(self.move_right(row +1, min(row+3, ROWS), 1, piece.color, right))
return moves
def move_left(self, start, stop, step, color, left, skipped=[]):
"""
The move left function moves a piece to the left.
"""
moves = {}
last = []
for r in range(start, stop, step):
if left < 0:
break
current = self.board[r][left]
if current == 0:
if skipped and not last:
break
elif skipped:
moves[(r, left)] = last + skipped
else:
moves[(r, left)] = last
if last:
if step == -1:
row = max(r-3, -1)
else:
row = min(r+3, ROWS)
moves.update(self.move_left(r+step, row, step, color, left-1,skipped=last))
moves.update(self.move_right(r+step, row, step, color, left+1,skipped=last))
break
elif current.color == color:
break
else:
last = [current]
left -= 1
return moves
def move_right(self, start, stop, step, color, right, skipped=[]):
"""
The move right function moves a piece to the right.
"""
moves = {}
last = []
for r in range(start, stop, step):
if right >= COLS:
break
current = self.board[r][right]
if current == 0:
if skipped and not last:
break
elif skipped:
moves[(r,right)] = last + skipped
else:
moves[(r, right)] = last
if last:
if step == -1:
row = max(r-3, -1)
else:
row = min(r+3, ROWS)
moves.update(self.move_left(r+step, row, step, color, right-1,skipped=last))
moves.update(self.move_right(r+step, row, step, color, right+1,skipped=last))
break
elif current.color == color:
break
else:
last = [current]
right += 1
return moves
def no_moves(self, color):
"""
The no moves function checks if there are no moves left for a given color and returns True if there are no moves left.
"""
for row in range(ROWS):
for col in range(COLS):
piece = self.board[row][col]
if piece != 0 and piece.color == color:
valid_moves = self.get_valid_moves(piece)
if valid_moves:
return False
return True