Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Tic Tac Toe in Python 3 The program is supposed to prompt two players to alternately enter token X and token O. Whenever a token

Tic Tac Toe in Python 3

The program is supposed to prompt two players to alternately enter token X and token O. Whenever a token is entered, the program redisplays the board on the console and determines the status of the game (win, draw, or continue).

I have the token part completed. It is letting me enter tokens but is not stopping the game when someone wins. I am not sure what I need to move around to make it stop the game to show the status (win or draw). This is what I have so far:

board = ['','','','','','','','','',''] player = 1

running = 0 tie = -1 win = 1

game = running mark = 'X'

def draw_board(): print("________________") print(board[1]," | ", board[2], " | ", board[3]) print("________________") print(board[4], " | ", board[5], " | ", board[6]) print("________________") print(board[7]," | ", board[8]," | ", board[9]) print("________________") def xo_spot(row,column): if row==0 and column==0: spot=1 elif row==0 and column==1: spot=2 elif row==0 and column==2: spot=3 elif row==1 and column==0: spot=4 elif row==1 and column==1: spot=5 elif row==1 and column==2: spot=6 elif row==2 and column==0: spot=7 elif row==2 and column==1: spot=8 elif row==2 and column==2: spot=9 return spot

def check_xo_spot(x): if(board[x] == ''): return True else: return False

def xo_win(): if(board[1]==board[2] and board[2]==board[3] and board[1]!=''): game=win elif(board[4]==board[5] and board[5]==board[6] and board[4]!=''): game=win elif(board[7]==board[8] and board[8]==board[9] and board[7]!=''): game=win elif(board[1]==board[4] and board[4]==board[7] and board[1]!=''): game=win elif(board[2]==board[5] and board[5]==board[8] and board[2]!=''): game=win elif(board[3]==board[6] and board[6]==board[9] and board[3]!=''): game=win elif(board[1]==board[5] and board[5]==board[9] and board[5]!=''): game=win elif(board[3]==board[5] and board[5]==board[7] and board[5]!=''): game=win elif(board[1]!='' and board[2]!='' and board[3]!='' and board[4]!='' and board[5]!='' and board[6]!='' and board[7]!='' and board[8]!='' and board[9]!=''): game=tie else: game=running def main(): player = 1 while(game==running): draw_board() if(player % 2 != 0): row=int(input("Enter a row (0,1,2) for player X: ")) column=int(input("Enter a column (0,1,2) for player X: ")) spot=xo_spot(row,column) mark='X' else: row=int(input("Enter a row (0,1,2) for player O: ")) column=int(input("Enter a column (0,1,2) for player O: ")) spot=xo_spot(row,column) mark='O' if(check_xo_spot(spot)): board[spot]=mark player+=1 xo_win() draw_board() if(game==tie): print("the game is a tie") elif(game==win): player-=1 if(player%2!=0): print("the winner is player X") else: print("the winner is player O") main()

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

DNA Databases

Authors: Stefan Kiesbye

1st Edition

0737758910, 978-0737758917

More Books

Students also viewed these Databases questions