Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. The __init__ method has been completed for you. Notice that it initializes two attributes: board and size. The size attribute defines how many rows

1. 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_NumTicTacToe.py) to test this method before moving on: self.board should be [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

2. 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: image text in transcribed

3. Complete the squareIsEmpty(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.

4. 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.

5. Complete the boardFull() method. This method returns True if there are no empty squares, False otherwise. Test this method. Which cases should you consider?

6. Complete the isWinner() 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?

image text in transcribedimage text in transcribedYour final output of the game when it runs should look like this: image text in transcribed

0 1 2 ----------- 1 | | 2 | | 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 for i in range(self.size): row = [] for j in range(self.size): row.append) 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 # # 1 # # 2 --------- 1 1 ------ 1 1 pass def squareIsEmpty(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 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 otherwise # 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 97 if __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() # assign a number to an empty square and display # try to assign a number to a non-empty square. What happens? # check if the board has a winner. Should there be a winner after only 1 entry? 111 112 113 114 115 # check if the board is full. Should it be full after only 1 entry? 116 # add values to the board so that any line adds up to 15. Display 117 118 # check if the board has a winner 119 120 # check if the board is full 121 122 # write additional tests, as needed 123 - - - - - - - - - - - - - - - - - - - - - - Starting new Numerical Tic Tac Toe game 0 1 2 1 2 Player 1, please enter an odd number (1-9): 5 Player 1, please enter a row: 1 Player 1, please enter a column: 1 0 1 2 ------- 1 51 2 Player 2, please enter an even number (2-8): 2 Player 2, please enter a row: 1 Player 2, please enter a column: 0 1 o 1 2 1 251 2 L 1 Player 1, please enter an odd number (1-9): 1 Player 1, please enter a row: 0 Player 1, please enter a column: 2 0 1 2 1 1 251 2 ] Player 2, please enter an even number (2-8): 8 Player 2, please enter a row: 1 Player 2, please enter a column: 2 0 1 2 1 0 1 2 5 8 2 Player 2 wins. Congrats! Do you want to play another game? (Y/N) n Thanks for playing! Goodbye

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Database Internals A Deep Dive Into How Distributed Data Systems Work

Authors: Alex Petrov

1st Edition

1492040347, 978-1492040347

More Books

Students also viewed these Databases questions

Question

Explain the various techniques of Management Development.

Answered: 1 week ago