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

TIC-TAC-TOE Create a Java program. Your task is to develop a tic-tac-toe game. T

ID: 3843012 • Letter: T

Question

TIC-TAC-TOE
Create a Java program. Your task is to develop a tic-tac-toe game. There are two options for you to choose from. One option is a tictac-toe game where two humans may play. The second option is one where a human may play against the computer. Option one is worth less points (90% of total points) than option two (100% of total points).


PROGRAM REQUIREMENTS
1. The program prompts users for their names and ensures (validates) them; users are allowed to enter a single username consisting of alpha chars ONLY.
2. Use one-dimensional arrays to keep track of the game: a. Human moves in the case of option one. b. Computer moves as well in the case of option two.
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.
a. 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”.
b. 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”.
c. Do not allow non-numeric entries.
5. The program must be developed using functions so that the main() function consists mostly of function calls.
6. Below is a suggested list of functions for you to consider in the development of this project:
a. splashScreen()//displays game and developer’s information
b. askForUserNames()//requests for username
c. validateUserName()//validate username
d. switchPlayer()//switch from one player to another
e. resetGame()//reset the game when one concludes; this includes filling the array with vales 0-8
f. displayGrid()//display the grid after each player makes a move
g. playerMakeMove()//prompts player to make a move, invokes validatePlayersMove, checkPositionAvailability
h. validatePlayersMove()//validates that user entry X is such that 0<=X<=8
i. checkPositionAvailability()//check that the position selected by the user is available
j. checkWin()//check for a winning player
k. checkTie()//check for a tie
7. In the case of option two, additional functions you may want to consider are:
a. makeBestMove()//select best option
b. computerMakeMove()//used to make the move, in other words populate the array
8. 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. 9. You must use meaningful variable names.
10. You must comment your code.
11. You must use variables of the correct type and initialize them with a proper value.
12. Your program must detect a winner if there is one
13. Your program must determine if a tie occurs

Restrictions:
1. No infinite loops, examples include:
a. for(;;)
b. while(1)
c. while(true)
d. do{//code}while(1);
2. No break statements to exit loops

D C: Users floresla DebuglProjectl.exe TIC TAC TOE Prof Flores Press any key to continue

Explanation / Answer

import javapackage.*; class TicTacToe { static final int EMPTY = 0; static final int NONE = 0; static final int USER = 1; static final int COMPUTER = 2; static final int STALEMATE = 3; public static void main(String[] args) { // Data objects // 1 = user, 2 = computer int turn = USER; // We will represent the board as nine cells // 0 = empty, 1 = user, 2 = computer int[][] board = new int[3][3]; // move: 1-9 representing ul through lr int move; // winner: 0 = none, 1 = user, 2 = computer, 3 = stalemate int winner; // Print Instructions System.out.println("This is a tic-tac-toe game"); System.out.println("You are playing against the computer!"); System.out.println("Enter 1-9 to indicate your move"); // Print the board print_board(board); // While (game not over) while(true) { if(turn == USER) { System.out.println("Your move"); move = -1; while (move9 || board[move/3][move%3] != EMPTY) { System.out.println("Please enter your move(0-9): "); move = Console.in.readInt(); Console.in.readChar(); } } else { move = computer_move(board); System.out.println("Computer move: " + move); } // Update the board board[(int)(move/3)][move%3] = turn; // Print the board print_board(board); // if game is over winner = checkWinner(board); if(winner != NONE) break; // switch turn if(turn == USER) { turn = COMPUTER; } else { turn = USER; } } // Print out the outcome switch(winner) { case USER: System.out.println("You won!"); break; case COMPUTER: System.out.println("Computer won!"); break; default: System.out.println("Tie!"); break; } } // Print the board public static void print_board(int[][] board) { System.out.print(printChar(board[0][0])); System.out.print("|"); System.out.print(printChar(board[0][1])); System.out.print("|"); System.out.println(printChar(board[0][2])); System.out.println("-----"); System.out.print(printChar(board[1][0])); System.out.print("|"); System.out.print(printChar(board[1][1])); System.out.print("|"); System.out.println(printChar(board[1][2])); System.out.println("-----"); System.out.print(printChar(board[2][0])); System.out.print("|"); System.out.print(printChar(board[2][1])); System.out.print("|"); System.out.println(printChar(board[2][2])); } // Return an X or O, depending upon whose move it was public static char printChar(int b) { switch(b) { case EMPTY: return ' '; case USER: return 'X'; case COMPUTER: return 'O'; } return ' '; } // See if the game is over public static int checkWinner(int[][] board) { // Check if someone won // Check horizontals // top row if((board[0][0] == board[0][1]) && (board[0][1] == board[0][2])) return board[0][0]; // middle row if((board[1][0] == board[1][1]) && (board[1][1] == board[1][2])) return board[1][0]; // bottom row if((board[2][0] == board[2][1]) && (board[2][1] == board[2][2])) return board[2][0]; // Check verticals // left column if((board[0][0] == board[1][0]) && (board[1][0] == board[2][0])) return board[0][0]; // middle column if((board[0][1] == board[1][1]) && (board[1][1] == board[2][1])) return board[0][1]; // right column if((board[0][2] == board[1][2]) && (board[1][2] == board[2][2])) return board[0][2]; // Check diagonals // one diagonal if((board[0][0] == board[1][1]) && (board[1][1] == board[2][2])) return board[0][0]; // the other diagonal if((board[0][2] == board[1][1]) && (board[1][1] == board[2][0])) return board[0][2]; // Check if the board is full if(board[0][0] == EMPTY || board[0][1] == EMPTY || board[0][2] == EMPTY || board[1][0] == EMPTY || board[1][1] == EMPTY || board[1][2] == EMPTY || board[2][0] == EMPTY || board[2][1] == EMPTY || board[2][2] == EMPTY) return NONE; return STALEMATE; } // Generate a random computer move public static int computer_move(int[][] board) { int move = (int)(Math.random()*9); while(board[move/3][move%3] != EMPTY) move = (int)(Math.random()*9); return move; } }