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

I need the player with O to be a computer that randomly makes a move in an unocc

ID: 3547621 • Letter: I

Question

I need the player with O to be a computer that randomly makes a move in an unoccupied cell.


here the code I have


package tictactoegame;


import java.util.Scanner;


public class TicTacToeGame {

    // positions on board

    

    public static final int EMPTY = 0;

    

    public static final int X = 1;

    

    public static final int O = 2;

    

    // Game status

    

    public static final int PLAYING = 0;

       

    public static final int DRAW = 1;

       

    public static final int XWins = 2;

       

    public static final int OWins = 3;

    

    // 3x3 Grid

    

    public static final int Row = 3, Column = 3;

    

    public static int[][] board = new int[Row][Column];

    

    

    public static int currentState;  

    


    

    public static int currentPlayer;

    

    public static int currntRow, currentCol;

    public static Scanner in = new Scanner(System.in);

    

    

    public static void main(String[] args) {

         

      Game();

    

      do {

         playerMove(currentPlayer);

         updateGame(currentPlayer, currntRow, currentCol);

         printBoard();

        

         if (currentState == XWins) {

            System.out.println("'X' won!!");

         } else if (currentState == OWins) {

            System.out.println("'O' won!");

         } else if (currentState == DRAW) {

            System.out.println("It's a Draw!");

         }

    

         currentPlayer = (currentPlayer == X) ? O : X;

      } while (currentState == PLAYING);

   }

   

   public static void Game() {

      for (int row = 0; row < Row; ++row) {

         for (int col = 0; col < Column; ++col) {

            board[row][col] = EMPTY;

         }

      }

      currentState = PLAYING;

      currentPlayer = X;  

   }

   

   public static void playerMove(int mark) {

      boolean validInput = false;

      do {

         if (mark == X) {

            System.out.print("Player 'X', enter your move (row[1-3] column[1-3]): ");

         } else {

            System.out.print("Player 'O', enter your move (row[1-3] column[1-3]): ");

         }

         int row = in.nextInt() - 1;  

         int col = in.nextInt() - 1;

         if (row >= 0 && row < Row && col >= 0 && col < Column && board[row][col] == EMPTY) {

            currntRow = row;

            currentCol = col;

            board[currntRow][currentCol] = mark;  

            validInput = true;

         } else {

            System.out.println("This move at (" + (row + 1) + "," + (col + 1)

                  + ") is not valid. Try again...");

         }

      } while (!validInput);

   }

   public static void updateGame(int mark, int currentRow, int currentCol) {

      if (hasWon(mark, currentRow, currentCol)) {

         currentState = (mark == X) ? XWins : OWins;

      } else if (isDraw()) {  

         currentState = DRAW;

      }


   }


   public static boolean isDraw() {

      for (int row = 0; row < Row; ++row) {

         for (int col = 0; col < Column; ++col) {

            if (board[row][col] == EMPTY) {

               return false;  

            }

         }

      }

      return true;  

   }

   

   public static boolean hasWon(int Token, int currentRow, int currentCol) {

      return (board[currentRow][0] == Token         

                   && board[currentRow][1] == Token

                   && board[currentRow][2] == Token

              || board[0][currentCol] == Token     

                   && board[1][currentCol] == Token

                   && board[2][currentCol] == Token

              || currentRow == currentCol            

                   && board[0][0] == Token

                   && board[1][1] == Token

                   && board[2][2] == Token

              || currentRow + currentCol == 2

                   && board[0][2] == Token

                   && board[1][1] == Token

                   && board[2][0] == Token);

   }

   public static void printBoard() {

      for (int row = 0; row < Row; ++row) {

         for (int col = 0; col < Column; ++col) {

            printCell(board[row][col]);

            if (col != Column - 1) {

               System.out.print("|");  

            }

         }

         System.out.println();

         if (row != Row - 1) {

            System.out.println("-----------");

         }

      }

      System.out.println();

   }

