-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecker_minimax.py
More file actions
134 lines (118 loc) · 4.7 KB
/
checker_minimax.py
File metadata and controls
134 lines (118 loc) · 4.7 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#==============================================
# Filename: checker_minimax.py
#==============================================
# Filetype: Python Source File
#==============================================
# Author(s):
# Angaar Hamid
# Andrew Doan
# Alejandro Ramos
#==============================================
# Last Modified: 12/15/2022
#==============================================
# Description:
# This file is responsible for displaying the
# iterations of the minimax algorithm and
# performing the minimax algorithm.
#==============================================
from copy import deepcopy
from constants import RED, WHITE
import pygame
#==============================================
# Function Name: minimax
#==============================================
# Description: minimax tree used by the A.I.
# to decide upon the best move to make.
# Calculates a value for each move and chooses
# the highest one to return.
#==============================================
# Input: Position of a piece, depth of how far
# in the tree to compute, max_player is to flag
# whether the current caller is a max_player,
# and game is the game this algorithm is
# running in
#==============================================
# Output: The highest score and the move to make
#==============================================
def minimax(position, depth, max_player, game):
if depth == 0 or position.winner() != None:
return position.evaluate(), position
if max_player:
maxEval = float('-inf')
best_move = None
for move in get_all_moves(position, WHITE, game):
evaluation = minimax(move, depth-1, False, game)[0]
maxEval = max(maxEval, evaluation)
if maxEval == evaluation:
best_move = move
return maxEval, best_move
else:
minEval = float('inf')
best_move = None
for move in get_all_moves(position, RED, game):
evaluation = minimax(move, depth-1, True, game)[0]
minEval = min(minEval, evaluation)
if minEval == evaluation:
best_move = move
return minEval, best_move
#==============================================
# Function Name: simulate_move
#==============================================
# Description: makes a specified move onto the
# board and returns the board after the move
# was made
#==============================================
# Input: Piece to move, Move to make, Board
# to use, Game this is running in, and the move
# to not store into the current board being
# displayed
#==============================================
# Output: The modified board after a move
# was made
#==============================================
def simulate_move(piece, move, board, game, skip):
board.move(piece, move[0], move[1])
if skip:
board.remove(skip)
return board
#==============================================
# Function Name: get_all_moves
#==============================================
# Description: Retrieves all the valid moves
# from all the pieces of a certain color
# currently on the board
#==============================================
# Input: Board to scan, Color of pieces to pick,
# and the game this is running in.
#==============================================
# Output: A list of all moves available from
# each piece
#==============================================
def get_all_moves(board, color, game):
moves = []
for piece in board.get_all_pieces(color):
valid_moves = board.get_valid_moves(piece)
for move, skip in valid_moves.items():
draw_moves(game, board, piece)
temp_board = deepcopy(board)
temp_piece = temp_board.get_piece(piece.row_position, piece.column_position)
new_board = simulate_move(temp_piece, move, temp_board, game, skip)
moves.append(new_board)
return moves
#==============================================
# Function Name: draw_moves
#==============================================
# Description: draws the pieces onto the board
# to be displayed
#==============================================
# Input: Game this is running in, board to
# draw on, and piece to move
#==============================================
# Output: None
#==============================================
def draw_moves(game, board, piece):
valid_moves = board.get_valid_moves(piece)
board.draw(game.win)
pygame.draw.circle(game.win, (0,255,0), (piece.x_position, piece.y_position), 50, 5)
game.draw_valid_moves(valid_moves.keys())
pygame.display.update()