Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

python code that has been used Python Program: def eraseTable(tab): ''' (list) -> None This function prepares the game table (array) by putting '-' in

python code that has been used

Python Program:

def eraseTable(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 that contains '-', 'X' or 'O' ''' # Iterating over rows for i in range(0, 3): for j in range(0, 3): tab[i][j] = ' ' def verifyWinner(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. ''' # Checking for draw if testDraw(tab): print("It is a draw") return True elif testRows(tab)=='X' or testCols(tab)=='X' or testDiags(tab)=='X': print("'X' has won, GAME OVER! !") return True elif testRows(tab)=='O' or testCols(tab)=='O' or testDiags(tab)=='O': print("'O' has won, GAME OVER! !") return True # to complete return False # to change

def testRows(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' ''' # Iterating over rows for lst in tab: c = 0 # Iterating over columns for ch in lst: if ch == 'X': c = c+1 # Checking for 3 X if c==3: return 'X' # Iterating over rows for lst in tab: c = 0 # Iterating over columns for ch in lst: if ch == 'O': c = c+1 # Checking for 3 O if c==3: return 'O' 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' ''' # Iterating over columns for i in range(0, 3): c = 0 # Iterating over rows for j in range(0, 3): if tab[j][i] == 'X': c = c+1 # Checking for 3 X if c==3: return 'X' # Iterating over columns for i in range(0, 3): c = 0 # Iterating over rows for j in range(0, 3): if tab[j][i] == 'O': c = c+1 # Checking for 3 O if c==3: return 'O' 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' ''' if (tab[0][0]=='X' and tab[1][1]=='X' and tab[2][2]=='X') or (tab[0][2]=='X' and tab[1][1]=='X' and tab[2][0]=='X'): return 'X' elif (tab[0][0]=='O' and tab[1][1]=='O' and tab[2][2]=='O') or (tab[0][2]=='O' and tab[1][1]=='O' and tab[2][0]=='O'): return 'O' 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' ''' # Iterating over rows for lst in tab: # Iterating over columns for ch in lst: # Checking for - if ch == ' ': return False return True def displayTable (tab): ''' (list) -> None display the table of the game Preconditions: tab is a reference to an nxn array that contains ' ', 'X' or 'O' ''' # Outer loop for rows for i in range(3): # Inner loop for columns for j in range(3): # Printing character if j != 2: print("%s | "%(tab[i][j]), end=""); else: print("%s "%(tab[i][j]), end=""); # Printing footer if i!=2: print(" - - - - - "); print(" ")

def play (tab, player): ''' (list, str) -> None Play a step of the game Preconditions: tab is a reference to the n x n tab containing '-', 'X' and 'O' The player is either X or O tab is modified (an element has changed) ''' valid = False while not valid: place = [-1,-1] # create a table with two elements while not((0

print("Welcome to Tic Tac Toe! X goes first... ") eraseTable(table) # prepare the game table winner = False # initialize winner variable while not winner: displayTable(table) # display the game table play(table,'X') # ask the player X to play winner = verifyWinner(table) # did he win? if not winner: # no winner, the other player can play displayTable(table) # dplay the game table play(table,'O') # ask the player O to play winner = verifyWinner(table) # did he win?

I just need the answers of those questions

image text in transcribed

image text in transcribed

image text in transcribed

Hints and Resources Here is a short example of how your program might look when you run it in the console. Welcome to Tic Tac Toe! x goes first... It is X's turn Select a row (1 - 3): 1 Select a column (1 - 3): 1 XIT II It is o's turn Select a row (1 - 3): 2 Select a column (1 - 3): 1 XII 011 It is X's turn Select a row (1 3): 2 Select a column (1 - 3): 2 XII 011 It is O's turn Select a row (1 3): 3 Select a column (1 - 3): 2 XII 01X1 101 It is x's turn Select a row (1 - 3): 3 Select a column (1 - 3): 3 'X' HAS WON, GAME OVER!! When checking if a user has won, you must check: Check each row and see if all of the letters ("X" or "O") in it are the same. Check each column and see if all of the letters in it are the same. Check each of the two diagonals and see if the 3 elements are the same. To check if the game has been tied: Check that the game has not been won. Check if the board is completely full of letters. How would you go about designing an Al (computer player) to play Tic Tac Toe? When outputting the board, get creative! See how nice you can make it look using just sym and letters. If you are stuck, you can use "l" and "-". Spend some time to decide how you will store the board state in a variable. Write some pseudocode and make sure it will work before you start writing code. What are the different ways you could store the game state (board) variable? Questions Using pseudocode, write a function that accepts a list variable called "board", and determin player has won the game. What do you think is the best way to store the game state (board) in a variable? Why? Page 6 of 6

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions