mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-11-26 03:47:18 +01:00
feat: add player move handling and minor fixes
This commit is contained in:
parent
98232581ff
commit
7926a3c170
124
lab2/main.py
124
lab2/main.py
@ -1,7 +1,9 @@
|
||||
import re
|
||||
"""
|
||||
Program that plays draughts (checkers) with user on 8x8 board using min-max with alpha-beta prunning
|
||||
"""
|
||||
|
||||
|
||||
class Game:
|
||||
"""Game"""
|
||||
|
||||
@ -9,8 +11,8 @@ class Game:
|
||||
self.board_size = size
|
||||
self.white_positions = self.initialize_white()
|
||||
self.black_positions = self.initialize_black()
|
||||
print(self.white_positions)
|
||||
print(self.black_positions)
|
||||
# print(self.white_positions)
|
||||
# print(self.black_positions)
|
||||
|
||||
def initialize_white(self):
|
||||
"""Initialize white pieces"""
|
||||
@ -19,10 +21,12 @@ class Game:
|
||||
for x_coordinate in range(self.board_size):
|
||||
if y_coordinate % 2 == 0:
|
||||
if x_coordinate % 2 == 1:
|
||||
white_positions.append((x_coordinate, y_coordinate, False))
|
||||
white_positions.append(
|
||||
(x_coordinate, y_coordinate, False))
|
||||
else:
|
||||
if x_coordinate % 2 == 0:
|
||||
white_positions.append((x_coordinate, y_coordinate, False))
|
||||
white_positions.append(
|
||||
(x_coordinate, y_coordinate, False))
|
||||
return white_positions
|
||||
|
||||
def initialize_black(self):
|
||||
@ -32,28 +36,32 @@ class Game:
|
||||
for x_coordinate in range(self.board_size):
|
||||
if y_coordinate % 2 == 0:
|
||||
if x_coordinate % 2 == 1:
|
||||
black_positions.append((x_coordinate, y_coordinate, False))
|
||||
black_positions.append(
|
||||
(x_coordinate, y_coordinate, False))
|
||||
else:
|
||||
if x_coordinate % 2 == 0:
|
||||
black_positions.append((x_coordinate, y_coordinate, False))
|
||||
black_positions.append(
|
||||
(x_coordinate, y_coordinate, False))
|
||||
return black_positions
|
||||
|
||||
def check_move_out_of_bounds(self, from_, to):
|
||||
"""Check if the move destination is out of the bounds of the board"""
|
||||
if to[0] < 0 or to[0] >= self.board_size:
|
||||
print(f"Illegal move! Final x coordinate must be between 0 and {self.board_size}!")
|
||||
print(
|
||||
f"Illegal move! Final x coordinate must be between 0 and {self.board_size-1}!")
|
||||
if to[1] < 0 or to[1] >= self.board_size:
|
||||
print(f"Illegal move! Final y coordinate must be between 0 and {self.board_size}!")
|
||||
print(
|
||||
f"Illegal move! Final y coordinate must be between 0 and {self.board_size-1}!")
|
||||
return True
|
||||
return False
|
||||
|
||||
def check_piece_exists(self, coords, color):
|
||||
"""Check if a piece of given color exists at a given spot"""
|
||||
if color == "white":
|
||||
if (*coords, False) | (*coords, False) in self.white_positions:
|
||||
if any(piece in self.white_positions for piece in ((*coords, False), (*coords, False))):
|
||||
return True
|
||||
else:
|
||||
if (*coords, False) | (*coords, False) in self.black_positons:
|
||||
if any(piece in self.black_positions for piece in ((*coords, False), (*coords, False))):
|
||||
return True
|
||||
return False
|
||||
|
||||
@ -97,18 +105,21 @@ class Game:
|
||||
print("Illegal move! Final position is out of the bounds of the board")
|
||||
return False
|
||||
if not self.check_piece_exists(from_, color):
|
||||
print("Illegal move! There is no piece on the starting position that belongs to the player")
|
||||
print(
|
||||
"Illegal move! There is no piece on the starting position that belongs to the player")
|
||||
return False
|
||||
if self.check_piece_exists(to, "white") or check_piece_exists(to, "black"):
|
||||
if self.check_piece_exists(to, "white") or self.check_piece_exists(to, "black"):
|
||||
print("Illegal move! Cannot move to position taken by another piece")
|
||||
return False
|
||||
capture = self.check_capture(from_, to, color)
|
||||
if capture == None:
|
||||
return self.check_move_piece_capable(from_, to, color)
|
||||
if self.check_move_piece_capable(from_, to, color):
|
||||
return True
|
||||
else:
|
||||
print('Illegal move!')
|
||||
return False
|
||||
return capture
|
||||
|
||||
|
||||
|
||||
def make_move(self, from_, to, color):
|
||||
"""Move a piece on the board and remove any captured pieces"""
|
||||
move_legal = self.check_move_legal(from_, to, color)
|
||||
@ -118,21 +129,20 @@ class Game:
|
||||
king = self.check_piece_king(from_, color)
|
||||
|
||||
if (color == "white"):
|
||||
self.white_pieces.remove(*from_, king)
|
||||
self.white_pieces.append(*to, king)
|
||||
self.white_positions.remove((*from_, king))
|
||||
self.white_positions.append((*to, king))
|
||||
if (capture):
|
||||
captured_king = self.check_piece_king(capture, "black")
|
||||
self.black_pieces.remove(*move_legal, captured_king)
|
||||
self.black_positions.remove((*move_legal, captured_king))
|
||||
|
||||
else:
|
||||
self.black_pieces.remove(*from_, king)
|
||||
self.black_pieces.append(*to, king)
|
||||
self.black_positions.remove((*from_, king))
|
||||
self.black_positions.append((*to, king))
|
||||
if (capture):
|
||||
captured_king = self.check_piece_king(capture, "white")
|
||||
self.white_pieces.remove(*move_legal, captured_king)
|
||||
self.white_positions.remove((*move_legal, captured_king))
|
||||
return True
|
||||
|
||||
|
||||
def print_board(self):
|
||||
"""Print the board in the console"""
|
||||
print(" ", end="")
|
||||
@ -187,50 +197,6 @@ class Game:
|
||||
# for s
|
||||
pass
|
||||
|
||||
def alpha_beta(self, depth, alpha, beta, player):
|
||||
if depth == 0:
|
||||
return self.evaluate(), None
|
||||
|
||||
if player == 'x':
|
||||
max_eval = float('-inf')
|
||||
best_move = None
|
||||
|
||||
for move in state.get_possible_moves(player):
|
||||
new_state = copy.deepcopy(state)
|
||||
new_state.move(*move)
|
||||
eval, _ = alpha_beta(new_state, depth-1, alpha, beta, 'o')
|
||||
|
||||
if eval > max_eval:
|
||||
max_eval = eval
|
||||
best_move = move
|
||||
|
||||
alpha = max(alpha, eval)
|
||||
|
||||
if alpha >= beta:
|
||||
break
|
||||
|
||||
return max_eval, best_move
|
||||
|
||||
else:
|
||||
min_eval = float('inf')
|
||||
best_move = None
|
||||
|
||||
for move in state.get_possible_moves(player):
|
||||
new_state = copy.deepcopy(state)
|
||||
new_state.move(*move)
|
||||
eval, _ = alpha_beta(new_state, depth-1, alpha, beta, 'x')
|
||||
|
||||
if eval < min_eval:
|
||||
min_eval = eval
|
||||
best_move = move
|
||||
|
||||
beta = min(beta, eval)
|
||||
|
||||
if beta <= alpha:
|
||||
break
|
||||
|
||||
return min_eval, best_move
|
||||
|
||||
def evaluate(self):
|
||||
white_score = 0
|
||||
black_score = 0
|
||||
@ -247,9 +213,31 @@ class Game:
|
||||
else:
|
||||
black_score += 2
|
||||
|
||||
return x_score - o_score
|
||||
return black_score - white_score
|
||||
|
||||
def input_to_coordinates(self, input):
|
||||
pos_x = ord(input[0])-ord('a')
|
||||
pos_y = int(input[1::])
|
||||
return pos_x, pos_y
|
||||
|
||||
def handle_player_move(self, color):
|
||||
has_moved = False
|
||||
while not has_moved:
|
||||
user_input = input('How do you want to move? (format: d6 e5)\n')
|
||||
regex = r"^[A-Za-z]\d+\s[A-Za-z]\d+$"
|
||||
match = re.search(regex, user_input)
|
||||
if not match:
|
||||
print('Invalid input, try again')
|
||||
continue
|
||||
[move_from, move_to] = user_input.split(' ')
|
||||
from_coordinates = self.input_to_coordinates(move_from)
|
||||
to_coordinates = self.input_to_coordinates(move_to)
|
||||
has_moved = self.make_move(
|
||||
from_coordinates, to_coordinates, color)
|
||||
self.print_board()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
game = Game(8)
|
||||
game.print_board()
|
||||
|
||||
game.handle_player_move('black')
|
||||
|
||||
Loading…
Reference in New Issue
Block a user