Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help creating this tic tac toe game. I do not understand what he wants and I am trying to do this by tomorrow.

I need help creating this tic tac toe game. I do not understand what he wants and I am trying to do this by tomorrow.

public class TicTacToeModel { /** * The contents of the Tic-Tac-Toe game board */ private TicTacToeSquare[][] board; /** * xTurn is true if X is the current player, or false if O is the current * player */ private boolean xTurn; /** * The dimension (width and height) of the game board */ private int dimension;

/** * Default Constructor (uses the default dimension) */ public TicTacToeModel() { this(TicTacToe.DEFAULT_DIMENSION); } /** * Constructor (uses specified dimension) * * @param dimension The dimension (width and height) of the new * Tic-Tac-Toe board. */ public TicTacToeModel(int dimension) { /* Initialize dimension; X goes first */ this.dimension = dimension; xTurn = true; /* Create board as a 2D TicTacToeSquare array */ board = new TicTacToeSquare[dimension][dimension];

/* Initialize board (fill with TicTacToeSquare.EMPTY) */ for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ board[i][j] = TicTacToeSquare.EMPTY; } } } /** * Use isValidSquare(int, int) to check if the specified square is in range, * and use isSquareMarked(int, int) to check if the square is currently * empty. If both conditions are satisfied, create a mark in the square for * the current player, then toggle xTurn from true to false (or vice-versa) * to switch to the other player before returning TRUE. Otherwise, return * FALSE. * * @param row the row (Y coordinate) of the square * @param col the column (X coordinate) of the square * @return a Boolean value representing the result of the attempt to * make this mark: true if the attempt was successful, and false otherwise * @see TicTacToeSquare */ public boolean makeMark(int row, int col) { board[row][col]=true; return true; //return false; this is a stub; you may need to remove it later! } /** * Checks if the specified square is within range (that is, within the bounds * of the game board). * * @param row the row (Y coordinate) of the square * @param col the column (X coordinate) of the square * @return a Boolean value: true if the square is in range, and false * if it is not */ private boolean isValidSquare(int row, int col) { return false; // this is a stub; you may need to remove it later! } /** * Checks if the specified square is marked. * * @param row the row (Y coordinate) of the square * @param col the column (X coordinate) of the square * @return a Boolean value: true if the square is marked, and false * if it is not */ private boolean isSquareMarked(int row, int col) { // INSERT YOUR CODE HERE return false; // this is a stub; you may need to remove it later! } /** * Returns a {@link TicTacToeSquare} object representing the content of the * specified square of the Tic-Tac-Toe board. * * @param row the row (Y coordinate) of the square * @param col the column (X coordinate) of the square * @return the content of the specified square * @see TicTacToeSquare */ public TicTacToeSquare getSquare(int row, int col) { // INSERT YOUR CODE HERE return null; // this is a stub; you should remove it later! } /** * Use isMarkWin() to determine if X or O is the winner, if the game is a * tie, or if the game is still in progress. Return the current state of the * game as a {@link TicTacToeState} object. * * @return the current state of the Tic-Tac-Toe game * @see TicTacToeState */ public TicTacToeState getState() { // INSERT YOUR CODE HERE return null; // this is a stub; you should remove it later! } /** * Check the squares of the Tic-Tac-Toe board to see if the specified player * is the winner. * * @param mark the mark representing the player to be checked (X or O) * @return true if the specified player is the winner, or false if not * @see TicTacToeSquare */ private boolean isMarkWin(TicTacToeSquare mark) { // INSERT YOUR CODE HERE return false; // this is a stub; you may need to remove it later! } /** * Check the squares of the board to see if the Tic-Tac-Toe game is currently * in a tie state. * * @return true if the game is currently a tie, or false otherwise */ private boolean isTie() { // INSERT YOUR CODE HERE return false; // this is a stub; you may need to remove it later! }

/** * Uses {@link #getState() getState} to checks if the Tic-Tac-Toe game is * currently over, either because a player has won or because the game is * in a tie state. * * @return true if the game is currently over, or false otherwise */ public boolean isGameover() { return TicTacToeState.NONE != getState(); }

/** * Getter for xTurn. * * @return true if X is the current player, or false if O is the current * player */ public boolean isXTurn() { return xTurn; } /** * Getter for dimension. * * @return the dimension (width and height) of the Tic-Tac-Toe * game board */ public int getDimension() { return dimension; } /** *

Returns the current content and state of the Tic-Tac-Toe game board as * a formatted String. This method must use a {@link StringBuilder} * to compose the output String, which should not include any empty lines.

*

Here is an example of how the output for a 3x3 game board should * appear (also see the example output on Canvas):

* 012 0 O 1 X 2 O X * @return the representation of the Tic-Tac-Toe game board * @see StringBuilder */ @Override public String toString() { StringBuilder output = new StringBuilder(); // INSERT YOUR CODE HERE return output.toString(); } }

public class TicTacToeController {

private final TicTacToeModel model; private final TicTacToeView view; /** * Constructor. Uses specified dimension to initialize Model and View * * @param dimension The dimension (width and height) of the new * Tic-Tac-Toe board. */ public TicTacToeController(int dimension) {

model = new TicTacToeModel(dimension); view = new TicTacToeView(); } /** *

Implements the main game loop, which repeats until the game is over. * This method should use {@link TicTacToeModel#isGameover()} to check if * the game is over; as long as the game is not over, the main loop should:

*

  1. *
  2. Display the board using the {@link TicTacToeView#showBoard(java.lang.String)} method,
  3. *
  4. Get the next move from player using the {@link TicTacToeView#getNextMove(boolean)} method,
  5. *
  6. Attempt to make the player's mark using the {@link TicTacToeModel#makeMark(int, int)} method.
  7. *

*

If the attempt failed, the {@link TicTacToeView#showInputError()} method * should be called to display an error message. The player should then be * prompted to enter another move until the attempt is successful.

*

After the game is over, use the {@link TicTacToeView#showBoard(java.lang.String)} method * to show the final state of the game board, and the {@link TicTacToeView#showResult(java.lang.String)} method * to show the final result: either X or O wins, or a tie condition.

*/

public void start() { /* MAIN LOOP (repeats until game is over) */

// INSERT YOUR CODE HERE /* Display Results and Exit */

view.showBoard(model.toString());

view.showResult(model.getState().toString()); }

}

public class TicTacToeView { private final Scanner keyboard; /** * Constructor. This version of the Tic-Tac-Toe game uses the console for * I/O, so a {@link Scanner} connected to standard input is initialized to * receive the user's input from the console keyboard. Output will be * directed to standard output using the {@link System#out} output stream. */ public TicTacToeView() { keyboard = new Scanner(System.in); } /** * Prompt the current player to enter his or her next move. Use iXTurn to * display an appropriate prompt for the current player. This method should: * receive the player's input (a row and column, separated by spaces), use * these values to initialize a new {@link TicTacToeMove} object, and then * return this object to the caller. * * @param isXTurn a Boolean representing the current player: true if X, or * false if O * @return a {@link TicTacToeMove} value representing the player's * input * @see TicTacToeMove */ public TicTacToeMove getNextMove(boolean isXTurn) { // INSERT YOUR CODE HERE (refer to the example output on Canvas!) return null; // this is a stub; you should remove it later!

} /** * This method displays a descriptive error message if there was a problem * with an attempt to enter a move. This can happen if the specified * location is invalid, already marked, or out of bounds. */ public void showInputError() {

System.out.println("Entered location is invalid, already marked, or out of bounds.");

} /** * This method prints the final result of the Tic-Tac-Toe game to the * console: the current state, appended by an exclamation point, on a line by * itself. (The result is provided as a String by the {@link TicTacToeModel} * class's {@link TicTacToeModel#getState()} method; this method simply * outputs it to the console.) * * @param result the result of the game, from {@link TicTacToeModel#getState()} */ public void showResult(String result) {

System.out.println(result + "!");

} /** * This method prints the current Tic-Tac-Toe game board to the console, * prepended by two blank lines. (The content of the game board is provided * as a String by the {@link TicTacToeModel#toString()} method of the * {@link TicTacToeModel} class; this method simply outputs it to the * console.) * * @param board the content of the game board, from {@link TicTacToeModel#toString()} * @see TicTacToeModel */ public void showBoard(String board) { System.out.println(" " + board); } }

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

Intelligent Information And Database Systems Second International Conference Acids Hue City Vietnam March 2010 Proceedings Part 1 Lnai 5990

Authors: Manh Thanh Le ,Jerzy Swiatek ,Ngoc Thanh Nguyen

2010th Edition

3642121446, 978-3642121449

More Books

Students also viewed these Databases questions

Question

Use a three-step process to develop effective business messages.

Answered: 1 week ago