Question: A pyhton TicTacToe game (command line), The incomplete code are showing below in text. Please finish it. class TicTacToe: def __init__(self): # Use as is
A pyhton TicTacToe game (command line), The incomplete code are showing below in text. Please finish it. class TicTacToe: def __init__(self): # Use as is """ initializes data fields (board and played) and prints the banner messages and prints the initial board on the screen """ self.board = [' '] * 9 # A list of 9 strings, one for each cell, # will contain ' ' or 'X' or 'O' self.played = set() # Set (of cell num) already played: to keep track of the played cells print("Welcome to Tic-Tac-Toe!") print("You play X (first move) and computer plays O.") print("Computer plays randomly, not strategically.") self.printBoard() def printBoard(self) -> None: """ prints the board on the screen based on the values in the self.board data field """ pass #To Implement def playerNextMove(self) -> None: """ prompts the player for a valid cell number; error checks that the input is a valid cell number; and prints the info and the updated self.board; """ pass #To Implement def computerNextMove(self) -> None: """ computer randomly chooses a valid cell, and prints the info and the updated self.board """ pass #To Implement def hasWon(self, who: str) -> bool: """ returns True if who (being passed 'X' or 'O') has won, False otherwise """ pass #To Implement def terminate(self, who: str) -> bool: """ returns True if who (being passed 'X' or 'O') has won or if it's a draw, False otherwise; it also prints the final messages: "You won! Thanks for playing." or "You lost! Thanks for playing." or "A draw! Thanks for playing." """ pass #To Implement if __name__ == "__main__": # Use as is ttt = TicTacToe() # initialize a game while True: ttt.playerNextMove() # X starts first if(ttt.terminate('X')): break # if X won or a draw, print message and terminate ttt.computerNextMove() # computer plays O if(ttt.terminate('O')): break # if O won or a draw, print message and terminate
Please use the code showing above.
The expected output are showing below.


At the beginning of the game the following banner is printed and then the game starts by allowing the player to state a move. The player is X (first move) and the computer is O. Welcome to Tic-Tac-Toe! You play X (first move) and computer plays o. Computer plays randomly, not strategically. 1 | 1 | 2 1 3 | 4 | 5 1 6 | 7 | 8 > Next move for X (state a valid cell num): The move is stated by the number of a valid cell. If the number is not valid (already taken, or not between 0 and 8) or if it is not a number, the game should display the error message (as shown) and repeat asking for the next move. > Next move for X (state a valid cell num): x Must be an integer > Next move for X (state a valid cell num): 13 Must enter a valid cell number > Next move for X (state a valid cell num): 4 You chose cell 4 0 | 1 | 2 | X 3 4 5 --+---+- 6 | 7 | 8 Computer chose cell 5 1 0 1 2 | XO 3 | 4 | 5 6 | 7 | 8 > Next move for X (state a valid cell num): 1 In the above image, the computer immediately made a move by choosing a valid cell number randomly and then it displays the updated board. If we choose a cell number that is already taken, an error message "Must enter a valid cell number" is printed and it will again prompts for an input. > Next move for X (state a valid cell num): 4 Must enter a valid cell number > Next move for X (state a valid cell num): 0 You chose cell o XL 0 | 1 | 2 --+---+-- 3 4 5 --+---+-- 6 | 7 | 8 Computer chose cell 7 X | 0 | 1 | 2 --+---+-- | x 10 3 | 4 | 5 --+---+ --+---+-- 10 6 | 7 | 8 > Next move for X (state a valid cell num): The game continues until either the player or the computer wins, or it is a draw. The game prints a final message: either of "You won! Thanks for playing." or "You lost! Thanks for playing." or "A draw! Thanks for playing." messages, and it terminates. > Next move for X (state a valid cell num): 8 You chose cell 8 x| 1 --+---+-- | x 0 --+---+-- 10 x 0 | 1 | 2 --+---+-- 3 | 4 | 5 --+---+-- 6 | 7 | 8 You won! Thanks for playing
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
