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

Tic Tac Toe class - Write a fifth game program that can play Tic Tac Toe. This g

ID: 3934824 • Letter: T

Question

Tic Tac Toe class - Write a fifth game program that can play Tic Tac Toe. This game displays the lines of the Tic Tac Toe game and prompts the player to choose a move. The move is recorded on the screen and the computer picks his move from those that are left. The player then picks his next move. The program should allow only legal moves. The program should indicate when there is a winner and tell who won. The player is then asked if he would like to play again. A sample output is shown in the attached file.

tic tac board is a set of four strings, java language

Explanation / Answer

import java.util.*;

public class TicTacToeGame {

   private int count;
   private char positn[] = new char[10];
   private char players;

   public static void main(String[] args) {
       String cha;
       TicTacToeGame Obj = new TicTacToeGame();
       do {
           Obj.beginBoard();
           Obj.play_game();
           System.out.println("Would you like to play again (Enter 'Y')? ");
           Scanner in = new Scanner(System.in);
           cha = in.nextLine();
           System.out.println("ch value is " + cha);
       } while (cha.equals("Y"));

   }

   public void beginBoard() {

       char posndef[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
       int i;
       count = 0;
       players = 'X';
       for (i = 1; i < 10; i++)
           positn[i] = posndef[i];
       presentBoard();
   }

   public String presentBoard() {
       System.out.println(" ");
       System.out.println(" ");
       System.out.println(" " + positn[1] + " | " + positn[2] + " | " + positn[3]);
       System.out.println(" | | ");
       System.out.println(" ___|____|___ ");
       System.out.println(" " + positn[4] + " | " + positn[5] + " | " + positn[6]);
       System.out.println(" | | ");
       System.out.println(" ___|____|___ ");
       System.out.println(" " + positn[7] + " | " + positn[8] + " | " + positn[9]);
       System.out.println(" | | ");
       System.out.println(" | | ");
       System.out.println(" ");
       return "presentBoard";
   }

   public void play_game() {
       int spot;
       char blank = ' ';

       System.out.println("Player " + getPlayer() + " will go first and be the letter 'X'");

       do {
           presentBoard();

           System.out.println(" Player " + getPlayer() + " choose a position.");

           boolean posTaken = true;
           while (posTaken) {

               Scanner in = new Scanner(System.in);
               spot = in.nextInt();
               posTaken = checkPosn(spot);
               if (posTaken == false)
                   positn[spot] = getPlayer();
           }
           System.out.println("Nice move.");

           presentBoard();

           nextPlayer();
       } while (getWinner() == blank);

   }

   public char getWinner() {
       char Winner = ' ';

       // Check if X wins
       if (positn[1] == 'X' && positn[2] == 'X' && positn[3] == 'X')
           Winner = 'X';
       if (positn[4] == 'X' && positn[5] == 'X' && positn[6] == 'X')
           Winner = 'X';
       if (positn[7] == 'X' && positn[8] == 'X' && positn[9] == 'X')
           Winner = 'X';
       if (positn[1] == 'X' && positn[4] == 'X' && positn[7] == 'X')
           Winner = 'X';
       if (positn[2] == 'X' && positn[5] == 'X' && positn[8] == 'X')
           Winner = 'X';
       if (positn[3] == 'X' && positn[6] == 'X' && positn[9] == 'X')
           Winner = 'X';
       if (positn[1] == 'X' && positn[5] == 'X' && positn[9] == 'X')
           Winner = 'X';
       if (positn[3] == 'X' && positn[5] == 'X' && positn[7] == 'X')
           Winner = 'X';
       if (Winner == 'X') {
           System.out.println("Player1 wins the game.");
           return Winner;
       }

       // Check if O wins
       if (positn[1] == 'O' && positn[2] == 'O' && positn[3] == 'O')
           Winner = 'O';
       if (positn[4] == 'O' && positn[5] == 'O' && positn[6] == 'O')
           Winner = 'O';
       if (positn[7] == 'O' && positn[8] == 'O' && positn[9] == 'O')
           Winner = 'O';
       if (positn[1] == 'O' && positn[4] == 'O' && positn[7] == 'O')
           Winner = 'O';
       if (positn[2] == 'O' && positn[5] == 'O' && positn[8] == 'O')
           Winner = 'O';
       if (positn[3] == 'O' && positn[6] == 'O' && positn[9] == 'O')
           Winner = 'O';
       if (positn[1] == 'O' && positn[5] == 'O' && positn[9] == 'O')
           Winner = 'O';
       if (positn[3] == 'O' && positn[5] == 'O' && positn[7] == 'O')
           Winner = 'O';
       if (Winner == 'O') {
           System.out.println("Player2 wins the game.");
           return Winner;
       }

       for (int i = 1; i < 10; i++) {
           if (positn[i] == 'X' || positn[i] == 'O') {
               if (i == 9) {
                   char Draw = 'D';
                   System.out.println(" Game is stalemate ");
                   return Draw;
               }
               continue;
           } else
               break;

       }

       return Winner;
   }

   public boolean checkPosn(int spot) {

       if (positn[spot] == 'X' || positn[spot] == 'O') {
           System.out.println("That position is already taken, please choose another");
           return true;
       } else {
           return false;
       }

       // counter++;

   }

   public void nextPlayer() {
       if (players == 'X')
           players = 'O';
       else
           players = 'X';

   }

   public String getTitle() {
       return "Tic Tac Toe";
   }

   public char getPlayer() {
       return players;

   }
}

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