Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

class NumTicTacToe: def __init__(self): self.board = [] # list of lists, where each internal list represents a row, is an attribute self.size = 3 #

image text in transcribedimage text in transcribedimage text in transcribedclass NumTicTacToe: def __init__(self): self.board = [] # list of lists, where each internal list represents a row, is an attribute self.size = 3 # number of columns and rows of board, is an attribute # 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): for i in range(self.size): print(' ', i ,end =' ') for row in range(self.size): print(' ',row,'', end = ' ') for col in range(self.size): if self.board[row][col] == 0: print(' ',end=' ') else: print(self.board[row][col], end=' ') if col + 1 != self.size: print('|',end=' ') if (row+1)!=self.size: print(' ------------', end='')

def squareIsEmpty(self, row, col): if self.board[row][col]==0: return True else: return False def update(self, row, col, mark): if self.squareIsEmpty(row,col): self.board[row][col] = num return True else: return False def boardFull(self): full = True for row in self.board: for col in row: if col ==0: full = False return full def isWinner(self): winner = False for row in self.board: if row[0] != 0 and row[1] != 0 and row[2] !=0: winner = True for col_index in range(0,3): column=[] for row_index in range(0,3): tile = self.board[row_index][col_index] column.append(tile) if column[0] != 0 and column[1] != 0 and column[2] != 0: if column[0]+column[1]+column[2] == 15: winner = True

if self.board[0][0] != 0 and self.board[1][1] != 0 and self.board[2][2] != 0: if self.board[0][0]+self.board[1][1]+self.board[2][2]==15: winner = True return winner def isNum(self): class ClassicTicTacToe: def __init__(self): self.board = [] # list of lists, where each internal list represents a row, is an attribute self.size = 3 # number of columns and rows of board, is an attribute # 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): for i in range(self.size): print(' ', i ,end =' ') for row in range(self.size): print(' ',row,'', end = ' ') for col in range(self.size): if self.board[row][col] == 0: print(' ',end=' ') else: print(self.board[row][col], end=' ') if col + 1 != self.size: print('|',end=' ') if (row+1)!=self.size: print(' ------------', end='')

def squareIsEmpty(self, row, col): if self.board[row][col]==0: return True else: return False def update(self, row, col, mark): if self.squareIsEmpty(row,col): self.board[row][col] = num return True else: return False def boardFull(self): full = True for row in self.board: for col in row: if col ==0: full = False return full def isWinner(self): winner = False for row in self.board: if row[0] != 0 and row[1] != 0 and row[2] !=0: winner = True for col_index in range(0,3): column=[] for row_index in range(0,3): tile = self.board[row_index][col_index] column.append(tile) if column[0] != 0 and column[1] != 0 and column[2] != 0: if column[0]+column[1]+column[2] == 15: winner = True

if self.board[0][0] != 0 and self.board[1][1] != 0 and self.board[2][2] != 0: if self.board[0][0]+self.board[1][1]+self.board[2][2]==15: winner = True return winner def isNum(self): # TO DO: delete pass (and this comment) and complete method class MetaTicTacToe: def __init__(self, configFile): # TO DO: delete pass (and this comment) and complete method pass def drawBoard(self): # TO DO: delete pass (and this comment) and complete method pass def squareIsEmpty(self, row, col): ''' Checks if a given square contains a non-played local game board ("empty"), or the result of a played local game board (not "empty"). 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 '''pass def update(self, row, col, result): ''' Assigns the string, result, 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 result (str) - entry to place in square Returns: True if attempted update was successful; False otherwise ''' pass def boardFull(self): ''' Checks if the board has any remaining "empty" squares (i.e. any un-played local boards). Inputs: none Returns: True if the board has no "empty" squares (full); False otherwise ''' 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) of their mark (three Xs for Player 1, three Os for Player 2), or 3 draws. That line can be horizontal, vertical, or diagonal. Inputs: none Returns: True if current player has won with their most recent move; False otherwise def getLocalBoard(self, row, col): ''' Returns the instance of the empty local board at the specified row, col location (i.e. either ClassicTicTacToe or NumTicTacToe). Inputs: row (int) - row index of square col (int) - column index of square Returns: instance of appropriate empty local board if un-played; None if local board has already been played '''

