Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use python3 to write the TicTacToe. I have finish the first two part but have trouble with the third part. The question is: 3)Write a

Use python3 to write the TicTacToe. I have finish the first two part but have trouble with the third part. The question is:

3)Write a program that allows two players, x and o, to alternate and provide their moves on the board. For instance asking the question: It is the turn for x . What is your move? The program should check the input and only allow a valid cell between 1 and 9. Also, a cell that is not available (already filled) should not be allowed as a new move. The program should iterate until the game is over and display who won or whether there is a draw.

Here are some examples of output at the end of a game.

x|x|o 7|8|9

----------- -----------

o|o|x 4|5|6

----------- -----------

x|o|x 1|2|3

It's a tie.

x|o|o 7|8|9

----------- -----------

x|x|o 4|5|6

----------- -----------

| |x 1|2|3

x wins. Congrats!

Here is my first two part code:

class TicTacToe:

def __init__(self):

# "board" is a list of 10 strings representing the board (ignore index 0)

self.board = [" "]*10

self.board[0]="#"

#-------------------------------------------------------------

def drawBoard(self):

# This method prints out the board with current plays adjacent to a board with index.

for i in range(len(self.board)):

self.board[i] = self.board[i].center(3)

print('%s|%s|%s'%(self.board[7],self.board[8],self.board[9]), ' 7 | 8 | 9 ', sep= "\t")

print('-----------','-----------', sep = '\t')

print('%s|%s|%s'%(self.board[4],self.board[5],self.board[6]), ' 4 | 5 | 6 ', sep= "\t")

print('-----------','-----------', sep = '\t')

print('%s|%s|%s'%(self.board[1],self.board[2],self.board[3]), ' 1 | 2 | 3 ', sep= "\t")

#-------------------------------------------------------------

def assignMove(self, cell,ch):

self.board[cell] = ch

#-------------------------------------------------------------

def boardFull(self):

if self.board.count(' ') > 0:

return False

else:

return True

#-------------------------------------------------------------

def cellIsEmpty(self, cell):

if self.board[cell] == ' ':

return True

else:

return False

#-------------------------------------------------------------

def whoWon(self):

# returns the symbol of the player who won if there is a winner, otherwise it returns an empty string.

if self.isWinner('x'):

return "x"

elif self.isWinner('o'):

return "o"

else:

return ""

#-------------------------------------------------------------

def isWinner(self, ch):

ch = ch.center(3)

v1 = [self.board[1],self.board[4],self.board[7]]

v2 = [self.board[2],self.board[5],self.board[8]]

v3 = [self.board[3],self.board[6],self.board[9]]

h1 = [self.board[1],self.board[2],self.board[3]]

h2 = [self.board[4],self.board[5],self.board[6]]

h3 = [self.board[7],self.board[8],self.board[9]]

d1 = [self.board[1],self.board[5],self.board[9]]

d2 = [self.board[3],self.board[5],self.board[7]]

list1=[v1,v2,v3,h1,h2,h3,d1,d2]

for i in list1:

if i.count(ch) == 3:

return True

return False

# Given a player's letter, this method returns True if that player has won.

def main():

game = TicTacToe()

game.drawBoard()

Please follow my code to continue. or help me to fix the problem. The problem is when I enter the filled cell, it change again and won't print the sentence.

Here is my rest part of my code:( the mark part is the problem part)

def main():

game = TicTacToe()

game.drawBoard()

num = 0

while game.whoWon() == '':

if num%2 == 0:

ch = input('It is the turn for x . What is your move?')

try:

if int(ch) > 9 or int(ch) < 1:

print('The number should between 1-9')

if not game.board[cell].cellIsEmpty():

print('The number is filled')

except:

print('Please enter a number between 1-9')

game.assignMove(int(ch), 'x')

num += 1

else:

ch = input('It is the turn for o . What is your move?')

try:

if int(ch) > 9 or int(ch) < 1:

print('The number should between 1-9')

if not game.board[cell].cellIsEmpty():

print('The number is filled')

except:

print('Please enter a number between 1-9')

game.assignMove(int(ch), 'o')

num += 1

game.drawBoard()

if game.whoWon() == '' and game.boardFull():

print('It\' a tie.')

break

if game.whoWon() != '':

print(game.whoWon(), 'wins. Congrats!')

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

Database Internals A Deep Dive Into How Distributed Data Systems Work

Authors: Alex Petrov

1st Edition

1492040347, 978-1492040347

More Books

Students also viewed these Databases questions

Question

=+ a. a family deciding whether to buy a new car

Answered: 1 week ago

Question

=+10. How are inflation and unemployment related in the short run?

Answered: 1 week ago