Question: The Problem (Using The Python) The le A10 starter.py contains a great deal of the code needed for an implementation of the Tic Tac Toe
The Problem (Using The Python) The le A10 starter.py contains a great deal of the code needed for an implementation of the Tic Tac Toe program. What you need to do is complete the un nished functions. There are eight of them. Read and understand the documentation associated with the functions. That should help you implement the functions. Code each function and test it independent of every other function. The lab we did in week 11 should guide you as to how to test each function independently. You must not change any of the functions that have been implemented nor change the names of any functions.
Restrictions Do not use any Python library functions. Do not intriduce any new variables. Do not change the names of any functions. Do not change the functionality of any of the already-implemented functions. If you do, you would lose points for related parts of your Python code.
********
''' Plays the Tic Tac Toe Game. The "board" is represented as a list of 3 elements, each of which is a list of 3 elements. [[ '0', '1', '2' ], [ '3', '4', '5' ], [ '6', '7', '8' ]]
The user inputs his/her choice of the cell to occupy by typing in one of the 9 digits 0 through 8. This is converted to the list index values by computing as below. row = cell // 3 column = cell % 3
The logic uses the row and column format throughout. Only the user sees the "sequential" representation. ''' ''' Checks if the game could still be won by one of the players: that is, if any of the rows, columns, or diagonals are still undecided. Check if there is any row, column, or diagonal that can be won. Returns True if and only if the game could still be won. ''' def gameUndecided(board): # Start with # if someRowUndecided(board) or # Now do the rest ''' Determines if the game has already been decided. If so, it returns True and the winner's identity ('H' for human and 'C' for computer). Otherwise, it returns False and "X". The function checks the rows, columns, down diagonal, and the up diagonal in that order. ''' def gameWon(board): result, winner = someRowWon(board) if result: return True, winner result, winner = someColumnWon(board) if result: return True, winner result, winner = downDiagonalWon(board) if result: return True, winner result, winner = upDiagonalWon(board) if result: return True, winner return False, "X" ''' Checks whether the game has been decided in one specific sequence of cells. If so, it returns True and the identity of the winner. Otherwise, it returns False and "X". ''' def winningSequence(board, row1, column1,row2, column2, row3, column3): # if all cells have the same value, there is only one possibility. # The game is over, and the value stored in any cell indicates the winner. # Return that.
# TO DO ''' Checks whether the game can proceed in one specific sequence of cells: that is, if the given cells have two unoccupied cells or two cells are occupied by the same player and the third is unoccupied. Notice that if two cells given here are occupied by two different players or if all cells given here are occupied, this set of cells cannnot be used by either player to win the game.
If the given cells here cannot be used for winning by either player, return False. Otherwise, return True. ''' def undecidedSequence(board, row1, column1,row2, column2, row3, column3): # TO DO ''' Checks whether the game can proceed in at least some row; if so, it returns True; otherwise, it returns False. ''' def someRowUndecided(board): for row in range(3): if undecidedSequence(board, row, 0, row, 1, row, 2): return True return False ''' Checks whether the game can proceed in at least some column; if so, it returns True; otherwise, it returns False. ''' def someColumnUndecided(board): # TO DO ''' Checks whether the game can proceed in the up digonal; if so, it returns True; otherwise, it returns False. ''' def upDiagonalUndecided(board): return undecidedSequence(board, 2, 0, 1, 1, 0, 2) ''' Checks whether the game can proceed in the down diagonal; if so, it returns True; otherwise, it returns False. ''' def downDiagonalUndecided(board): # TO DO
''' Checks whether the game has been won in some row; if so, it returns True and the identity of te=he winner ('C' for computer and 'H' for himan); otherwise, it returns False, "X". ''' def someRowWon(board): for row in range(3): win, winner = winningSequence(board, row, 0, row, 1, row, 2) if win: return win, winner return False, "X"
''' Checks whether the game has been won in some column; if so, it returns True and the identity of te=he winner ('C' for computer and 'H' for himan); otherwise, it returns False, "X". ''' def someColumnWon(board): # TO DO
''' Checks whether the game has been won in the up diagonal; if so, it returns True and the identity of te=he winner ('C' for computer and 'H' for himan); otherwise, it returns False, "X". ''' def upDiagonalWon(board): # TO DO: Check if the cells in the up diagonal form a winning sequence # Decide which cells are in the up diagonal
''' Checks whether the game has been won in the down diagonal; if so, it returns True and the identity of te=he winner ('C' for computer and 'H' for himan); otherwise, it returns False, "X". ''' def downDiagonalWon(board): # TO DO: Check if the cells in the down diagonal form a winning sequence # Decide which cells are in the down diagonal
''' Checks the status of the game. There are 4 return values: three Booleans indicating whether the game has been drawn, won, or still undecided, and the identity of the winner in case the second flag is True. If the second flag is False, there is, of course, no winner and the function returns "X" for the fourth value. ''' def status(board): win, winner = gameWon(board) if win: return False, True, False, winner if gameUndecided(board): return False, False, True, "X" return True, False, False, "X"
''' Checks if the cell is numeric. If so, it means it is unoccupied. ''' def isNumeric(board, row, column): return ord(board[row][column]) >= ord("0") and ord(board[row][column]) <= ord("9") ''' Determines the next row and column the computer selects. It simply searches for a vacant spot and returns the row and column for that. ''' def play(board): for row in range(3): for column in range(3): if board[row][column] != "H" and board[row][column] != "C": return row, column return -1, -1 ''' Updates the board for the cell that is now occupied. ''' def update(board, player, row, column): board[row][column] = player ''' Returns true iff the cell is occupied. ''' def occupied(board, row, column): return not isNumeric(board, row, column) ''' Gets the human's choice for his/her move. ''' def getInput(board): cell = eval(input("enter cell: ")) while not (cell >= 0 and cell <= 8 and not occupied(board, cell // 3, cell % 3)): cell = eval(input("invalid selection; reenter: ")) return cell // 3, cell % 3 ''' Displays the cells ''' def display(board): for row in range(3): print(board[row]) ''' Sets up and plays the game ''' def game(): board = [['0', '1', '2'], ['3', '4', '5'], ['6', '7', '8']] result = "U" turn = 'C' draw = False someoneWon = False unDecided = True display(board) while unDecided: if turn == 'C': row, column = play(board) else: row, column = getInput(board) update(board, turn, row, column) display(board) if turn == 'C': turn = 'H' else: turn = 'C' draw, someOneWon, unDecided, whoWon = status(board) if someOneWon: if whoWon == 'C': print("I won; better luck next time") else: print("Congratulations! You won") else: print("It's a draw") game()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
