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

THIS IS FINAL COURSE ASSIGNMENT, FOLLOWING EVERY SINGLE REQUIREMENT IS CRUCIAL!

ID: 3844738 • Letter: T

Question

THIS IS FINAL COURSE ASSIGNMENT, FOLLOWING EVERY SINGLE REQUIREMENT IS CRUCIAL!
Hello, please help write TIC-TAC-TOE game in JAVA, where a human plays against the computer.

PROGRAM REQUIREMENTS:
1) The program prompts user for name and ensures (validates) it; user is allowed to enter a single username consisting of alpha chars ONLY.
2) Use one-dimensional arrays to keep track of the game and computer.
3) Use functions to pass arrays and implement other program requirements such as input validation, checking to ensure that places selected by users are available on the “game board”.
4) Validate user input at every opportunity:
   Do not allow number entries less than 0 or entries greater than 8 (in the case you are using numbers 0-8 on the “grid”.
•   Do not allow number entries less than 0 or entries greater than 8 (in the case you are using numbers 1-9 on the “grid”.
Do not allow non-numeric entries.
Program must NOT crashes due to an invalid entry made by the user.
5) The program must be developed using functions so that the main() function consists mostly of function calls.
6) Below is a list of functions to use in the development of this project:
splashScreen() //displays game and developer’s information
askForUserNames() //requests for username
•   validateUserName() //validate username
•   resetGame() //reset the game when one concludes; this includes filling the array with vales 0-8
•   displayGrid() //display the grid after each player makes a move
playerMakeMove() //prompts player to make a move, invokes validatePlayersMove, checkPositionAvailability
•   validatePlayersMove() //validates that user entry X is such that 0<=X<=8
•   checkPositionAvailability() //check that the position selected by the user is available
checkWin()   //check for a winning player
checkTie()   //check for a tie
makeBestMove()   //select best option for computer
computerMakeMove()   //used to make the move, in other words populate the array
7) The main() function must use a loop to keep the user in the program until he/she wants to quit. Users should be allowed to play as many games as they want.
8) Meaningful variable names must be used.
9) Program must determine if a tie occurs
10) Program must detect a winner if there is one
11) Please use variables of the correct type and initialize them with a proper value.
12) Code SHOULD be properly commented

BELOW ARE THE SCREENSHOTS OF WORKING PROGRAM:

On the grid "H" stands for human move, "C" for computer move

TIC TAC TOE****** By Prof. Flores Press any key to continue

Explanation / Answer

/ This program plays the game of Tic-Tac-Toe with the user.

import java.util.Scanner;

public class TicTacToe
{
public static Scanner sc = new Scanner(System.in);

public static void main(String[] args)
{

final int SIZE = 3;
char[][] board = new char[SIZE][SIZE]; // game board

resetBoard(board); // initialize the board (with ' ' for all cells)

// First, welcome message and display the board.
System.out.println("===== WELCOME TO THE TIC-TAC-TOE GAME!! ===== ");
showBoard(board);

// Then ask the user which symbol (x or o) he/she wants to play.
System.out.print(" Which symbol do you want to play, "x" or "o"? ");
char userSymbol = sc.next().toLowerCase().charAt(0);
char compSymbol = (userSymbol == 'x') ? 'o' : 'x';

// Also ask whether or not the user wants to go first.
System.out.println();
System.out.print(" Do you want to go first (y/n)? ");
char ans = sc.next().toLowerCase().charAt(0);

int turn; // 0 -- the user, 1 -- the computer
int remainCount = SIZE * SIZE; // empty cell count

// THE VERY FIRST MOVE.
if (ans == 'y') {
turn = 0;
userPlay(board, userSymbol); // user puts his/her first tic
}
else {
turn = 1;
compPlay(board, compSymbol); // computer puts its first tic
}
// Show the board, and decrement the count of remaining cells.
showBoard(board);
remainCount--;

// Play the game until either one wins.
boolean done = false;
int winner = -1; // 0 -- the user, 1 -- the computer, -1 -- draw

while (!done && remainCount > 0) {
// If there is a winner at this time, set the winner and the done flag to true.
done = isGameWon(board, turn, userSymbol, compSymbol); // Did the turn won?

if (done)
winner = turn; // the one who made the last move won the game
else {
// No winner yet. Find the next turn and play.
turn = (turn + 1 ) % 2;

if (turn == 0)
userPlay(board, userSymbol);
else
compPlay(board, compSymbol);

// Show the board after one tic, and decrement the rem count.
showBoard(board);
remainCount--;
}
}

// Winner is found. Declare the winner.
if (winner == 0)
System.out.println(" ** YOU WON. CONGRATULATIONS!! **");
else if (winner == 1)
System.out.println(" ** YOU LOST.. Maybe next time :) **");
else
System.out.println(" ** DRAW... **");

}

public static void resetBoard(char[][] brd)
{
for (int i = 0; i < brd.length; i++)
for (int j = 0; j < brd[0].length; j++)
brd[i][j] = ' ';
}

public static void showBoard(char[][] brd)
{
int numRow = brd.length;
int numCol = brd[0].length;

System.out.println();

// First write the column header
System.out.print(" ");
for (int i = 0; i < numCol; i++)
System.out.print(i + " ");
System.out.print(' ');

System.out.println(); // blank line after the header

// The write the table
for (int i = 0; i < numRow; i++) {
System.out.print(i + " ");
for (int j = 0; j < numCol; j++) {
if (j != 0)
System.out.print("|");
System.out.print(" " + brd[i][j] + " ");
}

System.out.println();

if (i != (numRow - 1)) {
// separator line
System.out.print(" ");
for (int j = 0; j < numCol; j++) {
if (j != 0)
System.out.print("+");
System.out.print("---");
}
System.out.println();
}
}
System.out.println();
}

public static void userPlay(char[][] brd, char usym)
{
System.out.print(" Enter the row and column indices: ");
int rowIndex = sc.nextInt();
int colIndex = sc.nextInt();

while (brd[rowIndex][colIndex] != ' ') {
System.out.print(" !! The cell is already taken. Enter the row and column indices: ");
rowIndex = sc.nextInt();
colIndex = sc.nextInt();
}

brd[rowIndex][colIndex] = usym;
}

public static void compPlay(char[][] brd, char csym)
{
// Find the first empty cell and put a tic there.
for (int i = 0; i < brd.length; i++) {
for (int j = 0; j < brd[0].length; j++) {
if (brd[i][j] == ' ') { // empty cell
brd[i][j] = csym;
return;
}
}
}
}

public static boolean isGameWon(char[][] brd, int turn, char usym, char csym)
{
char sym;
if (turn == 0)
sym = usym;
else
sym = csym;

int i, j;
boolean win = false;

// Check win by a row
for (i = 0; i < brd.length && !win; i++) {
for (j = 0; j < brd[0].length; j++) {
if (brd[i][j] != sym)
break;
}
if (j == brd[0].length)
win = true;
}

// Check win by a column
for (j = 0; j < brd[0].length && !win; j++) {
for (i = 0; i < brd.length; i++) {
if (brd[i][j] != sym)
break;
}
if (i == brd.length)
win = true;
}

// Check win by a diagonal (1)
if (!win) {
for (i = 0; i < brd.length; i++) {
if (brd[i][i] != sym)
break;
}
if (i == brd.length)
win = true;
}

// Check win by a diagonal (2)
if (!win) {
for (i = 0; i < brd.length; i++) {
if (brd[i][brd.length - 1 - i] != sym)
break;
}
if (i == brd.length)
win = true;
}

// Finally return win
return win;
}
}