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

This is my question and i have the script below. Can someone fix this or create

ID: 3641501 • Letter: T

Question

This is my question and i have the script below. Can someone fix this or create a new script? I am so confused and overwhelmed now.

- 7.9*** (Game: playing a TicTacToe game) In a game of TicTacToe, two players take turns marking an available cell in a 3 X 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Create a program for playing TicTacToe.

The program prompts two players to enter X token and O token alternately. Whenever a token is entered, the program redisplays the board on the console and determines the status of the game (win, draw, or continue). Here is a sample run:

Explanation / Answer

import java.util.Scanner;

public class TicTac
{
    //Constants
    private final static int HEIGHT = 3;
    private final static int WIDTH = 3;
    private final static boolean CONTINUE = true;
    private final static boolean END = false;
    private final static char PLAYER1 = 'O';
    private final static char PLAYER2 = 'X';
    private final static char NONPLAYER = '-';

    //Atributes
    private char[][] grid;
    private boolean status;
    private char player;
    private boolean hasWinner;
    private int freeSlots;

    //Constructors
    public TicTac() {
        player = PLAYER1;
        status = CONTINUE;
        hasWinner = false;
        freeSlots = HEIGHT * WIDTH;
        grid = new char[HEIGHT][WIDTH];
        for(int i = 0; i < HEIGHT; i++)
            for(int j = 0; j < WIDTH; j++)
                grid[i][j] = NONPLAYER;
    }
  
    //Getters
    public boolean getStatus() { return status; }
    //
    public char getPlayer() { return player; }
    //
    public boolean hasWinner() { return hasWinner; }

    //Public methods
    public boolean placeMove(int row, int col)
    {
        if(isEmpty(row, col)) {
            grid[row][col] = player;
            freeSlots--;
            checkStatus(row, col);
            if (status == CONTINUE) changePlayer();
            return true;
        }
        return false;
    }
    //
    public String getGrid() {
        return "|" + grid[0][0] + "|" + grid[0][1] + "|" + grid[0][2] + "| "
             + "|" + grid[1][0] + "|" + grid[1][1] + "|" + grid[1][2] + "| "
             + "|" + grid[2][0] + "|" + grid[2][1] + "|" + grid[2][2] + "| ";
    }
  
    //Private methods
    private boolean isEmpty(int row, int col)
    {
        if (col > 2 || col < 0 || row > 2 || row < 0)
            return false;
        if (grid[row][col] == NONPLAYER)
            return true;
        return false;
    }
    //
    private void changePlayer() {
        player = player == PLAYER1 ? PLAYER2 : PLAYER1;
    }
    //
    private void checkStatus(int row, int col) {
        if (freeSlots == 0)
            status = END;
        //Check row
        if (grid[row][0] == grid[row][1] && grid[row][0] == grid[row][2]) {
            hasWinner = true;
            status = END;
        }
        //Check col
        if (grid[0][col] == grid[1][col] && grid[0][col] == grid[2][col]){
            hasWinner = true;
            status = END;
        }
        //Check diag
        if (row == col) {
            if (grid[0][0] == grid[1][1] && grid[0][0] == grid[2][2]) {
                hasWinner = true;
                status = END;
            }
        }
        //Check diag /
        if (row + col == 2) {
            if (grid[0][2] == grid[1][1] && grid[0][2] == grid[2][0]) {
                hasWinner = true;
                status = END;
            }
        }
    }
    //
    //END of TicTac
    //
  

    public static void main (String [] args) {
        Scanner keyboard = new Scanner(System.in);
        char response = 'y';
        TicTac tt;
        int row;
        int col;

        while (Character.toUpperCase(response) == 'Y') {
            tt = new TicTac();
            row = col = -1;
          
            while(tt.getStatus()) {
                while(!tt.placeMove(row, col)) {
                    System.out.print("Enter a row (1,2, or 3) for player "
                            + tt.getPlayer() + ": ");
                    row = keyboard.nextInt() - 1;
                    System.out.print("Enter a column (1,2, or 3) for player "
                            + tt.getPlayer() + ": ");
                    col = keyboard.nextInt() - 1;
                }
                System.out.print(tt.getGrid());
            }
          
            if (tt.hasWinner())
                System.out.println(tt.getPlayer() + " player has won");
            else
                System.out.println("Game draws");
          
            System.out.print(" Do you want to play a new game?(y/n) ");
            response = keyboard.next().charAt(0);
            System.out.println();
        }
    }
}

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