Question: position is a board object. This is the current board configuration. Given the current board configuration, the minimax should provide the optimal board configuration (
position is a board object. This is the current board configuration. Given the current board configuration, the minimax should provide the optimal board configuration best board configuration as we play.In this assignment, you will be able to develop an AI Checkers application using minimax algorithm.
The zip file contains folders assets checkers, minimax and the py file main.py
assets folder: contains resources for the game
checkers folder: contains the following py files
py the state of the board and board graphics
py defines constant parameters for the game
py defines the rules of the game
py defines piece drawing instructions and moving rules
minimax folder: contains the following py file
py contains the minimax algorithm. This is the file you should modify.
main.py file needed to run the simulation.
Part In this part, you need to complete the minimax algorithm in the algorithm.py file. The current function is defined as:
def minimaxposition depth, maxplayer, game:
# if we are in the root node depth and the game hasn't finished yet positionwinner None
# we return the current board position and the evaluation of that position.
if depth or position.winner None:
return position.evaluate position
if maxplayer:
# initialize the max evaluation at inf. This is needed when we compare with next evaluations.
maxEval floatinf'
# bestmove will store the best move we could make. It is initialized as None
bestmove None
# the function getallmoves will provide all possible moves of a given player WHITE in this case
# from its current position. The third argument game is used so that we can draw and update the board game.
for move in getallmovesposition WHITE, game:
# This is a recursive call
# Remember: we only evaluate a position using the minimax algorithm when we reach the end of the tree root node
# Note: minimax returns the best reward maxEval in this case, the maximum the best move that led to that reward.
# In this way, the below means that we only pick the first output maxEval
evaluation minimaxmove depth False, game
maxEval maxmaxEval evaluation
# If the maximum reward we found is equal to the current reward,
# this means that the best move is the current move.
# In this way, we can keep track of the best moves the AI agent can make during the game.
if maxEval evaluation:
bestmove move
return maxEval, bestmove
else:
#### INSERT CODE HERE ####
return minEval, bestmove
where:
depth represents how far we are extending the minimax tree. Every time we evaluate the minimax algorithm, we will decrease the depth by Remember: we only evaluate a position when we reach the end of the tree root node
maxplayer is a boolean value TrueFalse that selects the player maxplayer, if we are maximizing or minplayer if we are minimizing the reward.
game is an object defined in py and is used to draw and update the board as we play.
Given the maxplayer script, add the minplayer part to complete the minimax algorithm.
Part Now that the algorithm is completed and running, change the depth parameter of the minimax tree main.py to and then to and discuss your findings.
main.py:
import pygame
from checkers.constants import WIDTH, HEIGHT, SQUARESIZE, RED, WHITE
from checkers.game import Game
from minimax.algorithm import minimax
FPS
WIN pygame.display.setmodeWIDTH HEIGHT
pygame.display.setcaptionCheckers
def getrowcolfrommousepos:
x y pos
row y SQUARESIZE
col x SQUARESIZE
return row, col
def main:
run True
clock pygame.time.Clock
game GameWIN
depth
while run:
clock.tickFPS
if game.turn WHITE:
value, newboard minimaxgamegetboard depth, WHITE, game
game.aimovenewboard
if game.winner None:
printgamewinner
run False
for event in pygame.event.get:
if event.type pygame.QUIT:
run False
if event.type pygame.MOUSEBUTTONDOWN:
pos pygame.mouse.getpos
row, col getrowcolfrommousepos
game.selectrow col
game.update
pygame.quit
main
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
