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

Create a Tic Tac Toe game. In this game, two players alternate placing Xs and Os

ID: 3667104 • Letter: C

Question

Create a Tic Tac Toe game. In this game, two players alternate placing Xs and Os into a grid until one player has three matching symbols in a row, horizontally, vertically, or diagonally. Create a game in which the user is presented with a three-by-three grid containing the digits 1 through 9. When the user chooses a position by typing a number, place an X in the appropriate spot. Generate a random number for the position where the computer will place an O. Do not allow the player or the computer to place a symbol where one has already been placed. When either the player or computer has three symbols in a row, declare a winner; if all positions have been exhausted and no one has three symbols in a row, declare a tie. Save the game as TicTacToe.java

*********** Need help figuring out this problem !!!! *************

Explanation / Answer

Main.Java

package com.chegg.test;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class Main {

   public void play() {

       TicTacToe game = new TicTacToe();
       Random rn = new Random();
       int range = 9 - 1 + 1;
       Map<Integer, Integer> map = new HashMap<Integer, Integer>();
       game.setPlayer1("you");
       game.setPlayer2("System");
       game.setMarker1('O');
       // Set System marker
       game.setMarker2('X');
       boolean continuePlaying = true;
       continuePlaying = true;
       while (continuePlaying) {
           game.init();
           System.out.println(game.drawBoard());
           System.out.println();
           String player = null;
           while (!game.winner() && game.getPlays() < 9) {
               player = game.getCurrentPlayer() == 1 ? game.getPlayer1()
                       : game.getPlayer2();
               boolean validPick = false;

               while (!validPick) {
                   int pick = 0;

                   if (game.getCurrentPlayer() != 2) {
                       System.out.print("It " + player
                               + "r's turn. Pick a square: ");
                       String square = game.getPrompt();
                       if (square.length() == 1
                               && Character.isDigit(square.toCharArray()[0])) {

                           try {
                               pick = Integer.parseInt(square);
                               map.put(pick, null);
                           } catch (NumberFormatException e) {
                               // Do nothing here, it'll evaluate as an invalid
                               // pick on the next row.
                           }
                       }
                       validPick = game.placeMarker(pick);
                   } else {
                       System.out.print("It " + player + "'s turn now: ");
                       do {
                           int rand = rn.nextInt(range) + 1;
                           if (!map.containsKey(rand)) {
                               validPick = game.placeMarker(rand);
                           }
                       } while (!validPick);
                   }
                   if (!validPick) {
                       System.out.println("Square can not be selected. Retry");
                   }
               }
               game.switchPlayers();
               System.out.println();
               System.out.println(game.drawBoard());
               System.out.println();
           }
           if (game.winner()) {
               System.out.println("Game Over - " + player + " WINS!!!");
           } else {
               System.out.println("Game Over - Draw");
           }
           System.out.println();
           System.out.print("Play again? (Y/N): ");
           String choice = game.getPrompt();
           if (!choice.equalsIgnoreCase("Y")) {
               continuePlaying = false;
           }
       }
   }

   /**
   * @param args
   * the command line arguments
   */
   public static void main(String[] args) {
       Main main = new Main();
       main.play();
   }
}

TicTacToe.java

package com.chegg.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
*
* @Your name
*/
public class TicTacToe {

   private char[][] board = new char[3][3];
   private String player1;
   private String player2;
   private int currentPlayer;
   private char marker1;
   private char marker2;
   private int plays;
   private BufferedReader reader = new BufferedReader(new InputStreamReader(
           System.in));

   protected void init() {
       int counter = 0;
       for (int i = 0; i < 3; i++) {
           for (int i1 = 0; i1 < 3; i1++) {
               board[i][i1] = Character.forDigit(++counter, 10);
           }
       }
       currentPlayer = 1;
       plays = 0;
   }

   protected void switchPlayers() {
       if (getCurrentPlayer() == 1) {
           setCurrentPlayer(2);
       } else {
           setCurrentPlayer(1);
       }
       setPlays(getPlays() + 1);
   }

