Answer to Question #247563 in HTML/JavaScript Web Application for Paulina

Question #247563
(Tic-Tac-Toe ) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enumeration to represent the value in each cell of the array. The enumeration's constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY. Allow two human players. Wherever the first player moves, place an X in the specified square and place an O wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game has been won and whether it's a draw. Also allow the player to specify whether he or she wants to go first or second.
1
Expert's answer
2021-10-06T16:09:15-0400
import java.util.Scanner;

enum Cell {
	X, O, EMPTY
};
class TicTacToe {
	private Cell[][] board;
	private Scanner input = new Scanner(System.in);
	/**
	 * constructor
	 */
	public TicTacToe() {
		this.board = new Cell[3][3];
		// The constructor should initialize the board elements to EMPTY.
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				board[i][j] = Cell.EMPTY;
			}
		}
	}
	/**
	 * This method allows to play game
	 */
	public void displayBoard() {
		for (int i = 0; i < 3; i++) {
			if (i == 1 || i == 2) {
				System.out.println("——————————");
			}
			for (int j = 0; j < 3; j++) {
				System.out.print(cellToChar(board[i][j]));
				if (j < 2) {
					System.out.print(" | ");
				}
			}
			System.out.println();
		}
	}
	/**
	 * This method allows to play game
	 */
	public void play() {
		int move;
		System.out.print("Do you want to do first?(y/n): ");
		String goFirst = input.nextLine();
		boolean isWinner = false;
		for (move = 0; move < 9; move++) {
			displayBoard();
			// swap players
			if (goFirst.compareToIgnoreCase("y") == 0) {
				playerSelectsCell((move % 2 == 0) ? Cell.X : Cell.O);
			} else {
				playerSelectsCell((move % 2 == 0) ? Cell.O : Cell.X);
			}
			// check winner using aspect
			Cell winnerCell = isWinner();
			if (winnerCell != Cell.EMPTY) {
				displayBoard();
				System.out.println("\nPlayer " + cellToChar(winnerCell) + " won the game\n");
				move = 10;
				isWinner = true;
			}
		}
		if (!isWinner) {
			System.out.println("\nDraw\n");
		}
	}
	/**
	 * This method allows to select correct cell
	 * 
	 * @param playerCell
	 */
	private void playerSelectsCell(Cell playerCell) {
		int selectedRow = 0;
		int selectedColumn = 0;
		boolean isCorrectCell = false;
		System.out.println("\nPlayer " + cellToChar(playerCell) + " select cell\n");
		while (!isCorrectCell) {
			// get row from a user
			selectedRow = getCorrectPosition("row");
			// get column from a user
			selectedColumn = getCorrectPosition("column");
			if (board[selectedRow - 1][selectedColumn - 1] == Cell.EMPTY) {
				board[selectedRow - 1][selectedColumn - 1] = playerCell;
				isCorrectCell = true;
			} else {
				// display message: "Wrong cell, try again!"
				System.out.println("\nWrong cell, try again!\n");
			}
		}
		input.nextLine();
	}
	/**
	 * This function allows to get correct row or column
	 * 
	 * @param positionName
	 * @return
	 */
	private int getCorrectPosition(String positionName) {
		int position = -1;
		while (position == -1) {
			System.out.print("Enter the " + positionName + ": ");
			position = input.nextInt();
			// check if row or column is correct
			if (position < 1 || position > 3) {
				System.out.println("\nEnter a " + positionName + " on the board (between 1 and 3, inclusive).\n");
				position = -1;
			}
		}
		return position;
	}
	/**
	 * Cell to char
	 * @param cell
	 * @return
	 */
	public char cellToChar(Cell cell) {
		if (cell == Cell.X) {
			return 'X';
		}
		if (cell == Cell.O) {
			return 'O';
		}
		return ' ';
	}
	public Cell isWinner() {
		int horizontalCounter, verticalCounter, diagonalCounter;
		int diagonalStartIndex = 2;
		Cell player;
		// Check horizontal
		for (int row = 0; row < 3; row++) {
			horizontalCounter = 0;
			player = board[row][0];
			for (int column = 0; column < 3; column++) {
				if (board[row][column] == player) {
					horizontalCounter++;
				}
			}
			if (horizontalCounter == 3) {
				return player;
			}
		}
		// Check vertical
		for (int column = 0; column < 3; column++) {
			verticalCounter = 0;
			player = board[0][column];
			for (int row = 0; row < 3; row++) {
				if (board[row][column] == player) {
					verticalCounter++;
				}
			}
			if (verticalCounter == 3) {
				return player;
			}
		}
		// Check diagonal '\'
		player = board[0][0];
		diagonalCounter = 0;
		for (int row = 0; row < 3; row++) {
			if (board[row][row] == player) {
				diagonalCounter++;
			}
			if (diagonalCounter == 3) {
				return player;
			}
		}
		// Check diagonal '/'
		player = board[0][2];
		diagonalCounter = 0;
		for (int column = 0; column < 3; column++) {
			if (board[diagonalStartIndex][column] == player) {
				diagonalCounter++;
			}
			diagonalStartIndex--;
			if (diagonalCounter == 3) {
				return player;
			}
		}
		return Cell.EMPTY;
	}
}
public class TicTacToeProject {
	/**
	 * Main method
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		TicTacToe ticTacToe = new TicTacToe();
		ticTacToe.play();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS