Answered step by step
Verified Expert Solution
Question
1 Approved Answer
need to create a tic tac toe board using python and pygame including the menu options as shown in pictures. base code below, focused on
need to create a tic tac toe board using python and pygame including the menu options as shown in pictures. base code below, focused on board and menu creation not worried about ai aspect
PLEASE READ THE COMMENTS BELOW AND THE HOMEWORK DESCRIPTION VERY CAREFULLY BEFORE YOU START CODING
The file where you will need to create the GUI which should include i drawing the grid, ii call your MinimaxNegamax functions
at each step of the game, iii allowing the controls on the GUI to be managed eg setting board size, using
Minimax or Negamax, and other options
In the example below, grid creation is supported using pygame which you can use. In the init function, GRIDSIZE Line number is the variable that
sets the size of the grid. Once you have the Minimax code written in multiAgents.py file, it is recommended to test
your algorithm with alphabeta pruning on a x GRIDSIZE to see if the computer always tries for a draw and does
not let you win the game.
PLEASE CAREFULLY SEE THE PORTIONS OF THE CODEFUNCTIONS WHERE IT INDICATES "YOUR CODE BELOW" TO COMPLETE THE SECTIONS
import pygame
import numpy as np
from GameStatus import GameStatus
from multiAgents import minimax, negamax
import sys random
mode "playervsai # default mode for playing the game player vs AI
class RandomBoardTicTacToe:
def initself size :
self.size self.width, self.height size
# Define some colors
self.BLACK
self.WHITE
self.GREEN
self.RED
# Grid Size
self.GRIDSIZE
self. OFFSET
self.CIRCLECOLOR
self.CROSSCOLOR
# This sets the WIDTH and HEIGHT of each grid location
self.WIDTH self.sizeselfGRIDSIZE self.OFFSET
self.HEIGHT self.sizeselfGRIDSIZE self.OFFSET
# This sets the margin between each cell
self.MARGIN
# Initialize pygame
pygame.init
self.gamereset
def drawgameself:
# Create a dimensional array using the column and row variables
pygame.init
self.screen pygame.display.setmodeselfsize
pygame.display.setcaptionTic Tac Toe Random Grid"
self.screen.fillselfBLACK
# Draw the grid
YOUR CODE HERE TO DRAW THE GRID OTHER CONTROLS AS PART OF THE GUI
pygame.display.update
def changeturnself:
ifselfgamestate.turnO:
pygame.display.setcaptionTic Tac Toe Os turn"
else:
pygame.display.setcaptionTic Tac Toe Xs turn"
def drawcircleself x y:
YOUR CODE HERE TO DRAW THE CIRCLE FOR THE NOUGHTS PLAYER
def drawcrossself x y:
YOUR CODE HERE TO DRAW THE CROSS FOR THE CROSS PLAYER AT THE CELL THAT IS SELECTED VIA THE gui
def isgameoverself:
YOUR CODE HERE TO SEE IF THE GAME HAS TERMINATED AFTER MAKING A MOVE. YOU SHOULD USE THE ISTERMINAL
FUNCTION FROM GAMESTATUSPY FILE YOU WILL FIRST NEED TO COMPLETE ISTERMINAL FUNCTION
YOUR RETURN VALUE SHOULD BE TRUE OR FALSE TO BE USED IN OTHER PARTS OF THE GAME
def moveself move:
self.gamestate self.gamestate.getnewstatemove
def playaiself:
YOUR CODE HERE TO CALL MINIMAX OR NEGAMAX DEPENDEING ON WHICH ALGORITHM SELECTED FROM THE GUI
ONCE THE ALGORITHM RETURNS THE BEST MOVE TO BE SELECTED, YOU SHOULD DRAW THE NOUGHT OR CIRCLE DEPENDING
ON WHICH SYMBOL YOU SELECTED FOR THE AI PLAYER
THE RETURN VALUES FROM YOUR MINIMAXNEGAMAX ALGORITHM SHOULD BE THE SCORE, MOVE WHERE SCORE IS AN INTEGER
NUMBER AND MOVE IS AN XY LOCATION RETURNED BY THE AGENT
self.changeturn
pygame.display.update
terminal self.gamestate.isterminal
USE self.gamestate.getscoresterminal HERE TO COMPUTE AND DISPLAY THE FINAL SCORES
def gameresetself:
self.drawgame
YOUR CODE HERE TO RESET THE BOARD TO VALUE FOR ALL CELLS AND CREATE A NEW GAME STATE WITH NEWLY INITIALIZED
BOARD STATE
pygame.display.update
def playgameself mode "playervsai:
done False
clock pygame.time.Clock
while not done:
for event in pygame.event.get: # User did something
YOUR CODE HERE TO CHECK IF THE USER CLICKED ON A GRID ITEM. EXIT THE GAME IF THE USER CLICKED EXIT
YOUR CODE HERE TO HANDLE THE SITUATION
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started