Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is a Java Assignment to create a tic tac toe game between 2 players. I the 4 classes and what needs to be provided

This is a Java Assignment to create a tic tac toe game between 2 players. I the 4 classes and what needs to be provided in each class, also i have attached screen shots of what the teacher has provided as a starting point for the assignment

Our Implementation We are now ready to program our solution. We will only need four classes for this. For the assignment, you need to follow the patterns that we provide. You cannot change any of the signatures of the methods (that is you cannot modify the methods at all). You cannot add new public methods or variables. You can, however, add new private methods to improve the readability or the organization of your code.

GameState

GameState is an enum type which is used to capture the current state of the game. It has four possible values: PLAYING: this game is ongoing, DRAW: this game is a draw, XWIN: this game as been won by the first player, OWIN: this game as been won by the second player.

CellValue

CellValue is an enum type which is used to capture the state of a cell. It has three possible values:

EMPTY: the cell is empty,

X: there is a X in the cell,

O: there is a O in the cell.

TicTacToeGame

Instances of the class TicTacToeGame represent a game being played. Each object stores the actual board, which is saved in a single dimension array. There is an instance method that can be used to play the next move. The object figures out the players turn, so that information is not specified: we simply specify the index to play and the object knows to play either a X or a O. The object also tracks the state of the game automatically. The specification for our class TicTacToeGame is given in our zip file. You need to fill out all the missing parts, reading carefully all the comments before doing so. You cannot modify the methods or the variables that are provided. You can, however, add new private methods as required. The template that you are working with contains the following: 5 An instance variable which is a reference to an array of CellValue to record the state of the board. Some instance variables to record the games number of columns and lines, the number of cells to align, the number of turns played (level) and current state. Three constructors: the default one creates the usual game (the 3x3 grid, with a win being 3 similar cells aligned), a second one can be used to specify both lines and columns, and the last one is used to specify lines, columns and number of cells to align. As usual, all the instance variables must be initialized when the object is constructed. Getters for lines, columns, sizeWin, level and games current state A method to query the object about the next player (that is, is it Xs or Os turn to play?). A method play(int index) to play at a particular location in the game. This updates the game state and the board. We also have a helper method, private void setGameState(int index), which is used to compute and update the game state once a particular move is played in the method play. Finally, we have a method toString(), which returns a string representation of the current state of the board, as shown in the example before. An example of a String returned by toString would be, when printed out: | X | | O --------------- | O | X | --------------- | | | X There are a few situations that need our attention. For example, the index selected by the player may be invalid or illegal. We do not have a very good way to handle these situations yet, so for the time being we will simply write an error message. The subsequent behaviour of the method is unspecified, so simply implement something that seems to make sense1 . One other situation would be that players continue the game after one of them wins. For testing purpose, we actually want that to be possible, however, then game state should reflect the first winner of the game. So if the players keep going after a win, a message is printed out but the game continues as long as the moves are legal. The first winner remains. Note that the method toString() returns a reference to a String, it is not actually printing anything. So that one String instance, when printed, should produce the expected output (in other words, that one string instance, when printed, will span several lines).

TicTacToe

This class implements playing the game. You are provided with the initial part, which creates the instance of the class TicTacToeGame based on the parameters submitted by the user. All you need to do here is implementing the remainder of the main method, the part that plays the game. It basically loops through each step of the game until the game is over. At each step, it displays the current game and prompts the next player, X or a O, for a cell to play. It then plays that cell and keeps going until the game is over, at which point it finishes (so although we said that TicTacToeGame is able to play past the winning point, that situation should never occur from the main of TicTacToe). If the cell provided by the player is invalid or illegal, and a message is displayed to the user, who is asked to play again. Here are a couple of examples of this situation: $ java TicTacToe | | ----------- | | ----------- | | 1The reason we are not specifying any behaviour here is because once we will have the tools required to deal with these exceptional situations, we will see that we actually will not have to come up with an alternative behaviour at all. 6 X to play: 2 | X | ----------- | | ----------- | | O to play: 10 The value should be between 1 and 9 | X | ----------- | | ----------- | | O to play: 2 This cell has already been played | X | ----------- | | ----------- | | O to play: 3 | X | O ----------- | | ----------- | | X to play: Note that you can assume that the players are only providing integer values as inputs. You do not have to handle the case of other input types such as a character.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

