Answered step by step
Verified Expert Solution
Link Copied!

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 Minimax/Negamax functions
at each step of the game, (iii) allowing the controls on the GUI to be managed (e.g., 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, GRID_SIZE (Line number 36) 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 alpha-beta pruning) on a 3x3 GRID_SIZE 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 CODE/FUNCTIONS WHERE IT INDICATES "YOUR CODE BELOW" TO COMPLETE THE SECTIONS
"""
import pygame
import numpy as np
from GameStatus_5120 import GameStatus
from multiAgents import minimax, negamax
import sys, random
mode = "player_vs_ai" # default mode for playing the game (player vs AI)
class RandomBoardTicTacToe:
def __init__(self, size =(600,600)):
self.size = self.width, self.height = size
# Define some colors
self.BLACK =(0,0,0)
self.WHITE =(255,255,255)
self.GREEN =(0,255,0)
self.RED =(255,0,0)
# Grid Size
self.GRID_SIZE =4
self. OFFSET =5
self.CIRCLE_COLOR =(140,146,172)
self.CROSS_COLOR =(140,146,172)
# This sets the WIDTH and HEIGHT of each grid location
self.WIDTH = self.size[0]/self.GRID_SIZE - self.OFFSET
self.HEIGHT = self.size[1]/self.GRID_SIZE - self.OFFSET
# This sets the margin between each cell
self.MARGIN =5
# Initialize pygame
pygame.init()
self.game_reset()
def draw_game(self):
# Create a 2 dimensional array using the column and row variables
pygame.init()
self.screen = pygame.display.set_mode(self.size)
pygame.display.set_caption("Tic Tac Toe Random Grid")
self.screen.fill(self.BLACK)
# Draw the grid
"""
YOUR CODE HERE TO DRAW THE GRID OTHER CONTROLS AS PART OF THE GUI
"""
pygame.display.update()
def change_turn(self):
if(self.game_state.turn_O):
pygame.display.set_caption("Tic Tac Toe - O's turn")
else:
pygame.display.set_caption("Tic Tac Toe - X's turn")
def draw_circle(self, x, y):
"""
YOUR CODE HERE TO DRAW THE CIRCLE FOR THE NOUGHTS PLAYER
"""
def draw_cross(self, x, y):
"""
YOUR CODE HERE TO DRAW THE CROSS FOR THE CROSS PLAYER AT THE CELL THAT IS SELECTED VIA THE gui
"""
def is_game_over(self):
"""
YOUR CODE HERE TO SEE IF THE GAME HAS TERMINATED AFTER MAKING A MOVE. YOU SHOULD USE THE IS_TERMINAL()
FUNCTION FROM GAMESTATUS_5120.PY FILE (YOU WILL FIRST NEED TO COMPLETE IS_TERMINAL() FUNCTION)
YOUR RETURN VALUE SHOULD BE TRUE OR FALSE TO BE USED IN OTHER PARTS OF THE GAME
"""
def move(self, move):
self.game_state = self.game_state.get_new_state(move)
def play_ai(self):
"""
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 MINIMAX/NEGAMAX ALGORITHM SHOULD BE THE SCORE, MOVE WHERE SCORE IS AN INTEGER
NUMBER AND MOVE IS AN X,Y LOCATION RETURNED BY THE AGENT
"""
self.change_turn()
pygame.display.update()
terminal = self.game_state.is_terminal()
""" USE self.game_state.get_scores(terminal) HERE TO COMPUTE AND DISPLAY THE FINAL SCORES """
def game_reset(self):
self.draw_game()
"""
YOUR CODE HERE TO RESET THE BOARD TO VALUE 0 FOR ALL CELLS AND CREATE A NEW GAME STATE WITH NEWLY INITIALIZED
BOARD STATE
"""
pygame.display.update()
def play_game(self, mode = "player_vs_ai"):
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
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

Students also viewed these Databases questions

Question

3. What strategies might you use?

Answered: 1 week ago