Merge branch 'lab2' into alpha-beta

This commit is contained in:
Krzysztof Rudnicki 2023-04-08 16:03:47 +02:00
commit 3b327be121

View File

@ -1,7 +1,9 @@
import re
""" """
Program that plays draughts (checkers) with user on 8x8 board using min-max with alpha-beta prunning Program that plays draughts (checkers) with user on 8x8 board using min-max with alpha-beta prunning
""" """
class Game: class Game:
"""Game""" """Game"""
@ -9,8 +11,8 @@ class Game:
self.board_size = size self.board_size = size
self.white_positions = self.initialize_white() self.white_positions = self.initialize_white()
self.black_positions = self.initialize_black() self.black_positions = self.initialize_black()
print(self.white_positions) # print(self.white_positions)
print(self.black_positions) # print(self.black_positions)
def initialize_white(self): def initialize_white(self):
"""Initialize white pieces""" """Initialize white pieces"""
@ -19,10 +21,12 @@ class Game:
for x_coordinate in range(self.board_size): for x_coordinate in range(self.board_size):
if y_coordinate % 2 == 0: if y_coordinate % 2 == 0:
if x_coordinate % 2 == 1: if x_coordinate % 2 == 1:
white_positions.append((x_coordinate, y_coordinate, False)) white_positions.append(
(x_coordinate, y_coordinate, False))
else: else:
if x_coordinate % 2 == 0: if x_coordinate % 2 == 0:
white_positions.append((x_coordinate, y_coordinate, False)) white_positions.append(
(x_coordinate, y_coordinate, False))
return white_positions return white_positions
def initialize_black(self): def initialize_black(self):
@ -32,10 +36,12 @@ class Game:
for x_coordinate in range(self.board_size): for x_coordinate in range(self.board_size):
if y_coordinate % 2 == 0: if y_coordinate % 2 == 0:
if x_coordinate % 2 == 1: if x_coordinate % 2 == 1:
black_positions.append((x_coordinate, y_coordinate, False)) black_positions.append(
(x_coordinate, y_coordinate, False))
else: else:
if x_coordinate % 2 == 0: if x_coordinate % 2 == 0:
black_positions.append((x_coordinate, y_coordinate, False)) black_positions.append(
(x_coordinate, y_coordinate, False))
return black_positions return black_positions
def check_move_out_of_bounds(self, from_, to): def check_move_out_of_bounds(self, from_, to):
@ -81,7 +87,7 @@ class Game:
def check_capture(self, from_, to, color): def check_capture(self, from_, to, color):
"""Check if a piece was captured for a given move. Return captured piece coordinates or None""" """Check if a piece was captured for a given move. Return captured piece coordinates or None"""
# captures can only happen if the player moved twice-diagonally # captures can only happen if the player moved twice-diagonally
if(abs(to[0]-from_[0])!=2 or abs(to[1]-from_[1]!=2)): if (abs(to[0]-from_[0]) != 2 or abs(to[1]-from_[1] != 2)):
return None return None
middle = (abs(to[0]-from_[0])//2, abs(to[1]-from_[1])//2) middle = (abs(to[0]-from_[0])//2, abs(to[1]-from_[1])//2)
@ -98,18 +104,21 @@ class Game:
print("Illegal move! Final position is out of the bounds of the board") print("Illegal move! Final position is out of the bounds of the board")
return False return False
if not self.check_piece_exists(from_, color): 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 return False
if self.check_piece_exists(to, "white") or self.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") print("Illegal move! Cannot move to position taken by another piece")
return False return False
capture = self.check_capture(from_, to, color) capture = self.check_capture(from_, to, color)
if capture == None: 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 return capture
def make_move(self, from_, to, color): def make_move(self, from_, to, color):
"""Move a piece on the board and remove any captured pieces""" """Move a piece on the board and remove any captured pieces"""
move_legal = self.check_move_legal(from_, to, color) move_legal = self.check_move_legal(from_, to, color)
@ -118,22 +127,21 @@ class Game:
capture = move_legal if type(move_legal) is tuple else None capture = move_legal if type(move_legal) is tuple else None
king = self.check_piece_king(from_, color) king = self.check_piece_king(from_, color)
if(color == "white"): if (color == "white"):
self.white_pieces.remove(*from_, king) self.white_positions.remove((*from_, king))
self.white_pieces.append(*to, king) self.white_positions.append((*to, king))
if(capture): if (capture):
captured_king = self.check_piece_king(capture, "black") 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: else:
self.black_pieces.remove(*from_, king) self.black_positions.remove((*from_, king))
self.black_pieces.append(*to, king) self.black_positions.append((*to, king))
if(capture): if (capture):
captured_king = self.check_piece_king(capture, "white") 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 return True
def print_board(self): def print_board(self):
"""Print the board in the console""" """Print the board in the console"""
print(" ", end="") print(" ", end="")
@ -143,15 +151,15 @@ class Game:
line = 0 line = 0
for y in range(self.board_size*4): for y in range(self.board_size*4):
for x in range(self.board_size): for x in range(self.board_size):
bg = "#" if x%2==(y//4)%2 else " " bg = "#" if x % 2 == (y//4) % 2 else " "
checker = bg checker = bg
if((x, y//4, False) in self.white_positions): if ((x, y//4, False) in self.white_positions):
checker = "w" checker = "w"
elif((x, y//4, True) in self.white_positions): elif ((x, y//4, True) in self.white_positions):
checker = "W" checker = "W"
elif((x, y//4, False) in self.black_positions): elif ((x, y//4, False) in self.black_positions):
checker = "b" checker = "b"
elif((x, y//4, True) in self.black_positions): elif ((x, y//4, True) in self.black_positions):
checker = "B" checker = "B"
if(x==0): if(x==0):
@ -180,23 +188,7 @@ class Game:
print(f" {chr(ord('a')+x)}", end=" ") print(f" {chr(ord('a')+x)}", end=" ")
print() print()
def get_possible_move_up(self, from_, color): # Ran first in the code
# all moves "up":
legal_moves = []
move_up_left_two = (from_[0] - 2, from_[1] - 2)
move_up_right_two = (from_[0] - 2, from_[1] + 2)
move_up_left_one = (from_[0] - 1, from_[1] - 1)
move_up_right_one = (from_[0] - 1, from_[1] + 1)
if self.check_move_legal(from_, move_up_left_two, color) != False:
legal_moves.append((from_, move_up_two))
elif self.check_move_legal(from_, move_up_right_two, color) != False:
legal_moves.append((from_, move_up_right_two))
elif self.check_move_legal(from_, move_up_left_one, color) != False:
legal_moves.append((from_, move_up_left_one))
elif self.check_move_legal(from_, move_up_right_one, color) != False:
legal_moves.append((from_, move_up_right_one))
return legal_moves
def get_possible_move_down(self, from_, color): def get_possible_move_down(self, from_, color):
# all moves "down": # all moves "down":
legal_moves = [] legal_moves = []
@ -298,10 +290,31 @@ class Game:
return x_score - o_score return x_score - o_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()
# Ran first in the code # Ran first in the code
if __name__ == "__main__": if __name__ == "__main__":
game = Game(8) game = Game(8)
game.print_board() game.print_board()
print(game.get_possible_moves("white")) game.handle_player_move('black')
print(game.get_possible_moves("black"))