Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

WRITE IN PYTHON PLSSS BASIC FRAME: class NumTicTacToe: def __init__(self): ''' Initializes an empty Numerical Tic Tac Toe board. Inputs: none Returns: None ''' self.board

WRITE IN PYTHON PLSSS

image text in transcribedimage text in transcribed

BASIC FRAME:

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 # 0 | | # ----------- # 1 | | # ----------- # 2 | | 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 ''' # 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

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? # check if the board is full. Should it be full after only 1 entry? # add values to the board so that any line adds up to 15. Display # check if the board has a winner # check if the board is full # write additional tests, as needed

SAMPLE OUTPUT:

--------------------------------------- Starting new Numerical Tic Tac Toe game ---------------------------------------

0 1 2 0 | | ----------- 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 0 | | ----------- 1 | 5 | ----------- 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

0 1 2 0 | | ----------- 1 2 | 5 | ----------- 2 | | 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 0 | | 1 ----------- 1 2 | 5 | ----------- 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 0 | | 1 ----------- 1 2 | 5 | 8 ----------- 2 | | Player 2 wins. Congrats! Do you want to play another game? (Y/N) n Thanks for playing! Goodbye.

Background Information: Numerical Tic Tac Toe is a game for two players who take turns filling in a 3 by 3 grid with the numbers 1-9. The first player places an odd number in an empty square, and then the second player places an even number in an empty square. The players' turns continue to alternate until: 1) a player completes a line of three squares (horizontal, vertical, or diagonal) that adds up to 15 - in which case, the current player wins; or 2) all nine squares on the board are full, but no line adding to 15 is formed in which case, it's a tie. Note that player 1 must always place an odd number on the board, and player 2 an even number. In addition, each number from 1 to 9 can only appear on the board once. To view a complete sample game, please refer to lab3_sampleOut.txt on eClass. Problem Description: A colleague has already written a program to control the flow of this game, assuming that there was a Numerical Tic Tac Toe class. However, no such class currently exists. It is up to you to write the class, so that an instance of it can then be used in your colleague's program. Part 1: Download and save a copy of lab3_NumTicTacToe.py. This file contains the skeleton code for a class called NumTicTacToe, which you will complete. 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_Num TicTac Toe.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: 0 1 2 | 0 - i ! 1 | 1 N 1 3. 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. 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 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

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

Students also viewed these Databases questions

Question

What are the purposes of collection messages? (Objective 5)

Answered: 1 week ago