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

Need Help creating this program for my java class. Sample output is provided alo

ID: 3573577 • Letter: N

Question

 Need Help creating  this program for my java class. Sample output is provided along with a starting point set of code where I just need code filled in where it says. thank you   Create a Tic Tac Toe Program Sample output from your program should look like the following:   ===============================e       ---a------b------c---       |      |      |     |       ---d------e------f---       |      |      |     |       ---g------h------i---       |      |      |     |       --------------------- Entering input for: player1: O enter in cell  [a, b, ... i]e ===============================e       ---a------b------c---       |      |      |     |       ---d------e------f---       |      |  O   |     |       ---g------h------i---       |      |      |     |       --------------------- Entering input for: player2: X enter in cell  [a, b, ... i]b ===============================e       ---a------b------c---       |      |  X   |     |       ---d------e------f---       |      |  O   |     |       ---g------h------i---       |      |      |     |       --------------------- Entering input for: player1: O enter in cell  [a, b, ... i]i ===============================e       ---a------b------c---       |      |  X   |     |       ---d------e------f---       |      |  O   |     |       ---g------h------i---       |      |      |  O  |       --------------------- Entering input for: player2: X enter in cell  [a, b, ... i]a ===============================e       ---a------b------c---       |  X   |  X   |     |       ---d------e------f---       |      |  O   |     |       ---g------h------i---       |      |      |  O  |       --------------------- Entering input for: player1: O enter in cell  [a, b, ... i]c ===============================e       ---a------b------c---       |  X   |  X   |  O  |       ---d------e------f---       |      |  O   |     |       ---g------h------i---       |      |      |  O  |       --------------------- Entering input for: player2: X enter in cell  [a, b, ... i]f ===============================e       ---a------b------c---       |  X   |  X   |  O  |       ---d------e------f---       |      |  O   |  X  |       ---g------h------i---       |      |      |  O  |       --------------------- Entering input for: player1: O enter in cell  [a, b, ... i]g ********  We have a winner: player1: O ===============================e       ---a------b------c---       |  X   |  X   |  O  |       ---d------e------f---       |      |  O   |  X  |       ---g------h------i---       |  O   |      |  O  |       ---------------------  Do you want to play Tic-Tac-Toe (y/n)?y ===============================e       ---a------b------c---       |      |      |     |       ---d------e------f---       |      |      |     |       ---g------h------i---       |      |      |     |       --------------------- Entering input for: player1: O enter in cell  [a, b, ... i]e ===============================e       ---a------b------c---       |      |      |     |       ---d------e------f---       |      |  O   |     |       ---g------h------i---       |      |      |     |       --------------------- Entering input for: player2: X enter in cell  [a, b, ... i]a ===============================e       ---a------b------c---       |  X   |      |     |       ---d------e------f---       |      |  O   |     |       ---g------h------i---       |      |      |     |       --------------------- Entering input for: player1: O enter in cell  [a, b, ... i]c ===============================e       ---a------b------c---       |  X   |      |  O  |       ---d------e------f---       |      |  O   |     |       ---g------h------i---       |      |      |     |       --------------------- Entering input for: player2: X enter in cell  [a, b, ... i]g ===============================e       ---a------b------c---       |  X   |      |  O  |       ---d------e------f---       |      |  O   |     |       ---g------h------i---       |  X   |      |     |       --------------------- Entering input for: player1: O enter in cell  [a, b, ... i]d ===============================e       ---a------b------c---       |  X   |      |  O  |       ---d------e------f---       |  O   |  O   |     |       ---g------h------i---       |  X   |      |     |       --------------------- Entering input for: player2: X enter in cell  [a, b, ... i]f ===============================e       ---a------b------c---       |  X   |      |  O  |       ---d------e------f---       |  O   |  O   |  X  |       ---g------h------i---       |  X   |      |     |       --------------------- Entering input for: player1: O enter in cell  [a, b, ... i]h ===============================e       ---a------b------c---       |  X   |      |  O  |       ---d------e------f---       |  O   |  O   |  X  |       ---g------h------i---       |  X   |  O   |     |       --------------------- Entering input for: player2: X enter in cell  [a, b, ... i]b ===============================e       ---a------b------c---       |  X   |  X   |  O  |       ---d------e------f---       |  O   |  O   |  X  |       ---g------h------i---       |  X   |  O   |     |       --------------------- Entering input for: player1: O enter in cell  [a, b, ... i]i ===============================e       ---a------b------c---       |  X   |  X   |  O  |       ---d------e------f---       |  O   |  O   |  X  |       ---g------h------i---       |  X   |  O   |  O  |       --------------------- ********  Game Ends in Draw  Do you want to play Tic-Tac-Toe (y/n)?n Bye 
 A starting point for your program is provided (if you feel overly  constrained by this and have a better idea on structuring this  game you can create your own structure --  main should not have very much code): 
 package tictactoe;  import java.util.*;  class TicTacToe {     char ttt[][] = new char[3][3];     static final char player1 = 'O';     static final char player2 = 'X';     Scanner scan  =new Scanner(System.in);                   String playerID(char player)     {     // Prints the identity of the player     // For example:     // player2: X              if (player == player1)             return "player1: "+player;         else             return "player2: "+ player;     }          void getPlayerInput(char player)     {     // ******* Details to fill in ************             // Prompt the user for a cell input.  Make sure its a legal        // cell designator.  Also make sure the cell doesn't already        // have a player in it.  Prompt the user again in the case         // of any problem.  Once a valid spot has been found,         // you will issue a statement like:                  ttt[row][col]=player;                  }         boolean gameIsDraw()     {      // ******* Details to fill in ************               // If all ttt entries have been taken return true         // otherwise return false     }          boolean winner(char player)     {      // ******* Details to fill in ************            // Check to see if the parameter player has won      // Winning means that player shows up in a line of       // three.  The line can be a column, row or a diagonal      // Return true if player is a winner, otherwise return false.     }      void displayBoard()     {      // ******* Details to fill in ************               // If you want to skip the fun of this, I have a candidate displayBoard down below              }                void newgame()     {         char currPlayer = player1;         for(int i=0; i<3; i++)             for(int j=0; j<3; j++)                 ttt[i][j] =' ';                  boolean continueFlag = true;                 while (continueFlag)         {             displayBoard();             if (gameIsDraw())             {                 System.out.println("Game Ends in Draw");                 continueFlag = false;             }             else             {                 getPlayerInput(currPlayer);                 if (winner(currPlayer))                 {                     System.out.println("We have a winner: " + playerID(currPlayer));                     displayBoard();                     continueFlag = false;                 }                 else                 {                      if (currPlayer == player1) currPlayer = player2;                         else currPlayer = player1;                 }              }         }              }           public static void main(String[] args)     {         TicTacToe game = new TicTacToe();         String str;         do         {             game.newgame();                          System.out.println("Do you want to play Tic-Tac-Toe (y/n)?");             str= game.scan.next();         } while ("y".equals(str));                  System.out.println("Bye");     }     }    By the way, I'm going to give a version of displayBoard().  This routine is a bit  of a pain and I'm not sure how much learning happens by going through the pain.  Also, I'm not real happy with my version, but it at least works.  So... if you  want to use my version, here it is:  void displayBoard()     {         System.out.println("********************************");                 System.out.println("      ---a------b------c---");          for (int i=0; i<3; i++)         {             for (int j=0; j< 3; j++)             {               if (j == 0) System.out.print("      |  ");                System.out.print(ttt[i][j]);               if (j < 2) System.out.print( "   |  ");               if (j==2)  System.out.print("  |");             }             System.out.println();             switch (i)             {             case 0:                 System.out.println("      ---d------e------f---");                 break;             case 1:                 System.out.println("      ---g------h------i---");                 break;             case 2:                 System.out.println("      ---------------------");                 break;             }         }     } 

Explanation / Answer

TicTacToe.java

import java.util.Scanner;

public class TicTacToe {
   // Name-constants to represent the seeds and cell contents
   public static final int EMPTY = 0;
   public static final int CROSS = 1;
   public static final int NOUGHT = 2;

   // Name-constants to represent the various states of the game
   public static final int PLAYING = 0;
   public static final int DRAW = 1;
   public static final int CROSS_WON = 2;
   public static final int NOUGHT_WON = 3;

   // The game board and the game status
   public static final int ROWS = 3, COLS = 3; // number of rows and columns
   public static int[][] board = new int[ROWS][COLS]; // game board in 2D array
                                                       // containing (EMPTY,
                                                       // CROSS, NOUGHT)
   public static int currentState; // the current state of the game
                                   // (PLAYING, DRAW, CROSS_WON, NOUGHT_WON)
   public static int currentPlayer; // the current player (CROSS or NOUGHT)
   public static int currntRow, currentCol; // current seed's row and column

   public static Scanner in = new Scanner(System.in); // the input Scanner

   /** The entry main method (the program starts here) */
   public static void main(String[] args) {
       String choice;
       do{
       // Initialize the game-board and current status
       initGame();
       displayBoard();//initialize the game board
       do {
           playerMove(currentPlayer); // update currentRow and currentCol
           updateGame(currentPlayer, currntRow, currentCol); // update currentState
          
           displayBoard();
           // Print message if game-over
           if (currentState == CROSS_WON) {
               System.out.println("Player 1 'X' won! Bye!");
           } else if (currentState == NOUGHT_WON) {
               System.out.println("Player 2 'O' won! Bye!");
           } else if (currentState == DRAW) {
               System.out.println("******** Game Ends in Draw Bye");
           }
           // Switch player
           currentPlayer = (currentPlayer == CROSS) ? NOUGHT : CROSS;
       } while (currentState == PLAYING); // repeat if not game-over

       Scanner in2 = new Scanner(System.in);
       System.out.println("Do you want to play Tic-Tac-Toe (y/n)?");
       choice = in2.next();
       if(choice.equalsIgnoreCase("n")){
           System.out.println("Bye");
           return;
       }
       }
       while (choice.equalsIgnoreCase("y"));
   }

   /** Initialize the game-board contents and the current states */
   public static void initGame() {
       for (int row = 0; row < ROWS; ++row) {
           for (int col = 0; col < COLS; ++col) {
               board[row][col] = EMPTY; // all cells empty
           }
       }
       currentState = PLAYING; // ready to play
       currentPlayer = CROSS; // cross plays first
   }

   /**
   * Player with the "theSeed" makes one move, with input validation. Update
   * global variables "currentRow" and "currentCol".
   */
   public static void playerMove(int theSeed) {
       boolean validInput = false; // for input validation
       do {
           if (theSeed == CROSS) {
               System.out.println("Entering input for : player 1 : X ");
           } else {
               System.out.println("Entering input for : player 2 : O ");
           }
           System.out.println("Enter in cell [a, b, ... i]");
           String str = in.next();

           int row = 0;
           int col = 0;

           switch (str) {
           case "a":
               row = col = 0;
               break;
           case "b":
               row = 0;
               col = 1;
               break;
           case "c":
               row = 0;
               col = 2;
               break;
           case "d":
               row = 1;
               col = 0;
               break;
           case "e":
               row = 1;
               col = 1;
               break;
           case "f":
               row = 1;
               col = 2;
               break;
           case "g":
               row = 2;
               col = 0;
               break;
           case "h":
               row = 2;
               col = 1;
               break;
           case "i":
               row = 2;
               col = 2;
               break;
           }

           if (row >= 0 && row < ROWS && col >= 0 && col < COLS
                   && board[row][col] == EMPTY) {
               currntRow = row;
               currentCol = col;
               board[currntRow][currentCol] = theSeed; // update game-board
                                                       // content
               validInput = true; // input okay, exit loop
           } else {
               System.out.println("This move at (" + (row + 1) + ","
                       + (col + 1) + ") is not valid. Try again...");
           }
       } while (!validInput); // repeat until input is valid
   }

   /**
   * Update the "currentState" after the player with "theSeed" has placed on
   * (currentRow, currentCol).
   */
   public static void updateGame(int theSeed, int currentRow, int currentCol) {
       if (hasWon(theSeed, currentRow, currentCol)) { // check if winning move
           currentState = (theSeed == CROSS) ? CROSS_WON : NOUGHT_WON;
       } else if (isDraw()) { // check for draw
           currentState = DRAW;
       }
       // Otherwise, no change to currentState (still PLAYING).
   }

   /** Return true if it is a draw (no more empty cell) */
   public static boolean isDraw() {
       for (int row = 0; row < ROWS; ++row) {
           for (int col = 0; col < COLS; ++col) {
               if (board[row][col] == EMPTY) {
                   return false; // an empty cell found, not draw, exit
               }
           }
       }
       return true; // no empty cell, it's a draw
   }

   /**
   * Return true if the player with "theSeed" has won after placing at
   * (currentRow, currentCol)
   */
   public static boolean hasWon(int theSeed, int currentRow, int currentCol) {
       return (board[currentRow][0] == theSeed // 3-in-the-row
               && board[currentRow][1] == theSeed
               && board[currentRow][2] == theSeed
               || board[0][currentCol] == theSeed // 3-in-the-column
               && board[1][currentCol] == theSeed
               && board[2][currentCol] == theSeed
               || currentRow == currentCol // 3-in-the-diagonal
               && board[0][0] == theSeed
               && board[1][1] == theSeed
               && board[2][2] == theSeed || currentRow + currentCol == 2 // 3-in-the-opposite-diagonal
               && board[0][2] == theSeed
               && board[1][1] == theSeed
               && board[2][0] == theSeed);
   }

   /** Print a cell with the specified "content" */
   public static void printCell(int content) {
       switch (content) {
       case EMPTY:
           System.out.print("");
           break;
       case NOUGHT:
           System.out.print("O");
           break;
       case CROSS:
           System.out.print("X");
           break;
       }
   }

   public static void displayBoard() {
       System.out.println("====================================");
       System.out.println(" ---a------b-----c---");

       for (int i = 0; i < 3; i++) {
           System.out.print(" |");
           for (int j = 0; j < 3; j++) {
               printCell(board[i][j]);
               if (j == 0)
                   System.out.print(" | ");
               else if (j < 2)
                   System.out.print(" | ");
               else if (j == 2)
                   System.out.print(" |");

           }
           System.out.println();
           switch (i) {
           case 0:
               System.out.println(" ---d------e-----f---");
               break;
           case 1:
               System.out.println(" ---g------h-----i---");
               break;
           case 2:
               System.out.println(" ---------------------");
               break;
           }
       }
   }
}

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