Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete the following code Using python turtle and follow these instruction: a. First player is always x. Then each turn goes to the next player.

Complete the following code Using python turtle and follow these instruction:

a. First player is always x. Then each turn goes to the next player. b. X must be drawn in red color in the center of the square. c. O must be drawn in red color in the center of the square. d. The game must not allow choosing the same square twice. e. The game must not allow further playing if there is a winner or if it is a tie. f. The game should announce the winner name if it is detected. g. The game must not allow any clicking outside the grid.

please continue writing on the code below and USE (import turtle)

Code:

import turtle as t import functions as f

f.reset(None,None) t.onscreenclick(f.mark,btn = 1) t.onscreenclick(f.reset,btn = 3) t.done()

def mark(x,y): # This function is called when the left mouse button is clicked the input argument x and y are the screen coordinate. t.penup() #delete this t.goto(x,y) #delete this t.pendown() #delete this t.circle(40) #delete this

#------------------------------------------------------------------------------------------------ #do not change the code below def reset(x,y): #resets when right mouse button is clicked t.reset() t.delay(0) t.hideturtle() draw_grid() t.pensize(5) shared_variables.x00 = None shared_variables.x01 = None shared_variables.x02 = None shared_variables.x10 = None shared_variables.x11 = None shared_variables.x12 = None shared_variables.x20 = None shared_variables.x21 = None shared_variables.x22 = None shared_variables.x_is_playing = True shared_variables.flag_status = True

def draw_grid(): #Draws the grid. The width and hight of each square is 100. The first vertical line from the left starts at -150. the first horizontal line from the bottom starts at -150 for i in range(4): t.penup() t.goto(-150+100*i,150) t.pendown() t.goto(-150+100*i,-150) for i in range(4): t.penup() t.goto(-150,150-100*i) t.pendown() t.goto(150,150-100*i) class shared_variables: #this is a class. you did not take this yet, but we will use it anyway. The variable in the class will be used as a global variable so we can store values to them and use them between functions. x00 = None #grid variables to store 0 for X or 1 for O for each position. x01 = None # x02 | x12 | x22 x02 = None #---------------- x10 = None # x01 | x11 | x21 x11 = None #---------------- x12 = None # x00 | x10 | x20 x20 = None x21 = None x22 = None x_is_playing = True #True if it is X's turn. Flase if it O's turn flag_status = True #True if the game is still going. Flase if the game is finished. def assign_avariables(x,y,value): # gives the grid variable 0 for X or 1 for O. make sure that the input arguments x and y are either 0, 1, or 2. if x == 0 and y == 0: shared_variables.x00 = value elif x == 0 and y == 1: shared_variables.x01 = value elif x == 0 and y == 2: shared_variables.x02 = value elif x == 1 and y == 0: shared_variables.x10 = value elif x == 1 and y == 1: shared_variables.x11 = value elif x == 1 and y == 2: shared_variables.x12 = value elif x == 2 and y == 0: shared_variables.x20 = value elif x == 2 and y == 1: shared_variables.x21 = value elif x == 2 and y == 2: shared_variables.x22 = value def check_location(x,y): #checks if the location is empty (True) or used before (False). Make sure that the input arguments x and y are either 0, 1, or 2. if x == 0 and y == 0 and shared_variables.x00 != None: return False elif x == 0 and y == 1 and shared_variables.x01 != None: return False elif x == 0 and y == 2 and shared_variables.x02 != None: return False elif x == 1 and y == 0 and shared_variables.x10 != None: return False elif x == 1 and y == 1 and shared_variables.x11 != None: return False elif x == 1 and y == 2 and shared_variables.x12 != None: return False elif x == 2 and y == 0 and shared_variables.x20 != None: return False elif x == 2 and y == 1 and shared_variables.x21 != None: return False elif x == 2 and y == 2 and shared_variables.x22 != None: return False else: return True def check_status(): #checks if the game is still running (returns None) or if it is finished (returns 0 for X or 1 for O). if shared_variables.x00 == shared_variables.x11 == shared_variables.x22 != None: #x00 return shared_variables.x00 elif shared_variables.x00 == shared_variables.x10 == shared_variables.x20 != None: return shared_variables.x00 elif shared_variables.x00 == shared_variables.x01 == shared_variables.x02 != None: return shared_variables.x00 elif shared_variables.x01 == shared_variables.x11 == shared_variables.x21 != None: #x01 return shared_variables.x01 elif shared_variables.x02 == shared_variables.x12 == shared_variables.x22 != None: #x02 return shared_variables.x02 elif shared_variables.x02 == shared_variables.x11 == shared_variables.x20 != None: return shared_variables.x02 elif shared_variables.x10 == shared_variables.x11 == shared_variables.x12 != None: #x10 return shared_variables.x10 elif shared_variables.x22 == shared_variables.x21 == shared_variables.x20 != None: #x22 return shared_variables.x22 else: return None

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

Step: 3

blur-text-image

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

More Books

Students also viewed these Databases questions