-
Notifications
You must be signed in to change notification settings - Fork 0
/
greedy?.py
111 lines (88 loc) · 3.61 KB
/
greedy?.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
"""
Functions implementing the Minimax search algorithm.
"""
import math
import random
from typing import Optional
from structures import CheckersGame, utility, Move
def AggressiveBlack(state: CheckersGame, d: int) -> Move:
"""
Returnss the most aggressive move, containing the move with the highest score (first element) and the score itself
(second element), computed by a search with depth d.
"""
if state.get_winner() is not None and state.get_turn():
poss_moves = state.get_black_moves()
difflist = []
for move in poss_moves:
rednumber = state.red_survivors
new_state = state.copy_and_record_move(move)
diff = rednumber - new_state.red_survivors
difflist.append(diff)
diffmax = max(difflist)
aggmoves = [poss_moves[i] for i in range(0, len(poss_moves)) if difflist[i] == diffmax]
return random.choice(aggmoves)
def AggressiveRed(state: CheckersGame, d: int) -> Move:
"""
Returnss the most aggressive move, containing the move with the highest score (first element) and the score itself
(second element), computed by a search with depth d.
"""
if state.get_winner() is not None and state.get_turn():
poss_moves = state.get_black_moves()
difflist = []
for move in poss_moves:
rednumber = state.red_survivors
new_state = state.copy_and_record_move(move)
diff = rednumber - new_state.red_survivors
difflist.append(diff)
diffmax = max(difflist)
aggmoves = [poss_moves[i] for i in range(0, len(poss_moves)) if difflist[i] == diffmax]
return random.choice(aggmoves)
def maximize(state: CheckersGame, d: int) -> tuple[Optional[Move], float]:
"""
Returns a tuple (move, score), containing the move with the highest score (first element) and the score itself
(second element), computed by a search with depth d.
"""
if d == 0 or (state.get_winner() is not None):
return (None, utility(state))
if state.get_turn() == True: # Black player's turn
poss_moves = state.get_black_moves()
else:
poss_moves = state.get_red_moves()
max_score = -math.inf
max_move = None
for move in poss_moves:
# print(f'move: {move.stone_to_move.ID, move.new_position}')
new_state = state.copy_and_record_move(move)
new_score = minimize(new_state, d-1)[1]
# print('maximizer new_score: ', new_score)
if new_score > max_score:
max_score = new_score
max_move = move
return (max_move, max_score)
def minimize(state: CheckersGame, d: int) -> tuple[Optional[Move], float]:
"""
Returns a tuple (move, score), containing the move with the lower score (i.e. that yields the lowest score)
and the score itself (second element), computed by a search with depth d.
"""
if d == 0 or (state.get_winner() is not None):
return (None, utility(state))
if state.get_turn() == True: # Black player's turn
poss_moves = state.get_black_moves()
else:
poss_moves = state.get_red_moves()
min_score = math.inf
min_move = None
for move in poss_moves:
new_state = state.copy_and_record_move(move)
new_score = maximize(new_state, d-1)[1]
# print('minimizer new_score: ', new_score)
if new_score < min_score:
min_score = new_score
min_move = move
return (min_move, min_score)
def show_board(state: CheckersGame) -> None:
"""
Print a visual of the board based on the passed state.
"""
for col in state.board:
print(col)