THIS IS FINAL COURSE ASSIGNMENT, FOLLOWING EVERY SINGLE REQUIREMENT IS CRUCIAL!
ID: 3845324 • 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 continueExplanation / Answer
import java.util.Random;
import java.util.Scanner;
public class TicTacToe {final static int HUMAN_PLAYER = 0;
final static int COMPUTER_PLAYER = 1;final static char COMPUTER = 'C';
final static char HUMAN = 'H';
/*** display the splash screen*/
public static void splashScreen()
{System.out.println("************************");
System.out.println("************************");
System.out.println("************************");
System.out.println("****** *******");
System.out.println("***** TIC TAC TOE ******");
System.out.println("****** By Human ****");
System.out.println("************************");
System.out.println("************************");
System.out.println("************************");
}/*** reset the game when one concludes;
this includes filling the array with* vales 0-8* * @param grid* grid of the game
*/
public static void resetGame(char[] grid)
{
for (int i = 0; i < 9; i++)
{
String tmp = Integer.toString(i);
grid[i] = tmp.charAt(0);
}
}
/*** display the grid* * @param grid* grid of the game*/
public static void displayGrid(char[] grid)
{
System.out.println();
System.out.println(" TIC-TAC-TOE");
System.out.println(" |-----|");
System.out.printf(" |%c|%c|%c| ", grid[0], grid[1], grid[2]);
System.out.printf(" |%c|%c|%c| ", grid[3], grid[4], grid[5]);
System.out.printf(" |%c|%c|%c| ", grid[6], grid[7], grid[8]);
System.out.println(" |-----|"); System.out.println();
}
/**
public static void playerMakeMove(int player, char[] grid, Scanner sc)
{
if (player == HUMAN_PLAYER)
{
boolean validMove;
do
{
System.out.println(" It is the human's turn");
System.out.println("Give me your best move!");
String input = sc.nextLine();
validMove = validatePlayersMove(grid, input);
if (!validMove) {System.out.println("Make a different choice.");
}
}
while (!validMove);
}
else
{
computerMakeMove(grid);
}
}
public static void computerMakeMove(char[] grid) {Random rand = new Random();
int index;
do {index = rand.nextInt(9);
}
while (!checkPositionAvailability(grid, index));
grid[index] = COMPUTER;
System.out.println(" Computer played at " + index + " ");
}
/*** Be sure to validate user entries;
no white space, no numbers X, such that* 0<=X<=8, no alpha characters* * @param grid* @param input* @return*/
public static boolean validatePlayersMove(char[] grid, String input)
{
if (input.length() != 1 || input.charAt(0) < '0' || input.charAt(0) > '8')
{
System.out.println("Invalid input!");
return false;
}
int index = Integer.parseInt(input);
if (!checkPositionAvailability(grid, index))
{
System.out.println("Position not available!");
return false;
}
grid[index] = HUMAN;
return true;
}
public boolean checkWin(){
int countOnes = 0;
int countZeros = 0;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if(board[i][j] == 0) countZeros++;
else if(board[i][j] == 1)countOnes++;
}
if(countZeros == size || countOnes == size) return true;
break;
}
countOnes = 0;
countZeros = 0;
//check diagonal right
for (int i = 0, j= 0; i <size ; i++, j++) {
if (board[i][j] == 0) countZeros++;
else if(board[i][j] == 1) countOnes++;
}
if(countOnes == size || countZeros == size) return true;
//check diagonal left
for (int i = size-1, j= size-1; i >= 0 ; i--, j--) {
if (board[i][j] == 0) countZeros++;
else if(board[i][j] == 1) countOnes++;
}
if(countOnes == size || countZeros == size) return true;
return false;
}
}
public static bool BoardFull(char [] [] Board)
{
for (int row = 0; row < 3; ++row)
{
for (int column = 0; column < 3; ++column)
{
if (Board [row] [column] == '.')
{
// a move still exists - board is not full
return false;
}
}
}
return true;
}
ublic static String makeBestMove(String board, char player) {
int bestRow = 0;
int bestCol = 0;
double bestScore = -2;
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= 3; col++) {
if (getMark(board, row, col) == ' ') {
String newBoard = setMark(board, player, row, col);
double newScore = moveScore(newBoard, player == 'x' ? 'o' : 'x', 1);
if (newScore > bestScore) {
bestRow = row;
bestCol = col;
bestScore = newScore;
}
}
}
}
return setMark(board, player, bestRow, bestCol);
}
int
computerMakeMove(Board *board)
{ int RandIndex = 0;
int **spaces = board->spaces;
int columns = board->columns;
int *arrayoflegalmoves = malloc(sizeof(int) * (columns));
int columncheck = 0;
int legalmoveindex = 0;
while (columncheck <= columns - 1)
{
if (spaces[columncheck][0] == 0)
{
arrayoflegalmoves[legalmoveindex] = columncheck;
++legalmoveindex;
++columncheck;
}
else
{
++columncheck;
}
arrayoflegalmoves = realloc(arrayoflegalmoves, (legalmoveindex) * sizeof(int));
}
if (legalmoveindex == 1)
{
return arrayoflegalmoves[0];
}
else
{
RandIndex = rand() % (legalmoveindex);
return arrayoflegalmoves[RandIndex];
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.