Answered! 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….

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:

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

On the grid “H” stands for human move, “C” for computer move

Expert Answer

 Solution:

// import required package

import java.io.*;

import java.util.*;

public class TICTACTOEGM

{

// main method

public static void main(String[] args)

{

char[] TTTboard=new char[9];

Scanner snr=new Scanner(System.in);

int plrop=1;

String plrName=null;

int rwin=0;

int rtie=0;

screendisp();

Gamereset(TTTboard);

getusrName(snr,plrName);

while(true)

{

displayGrid(TTTboard);

if(plrop==1)

{

playerMakeMove(snr,TTTboard);

}

else if(plrop==2)

computerMakeMove(TTTboard);

if(plrop==1)

rwin=checkWin(TTTboard,’H’);

else

rwin=checkWin(TTTboard,’C’);

if(rwin==1)

{

if(plrop==1)

System.out.println(“Human wins”);

else if(plrop==2)

System.out.println(“Computer wins”);

break;

}

if(rwin==-1)

{

System.out.println(“GAME TIE”);

break;

}

plrop = (plrop%2==0)?1:2;

}

}

// function to display message on screen

public static void screendisp()

{

System.out.println(“***********************”);

System.out.println(“***********************”);

System.out.println(“***********************”);

System.out.println(“**************** ******”);

System.out.println(“******TIC TAC TOE******”);

System.out.println(“****** By Prof. *******”);

System.out.println(“***********************”);

System.out.println(“***********************”);

System.out.println(“***********************”);

System.out.println(“***********************”);

System.out.println(“***********************”);

}

// function to get user name

public static void getusrName(Scanner snr, String plrName)

{

System.out.println(“Player, enter your name”);

while(true)

{

System.out.println(“enter a single word with alpha Chars only!”);

plrName=snr.nextLine();

if(!validateUserName(plrName))

{

System.out.println(“invalid name”);

continue;

}

break;

}

}

// function to validate user name

public static boolean validateUserName(String plrName)

{

if(plrName.isEmpty())

{

return false;

}

else if(plrName.matches(“^\d+”))

{

return false;

}

return true;

}

// function to reset the game

public static void Gamereset(char[] TTTboard)

{

for(int lp=0;lp<9;lp++)

TTTboard[lp]=’ ‘;

}

// function to display the grid

public static void displayGrid(char[] TTTboard)

{

System.out.println(“”);

System.out.println(“TIC TAC TOE”);

System.out.println();

for(int lp=0;lp<9;lp++)

{

if(lp==0 || lp==3 ||lp==6)

{

System.out.println(“”);

System.out.print(“|—–|n|”);

}

if(TTTboard[lp]==’ ‘)

System.out.print((char)(lp+’0′)+”|”);

else

System.out.print(TTTboard[lp]+”|”);

}

System.out.println(“”);

}

// function to make game movement

public static void playerMakeMove(Scanner snr,char[] TTTboard)

{

int mv=0;

while(true)

{

System.out.println(“it is the humn’s turn”);

System.out.println(“Give me your best mv!”);

mv=snr.nextInt();

if(validatePlayersMove(mv))

{

if(checkPositionAvailability(mv, TTTboard))

break;

}

}

TTTboard[mv]=’H’;

}

// function to validate the player move

public static boolean validatePlayersMove(int mv)

{

return (mv>=0 && mv<=8);

}

// function to position availability

public static boolean checkPositionAvailability(int mv, char[] TTTboard)

{

if(TTTboard[mv]!=’ ‘ )

return false;

return true;

}

// function to check winning

public static int checkWin(char[] TTTboard, char plrop)

{

if (TTTboard[0] == TTTboard[1] && TTTboard[0] == TTTboard[2]&& TTTboard[0]==plrop)

return 1;

else if (TTTboard[3] == TTTboard[4] && TTTboard[3] == TTTboard[5]&& TTTboard[2]==plrop)

return 1;

else if (TTTboard[6] == TTTboard[7] && TTTboard[6] == TTTboard[8]&& TTTboard[6]==plrop)

return 1;

else if (TTTboard[0] == TTTboard[3] && TTTboard[0] == TTTboard[6]&& TTTboard[0]==plrop)

return 1;

else if (TTTboard[1] == TTTboard[4] && TTTboard[1] == TTTboard[7]&& TTTboard[1]==plrop)

return 1;

else if (TTTboard[2] == TTTboard[5] && TTTboard[2] == TTTboard[8]&& TTTboard[2]==plrop)

return 1;

else if (TTTboard[0] == TTTboard[4] && TTTboard[0] == TTTboard[8]&& TTTboard[0]==plrop)

return 1;

else if (TTTboard[2] == TTTboard[4] && TTTboard[2] == TTTboard[6]&& TTTboard[2]==plrop)

return 1;

else

return checkTie(TTTboard);

}

// function to check game tie

public static int checkTie(char TTTboard[])

{

if (TTTboard[0] != ‘0’ && TTTboard[1] != ‘1’ && TTTboard[2] != ‘2’ && TTTboard[3] != ‘3’&& TTTboard[4] != ‘4’ && TTTboard[5] != ‘5’ && TTTboard[6] != ‘6’&& TTTboard[7] != ‘7’ && TTTboard[8] != ‘8’)

{

return 0;

}

else

{

return -1;

}

}

// function to make best movement

public static int makeBestMove(char[] TTTboard)

{

if (TTTboard[0] ==’1′ && TTTboard[1] == ‘C’ && TTTboard[2]==’C’ )

return 0;

else if (TTTboard[0] ==’C’ && TTTboard[1] == ‘1’ && TTTboard[2]==’C’ )

return 1;

else if (TTTboard[0] ==’C’ && TTTboard[1] == ‘C’ && TTTboard[2]==’2′ )

return 2;

else if (TTTboard[3] ==’3′ && TTTboard[4] == ‘C’ && TTTboard[5]==’C’ )

return 3;

else if (TTTboard[3] ==’C’ && TTTboard[4] == ‘4’ && TTTboard[5]==’C’ )

return 4;

else if (TTTboard[3] ==’C’ && TTTboard[4] == ‘C’ && TTTboard[5]==’5′ )

return 5;

else if (TTTboard[6] ==’6′ && TTTboard[7] == ‘C’ && TTTboard[8]==’C’ )

return 6;

else if (TTTboard[6] ==’C’ && TTTboard[7] == ‘7’ && TTTboard[8]==’C’ )

return 7;

else if (TTTboard[6] ==’C’ && TTTboard[7] == ‘C’ && TTTboard[8]==’8′ )

return 8;

else if (TTTboard[0] ==’0′ && TTTboard[3] == ‘C’ && TTTboard[6]==’C’ )

return 0;

else if (TTTboard[0] ==’C’ && TTTboard[3] == ‘3’ && TTTboard[6]==’C’ )

return 3;

else if (TTTboard[0] ==’C’ && TTTboard[3] == ‘C’ && TTTboard[6]==’6′ )

return 6;

else if (TTTboard[1] ==’1′ && TTTboard[4] == ‘C’ && TTTboard[7]==’C’ )

return 1;

else if (TTTboard[1] ==’C’ && TTTboard[4] == ‘4’ && TTTboard[7]==’C’ )

return 4;

else if (TTTboard[1] ==’C’ && TTTboard[4] == ‘C’ && TTTboard[7]==’7′ )

return 7;

else if (TTTboard[2] ==’2′ && TTTboard[5] == ‘C’ && TTTboard[8]==’C’ )

return 2;

else if (TTTboard[2] ==’C’ && TTTboard[5] == ‘5’ && TTTboard[8]==’C’ )

return 5;

else if (TTTboard[2] ==’C’ && TTTboard[5] == ‘C’ && TTTboard[8]==’8′ )

return 8;

else if (TTTboard[0] ==’0′ && TTTboard[4] == ‘C’ && TTTboard[8]==’C’ )

return 0;

else if (TTTboard[0] ==’C’ && TTTboard[4] == ‘4’ && TTTboard[8]==’C’ )

return 4;

else if (TTTboard[0] ==’C’ && TTTboard[4] == ‘C’ && TTTboard[8]==’8′ )

return 8;

else if (TTTboard[2] ==’2′ && TTTboard[4] == ‘C’ && TTTboard[6]==’C’ )

return 2;

else if (TTTboard[2] ==’C’ && TTTboard[4] == ‘4’ && TTTboard[6]==’C’ )

return 4;

else if (TTTboard[2] ==’C’ && TTTboard[4] == ‘C’ && TTTboard[6]==’6′ )

return 6;

//CHeck possiblity

else if (TTTboard[0] ==’1′ && TTTboard[1] == ‘H’ && TTTboard[2]==’H’ )

return 0;

else if (TTTboard[0] ==’H’ && TTTboard[1] == ‘1’ && TTTboard[2]==’H’ )

return 1;

else if (TTTboard[0] ==’H’ && TTTboard[1] == ‘H’ && TTTboard[2]==’2′ )

return 2;

else if (TTTboard[3] ==’3′ && TTTboard[4] == ‘H’ && TTTboard[5]==’H’ )

return 3;

else if (TTTboard[3] ==’H’ && TTTboard[4] == ‘4’ && TTTboard[5]==’H’ )

return 4;

else if (TTTboard[3] ==’H’ && TTTboard[4] == ‘H’ && TTTboard[5]==’5′ )

return 5;

else if (TTTboard[6] ==’6′ && TTTboard[7] == ‘H’ && TTTboard[8]==’H’ )

return 6;

else if (TTTboard[6] ==’H’ && TTTboard[7] == ‘7’ && TTTboard[8]==’H’ )

return 7;

else if (TTTboard[6] ==’H’ && TTTboard[7] == ‘H’ && TTTboard[8]==’8′ )

return 8;

else if (TTTboard[0] ==’0′ && TTTboard[3] == ‘H’ && TTTboard[6]==’H’ )

return 0;

else if (TTTboard[0] ==’H’ && TTTboard[3] == ‘3’ && TTTboard[6]==’H’ )

return 3;

else if (TTTboard[0] ==’H’ && TTTboard[3] == ‘H’ && TTTboard[6]==’6′ )

return 6;

else if (TTTboard[1] ==’1′ && TTTboard[4] == ‘H’ && TTTboard[7]==’H’ )

return 1;

else if (TTTboard[1] ==’H’ && TTTboard[4] == ‘4’ && TTTboard[7]==’H’ )

return 4;

else if (TTTboard[1] ==’H’ && TTTboard[4] == ‘H’ && TTTboard[7]==’7′ )

return 7;

else if (TTTboard[2] ==’2′ && TTTboard[5] == ‘H’ && TTTboard[8]==’H’ )

return 2;

else if (TTTboard[2] ==’H’ && TTTboard[5] == ‘5’ && TTTboard[8]==’H’ )

return 5;

else if (TTTboard[2] ==’H’ && TTTboard[5] == ‘H’ && TTTboard[8]==’8′ )

return 8;

else if (TTTboard[0] ==’0′ && TTTboard[4] == ‘H’ && TTTboard[8]==’H’ )

return 0;

else if (TTTboard[0] ==’H’ && TTTboard[4] == ‘4’ && TTTboard[8]==’H’ )

return 4;

else if (TTTboard[0] ==’H’ && TTTboard[4] == ‘H’ && TTTboard[8]==’8′ )

return 8;

else if (TTTboard[2] ==’2′ && TTTboard[4] == ‘H’ && TTTboard[6]==’H’ )

return 2;

else if (TTTboard[2] ==’H’ && TTTboard[4] == ‘4’ && TTTboard[6]==’H’ )

return 4;

else if (TTTboard[2] ==’H’ && TTTboard[4] == ‘H’ && TTTboard[6]==’6′ )

return 6;

else {

Random r=new Random();

return r.nextInt(10);

}

}

// function to make computer movement

public static void computerMakeMove(char[] TTTboard)

{

int mv =0;

while(true)

{

mv=makeBestMove(TTTboard);

if(checkPositionAvailability(mv,TTTboard))

{

TTTboard[mv]=’C’;

break;

}

}

}

}

Result:

***********************

***********************

***********************

**************** ******

******TIC TAC TOE******

****** By Prof. *******

***********************

***********************

***********************

***********************

***********************

Player, enter your name

enter a single word with alpha Chars only!

0

invalid name

enter a single word with alpha Chars only!

vk

TIC TAC TOE

|—–|

|0|1|2|

|—–|

|3|4|5|

|—–|

|6|7|8|

it is the humn’s turn

Give me your best mv!

0

TIC TAC TOE

|—–|

|H|1|2|

|—–|

|3|4|5|

|—–|

|6|7|8|

TIC TAC TOE

|—–|

|H|1|2|

|—–|

|C|4|5|

|—–|

|6|7|8|

it is the humn’s turn

Give me your best mv!

1

TIC TAC TOE

|—–|

|H|H|2|

|—–|

|C|4|5|

|—–|

|6|7|8|

TIC TAC TOE

|—–|

|H|H|2|

|—–|

|C|C|5|

|—–|

|6|7|8|

it is the humn’s turn

Give me your best mv!

2

Human wins

Still stressed from student homework?
Get quality assistance from academic writers!