enum CellValue { // YOUR CODE HERE enum GameState{ // YOUR CODE HERE TicTacToe.java X import java.io.Console; public class Tictactoe{ * main of the application. Creates the instance of TicTacToeGame * and starts the game. If two parameters lines and columns * are passed, they are used. If the paramters lines, columns * and win are passed, they are used. * Otherwise, a default value is used. Defaults values (3) are also * used if the paramters are too small (less than 2). * Here, we assume that the command lines arguments are indeed integers * @param args command lines parameters public static void main(String[] args) { StudentInfo.display(); Console console = System.console(); TicTacToeGame game; int lines, columns, win; lines = 3; columns = 3; win = 3; if (args. length >= 2) { lines = Integer.parseInt(args[0]); if(lines 2) { System.out.println("Invalid argument, using default..."); lines = 3; columns = Integer.parseInt(args[1]); if(columns 2) { System.out.println("Invalid argument, using default..."); columns = 3; if (args. length >= 3){ win = Integer.parseInt(args[2]); if(win 3){ System.out.println("Too many arguments. Only the first 3 are used."); game = new TicTacToeGame(lines, columns, win); // YOUR CODE HERE Line 1, Column 1 Spaces: 4 Java public class TicTacToeGame { // FINISH THE VARIABLE DECLARATION * The board of the game, stored as a one dimension array. board; * level records the number of rounds that have been * played so far. level; * gameState records the current state of the game. gameState; * lines is the number of lines in the grid lines; * columns is the number of columns in the grid * columns; * sizeWin is the number of cell of the same type * that must be aligned to win the game sizeWin; * default constructor, for a game of 3x3, which must * align 3 cells public TicTacToeGame() { // YOUR CODE HERE * constructor allowing to specify the number of lines * and the number of columns for the game, as well as * the number of cells that must be aligned to win. * @param lines * the number of lines in the game * @param columns * the number of columns in the game * @param sizeWin * the number of cells that must be aligned to win. public TicTacToeGame(int lines, int columns, int sizeWin){ // YOUR CODE HERE * getter for the variable lines * @return * the value of lines public int getLines() { // YOUR CODE HERE * getter for the variable columns * @return * the value of columns public int getColumns() { // YOUR CODE HERE /** * getter for the variable level * @return * the value of level * public int getLevel(){ // YOUR CODE HERE * getter for the variable sizeWin * @return * the value of sizeWin */ public int getSizeWin() { // YOUR CODE HERE * getter for the variable gameState * @return * the value of gameState public GameState getGameState() { // YOUR CODE HERE * returns the cellValue that is expected next, * in other word, which played (X or O) should * play next. * This method does not modify the state of the * game. * @return * the value of the enum CellValue corresponding * to the next expected value. public CellValue nextCellValue() { // YOUR CODE HERE * returns the value of the cell at * index i. * If the index is invalid, an error message is printed out. The behaviour is then unspecified * @param i * the index of the cell in the array board * @return * the value at index i in the variable board. */ public CellValue valueAt(int i) { // YOUR CODE HERE * This method is called when the next move has been * decided by the next player. It receives the index * of the cell to play as parameter. * If the index is invalid, an error message is * printed out. The behaviour is then unspecified * If the chosen cell is not empty, an error message: * printed out. The behaviour is then unspecified * If the move is valide, the board is updated, as well * as the state of the game. * To faciliate testing, is is acceptable to keep playing * after a game is already won. If that is the case, the * a message should be printed out and the move recorded. * the winner of the game is the player who won first * @param i * the index of the cell in the array board that has been * selected by the next player public void play(int i) { // YOUR CODE HERE * A helper method which updates the gameState variable * correctly after the cell at index i was just set in * the method play(int i) * The method assumes that prior to setting the cell * at index i, the gameState variable was correctly set. * it also assumes that it is only called if the game was * not already finished when the cell at index i was played * (i.e. the game was playing). Therefore, it only needs to * check if playing at index i has concluded the game, and if * set the oucome correctly * @param i * the index of the cell in the array board that has just * been set private void setGameState(int i) { // YOUR CODE HERE * @param i * the index of the cell in the array board that has just * been set private void setGameState(int i) { // YOUR CODE HERE * Returns a String representation of the game matching * the example provided in the assignment's description * @return * String representation of the game public String toString() { // YOUR CODE HERE enum CellValue { // YOUR CODE HERE enum GameState{ // YOUR CODE HERE TicTacToe.java X import java.io.Console; public class Tictactoe{ * main of the application. Creates the instance of TicTacToeGame * and starts the game. If two parameters lines and columns * are passed, they are used. If the paramters lines, columns * and win are passed, they are used. * Otherwise, a default value is used. Defaults values (3) are also * used if the paramters are too small (less than 2). * Here, we assume that the command lines arguments are indeed integers * @param args command lines parameters public static void main(String[] args) { StudentInfo.display(); Console console = System.console(); TicTacToeGame game; int lines, columns, win; lines = 3; columns = 3; win = 3; if (args. length >= 2) { lines = Integer.parseInt(args[0]); if(lines 2) { System.out.println("Invalid argument, using default..."); lines = 3; columns = Integer.parseInt(args[1]); if(columns 2) { System.out.println("Invalid argument, using default..."); columns = 3; if (args. length >= 3){ win = Integer.parseInt(args[2]); if(win 3){ System.out.println("Too many arguments. Only the first 3 are used."); game = new TicTacToeGame(lines, columns, win); // YOUR CODE HERE Line 1, Column 1 Spaces: 4 Java public class TicTacToeGame { // FINISH THE VARIABLE DECLARATION * The board of the game, stored as a one dimension array. board; * level records the number of rounds that have been * played so far. level; * gameState records the current state of the game. gameState; * lines is the number of lines in the grid lines; * columns is the number of columns in the grid * columns; * sizeWin is the number of cell of the same type * that must be aligned to win the game sizeWin; * default constructor, for a game of 3x3, which must * align 3 cells public TicTacToeGame() { // YOUR CODE HERE * constructor allowing to specify the number of lines * and the number of columns for the game, as well as * the number of cells that must be aligned to win. * @param lines * the number of lines in the game * @param columns * the number of columns in the game * @param sizeWin * the number of cells that must be aligned to win. public TicTacToeGame(int lines, int columns, int sizeWin){ // YOUR CODE HERE * getter for the variable lines * @return * the value of lines public int getLines() { // YOUR CODE HERE * getter for the variable columns * @return * the value of columns public int getColumns() { // YOUR CODE HERE /** * getter for the variable level * @return * the value of level * public int getLevel(){ // YOUR CODE HERE * getter for the variable sizeWin * @return * the value of sizeWin */ public int getSizeWin() { // YOUR CODE HERE * getter for the variable gameState * @return * the value of gameState public GameState getGameState() { // YOUR CODE HERE * returns the cellValue that is expected next, * in other word, which played (X or O) should * play next. * This method does not modify the state of the * game. * @return * the value of the enum CellValue corresponding * to the next expected value. public CellValue nextCellValue() { // YOUR CODE HERE * returns the value of the cell at * index i. * If the index is invalid, an error message is printed out. The behaviour is then unspecified * @param i * the index of the cell in the array board * @return * the value at index i in the variable board. */ public CellValue valueAt(int i) { // YOUR CODE HERE * This method is called when the next move has been * decided by the next player. It receives the index * of the cell to play as parameter. * If the index is invalid, an error message is * printed out. The behaviour is then unspecified * If the chosen cell is not empty, an error message: * printed out. The behaviour is then unspecified * If the move is valide, the board is updated, as well * as the state of the game. * To faciliate testing, is is acceptable to keep playing * after a game is already won. If that is the case, the * a message should be printed out and the move recorded. * the winner of the game is the player who won first * @param i * the index of the cell in the array board that has been * selected by the next player public void play(int i) { // YOUR CODE HERE * A helper method which updates the gameState variable * correctly after the cell at index i was just set in * the method play(int i) * The method assumes that prior to setting the cell * at index i, the gameState variable was correctly set. * it also assumes that it is only called if the game was * not already finished when the cell at index i was played * (i.e. the game was playing). Therefore, it only needs to * check if playing at index i has concluded the game, and if * set the oucome correctly * @param i * the index of the cell in the array board that has just * been set private void setGameState(int i) { // YOUR CODE HERE * @param i * the index of the cell in the array board that has just * been set private void setGameState(int i) { // YOUR CODE HERE * Returns a String representation of the game matching * the example provided in the assignment's description * @return * String representation of the game public String toString() { // YOUR CODE HERE

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_2

Step: 3

blur-text-image_3

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

Databases Illuminated

Authors: Catherine M. Ricardo

1st Edition

0763733148, 978-0763733148

More Books

Students also viewed these Databases questions