Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please edit this python code, remove comment(#) and replace it with a code line that completes the code. please don't provide a whole new code

Please edit this python code, remove comment(#) and replace it with a code line that completes the code. please don't provide a whole new code just edit the code below with commented instructions to make the code.

#

from ezgraphics import GraphicsWindow

win = GraphicsWindow(800, 800)

win.setTitle("Tic-Tac-Toe")

canvas = win.canvas()

side=(int)(800/3)

x=[]

for i in range(9):

value = (i % 3) * side

x.append(value)

print(x)

y=[]

for i in range(9):

if i < 3:

y.append(0)

elif i < 6:

y.append(side)

else:

y.append(side*2)

print(y)

boxXY=[]

for i in range(9):

boxXY.append([x[i], y[i], 0 ]) # The third element, initialized to zero is to represent empty, X or O,

print(boxXY) # empty, X or O are the possible values in each box.

# We will use 0 to represent empty, 1 for X and -1 for O.

def ticTacToeBoard():

for i in range(9):

canvas.drawRectangle(x[i], y[i], side, side)

canvas.drawText(x[i]+10, y[i]+10,i+1)

def drawO(x,y,size):

canvas.drawOval(x, y, size, size)

def drawX(x,y):

canvas.drawLine(x, y, x+100, y+100)

canvas.drawLine(x, y+100, x+100, y)

def findBox(): #

box = -1

side=(int)(800/3)

while (box == -1):

boxTuple=win.getMouse() # get the XY coordinate of the mouse.

mouseX = boxTuple[0]

mouseY = boxTuple[1]

for i in range(9):

if(x[i]mouseX):

if(y[i]mouseY):

box = i+1

# Q1: add code to detect empty box

# if the box already has a O or X in it

# if there is no X or O, then return the box number found

# but there is already O or X, then stay in the while loop

# keep waiting for another mouse click

return(box)

def playO(boxnumber):

size=100

margin=(int)((side-size)/2)

x1=x[boxnumber-1]+margin

y1=y[boxnumber-1]+margin

drawO(x1, y1, size)

boxXY[boxnumber][2] = -1 # -1 means played an Oh

def playX(boxnumber):

size=100

margin=(int)((side-size)/2)

x1=x[boxnumber-1]+margin

y1=y[boxnumber-1]+margin

drawX(x1, y1)

boxXY[boxnumber-1][2] = 1 #+1 means played an X

def TicTacToeOver():

#

# Question 2: Please add code to detect game over conditions:

# 1. there is a line forming with 3X or 3 'O'

# 2. All the boxes are played, but no lines (draw)

# Important here is to use the date in boxXY, especially the value of the third element

# boxXY[i][2]. To start, you can make a list with just those nine data values.

#

return(False) # place holder

#

# Below is Placeholder/filler code that allows a human to decide if the game is over...

stop=input("Do you want to end the game? ")

if(stop=="yes"):

return True

else:

return False

def playTicTacToe():

ticTacToeBoard()

playOption = input("O will be first. Please select 1. two human play; 2: human O, computer X play random; 3. computer O play smart, human X; 4. two computers playing random or 5. one computer play random, the other plays smart: ")

if (playOption == "1"):

while 1: # Optional Question: Can you change the loop? max of 9 moves can be made.

oNumber = findBox()

playO(int(oNumber))

if(TicTacToeOver()==True):

#print("Game is over")

break

xNumber = findBox()

playX(int(xNumber))

if(TicTacToeOver()==True):

#print("Game is over")

break

else:

#print("Keep going!")

continue

elif (playOption == "2"):

#

while 1:

oNumber = findBox()

playO(int(oNumber))

if(TicTacToeOver()==True):

#print("Game is over")

break

xNumber = computerPlayTicTacToeRandom()

playX(int(xNumber))

if(TicTacToeOver()==True):

#print("Game is over")

break

else:

#print("Keep going!")

continue

elif (playOption == "3"):

# Question 3, can you make this work?

#

while 1:

oNumber = computerPlayTicTacToeSmart()

playO(oNumber)

if(TicTacToeOver()==True):

#print("Game is over")

break

xNumber = findBox()

playX(int(xNumber))

if(TicTacToeOver()==True):

#print("Game is over")

break

else:

#print("Keep going!")

continue

elif (playOption == "4"):

# Question 4, can you make this option work?

while 1:

oNumber = computerPlayTicTacToeRandom()

playO(int(oNumber))

if(TicTacToeOver()==True):

#print("Game is over")

break

xNumber = computerPlayTicTacToeRandom()

playX(int(xNumber))

if(TicTacToeOver()==True):

#print("Game is over")

break

else:

#print("Keep going!")

continue

elif (playOption == "5"):

while 1:

break

# Question 5: Can you make the option 5 work ?

#

# Question 6: after the game is onver

storePlay(boxXY) # boxXY has the data.

# Add the code to record the Board using an excel spreadsheet with nine columns

# each time the game is played, we APPEND a row into the sheet.

# This will require you to:

# 1. add code to access the openpyxl library in the beginning of the code file

# 2. open the file before the play.

# 3. put the board final config into a list

# 4. write/append the list into EXCEL

# Hint: the boxXY[] array has the data of each play in the third column.

#

#

def computerPlayTicTacToeRandom():

# Question 4.using the code above and the random() in the random library.

print("This does not work yet")

return(0) # Just a placeholder

def twoInALine( xorO ): # the parameter is 1 or -1, representing x or o

return(theThirdBox) # if there is 2 in a line, return the third box to stop the line forming

def computerPlayTicTacToeSmart( xOrO ): # the parameter indicates the opponent's symbol O or X

# make the computer play according to these rules:

# if the there are two of my symbol in a line, play the third to win.

mySymbol = -xOrO # 1 or minus 1

thirdBox = twoInALine( mySymbol )

if ( thirdBox > 0 ):

if (mySymbol == 1 ):

playX(thirBox)

else: playO(thirdBox)

return(thirdBox)

# if the opponent has two in a line, then stop the oppnent

thirdBox = twoInALine( xOrO )

if ( thirdBox > 0):

if (mySymbol == 1 ):

playX(thirBox)

else: playO(thirdBox)

return(thirdBox)

# if the center is empty, play the center

if (boxXY[5][2] == 0):

if (mySymbol == 1 ):

playX(5)

else: playO(5)

# else if the opponent has a middle square on the side, play one of the corner next to the square

# Hint: Please write the function that

# 1. detect the opponent has a middle square on the side

# 2. play the first corner next to the middle square

#

playTicTacToe()

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

More Books

Students also viewed these Databases questions

Question

What is a favorable balance of trade? In what way is it favorable?

Answered: 1 week ago

Question

Express the number as a ratio of integers. 3.417 3.417417417

Answered: 1 week ago