Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Download and save a copy of lab3_Num TicTacToe.py. This file contains the skeleton code for a class called NumTicTacToe, which you will complete. The __init_method
Download and save a copy of lab3_Num TicTacToe.py. This file contains the skeleton code for a class called NumTicTacToe, which you will complete. The __init_method has been completed for you. Notice that it initializes two attributes: board and size. The size attribute defines how many rows (and columns) are on the board. The board attribute represents an empty 3 by 3 board - it is a list of lists, where each internal list represents a row on the board. The value 0 is used to represent an empty square. Run the test provided (i.e. by running lab3_Num TicTacToe.py) to test this method before moving on: self.board should be [[0, 0, 0], [0, 0, 0], [0, 0, 0]] Complete the drawBoard method so that it displays the current state of the board, along with the column indices on top of the board, and the row indices to the left of the board. If the content of a given square is 0, an empty space should be displayed. Test your method before moving on: When self. board is [[0, 0, 0], [0, 0, 0], [0, 0, 0]], the following should be printed: 0 1 2 OLI - - -- - - -- - -- 1 1 1 -- -- - --- - 2 L Complete the squarelsEmpty(row, col) method. This method checks the contents of the board at the given row index (integer) and column index (integer). If the board at that square is "empty", the method returns True. Otherwise, the method returns False. Test your method before moving on. Complete the update(row, col, num) method. This method should check if the square at the provided row index (integer) and column index (integer) is empty". (Hint: do you already have a method you can invoke to check this?) If it is "empty", the contents of the board at that square should be changed to num (integer). If the square is successfully updated, this method should return True. If the square is not empty, the contents of the board should not be changed, and the method should return false. Test your method. Complete the boardFull() method. This method returns True if there are no "empty" squares, False otherwise. Test this method. Which cases should you consider? Complete the is Winner() method. This method returns True if there is one line of three squares (horizontal, vertical, or diagonal) that adds up to 15. Otherwise, it returns False. Test this method. Which cases should you consider? class NumTicTacToe: def __init__(self): Initializes an empty Numerical Tic Tac Toe board. Inputs: none Returns: None self.board = [] # list of lists, where each internal list represents a row self.size = 3 # number of columns and rows of board # populate the empty squares in board with 0 for i in range (self.size): row = [] for j in range (self.size): row.append (0) self.board.append(row) def drawBoard (self): Displays the current state of the board, formatted with column and row indicies shown. Inputs: none Returns: None # TO DO: delete pass and print out formatted board # e.g. an empty board should look like this: 0 1 2 #OL ----------- 1 1 1 ----------- 2 pass def square IsEmpty (self, row, col): Checks if a given square is empty, or if it already contains a number greater than 0. Inputs: row (int) - row index of square to check col (int) - column index of square to check Returns: True if square is empty; False otherwise # TO DO: delete pass and complete method pass def update (self, row, col, num): Assigns the integer, num, to the board at the provided row and column, but only if that square is empty. Inputs: row (int) - row index of square to update col (int) - column index of square to update num (int) - entry to place in square Returns: True if attempted update was successful; False otherwise # TO DO: delete pass and complete method pass def boardFull (self): Checks if the board has any remaining empty squares. Inputs: none Returns: True if the board has no empty squares (full); False otherwis # TO DO: delete pass and complete method pass def isWinner (self): Checks whether the current player has just made a winning move. In order to win, the player must have just completed a line (of 3 squares) that adds up to 15. That line can be horizontal, vertical, or diagonal. Inputs: none Returns: True if current player has won with their most recent move; False otherwise # TO DO: delete pass and complete method pass _name__ == "__main__": # TEST EACH METHOD THOROUGHLY HERE # suggested tests are provided as comments, but more tests may be required # start by creating empty board and checking the contents of the board attribute myBoard = NumTicTacToe () print ('Contents of board attribute when object first created:') print (myBoard.board) # does the empty board display properly? myBoard.drawBoard()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started