Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Main topics: Programmer Defined Methods Program Specification: Write a program t

ID: 3534244 • Letter: M

Question

Main topics:     Programmer Defined Methods

Program Specification:
Write a program that allows two individuals to play a single game of Tic-Tac-Toe.

Rules and Requirements:

    You must use a the following String variable to represent the nine squares of the board.

    String board = "| | | | | | | | | | | | ";

    You are given the following method to use:

      // returns true exactly when move is in [1, 9] and the corresponding
      // character in board String is not already set to X or O
      public static boolean isOpenMove(String board, int move)
      {
        if (move < 1 || move > 9)
          return false;
       
        if (move == 1 && board.charAt(1) == ' ')
          return true;
        if (move == 2 && board.charAt(3) == ' ')
          return true;
        if (move == 3 && board.charAt(5) == ' ')
          return true;
        if (move == 4 && board.charAt(9) == ' ')
          return true;
        if (move == 5 && board.charAt(11) == ' ')
          return true;
        if (move == 6 && board.charAt(13) == ' ')
          return true;
        if (move == 7 && board.charAt(17) == ' ')
          return true;
        if (move == 8 && board.charAt(19) == ' ')
          return true;
        if (move == 9 && board.charAt(21) == ' ')
          return true;
       
        return false;
      }

    Given the following Heading, you must write the corresponding definition for a int return method that uses a user validation loop and the isOpenMove method above, to obtain a square number [1, 9] whose corresponding character in the board String is not already set to X or O in the board.

      public static int getMove(Scanner stdIn, String board)

    You are given the following method to use:

      // creates and returns a new board String by using the square
      // number given by move to "replace" the corresponding character
      // in the "old" board String with symbol
      public static String recordMove(String board, int move, char symbol)
      {
        if (move == 1)
          board = board.substring(0, 1) + symbol + board.substring(2);
        else if (move == 2)
          board = board.substring(0, 3) + symbol + board.substring(4);
        else if (move == 3)
          board = board.substring(0, 5) + symbol + board.substring(6);
        else if (move == 4)
          board = board.substring(0, 9) + symbol + board.substring(10);
        else if (move == 5)
          board = board.substring(0, 11) + symbol + board.substring(12);
        else if (move == 6)
          board = board.substring(0, 13) + symbol + board.substring(14);
        else if (move == 7)
          board = board.substring(0, 17) + symbol + board.substring(18);
        else if (move == 8)
          board = board.substring(0, 19) + symbol + board.substring(20);
        else if (move == 9)
          board = board.substring(0, 21) + symbol + board.substring(22);
       
        return board;
      }

    You are given the following method to use:

      // returns the character {'X', 'O'} that is common to the three board
      // squares specified by pos1, pos2, pos3 if there is one, otherwise
      // returns ' '
      public static char checkThree(String board, int pos1, int pos2, int pos3)
      {
        if (board.charAt(pos1) == board.charAt(pos2) &&
            board.charAt(pos2) == board.charAt(pos3) &&
            board.charAt(pos1) != ' ')
          return board.charAt(pos1);
        else
          return ' ';
       }

    Given the following Heading, you must write the corresponding definition for a boolean return method that returns true exactly when the corresponding characters in any row, colum, or diagonal of the 3x3 board are all 'X's or all 'O's.

      public static boolean isWinner(String board)

    Your program may use additional methods where appropriate.

    You must write a main method that allows a pair of users to play a game of Tic-Tac-Toe such that:

    Each turn of the game:
        It must be clear whose turn it is X's or O's.

        The current board configuration must be displayed in a reasonable format.

        An avaliable square number [1, 9] is obtained from a player and is used make their (valid) move on the board.

    The game ends when there is a Tic-Tac-Toe (win) or all the positions of the board are full (stalemate). A simple report indicates which has occurred and in the case of a win, who has won X or O.

