For the assignment this week, the tic-tac-toe game they have as the exercise at the end of the module is actually a great implementation of a lot of the key concepts from both this week and the...

1 answer below »
For the assignment this week, the tic-tac-toe game they have as the exercise at the end of the module is actually a great implementation of a lot of the key concepts from both this week and the previous week. So I'm copying the instructions in here below. You can simply use your existing main.py file for this, deleting the previous methods and pasting in the starter functions provided by the exercise.


Your task is to writea simple program which pretends to playtic-tac-toewith the user. To make it all easier for you, we've decided to simplify the game. Here are our assumptions:


  • the computer (i.e., your program) should play the game using'X's;

  • the user (e.g., you) should play the game using'O's;

  • the first move belongs to the computer − it always puts its first'X'in the middle of the board;

  • all the squares are numbered row by row starting with1(see the example session below for reference)

  • the user inputs their move by entering the number of the square they choose − the number must be valid, i.e., it must be an integer, it must be greater than0and less than10, and it cannot point to a field which is already occupied;

  • the program checks if the game is over − there are four possible verdicts: the game should continue, the game ends with a tie, you win, or the computer wins;

  • the computer responds with its move and the check is repeated;

  • don't implement any form of artificial intelligence − a random field choice made by the computer is good enough for the game.


The example session with the program may look as follows:















Requirements


Implement the following features:



  • the board should be stored as a three-element list, while each element is another three-element list (the inner lists represent rows) so that all of the squares may be accessed using the following syntax:



    board[row][column]



  • each of the inner list's elements can contain'O','X', or a digit representing the square's number (such a square is considered free)

  • the board's appearance should be exactly the same as the one presented in the example.

  • implement the functions defined for you in the editor.




Drawing a random integer number can be done by utilizing a Python function calledrandrange(). The example program below shows how to use it (the program prints ten random numbers from 0 to 8).


Note: thefrom-importinstruction provides access to therandrangefunction defined within an external Python module callledrandom.


from random import randrange for i in range(10): print(randrange(8))



The prompt functions are: (you can also copy/paste them from within the exercise)

Answered Same DaySep 29, 2021

Answer To: For the assignment this week, the tic-tac-toe game they have as the exercise at the end of the...

Amar Kumar answered on Sep 30 2021
142 Votes
import random
class TicTacToe:
def __init__(self):
self.board = []
def create_bo
ard(self):
for i in range(3):
row = []
for j in range(3):
row.append('-')
self.board.append(row)
def get_random_first_player(self):
return random.randint(0, 1)
def fix_spot(self, row, col, player):
self.board[row][col] = player
def is_player_win(self, player):
win = None
n = len(self.board)
# checking rows
for i in range(n):
win = True
for j in range(n):
if self.board[i][j] != player:
win = False
break
if win:
return win
# checking columns
for i in range(n):
win = True
for j in range(n):
if self.board[j][i] !=...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here