-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.py
More file actions
74 lines (64 loc) · 3.22 KB
/
algorithm.py
File metadata and controls
74 lines (64 loc) · 3.22 KB
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
from board import Board
from heuristics import Heuristic_Model
import copy
#takes in board state and returns the best move to make based on heuristics
#takes in the depth as to see how many move ahead to look
#need to keep track of the move and the evaluated heuristic value
#need to connect piece color to maximizing_player
class Algorithm:
def __init__(self, depth: int) -> None:
self.model = Heuristic_Model()
self.depth = depth
#returns [value, start_pos, end_pos]
def minimax(self, state: Board, depth: int, maximizing_player: bool, player_color: str) -> list:
#if depth reached or game is over, return the evaluation of the state
if depth == 0 or state.is_game_over():
return self.model.combined_hueristic_evaluation(state)
if maximizing_player:
best_move_value = -float('inf')
best_move = ['','']
#go through all possible moves
pieces = state.pieces[player_color]
for _, positions in pieces.items():
for position in positions:
possible_moves = state.get_moves(position)
#change state, recurse
for move in possible_moves:
#does this function take in code or human?
end_pos = state.to_notation(move)
next_state = copy.deepcopy(state)
next_state.move(position, end_pos)
#see if move better than current best move
new_player_color = 'W' if player_color == 'B' else 'B'
branch_val = self.minimax(next_state, depth - 1, False, new_player_color)
if branch_val > best_move_value:
best_move_value = branch_val
best_move[0] = position
best_move[1]= end_pos
if depth == self.depth:
return best_move
else:
return best_move_value
else:
worst_move_value = float('inf')
#go through all possible moves
pieces = state.pieces[player_color]
for _, positions in pieces.items():
for position in positions:
possible_moves = state.get_moves(position)
#change state, recurse
for move in possible_moves:
#does this function take in code or human?
end_pos = state.to_notation(move)
next_state = copy.deepcopy(state)
next_state.move(position, end_pos)
#see if move better than current best move
new_player_color = 'W' if player_color == 'B' else 'B'
branch_val = self.minimax(next_state, depth - 1, False, new_player_color)
if branch_val < worst_move_value:
worst_move_value = branch_val
return worst_move_value
#returns [start_pos, end_pos]
def get_move(self, state: Board, color: str) -> list:
best_move = self.minimax(state, self.depth, True, color)
return best_move