Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Do In Python Using the model for ticket-tac-toe described in chapter 5 create a

ID: 3735589 • Letter: D

Question

Do In Python Using the model for ticket-tac-toe described in chapter 5 create a complete tic-tac-toe game. The game should be able to determine if there is a winner and display who the winner is X or O. The game should have 1 player and two player mode. 2 player mode you would take turns with a friend. 1 player mode the computer would automatically select a free space. The game should only allow the user to select a free space for their move. After the game has finished and reported a winner the program should ask if the user would like to play again. - 1 or 2 player mode for tic-tac-toe game error check space entered -display the winner at the end of each game - ask users to play again -the model for the tic-tac-toe board should be a dictionary that was defined in chapter 5 -only allow users to move to a space that is not occupied and is valid

Explanation / Answer

// If you find any error feel free to ask

import random

def DrawBox(array):

print(' | | ')

print(' '+array[1]+' | '+array[2]+' | '+array[3])

print(' | | ')

print('***********************')

print(' | | ')

print(' '+array[4]+' | '+array[5]+' | '+array[6])

print(' | | ')

print('***********************')

print(' | | ')

print(' '+array[7]+' | '+array[8]+' | '+array[9])

print(' | | ')

print('***********************')

def PlayerSymbol():

no=random.randint(1,2)

if(no==1):

return ['X','O']

else:

return ['O','X']

def WhoWillStart():

no=random.randint(1,2)

if(no==1):

return 'player'

else:

return 'computer'

def PlayMore():

print("Do You want to play ? (Y/N)")

yes=input()

if(yes=='Y' or yes=='y' or yes=='YES' or yes=='yes'):

return True

else:

return False

  

def PlayTurn(box,n,letter):

box[n]=letter

def IsWin(box,symbol):

if(box[1]==symbol and box[2]==symbol and box[3]==symbol):

return True

if(box[1]==symbol and box[5]==symbol and box[9]==symbol):

return True

if(box[1]==symbol and box[4]==symbol and box[7]==symbol):

return True

if(box[2]==symbol and box[5]==symbol and box[8]==symbol):

return True

if(box[4]==symbol and box[5]==symbol and box[6]==symbol):

return True

if(box[3]==symbol and box[6]==symbol and box[9]==symbol):

return True

if(box[3]==symbol and box[5]==symbol and box[7]==symbol):

return True

if(box[7]==symbol and box[8]==symbol and box[9]==symbol):

return True

else:

return False

def GetDuplicate(box):

dupl=[]

for i in box:

dupl.append(i)

return dupl

def IsFree(box,n):

if(box[n]==' '):

return True

else:

return False

def PlayerInput(box):

play=' '

while play not in '1 2 3 4 5 6 7 8 9'.split() or not IsFree(box,int(play)):

print('play your turn between (1-9)')

play=input()

return int(play)

def ChooseRandom(box):

freelist=[]

i=0

while i<10:

if(IsFree(box,i)):

freelist.append(i)

i+=1

return random.choice(freelist)

def ComputerInput(box,computer):

if(computer=='X'):

player='O'

else:

player='X'

for i in range(1,10):

duplBox=GetDuplicate(box)

if(IsFree(duplBox,i)):

PlayTurn(duplBox,i,computer)

if(IsWin(duplBox,computer)):

return i

for i in range(1,10):

duplBox=GetDuplicate(box)

if(IsFree(duplBox,i)):

PlayTurn(duplBox,i,player)

if(IsWin(duplBox,player)):

return i

play=ChooseRandom(box)

return play

def IsBoxFull(box):

for i in range(1,10):

if(box[i]==' '):

return False

return True

print("Welcome To The Tic-Tac-Toi Game")

while True:

box=[' ']*10

print('1 Player Mode or 2 Player Mode')

mode = input()

if(mode=='1'):

player,computer=PlayerSymbol()

print('Symbol of Computer : '+computer)

print('Symbol of player : '+player)

turn=WhoWillStart()

print('The '+turn+' will start')

flag=True

while flag:

if turn=='player':

DrawBox(box)

pinput=PlayerInput(box)

PlayTurn(box,pinput,player)

if IsWin(box,player)==True:

DrawBox(box)

print("Yeeee !!!!! We have won the match")

flag=False

else:

if(IsBoxFull(box)):

DrawBox(box)

print("ohhhh !!!!! match tied")

flag=False

else:

turn='computer'

  

else:

#DrawBox(box)

cinput=ComputerInput(box,computer)

PlayTurn(box,cinput,computer)

if IsWin(box,computer)==True:

DrawBox(box)

print("Fushhhh !!!!! You lose Computer Won")

flag=False

  

else:

if(IsBoxFull(box)):

DrawBox(box)

print("ohhhh !!!!! match tied")

flag=False

else:

turn='player'

elif(mode=='2'):

player1,player2=PlayerSymbol()

print('Symbol of player2 : '+player2)

print('Symbol of player1 : '+player1)

turn=WhoWillStart()

print('The '+turn+' will start')

flag=True

while flag:

if turn=='player1':

DrawBox(box)

pinput=PlayerInput(box)

PlayTurn(box,pinput,player1)

if IsWin(box,player1)==True:

DrawBox(box)

print("Yeeee !!!!! Player1 have won the match")

flag=False

else:

if(IsBoxFull(box)):

DrawBox(box)

print("ohhhh !!!!! match tied")

flag=False

else:

turn='player2'

  

else:

DrawBox(box)

pinput=PlayerInput(box)

PlayTurn(box,pinput,player2)

if IsWin(box,player2)==True:

DrawBox(box)

print("Yeeee !!!!! Player2 have won the match")

flag=False

else:

if(IsBoxFull(box)):

DrawBox(box)

print("ohhhh !!!!! match tied")

flag=False

else:

turn='player1'

else:

print("choose wrong option")

if(PlayMore()!=True):

break

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote