Question: Hi, I need to Create a Tic Tac Toe Game in Python. I Am given this Complete code In Bold below that we DO NOT

Hi, I need to Create a Tic Tac Toe Game in Python. I Am given this Complete code In Bold below that we DO NOT MODIFY, However I need to do the Functions below the Star line. There is a catch. We have to use the given Function names, We cannot Add functions to the code and we have to do exactly what the text says inside the in ''' ''' of the function. Here are some instructions that were given to me.

1) The main program controls the game.

a. It asks the user to start a game : if the response is not o or O, the program ends; If the response is o or O, it leads the game by using the following operations :

i. erase the table (with function eraseTable(tab)).

ii. display the table (with function displayble(tab)).

iii. play a step (with function play(tab, player) including the request made to the player for the new position).

iv. verify if the player has won or if it is a draw (with function verifyWin(tab)).

v. if the game is not completed, play again a step with the other player (from iii).

Note: the array of the game table is created in the main part of the program in a4q2.py and is passes as reference to other functions.

b. after each game, you are asked to start another game (restart from a.).

2) the function verifyWin call the following functions:

a. testRows(tab) to check if a row has won.

b. testCols(tab) to check if a column has won.

c. testDiags(tab) to check if a diagonal has won.

d. testDraw(tab) to check for a draw.

3) The function verifyWin Displays the message "Draw" instead of "Player X has won!" or " Player O has won!" when there is a draw

4) The new row and new column are given with input() are are stored in a list with two elements (the first is the row and the second is the column)

5) the functions testRows, testCols and testDiags return one of the characters "-", "X" or "O". If "-" is returned, no one has won, otherwise the character for the winning okayer is returned.

from a4q2Lib import * def eraseTable (tab): ''' (list) -> None Display the game table Preconditions: tab is a reference to an n x n array that contains '-', 'X' or 'O' The format is: 0 1 2 0 - - O 1 - X - 2 - - X ''' print(" ", end="") col = 0 while col < len(tab): print(col, end=" ") col += 1 print() row = 0 while row < len(tab): print(row, end="") col = 0 while col < len(tab[row]): print(" ",tab[row][col], end="") col += 1 print() row += 1

def play (tab, player): ''' (list, str) -> None Plays a step of the game Preconditions: tab is a reference to the n x n tab containing '-', 'X' and 'O' The player is X or O tab is modified (an elementis changed) ''' valid = False while not valid: place = [-1,-1] # create a table with two elements while not((0 <= place[0] < len(tab)) and (0 <= place[1] < len(tab))): print ("Player ",player, end="") print(", Please provide the row and the columm from 0 to", (len(tab)-1), ": ") place[0] = int(input("Row: ")) # place[1] = int(input("Column: ")) #find a position that is not busy that contains '- if tab[place[0]][place[1]] != '-': print("The position", place[0], place[1], "is occupied"); valid = False else: valid = True # put the player in the array tab[place[0]][place[1]] = player # no result

# Create the game table table = [['-','-','-'],['-','-','-'],['-','-','-']] # The only array used in the program. response = input("Start a game (O or N): "); while response == 'o' or response == 'O': eraseTable(table) # prepares the game table winner = False # initializes the variable winner while not winner: displyTable(table) # display the game table play(table,'X') # ask player X to play winner = verifyWin(table) # Did he win? if not winner: # no winner, the other player can play displayTable(table) # display the game table play(table,'O') # ask player O to play winner = verifyWin(table) # Did he win? displayTable(table) # display the game table response = input("Start a game (O or N): ") # new game?

***********************************************************************************************************************

def erasTable (tab): ''' (list) -> None This function prepares the game table (array) by putting '-' in all the elements. It does not create a new array Preconditions: tab is a reference to an nxn array matrice n x n that contains '-', 'X' or 'O' ''' # to complete # returns nothing

def verifyWin(tab): '''(list) -> bool * Preconditions: tab is a reference to an nxn array that contains '-', 'X' or 'O' * Verify if there is a winner. * Look for 3 X's and O's in a row, column, and diagonal. * If we find one, we display the winner (the message "Player X has won" * or "Player O has won!") and returns True. * If there is a draw (verify it with the function testdraw), * display "It is a draw" and returns True. * If the game is not over, returns False. * The function call the functions testrows, testCols, testDiags * pour verifier s'il y a un gagnant. * Those functions returns the winner 'X' or 'O', or '-' if there is no winner. '''

# to complete return False # to change

def testLignes(tab): ''' (list) -> str * verify if there is a winning row. * Look for three 'X' or three 'O' in a row. * If they are found, the character 'X' or 'O' is returned, otherwise '-' is returned. * Preconditions: tab is a reference to an nxn array that contains '-', 'X' or 'O' '''

# to complete return '-' # to be modified so that it returns the winner, or '-' if there is no winner

def testCols(tab): ''' (list) -> str * verify a winning column. * look for three 'X' or three 'O' in a column. * If it is the case the character 'X' or 'O' is returned, otherwise '-' is returned. * Preconditions: tab is a reference to an nxn array that contains '-', 'X' or 'O' ''' # to complete return '-' #to be modified so that it returns the winner, or '-' if there is no winner

def testDiags(tab): ''' (list) -> str * Look for three 'X' or three 'O' in a diagonal. * If it is the case, the character 'X' or 'O' is returned * otherwise '-' is returned. * Preconditions: tab is a reference to an nxn array that contains '-', 'X' or 'O' '''

# to complete return '-' # #to be modified so that it returns the winner, or '-' if there is no winner

def testDraw(tab): ''' (list) -> bool * verify if there is a draw * check if all the array elements have X or O, not '-'. * If we do not find find any '-' in the array, return True. * If there is any '-', return false. * Preconditions: tab is a reference to an nxn array that contains '-', 'X' or 'O' ''' # to complete return False # to BE modiffied

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!