   public static void printCell(int content) {

      switch (content) {

         case EMPTY:  System.out.print("   "); break;

         case O: System.out.print(" O "); break;

         case X:  System.out.print(" X "); break;

      }

    }

}

Explanation / Answer

package tictactoegame;

import java.util.Random;
import java.util.Scanner;

public class TicTacToeGame {

    // positions on board

    public static final int EMPTY = 0;

    public static final int X = 1;

    public static final int O = 2;

    // Game status

    public static final int PLAYING = 0;

    public static final int DRAW = 1;

    public static final int XWins = 2;

    public static final int OWins = 3;

    // 3x3 Grid

    public static final int Row = 3, Column = 3;

    public static int[][] board = new int[Row][Column];

    public static int currentState;

    public static int currentPlayer;

    public static int currntRow, currentCol;

    public static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {

        Game();

        do {

            playerMove(currentPlayer);

            updateGame(currentPlayer, currntRow, currentCol);

            printBoard();

            if (currentState == XWins) {

                System.out.println("'X' won!!");

            } else if (currentState == OWins) {

                System.out.println("'O' won!");

            } else if (currentState == DRAW) {

                System.out.println("It's a Draw!");

            }

            currentPlayer = (currentPlayer == X) ? O : X;

        } while (currentState == PLAYING);

    }

    public static void Game() {

        for (int row = 0; row < Row; ++row) {

            for (int col = 0; col < Column; ++col) {

                board[row][col] = EMPTY;

            }

        }

        currentState = PLAYING;

        currentPlayer = X;

    }

    public static void playerMove(int mark) {

        boolean validInput = false;
        Random random = new Random();
        do {

            int row;
            int col;
            if (mark == X) {

                System.out
                        .print("Player 'X', enter your move (row[1-3] column[1-3]): ");
                row = in.nextInt() - 1;
                col = in.nextInt() - 1;

            } else {

               
                row = random.nextInt(Row);
                col = random.nextInt(Column);

            }

            if (row >= 0 && row < Row && col >= 0 && col < Column
                    && board[row][col] == EMPTY) {
                System.out
                .print("Player 'O', enter your move (row[1-3] column[1-3]): ");
                currntRow = row;

                currentCol = col;

                board[currntRow][currentCol] = mark;

                validInput = true;

            } else {

                if (mark == X) {
                    System.out.println("This move at (" + (row + 1) + ","
                            + (col + 1)

                            + ") is not valid. Try again...");
                }

            }

        } while (!validInput);

    }

    public static void updateGame(int mark, int currentRow, int currentCol) {

        if (hasWon(mark, currentRow, currentCol)) {

            currentState = (mark == X) ? XWins : OWins;

        } else if (isDraw()) {

            currentState = DRAW;

        }

    }

    public static boolean isDraw() {

        for (int row = 0; row < Row; ++row) {

            for (int col = 0; col < Column; ++col) {

                if (board[row][col] == EMPTY) {

                    return false;

                }

            }

        }

        return true;

    }

    public static boolean hasWon(int Token, int currentRow, int currentCol) {

        return (board[currentRow][0] == Token

        && board[currentRow][1] == Token

        && board[currentRow][2] == Token

        || board[0][currentCol] == Token

        && board[1][currentCol] == Token

        && board[2][currentCol] == Token

        || currentRow == currentCol

        && board[0][0] == Token

        && board[1][1] == Token

        && board[2][2] == Token

        || currentRow + currentCol == 2

        && board[0][2] == Token

        && board[1][1] == Token

        && board[2][0] == Token);

    }

    public static void printBoard() {

        for (int row = 0; row < Row; ++row) {

            for (int col = 0; col < Column; ++col) {

                printCell(board[row][col]);

                if (col != Column - 1) {

                    System.out.print("|");

                }

            }

            System.out.println();

            if (row != Row - 1) {

                System.out.println("-----------");

            }

        }

        System.out.println();

    }

    public static void printCell(int content) {

        switch (content) {

        case EMPTY:
            System.out.print(" ");
            break;

        case O:
            System.out.print(" O ");
            break;

        case X:
            System.out.print(" X ");
            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