mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-11-22 04:12:22 +01:00
feat: allow for multiple takes in one turn
This commit is contained in:
parent
c8e0b10590
commit
023885402c
56
lab2/main.py
56
lab2/main.py
@ -3,6 +3,7 @@ Program that plays draughts (checkers) with user on 8x8 board using min-max with
|
|||||||
"""
|
"""
|
||||||
import re
|
import re
|
||||||
import copy
|
import copy
|
||||||
|
import math
|
||||||
|
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
@ -16,7 +17,7 @@ class Game:
|
|||||||
def initialize_white(self):
|
def initialize_white(self):
|
||||||
"""Initialize white pieces"""
|
"""Initialize white pieces"""
|
||||||
white_positions = []
|
white_positions = []
|
||||||
for y_coordinate in range(3):
|
for y_coordinate in range(math.floor((self.board_size - 2) / 2)):
|
||||||
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:
|
||||||
@ -31,7 +32,7 @@ class Game:
|
|||||||
def initialize_black(self):
|
def initialize_black(self):
|
||||||
"""Initialize black pieces"""
|
"""Initialize black pieces"""
|
||||||
black_positions = []
|
black_positions = []
|
||||||
for y_coordinate in range(self.board_size - 3, self.board_size):
|
for y_coordinate in range(self.board_size - math.floor((self.board_size - 2) / 2), self.board_size):
|
||||||
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:
|
||||||
@ -273,8 +274,8 @@ class Game:
|
|||||||
(black_position[0], black_position[1]), color)
|
(black_position[0], black_position[1]), color)
|
||||||
|
|
||||||
if len(captures) > 0:
|
if len(captures) > 0:
|
||||||
return captures
|
return (captures, True)
|
||||||
return legal_moves + captures
|
return (legal_moves + captures, False)
|
||||||
|
|
||||||
def alpha_beta(self, depth, alpha_beta, color, current_color=None):
|
def alpha_beta(self, depth, alpha_beta, color, current_color=None):
|
||||||
"""Do alpha beta pruning for given parameters
|
"""Do alpha beta pruning for given parameters
|
||||||
@ -291,7 +292,7 @@ class Game:
|
|||||||
max_eval = float('-inf')
|
max_eval = float('-inf')
|
||||||
best_move = None
|
best_move = None
|
||||||
|
|
||||||
for move in self.get_possible_moves(current_color):
|
for move in self.get_possible_moves(current_color)[0]:
|
||||||
new_state = copy.deepcopy(self)
|
new_state = copy.deepcopy(self)
|
||||||
new_state.make_move(*move, current_color)
|
new_state.make_move(*move, current_color)
|
||||||
eval_, _ = new_state.alpha_beta(
|
eval_, _ = new_state.alpha_beta(
|
||||||
@ -312,7 +313,7 @@ class Game:
|
|||||||
min_eval = float('inf')
|
min_eval = float('inf')
|
||||||
best_move = None
|
best_move = None
|
||||||
|
|
||||||
for move in self.get_possible_moves(current_color):
|
for move in self.get_possible_moves(current_color)[0]:
|
||||||
new_state = copy.deepcopy(self)
|
new_state = copy.deepcopy(self)
|
||||||
new_state.make_move(*move, current_color)
|
new_state.make_move(*move, current_color)
|
||||||
eval_, _ = new_state.alpha_beta(
|
eval_, _ = new_state.alpha_beta(
|
||||||
@ -358,7 +359,7 @@ class Game:
|
|||||||
def handle_player_move(self, color):
|
def handle_player_move(self, color):
|
||||||
"""Prompt player to move, validate their input and make move"""
|
"""Prompt player to move, validate their input and make move"""
|
||||||
has_moved = False
|
has_moved = False
|
||||||
possible_moves = self.get_possible_moves(color)
|
possible_moves = self.get_possible_moves(color)[0]
|
||||||
while not has_moved:
|
while not has_moved:
|
||||||
user_input = input(
|
user_input = input(
|
||||||
f'You are {color}. How do you want to move? (format: d6 e5)\n')
|
f'You are {color}. How do you want to move? (format: d6 e5)\n')
|
||||||
@ -391,25 +392,30 @@ class Game:
|
|||||||
game.print_board(player_color == 'white')
|
game.print_board(player_color == 'white')
|
||||||
if player_color == 'white':
|
if player_color == 'white':
|
||||||
game.handle_player_move('white')
|
game.handle_player_move('white')
|
||||||
|
algorithm_depth = 5
|
||||||
while True:
|
while True:
|
||||||
possible_moves_ai = game.get_possible_moves(ai_color)
|
ai_turn = True
|
||||||
if len(possible_moves_ai) == 0:
|
while ai_turn:
|
||||||
print(f'Game over, {player_color} wins')
|
possible_moves_ai = game.get_possible_moves(ai_color)
|
||||||
return
|
if len(possible_moves_ai[0]) == 0:
|
||||||
_, ai_move = game.alpha_beta(1, (5, 10), ai_color)
|
print(f'Game over, {player_color} wins')
|
||||||
game.make_move(*ai_move, ai_color)
|
return
|
||||||
print(
|
_, ai_move = game.alpha_beta(algorithm_depth, (5, 10), ai_color)
|
||||||
"AI's move: "
|
game.make_move(*ai_move, ai_color)
|
||||||
f"{chr(ord('a')+ai_move[0][0])}{ai_move[0][1]} "
|
print(
|
||||||
f"{chr(ord('a')+ai_move[1][0])}{ai_move[1][1]}")
|
"AI's move: "
|
||||||
game.print_board(player_color == 'white')
|
f"{chr(ord('a')+ai_move[0][0])}{ai_move[0][1]} "
|
||||||
|
f"{chr(ord('a')+ai_move[1][0])}{ai_move[1][1]}")
|
||||||
possible_moves_player = game.get_possible_moves(player_color)
|
game.print_board(player_color == 'white')
|
||||||
if len(possible_moves_player) == 0:
|
ai_turn = game.get_possible_moves(ai_color)[1] and possible_moves_ai[1]
|
||||||
print(f'Game over, {ai_color} wins')
|
player_turn = True
|
||||||
return
|
while player_turn:
|
||||||
game.handle_player_move(player_color)
|
possible_moves_player = game.get_possible_moves(player_color)
|
||||||
|
if len(possible_moves_player[0]) == 0:
|
||||||
|
print(f'Game over, {ai_color} wins')
|
||||||
|
return
|
||||||
|
game.handle_player_move(player_color)
|
||||||
|
player_turn = game.get_possible_moves(player_color)[1] and possible_moves_player[1]
|
||||||
|
|
||||||
|
|
||||||
# Ran first in the code
|
# Ran first in the code
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user