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

In this assignment, you will implement a simple Tic-Tac-Toe game. It will be pos

ID: 3588703 • Letter: I

Question

In this assignment, you will implement a simple Tic-Tac-Toe game. It will be possibk to pay human-vs-human, human-vs-computer, or computer-vs-computer Tic-Tac-Toe, also called X's and O's, Noughts and Crosses, and X and 0 is a simple game played on a 3x3 grid, referred to as the board. The grid starts empty, and the two players take turns placing their respective symbols, X and O, on an empty grid cell, referred to as making a move. The first payer to get a straight line of three symbols wins. Ifall the cells on the board are filled and ne ither player has a line of3 symbols, the game is a tie. Lines may be horizontal, vertical, or diagonal. You will implement a Board cass to represent the 3x3 grid. This class will ha ve functions to determine which symbol if any, is in a cell, to place a symbol in a cell, to determine the winner, if any so far, and to print the board to standard output. The board should appear as below: You will implement an abstract Player cass to determine the player's moves. The Player class should store which symbol it will place and contain a pure virtual function to determine the Player's next move. This function will be overridden by the subclasses of Player. You wil implement PlayerHuman as a subcass of Player. When this subclass is to choose a move, it shoukd print out the curre nt board and ask the user for the row and co lumn to place a symbol This cass should detect if the user enters an invalid location, either because it is not in the grid or it already has a symbol, and if the location is invalid, ask the user again. You will implement PlayerRandom as a subclass of Player. When this subclass is to choose a move, it should return a random position in the grid that does not yet ha ve a symbol. You will implement a program to play the Tic Tac Toe game. The program should begin by asking the user if each team should be controlled by a human or by the computer. The p should then use dynamic memory allocation to create instances ofthe appropriate s Player. These must be stored with pointers of type Player*. The first team should place X symbols, while the second team should pace O symbols. The program shoukd then alternate asking the players for moves until one player wins, or the board is full and the game is a tie. After the game is over, the program should print the winner and the board and exit. rogram subclasses of

Explanation / Answer

package Test;/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
* File Name:Board.java
*/

import java.util.Arrays;

public class Board {

    private char[][] board;
    private int row;
    private int col;
    private char playerOne;
    private char playerTwo;

    /**
     * Default constructor
     */

    public Board() {
        this.row = 6;
        this.col = 7;
        board = new char[this.row][this.col];
        this.initBoard();
    }

    /**
     * @param row
     * @param col
     */
    public Board(int row, int col) {
        this.row = row;
        this.col = col;
        board = new char[row][col];
        this.initBoard();
    }

    public final void initBoard() {
        for (char[] x : this.board) {
            Arrays.fill(x, ' ');
        }
    }

    public int getNumRows() {
        return this.row;
    }

    public int getNumCols() {
        return this.col;
    }

    public char getPlayerOne() {
        return this.playerOne;
    }

    public char getPlayerTwo() {
        return this.playerTwo;
    }

    public void setPlayerOne(char o) {
        this.playerOne = o;
    }

    public void setPlayerTwo(char t) {
        this.playerTwo = t;
    }

    /**
     * @param row
     * @param col
     * @return
     */

    public char getToken(int row, int col) {
        try {
            return this.board[row][col];
        } catch (ArrayIndexOutOfBoundsException ex) {
            return '';
        }
    }