Notes and Hints:

    Use the board's .charAt() method to access the nine individual char locations in the board string:

        1 2 3 4 5 6 7 8 9   logical square number
        1 3 5 9 11 13 17 19 21   actual string index

    Each of the nine char locations with in the board string should at all times hold exactly one of the following char literals:
        ' ', indicating the square is available.
        'X', indicating the square has an X in it.
        'O', indicating the square has an O in it.

    Use a char variable to keep track of who's turn it is: X's or O's.

    Use a int variable to keep track of how many turns have passed in the game (nine at most).

    Just let X go first.

Sample run(s):

Welcome to Tic-Tac-Toe!
---------------------------------------
To start the game, X goes first.

| | | |
| | | |
| | | |

Please enter a square (1-9): 5
---------------------------------------
Thank you, it is now O's turn.

| | | |
| |X| |
| | | |

Please enter a square (1-9): 2
---------------------------------------
Thank you, it is now X's turn.

| |O| |
| |X| |
| | | |

Please enter a square (1-9): 1
---------------------------------------
Thank you, it is now O's turn.

|X|O| |
| |X| |
| | | |

Please enter a square (1-9): 9
---------------------------------------
Thank you, it is now X's turn.

|X|O| |
| |X| |
| | |O|

Please enter a square (1-9): 4
---------------------------------------
Thank you, it is now O's turn.
|X|O| |

|X|X| |
| | |O|

Please enter a square (1-9): 6
---------------------------------------
Thank you, it is now X's turn.

|X|O| |
|X|X|O|
| | |O|

Please enter a square (1-9): 7
---------------------------------------
Thank you, X has won!

|X|O| |
|X|X|O|
|X| |O|


---------------------------------------
---------------------------------------



Welcome to Tic-Tac-Toe!
---------------------------------------
To start the game, X goes first.

| | | |
| | | |
| | | |

Please enter a square (1-9): 1
---------------------------------------
Thank you, it is now O's turn.

|X| | |
| | | |
| | | |

Please enter a square (1-9): 5
---------------------------------------
Thank you, it is now X's turn.

|X| | |
| |O| |
| | | |

Please enter a square (1-9): 4
---------------------------------------
Thank you, it is now O's turn.

|X| | |
|X|O| |
| | | |

Please enter a square (1-9): 7
---------------------------------------
Thank you, it is now X's turn.

|X| | |
|X|O| |
|O| | |

Please enter a square (1-9): 3
---------------------------------------
Thank you, it is now O's turn.

|X| |X|
|X|O| |
|O| | |

Please enter a square (1-9): 2
---------------------------------------
Thank you, it is now X's turn.

|X|O|X|
|X|O| |
|O| | |

Please enter a square (1-9): 8
---------------------------------------
Thank you, it is now O's turn.

|X|O|X|
|X|O| |
|O|X| |

Please enter a square (1-9): 9
---------------------------------------
Thank you, it is now X's turn.

|X|O|X|
|X|O| |
|O|X|O|

Please enter a square (1-9): 6
---------------------------------------
Thank you, the game is a Stalemate!

|X|O|X|
|X|O|X|
|O|X|O|


Explanation / Answer

/** Please note: The program consists of 3 parts...The main program, the cell and the board classes * The Cell class models each individual cell of the game board. */ public class Cell { // save as Cell.java // package access Seed content; // content of this cell of type Seed. // take a value of Seed.EMPTY, Seed.CROSS, or Seed.NOUGHT int row, col; // row and column of this cell, not used in this program /** Constructor to initialize this cell */ public Cell(int row, int col) { this.row = row; this.col = col; clear(); // clear content } /** Clear the cell content to EMPTY */ public void clear() { content = Seed.EMPTY; } /** Paint itself */ public void paint() { switch (content) { case CROSS: System.out.print(" X "); break; case NOUGHT: System.out.print(" O "); break; case EMPTY: System.out.print(" "); break; } } }/** * The Board class models the game-board. */ public class Board { // save as Board.java // Named-constants for the dimensions public static final int ROWS = 3; public static final int COLS = 3; // package access Cell[][] cells; // a board composes of ROWS-by-COLS Cell instances int currentRow, currentCol; // the current seed's row and column /** Constructor to initialize the game board */ public Board() { cells = new Cell[ROWS][COLS]; // allocate the array for (int row = 0; row
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote