Create a Java Program that will play the game of Tic-Tac-Toe with a user. (a) Cr
ID: 3697753 • Letter: C
Question
Create a Java Program that will play the game of Tic-Tac-Toe with a user.
(a) Create a document that includes the functional specifications of the program.
Identify the various methods you will need to implement the functionality.
Identify any class variables you would like to use.
Describe the algorithm for the program.
Make use of value returning and void methods. Must use one dimension or two dimension arrays.
(b) Organize the algorithm via functions that is easy to modify the requirements of the project. Allow the use to select 'X' or 'O' when it is their turn. Same as when it is the computer's turn.
Explanation / Answer
//Program
import java.util.Random;
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
/*Read initial status of the board from user*/
System.out.println("Enter the Initial Status: ");
Scanner scan=new Scanner(System.in);
String InitStatus=scan.nextLine();
/*Split the status into set of characters*/
char square[]=InitStatus.toCharArray();
/*player 2 is Human, player 1 is computer*/
/*Initially assume the player is 2*/
int player=2;
int choice,i=0;
int size=0;
int flag=0;
/* till no body win && number of squares filled is less than 9,
this loop will works
*/
while(i!=1 &&size<9)
{
if(flag==1)
{
/*display the current status of board*/
size=printBoard(square);
/* if all squares filled, break */
if(size==9) break;
}
flag=0;
/* exchange the turn from human player to computer player*/
if(player%2==0){
player=1;
/*to get the computer choice for cell, use random number */
Random rn = new Random();
choice =rn.nextInt(10) ;
//display computer's choice
System.out.println("The computer chooses to fill the cell #"+choice);
}
/*exchange the turn from Computer to human player*/
else{
player=2;
/*read the choice for cell from Human*/
System.out.println("Human player please select a cell number to fill:");
choice=scan.nextInt();}
/*if player 1 , mark is 'X' . else 'O' */
char mark=(player == 1) ? 'X' : 'O';
/*if the square is not marked , i.e it equals '_' then mark the square*/
if (choice == 1 && square[0]=='_')
square[0] = mark;
else if (choice == 2 && square[1]=='_')
square[1] = mark;
else if (choice == 3 && square[2]=='_')
square[2] = mark;
else if (choice == 4 && square[3]=='_')
square[3] = mark;
else if (choice == 5 && square[4]=='_')
square[4] = mark;
else if (choice == 6 && square[5]=='_')
square[5] = mark;
else if (choice == 7 && square[6]=='_')
square[6] = mark;
else if (choice == 8 && square[7]=='_')
square[7] = mark;
else if (choice == 9 && square[8]=='_')
square[8] = mark;
/*if an already filled square is selected, then it is an invalid move.
Then exchange the players turn
*/
else
{
System.out.println("Invalid move,please try again. ");
flag=1;
if(player==1)
player=2;
else
player=1;
}
/* if it is a valid move, call checkwin function, to check who is win */
/*i will be true, if anybody win, else false*/
if(flag==0)
i=checkwin(square);
}//end of while loop
//print the board
System.out.println("Thanks. Now the board looks like:");
printBoard(square);
/* if i is true and player turn is 1, display computer won */
if(i==1 && player==1)
System.out.println("The ComputerWon!");
/* if i is true and player turn is 2, display human won */
else if(i==1 && player==2)
System.out.println("The ComputerWon!");
//if no body win (but all squares filled), then game is draw
else
System.out.println("==>Game draw");
}
/*
function to display the board.
*/
private static int printBoard(char square[]) {
int size=0;
System.out.println(" | | ");
System.out.println(" " + square[0] + " | " + square[1] + " | " +square[2] );
System.out.println("_____|_____|_____");
System.out.println(" | | ");
System.out.println(" " +square[3] + " | " + square[4] + " | " + square[5] );
System.out.println("_____|_____|_____");
System.out.println(" | | ");
System.out.println(" " +square[6] + " | " + square[7] + " | " + square[8] );
System.out.println(" | | ");
/* counting number of sqaures filled in board*/
for(int i=0;i<square.length;i++)
{
if(square[i]!='_')
size++;
}
return size;
}
/* to check win or game draw */
private static int checkwin(char[] square) {
{
// horizontally , first row of the board contains same mark, it is win
if (square[0] == square[1] && square[1] == square[2])
return 1;
// horizontally , second row of the board contains same mark, it is win
else if (square[3] == square[4] && square[4] == square[5])
return 1;
// horizontally , third row of the board contains same mark, it is win
else if (square[6] == square[7] && square[7] == square[8])
return 1;
// vertically , first column of the board contains same mark, it is win
else if (square[0] == square[3] && square[3] == square[6])
return 1;
// vertically , second column of the board contains same mark, it is win
else if (square[1] == square[4] && square[4] == square[7])
return 1;
// vertically , third column of the board contains same mark, it is win
else if (square[2] == square[5] && square[5] == square[8])
return 1;
//diagonal check for same mark. if yes , then it is win
else if (square[0] == square[4] && square[4] == square[8])
return 1;
//diagonal check for same mark. if yes , then it is win
else if (square[2] == square[4] && square[4] == square[6])
return 1;
// if all the above cases fails, then it is lose
else
return -1;
}
}
}