    public boolean canPlay() {
        for (int i = 0; i < this.row; i++) {
            for (int j = 0; j < this.col; j++) {
                if (' ' == this.board[0][0]) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * @param p player number
     * @param c column number in which player want to put
     * @return
     */
    public boolean play(int p, int c) {
        for (int i = this.row - 1; i >= 0; i--) {

            if (this.board[i][c - 1] == ' ') {
                if (p == 1) {

                    this.board[i][c - 1] = this.playerOne;
                    return true;
                } else if (p == 2) {
                    this.board[i][c - 1] = this.playerTwo;
                    return true;
                }
            }
        }
        return false;
    }

    public int isFinished() {
        int winner = this.winByRow();
        if (winner != -1) {
            return winner;
        }
        winner = this.winByCol();
        if (winner != -1) {
            return winner;
        }
        winner = this.winByDia();
        if (winner != -1) {
            return winner;
        }
        for (int i = 0; i < this.row; ++i) {
            for (int j = 0; j < this.col; ++j) {
                if (this.board[i][j] == ' ') {
                    return 0;
                }
            }
        }
        return winner;
    }

    private int winByRow() {
        for (int row = 0; row < this.row; ++row) {
            int count = 0;
            for (int column = 1; column < this.col; ++column) {

                if (this.board[row][column] != ' ' && this.board[row][column] == this.board[row][column - 1]) {
                    ++count;
                } else {
                    count = 1;
                }
                if (count >= 4) {
                    if (this.board[row][column] == this.playerOne) {
                        return 1;
                    } else if (this.board[row][column] == this.playerTwo) {
                        return 2;
                    }
                }
            }
        }
        return -1;
    }

    private int winByCol() {

        for (int column = 0; column < this.col; column++) {
            int count = 0;
            for (int row = 1; row < this.row; row++) {
                if (this.board[row][column] != ' ' && this.board[row][column] == this.board[row - 1][column]) {
                    count++;
                } else {
                    count = 1;
                }
                if (count >= 4) {
                    // Return char of the winner
                    if (this.board[row][column] == this.playerOne) {
                        return 1;
                    } else if (this.board[row][column] == this.playerTwo) {
                        return 2;
                    }
                }
            }
        }
        return -1;
    }

    private int winByDia() {

        for (int column = 0; column < this.col; column++) {
            int count = 0;

            for (int row = 1; row < this.row; row++) {

                System.out.println("i:" + row + "j:" + column + " " + (row + column));
                if (column + row >= this.col) {
                    break;
                }
                if (this.board[row][column + row] != ' ' && this.board[row - 1][column + row - 1] == this.board[row][column + row]) {
                    ++count;
                } else {
                    count = 1;
                }
                if (count >= 4) {
                    if (this.board[row][column] == this.playerOne) {
                        return 1;
                    } else if (this.board[row][column] == this.playerTwo) {
                        return 2;
                    }
                }
            }
        }

        for (int row = 0; row < this.row; ++row) {
            int count = 0;
            for (int column = 1; column < this.col; ++column) {

                if (column + row >= this.row) {
                    break;
                }
                if (this.board[row + column][column] != ' ' && this.board[row + column - 1][column - 1] == this.board[row + column][column]) {
                    ++count;
                } else {
                    count = 1;
                }
                if (count >= 4) {
                    if (this.board[row][column] == this.playerOne) {
                        return 1;
                    } else if (this.board[row][column] == this.playerTwo) {
                        return 2;
                    }
                }

            }
        }

        for (int column = 0; column < this.col; ++column) {
            int count = 0;
            for (int row = 1; row < this.row; ++row) {
                if (column - row < 0) {
                    break;
                }
                if (this.board[row][column - row] != ' ' && this.board[row - 1][column - row + 1] == this.board[row][column - row]) {
                    ++count;
                } else {
                    count = 1;
                }
                if (count >= 4) {
                    if (this.board[row][column] == this.playerOne) {
                        return 1;
                    } else if (this.board[row][column] == this.playerTwo) {
                        return 2;
                    }
                }

            }
        }

        for (int row = 0; row < this.row; ++row) {
            int count = 0;

            for (int column = this.col - 1; column >= 0; column--) {

                if (row - column < 0) {
                    break;
                }

                if (this.board[column - row][column] != ' ' && this.board[column - row - 1][column + 1] == this.board[column - row][column]) {
                    ++count;
                } else {
                    count = 1;
                }
                if (count >= 4) {
                    if (this.board[row][column] == this.playerOne) {
                        return 1;
                    } else if (this.board[row][column] == this.playerTwo) {
                        return 2;
                    }
                }

            }
        }
        return -1;
    }

    public static void main(String[] args) {
        Board b = new Board();
        b.setPlayerOne('-');
        b.setPlayerTwo('*');
        System.out.println(b.play(1, 2));
        System.out.println(b.play(2, 1));
        System.out.println(b.play(1, 2));
        System.out.println(b.play(2, 3));
        System.out.println(b.play(1, 2));
        System.out.println(b.play(2, 1));
        System.out.println(b.play(1, 2));
        System.out.println(b.play(2, 2));
        System.out.println("Winner is Player "+b.isFinished());
        for (int i = 0; i < b.col; i++) {
            System.out.print(" __ ");
        }
        System.out.println("");
        for (char[] x : b.board) {
            String s = "|_";

            for (char y : x)

                s += y + "_|_";
            System.out.println(s.substring(0, s.length() - 1));

        }
    }
}