if __name__ == "__main__": # TEST EACH CLASS THOROUGHLY HERE 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 myBoard.update(1,1,2) myBoard.drawBoard() # try to assign a number to a non-empty square. What happens? myBoard.update(1,1,3) myBoard.drawBoard() # check if the board has a winner. Should there be a winner after only 1 entry? myBoard.update(0,1,15) print(myBoard.board) print(myBoard.isWinner()) # check if the board is full. Should it be full after only 1 entry? print(myBoard.boardFull()) # add values to the board so that any line adds up to 15. Display myBoard.update(0,0,9) myBoard.update(2,2,4) myBoard.drawBoard() # check if the board has a winner # check if the board is full # write additional tests, as needed

Meta Tic Tac Toe is a board game composed of nine tic-tac-toe boards arranged in a 3-by-3 grid. In other words, it is a Tic-Tac-Toe board with 9 tic-tac-toe games. Players take turns playing in the smaller tic-tac-toe boards until one of them wins in the larger tic-tac-toe board. Each small 3-by-3 tic-tac-toe board is referred to as a local board, and the larger 3-by-3 board is referred to as the global board. In our ultimate meta tic-tac-toe game, we have a mixture of tic-tac-toes. Each local board can be either classical (XO) or numerical (see Lab3) tic tac toe. They are initialized at the beginning of the game based on a configuration file MetaTTTconfig.txt. There are two players for the game. The game starts with Player 1 choosing any local board from the global board to start, and makes a move on that local board. Player 2 then makes a move on that same local board, then Player 1, and so on. The next moves must all be in that local board, until that local board finishes. A local board is finished once it is won by a player, or it is filled completely without a winner (i.e. a draw). Then the next player chooses another non-played local board to play, and makes a move on that local board. After a local board is finished, the corresponding square on the global board will be marked as X (when Player 1 wins the local board), o (when Player 2 wins the local board) or D (the result of the local board is a draw). To win the whole game, a player needs to win the global board. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row is the winner. Moreover, a player may win the global game if the player is the last to play a draw for a local board which results in 3 draws on the horizontal, vertical, or diagonal of the global board. Game play ends when either a player wins the global board, or all 9 local boards have been finished without a global winner (in which case the global game is a draw). The game starts with asking Player 1 to choose from the global board. In the global board, 'c' represents a non-played classical tic-tac-toe, 'n' represents a non-played numerical tic-tac-toe, 'X' represents a played tic-tac-toe won by Player 1, 'O' represents a played tic-tac-toe won by Player 2, and 'D' represents a played tic-tac-toe which was a draw. The configuration file Meta TTTconfig.txt just contains 3*3 letters which are either 'c' or 'n', separated by a single white space. An example of a configuration file can be like this: cnc non Using the above configuration, a complete sample game is shown in SampleOut.txt. ***Notice that if Player 2 is the first player to make a move in a local Classical Tic Tac Toe game, Player 2's mark in that local board is 'X'. Similarly, if Player 2 is the first player to make a move in a local Numerical Tic Tac Toe game, Player 2 enters odd numbers on that local board. Task 1 Your task will be to write the two classes: Num Tic Tac Toe and Classic Tic Tac Toe. Hint: these are very similar to your Lab 3 exercise, and you may reuse code that you wrote for that lab (just be sure to reference that with a comment at the top of your class). Each class must implement the following methods: - __init__(): initialize the local game board, so that it is initially "empty". You may represent empty squares however you like (e.g. using a zero, or empty string, or some other representation) - drawBoard(): displays the current state of the local game board, along with the column indices on top of the board, and the row indices to the left of the board. Should not change the contents of the local game board itself. - squarelsEmpty(row, col): returns True if the local board's square at the given row, column location is "empty"; returns False otherwise. - update(row, col, mark): attempts to update the local board's contents at the given row, column location, so that the specified square changes from being empty to containing the given mark. (The mark will be a string for the Classic version; it will be an int for the Numeric version.) Returns True if update is made successfully; False otherwise. - isWinner(); returns True if the local board contains a winning line of THREE squares (horizontal, vertical, diagonal); False otherwise. In the Classic version, a winning line consists of three of the same letters (either X or O). In the Numeric version, a winning line must have a line of three squares that add up to exactly 15. - boardFull(): returns True if there are no "empty" squares; False otherwise. - isNum(): returns True for Numeric boards; False for Classic boards. Do not forget to test your classes in isolation before you write the code of the game itself. Test your class methods, one by one. Include these tests (along with comments, as appropriate) under if name == "main ": at the bottom of your Ultimate MetaTTT.pyfile. You will lose marks if you do not include these tests for us to see. Task 2 Your task will be to design a Meta Tic Tac Toe class, and use the class to play games. The class will store the state of the global game. We require the following methods to be implemented: - __init__(configFile): create the game state by initializing it from the configuration file. The name of that configuration file is provided as input. Note that we may test this using different file contents than what is provided as an example in this description. - drawBoard(): displays the current state of the global board (i.e. using 'n', 'c', 'X', 'O', or 'D'), along with the column indices on top of the board, and the row indices to the left of the board. Should not change the contents of the global game board itself. - SquarelsEmpty (row, col): checks if a local board at the given row, column location has been played yet or not. Returns True if the local board has not been played; False otherwise. - update(row, col, result): attempts to update the global board at the given row, column location with the result ('X', 'O', or 'D') from the local board at that location. Returns True if that board was previously marked as unplayed and the update was made successfully; False otherwise. - is Winner(): checks if the current player is a winner of the global game based on their most recent move (i.e. if their most recent move ended a local game, resulting in an update of the global game board). Returns True if the current player is a winner; False otherwise. - boardFull(): returns True if the global board has no unplayed local boards; returns False if there is any local board still to be played. - getLocalBoard(row, col): returns the instance of the local board at the specified row, column location (i.e. either Classic Tic Tac Toe or Num Tic Tac Toe, based on initialization), if it hasn't been played. Returns None if the local board has already been played. name__ == "_main__": Do not forget to test your class in isolation before you write the code of the game itself. Include your tests (along with comments, as appropriate) under if at the bottom of your Ultimate Meta TTT.py file. You will lose marks if you do not include these tests for us to see. The number of required methods may seem like a lot, but many of these methods are quite small, and are designed so that interacting with the class is easier. A skeleton template class has been provided with comments as to what is expected for each method, as well as a text file of sample output and input. Meta Tic Tac Toe is a board game composed of nine tic-tac-toe boards arranged in a 3-by-3 grid. In other words, it is a Tic-Tac-Toe board with 9 tic-tac-toe games. Players take turns playing in the smaller tic-tac-toe boards until one of them wins in the larger tic-tac-toe board. Each small 3-by-3 tic-tac-toe board is referred to as a local board, and the larger 3-by-3 board is referred to as the global board. In our ultimate meta tic-tac-toe game, we have a mixture of tic-tac-toes. Each local board can be either classical (XO) or numerical (see Lab3) tic tac toe. They are initialized at the beginning of the game based on a configuration file MetaTTTconfig.txt. There are two players for the game. The game starts with Player 1 choosing any local board from the global board to start, and makes a move on that local board. Player 2 then makes a move on that same local board, then Player 1, and so on. The next moves must all be in that local board, until that local board finishes. A local board is finished once it is won by a player, or it is filled completely without a winner (i.e. a draw). Then the next player chooses another non-played local board to play, and makes a move on that local board. After a local board is finished, the corresponding square on the global board will be marked as X (when Player 1 wins the local board), o (when Player 2 wins the local board) or D (the result of the local board is a draw). To win the whole game, a player needs to win the global board. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row is the winner. Moreover, a player may win the global game if the player is the last to play a draw for a local board which results in 3 draws on the horizontal, vertical, or diagonal of the global board. Game play ends when either a player wins the global board, or all 9 local boards have been finished without a global winner (in which case the global game is a draw). The game starts with asking Player 1 to choose from the global board. In the global board, 'c' represents a non-played classical tic-tac-toe, 'n' represents a non-played numerical tic-tac-toe, 'X' represents a played tic-tac-toe won by Player 1, 'O' represents a played tic-tac-toe won by Player 2, and 'D' represents a played tic-tac-toe which was a draw. The configuration file Meta TTTconfig.txt just contains 3*3 letters which are either 'c' or 'n', separated by a single white space. An example of a configuration file can be like this: cnc non Using the above configuration, a complete sample game is shown in SampleOut.txt. ***Notice that if Player 2 is the first player to make a move in a local Classical Tic Tac Toe game, Player 2's mark in that local board is 'X'. Similarly, if Player 2 is the first player to make a move in a local Numerical Tic Tac Toe game, Player 2 enters odd numbers on that local board. Task 1 Your task will be to write the two classes: Num Tic Tac Toe and Classic Tic Tac Toe. Hint: these are very similar to your Lab 3 exercise, and you may reuse code that you wrote for that lab (just be sure to reference that with a comment at the top of your class). Each class must implement the following methods: - __init__(): initialize the local game board, so that it is initially "empty". You may represent empty squares however you like (e.g. using a zero, or empty string, or some other representation) - drawBoard(): displays the current state of the local game board, along with the column indices on top of the board, and the row indices to the left of the board. Should not change the contents of the local game board itself. - squarelsEmpty(row, col): returns True if the local board's square at the given row, column location is "empty"; returns False otherwise. - update(row, col, mark): attempts to update the local board's contents at the given row, column location, so that the specified square changes from being empty to containing the given mark. (The mark will be a string for the Classic version; it will be an int for the Numeric version.) Returns True if update is made successfully; False otherwise. - isWinner(); returns True if the local board contains a winning line of THREE squares (horizontal, vertical, diagonal); False otherwise. In the Classic version, a winning line consists of three of the same letters (either X or O). In the Numeric version, a winning line must have a line of three squares that add up to exactly 15. - boardFull(): returns True if there are no "empty" squares; False otherwise. - isNum(): returns True for Numeric boards; False for Classic boards. Do not forget to test your classes in isolation before you write the code of the game itself. Test your class methods, one by one. Include these tests (along with comments, as appropriate) under if name == "main ": at the bottom of your Ultimate MetaTTT.pyfile. You will lose marks if you do not include these tests for us to see. Task 2 Your task will be to design a Meta Tic Tac Toe class, and use the class to play games. The class will store the state of the global game. We require the following methods to be implemented: - __init__(configFile): create the game state by initializing it from the configuration file. The name of that configuration file is provided as input. Note that we may test this using different file contents than what is provided as an example in this description. - drawBoard(): displays the current state of the global board (i.e. using 'n', 'c', 'X', 'O', or 'D'), along with the column indices on top of the board, and the row indices to the left of the board. Should not change the contents of the global game board itself. - SquarelsEmpty (row, col): checks if a local board at the given row, column location has been played yet or not. Returns True if the local board has not been played; False otherwise. - update(row, col, result): attempts to update the global board at the given row, column location with the result ('X', 'O', or 'D') from the local board at that location. Returns True if that board was previously marked as unplayed and the update was made successfully; False otherwise. - is Winner(): checks if the current player is a winner of the global game based on their most recent move (i.e. if their most recent move ended a local game, resulting in an update of the global game board). Returns True if the current player is a winner; False otherwise. - boardFull(): returns True if the global board has no unplayed local boards; returns False if there is any local board still to be played. - getLocalBoard(row, col): returns the instance of the local board at the specified row, column location (i.e. either Classic Tic Tac Toe or Num Tic Tac Toe, based on initialization), if it hasn't been played. Returns None if the local board has already been played. name__ == "_main__": Do not forget to test your class in isolation before you write the code of the game itself. Include your tests (along with comments, as appropriate) under if at the bottom of your Ultimate Meta TTT.py file. You will lose marks if you do not include these tests for us to see. The number of required methods may seem like a lot, but many of these methods are quite small, and are designed so that interacting with the class is easier. A skeleton template class has been provided with comments as to what is expected for each method, as well as a text file of sample output and input

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

Beginning Microsoft SQL Server 2012 Programming

Authors: Paul Atkinson, Robert Vieira

1st Edition

1118102282, 9781118102282

More Books

Students also viewed these Databases questions

Question

What do you like to do for fun/to relax?

Answered: 1 week ago

Question

4. When is it appropriate to show grace toward others?

Answered: 1 week ago