   protected boolean placeMarker(int play) {
       for (int i = 0; i < 3; i++) {
           for (int i1 = 0; i1 < 3; i1++) {
               if (board[i][i1] == Character.forDigit(play, 10)) {
                   board[i][i1] = (getCurrentPlayer() == 1) ? getMarker1()
                           : getMarker2();
                   return true;
               }
           }
       }
       return false;
   }

   protected boolean winner() {
       // Checking rows
       char current = ' ';
       for (int i = 0; i < 3; i++) {
           int i1 = 0;
           for (i1 = 0; i1 < 3; i1++) {
               if (!Character.isLetter(board[i][i1])) {
                   break;
               }
               if (i1 == 0) {
                   current = board[i][i1];
               } else if (current != board[i][i1]) {
                   break;
               }
               if (i1 == 2) {
                   // Found winner
                   return true;
               }
           }
       }
       // Checking columns
       for (int i = 0; i < 3; i++) {
           current = ' ';
           int i1 = 0;
           for (i1 = 0; i1 < 3; i1++) {
               if (!Character.isLetter(board[i1][i])) {
                   break;
               }
               if (i1 == 0) {
                   current = board[i1][i];
               } else if (current != board[i1][i]) {
                   break;
               }
               if (i1 == 2) {
                   // Found winner
                   return true;
               }
           }
       }
       // Checking diagonals
       current = board[0][0];
       if (Character.isLetter(current) && board[1][1] == current
               && board[2][2] == current) {
           return true;
       }
       current = board[2][0];
       if (Character.isLetter(current) && board[1][1] == current
               && board[0][2] == current) {
           return true;
       }
       return false;
   }

   protected String getPrompt() {
       String prompt = "";
       try {
           prompt = reader.readLine();
       } catch (IOException ex) {
           ex.printStackTrace();
       }
       return prompt;
   }

   protected String drawBoard() {
       StringBuilder builder = new StringBuilder("Game board: ");
       for (int i = 0; i < 3; i++) {
           for (int i1 = 0; i1 < 3; i1++) {
               builder.append("[" + board[i][i1] + "]");
           }
           builder.append(" ");
       }
       return builder.toString();
   }

   public int getCurrentPlayer() {
       return currentPlayer;
   }

   public void setCurrentPlayer(int currentPlayer) {
       this.currentPlayer = currentPlayer;
   }

   public char getMarker1() {
       return marker1;
   }

   public void setMarker1(char marker1) {
       this.marker1 = marker1;
   }

   public char getMarker2() {
       return marker2;
   }

   public void setMarker2(char marker2) {
       this.marker2 = marker2;
   }

   public int getPlays() {
       return plays;
   }

   public void setPlays(int plays) {
       this.plays = plays;
   }

   public String getPlayer1() {
       return player1;
   }

   public void setPlayer1(String player1) {
       this.player1 = player1;
   }

   public String getPlayer2() {
       return player2;
   }

   public void setPlayer2(String player2) {
       this.player2 = player2;
   }
}

Output:

Game board:
[1][2][3]
[4][5][6]
[7][8][9]


It your's turn. Pick a square: 2

Game board:
[1][O][3]
[4][5][6]
[7][8][9]


It System's turn now:
Game board:
[1][O][3]
[X][5][6]
[7][8][9]


It your's turn. Pick a square: 6

Game board:
[1][O][3]
[X][5][O]
[7][8][9]


It System's turn now:
Game board:
[X][O][3]
[X][5][O]
[7][8][9]


It your's turn. Pick a square: 8

Game board:
[X][O][3]
[X][5][O]
[7][O][9]


It System's turn now:
Game board:
[X][O][3]
[X][5][O]
[7][O][X]


It your's turn. Pick a square: 3

Game board:
[X][O][O]
[X][5][O]
[7][O][X]


It System's turn now:
Game board:
[X][O][O]
[X][5][O]
[X][O][X]


Game Over - System WINS!!!

Play again? (Y/N): n