Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will be writing two classes (Tic Tac Toe.java and ConnectFour.java) to complete this lab. ConnectFour is a child of Tic Tac Toe Tic Tac

image text in transcribed
image text in transcribed
image text in transcribed
You will be writing two classes (Tic Tac Toe.java and ConnectFour.java) to complete this lab. ConnectFour is a child of Tic Tac Toe Tic Tac Toe will represent annxn tic tac toe board. The board will be a two dimensional array of int's, where O represents an empty space, 1 represents a space occupied by player 1, and a 2 represents a place occupied by player 2. Tic Tac Toe needs to include the following: Tic Tac Toe(int n): Constructor that sets up a 2 dimensional array, nxn size boolean makeMove(int x, int y): places the current player in location x, y, where x is an index between and row length - 1, and y is an index between 0 and column length - 1. If there is currently another player occupying the space, no move is made, and a false is returned. If the player successfully occupies the space, a true is returned. int turn();returns the player number who's turn it is int gameStatus();returns a O if there are currently no winners - 1 if the game ends in a tie, a 1 if player 1 is a winner, and 2 if player 2 is a winner boolean gameOver();return false if the game is still in progress String toString();returns a String that represents the current state of the board. Each row should be a separate line, and there should be no white space between the columns void loadBoard(String fileName): overwrites the current contents of the board with the state of the board in the file, fileName. You may assume that fileName contains a valid Tic Tac Toe board, where each line represents a row, and there is no white space between the columns void saveBoard(String fileName): writes the current state of the board to a textfile, fleName. You should use the same formatting as toString() int checkPosition(int x, int y): accessor method that allows access to ONE position on the board at row x and column y. If the row or column is invalid, return a -1. The reason why you should NOT return the entire board is because the board is an object, and by returning an object you are giving clients access to make changes directly, which violates our encapsulation philosophy. **Note: You may NOT add any other public method to either classes. You may, however, add any number of PRIVATE helper methods you see appropriate. Connect Four is similar to Tic Tac Toe. The difference is, you do not need to occupy an entire row, or column, you simply need to have four in a row. The other difference is gravity determines the row number, and the player simply has a choice of the column. The purpose of utilizing inheritance is to reuse the code you already have, so think about the relationship between Tic Tac Toe and ConnectFour before implementing ConnectFour. You will need to determine which methods you should overwrite or overload. Note: you will only need two methods and one constructor for ConnectFour. ConnectFour should create a 6x game board. When making a move, you should specify one number, the column number, it should return true if the space can be occupied, and false if the column is "full". ConnectFour MUST be a subclass/child of Tic Tac Toe, and appropriate inheritance should be used to receive full credit for this assignment. Error Checking: You can assume that the input will be valid index numbers. Please use the Test File to sanity check your code. Error Handling: Your code must handle exceptions, such as when a file is not found. public void test50) Connect Four test = new ConnectFour(); test.makeMove ( 0); test.makeMove ( 0); test.makeMove( 1 ); test.makeMove( 1 ); test.makeMove( 2 ); test.makeMove( 2 ); test.makeMove( 3 ); assertEquals( true, test.gameOver() ); assertEquals( 1, test.gameStatus()); test. saveBoard("LAB3_OUTPUT.txt"); try FileReader fileReader = new FileReader( "LAB3_OUTPUT.txt" ) BufferedReader read = new BufferedReader( fileReader) String output = ""; String x = read.readLine(); while( x != null ) output += x + " "; x = read.readLine(); System.out.println( output ); assertEquals("000000 000000 000000 000000 222000 111100 ", output) catch( Exception e) System.out.println( e ); public void test6() Connect Four test = new ConnectFour(); test. LoadBoard( "LAB3_INPUT.txt" ); assertEquals( 1, test.gameStatus()); assertEquals( true, test.gameOver() ); assertEquals("000001 210012 120222 211211 221112 212121 ", test.toString(); 1000001 210012 | 120222 211211 221112 212121 You will be writing two classes (Tic Tac Toe.java and ConnectFour.java) to complete this lab. ConnectFour is a child of Tic Tac Toe Tic Tac Toe will represent annxn tic tac toe board. The board will be a two dimensional array of int's, where O represents an empty space, 1 represents a space occupied by player 1, and a 2 represents a place occupied by player 2. Tic Tac Toe needs to include the following: Tic Tac Toe(int n): Constructor that sets up a 2 dimensional array, nxn size boolean makeMove(int x, int y): places the current player in location x, y, where x is an index between and row length - 1, and y is an index between 0 and column length - 1. If there is currently another player occupying the space, no move is made, and a false is returned. If the player successfully occupies the space, a true is returned. int turn();returns the player number who's turn it is int gameStatus();returns a O if there are currently no winners - 1 if the game ends in a tie, a 1 if player 1 is a winner, and 2 if player 2 is a winner boolean gameOver();return false if the game is still in progress String toString();returns a String that represents the current state of the board. Each row should be a separate line, and there should be no white space between the columns void loadBoard(String fileName): overwrites the current contents of the board with the state of the board in the file, fileName. You may assume that fileName contains a valid Tic Tac Toe board, where each line represents a row, and there is no white space between the columns void saveBoard(String fileName): writes the current state of the board to a textfile, fleName. You should use the same formatting as toString() int checkPosition(int x, int y): accessor method that allows access to ONE position on the board at row x and column y. If the row or column is invalid, return a -1. The reason why you should NOT return the entire board is because the board is an object, and by returning an object you are giving clients access to make changes directly, which violates our encapsulation philosophy. **Note: You may NOT add any other public method to either classes. You may, however, add any number of PRIVATE helper methods you see appropriate. Connect Four is similar to Tic Tac Toe. The difference is, you do not need to occupy an entire row, or column, you simply need to have four in a row. The other difference is gravity determines the row number, and the player simply has a choice of the column. The purpose of utilizing inheritance is to reuse the code you already have, so think about the relationship between Tic Tac Toe and ConnectFour before implementing ConnectFour. You will need to determine which methods you should overwrite or overload. Note: you will only need two methods and one constructor for ConnectFour. ConnectFour should create a 6x game board. When making a move, you should specify one number, the column number, it should return true if the space can be occupied, and false if the column is "full". ConnectFour MUST be a subclass/child of Tic Tac Toe, and appropriate inheritance should be used to receive full credit for this assignment. Error Checking: You can assume that the input will be valid index numbers. Please use the Test File to sanity check your code. Error Handling: Your code must handle exceptions, such as when a file is not found. public void test50) Connect Four test = new ConnectFour(); test.makeMove ( 0); test.makeMove ( 0); test.makeMove( 1 ); test.makeMove( 1 ); test.makeMove( 2 ); test.makeMove( 2 ); test.makeMove( 3 ); assertEquals( true, test.gameOver() ); assertEquals( 1, test.gameStatus()); test. saveBoard("LAB3_OUTPUT.txt"); try FileReader fileReader = new FileReader( "LAB3_OUTPUT.txt" ) BufferedReader read = new BufferedReader( fileReader) String output = ""; String x = read.readLine(); while( x != null ) output += x + " "; x = read.readLine(); System.out.println( output ); assertEquals("000000 000000 000000 000000 222000 111100 ", output) catch( Exception e) System.out.println( e ); public void test6() Connect Four test = new ConnectFour(); test. LoadBoard( "LAB3_INPUT.txt" ); assertEquals( 1, test.gameStatus()); assertEquals( true, test.gameOver() ); assertEquals("000001 210012 120222 211211 221112 212121 ", test.toString(); 1000001 210012 | 120222 211211 221112 212121

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

Students also viewed these Databases questions