-
Notifications
You must be signed in to change notification settings - Fork 0
/
prototype.py
97 lines (69 loc) · 2.58 KB
/
prototype.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
# prototype.py
# Connect Four MiniMax AI
# Ryan Kerr, Milan Ravenell, Matt Tesfalul, Evan Sahdhoefner
# CS51 final project
# Prototype file for minimax algorithm
from board_functions import *
from evaluate import *
# minimax takes a board array and depth int
# minimax outputs the best MOVE int
def minimax(board, depth):
# get array of possible moves
next_moves = possible_moves(board)
best_move = next_moves[0]
best_score = float("-inf")
# go through all of those boards
for move in next_moves:
# create new board from move
new_board = go_next(board, move, "R")
# call min on that new board
board_score = min_player(new_board, depth - 1) + (move % 4)
if board_score > best_score:
best_score = board_score
best_move = move
return best_move
# min player is given a board and choses the lowest option, returns a SCORE
def min_player(board, depth):
if is_terminal(board, "R"):
# this needs to say wether the terminal state is a draw, win, loss
return evaluate(board)
else:
next_moves = possible_moves(board)
min_score = float("inf")
# if end of tree evaluate scores
for move in next_moves:
new_board = go_next(board, move, "Y")
board_score = 0
if depth == 0:
board_score = evaluate(new_board)
else:
board_score = max_player(new_board, depth - 1)
if board_score < min_score:
min_score = board_score
return min_score
# max player picks max move and outputs SCORE
def max_player(board, depth):
if is_terminal(board, "Y"):
# this needs to say wether the terminal state is a draw, win, loss
return evaluate(board)
else:
next_moves = possible_moves(board)
max_score = float("-inf")
# if end of tree, evaluate scores
for move in next_moves:
new_board = go_next(board, move, "R")
board_score = 0
if depth == 0:
board_score = evaluate(new_board)
else:
board_score = min_player(new_board, depth - 1)
if board_score > max_score:
max_score = board_score
return max_score
# outside of minimax call
# if we give minimax a board one move away from winning, it should output the
# winning move. Then we change the board with that move. Then we evaluate if
# that board is terminal and see if:
# is it a draw?
# is it a red player wins?
# is it a black player wins?