I need help with this small project about tic Tic Tac Toe in java? I am having a
ID: 3576097 • Letter: I
Question
I need help with this small project about tic Tic Tac Toe in java? I am having a hard time
can you please follow the outline
Description:
Your program will consist of a class called TicTacToe that will encapsulating the concept of a tic-tac-toe game, and a client application class that will control the play of the game using the methods from the TicTacToe class.
UML Diagram
+ PLAYER_X: char = ‘X’
+ PLAYER_O: char = ‘O’
- xName: String
- oName: String
- board: char[]
- moveCount: int
+ TicTacToe(playerXName: String, playerOName: String )
+ initializeBoard( )
+ takeATurn(keyboard: Scanner, player: char ): boolean
+ isWinner(char player ): boolean
+ displayBoard( )
+ displayResults( )
- validMove(int move): boolean
The constructor will initialize the player names to the names passed to the parameters. It will instantiate the board array, and then it will call initializeBoard to fill the board array and initialize moveCount.
initializeBoard will initialize the moveCount to 0 and fill the board array by putting the character ‘1’ into board[1], the character ‘2’ into board[2], etc… This method will be called to reset the board and moveCount every time a new game is started.
takeATurn will execute one complete turn for the player passed to the method. The prerequisites for
this method are:
o there is not yet a winner
o there is still a place on the board to move
o player is either PLAYER_X or PLAYER_O
This method will determine what name to user for the player in the prompts to the user.
It will ask the user where he/she wishes to play and then if the user’s response is a valid move it will place the player’s ‘X’ or ‘O’ in that space on the board. If it is not a valid move (use the validMove method), it will let the user know and again ask the user where he/she wishes to play. The method will not end until the user has selected a valid move and the move has been recorded in the board array. When the move is complete, the move count will be incremented.
isWinner will return true if player has won the game, false otherwise. Prerequisite for this method is that player is either PLAYER_X or PLAYER_O. To determine if the player is a winner the method will use a long boolean expression to look at all the possible combinations of ways to win. i.e. are all the characters in the first row equal to player, or are all the characters in the second row equal to player, or are all the characters in the third row equal to player etc….
dislayBoard displays to the console, the current state of the board array in the format shown in the output on the last page of this document.
displayResults will use the isWinner method to determine who the winner of the game is. The prerequisite for this method is that the game is over. i.e. either there is a winner or else all the spaces on the board have been played and there is no winner, i.e.”the cat won”. The method will display an appropriate method on the console that will either congratulate the winner, or else declare “The game is a tie. – The CAT won.”
validMove will return true if move is in the proper range 1..9 and the board at that position is not an‘O’ or ‘X’. Otherwise it will return false.
Outline:
Explanation / Answer
import java.util.Scanner;
public class HelloWorld{
char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
char symbol[3];
string playername[3];
const bool CLEAR_SCREEN = true;
public static void main(String []args){
Scanner sc = new Scanner(System.in);
System.out.println(" *** WelCome *** Enter player names 1. "); playername[1] = sc.nextLine();
System.out.print(" 2. ") playername[2] = sc.nextLine();
System.out.print(" " +playername[1]+", Chose your Symbol : X or O ");
symbol[1] = sc.nextChar();
if(symbol[1] == 'X')
symbol[2] = 'O';
if(symbol[1] == 'O')
symbol[2] = 'X';
System.out.println(" "+playername[2]+", Your's Symbol : " +symbol[2]);
takeATurn(playername);
char ch;
do{
System.out.print("Do you want play more (Y/y)? : ");
ch = sc.nextChar();
if(ch!= 'Y' && ch!= 'y')
return 0;
newBroad();
}while(ch == 'Y' || ch == 'y');
displayBoard(playername);
}
public void newBroad()
{
if (CLEAR_SCREEN) {
System.out.print("c");
}
System.out.println(" **** Initial state ********* ");
square[1] = '1'; square[2] = '2'; square[3] = '3';
square[4] = '4'; square[5] = '5'; square[6] = '6';
square[7] = '7'; square[8] = '8'; square[9] = '9';
takeATurn(playername);
}
public void takeATurn(string playername[]){
int player = 1,i, position;
char mark;
Scanner sc = new Scanner(System.in);
do
{
display_board(playername);
player=(player%2)?1:2;
System.out.println(" "+playername[player] + ", enter a number: ");
position = sc.nextInt();
//mark=(player == 1) ? 'X' : 'O';
if(symbol[player] == 'X')
mark = 'X';
if(symbol[player] == 'O')
mark = 'O';
if ( position == 1 && square[1] == '1')
square[1] = mark;
else if (position == 2 && square[2] == '2')
square[2] = mark;
else if (position == 3 && square[3] == '3')
square[3] = mark;
else if (position == 4 && square[4] == '4')
square[4] = mark;
else if (position == 5 && square[5] == '5')
square[5] = mark;
else if (position == 6 && square[6] == '6')
square[6] = mark;
else if (position == 7 && square[7] == '7')
square[7] = mark;
else if (position == 8 && square[8] == '8')
square[8] = mark;
else if (position == 9 && square[9] == '9')
square[9] = mark;
else
{
System.out.println(" Invalid move ");
player--;
//cin.ignore();
//cin.get();
}
i = displayResults();
player++;
}while(i==-1);
display_board(playername);
if(i==1)
System.out.println(" Player "+playername[--player]+" won !!!");
else
System.out.println(" Game Draw");
//cin.ignore();
//cin.get();
}
/*********************************************
*
* FUNCTION TO RETURN GAME STATUS
* 1 FOR GAME IS OVER WITH RESULT
* -1 FOR GAME IS IN PROGRESS
* O GAME IS OVER AND NO RESULT
***********************************************/
public void displayResults()
{
enum status {PLAYING, X_WINS=1, Y_WINS=1, TIE=-1, UNDEF};
if (square[1] == square[2] && square[2] == square[3])
return X_WINS;
else if (square[4] == square[5] && square[5] == square[6])
return X_WINS;
else if (square[7] == square[8] && square[8] == square[9])
return X_WINS;
else if (square[1] == square[4] && square[4] == square[7])
return X_WINS;
else if (square[2] == square[5] && square[5] == square[8])
return Y_WINS;
else if (square[3] == square[6] && square[6] == square[9])
return Y_WINS;
else if (square[1] == square[5] && square[5] == square[9])
return Y_WINS;
else if (square[3] == square[5] && square[5] == square[7])
return Y_WINS;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4' && square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8' && square[9] != '9')
return PLAYING;
else
return TIE;
}
/*******************************************************************
* FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
*********************************************************************/
public void displayBoard(string playername[])
{
System.out.print(" Tic Tac Toe ");
System.out.println(" | | ");
System.out.println(" " + square[1] + " | " + square[2] + " | " + square[3]);
System.out.println(" _____|_____|_____");
System.out.println(" | | ");
System.out.println(" " + square[4] + " | " + square[5] + " | " + square[6]);
System.out.println(" _____|_____|_____");
System.out.println(" | | ");
System.out.println(" " + square[7] + " | " + square[8] + " | " + square[9]);
System.out.println(" | | ");System.out.println(" ");
System.out.println(" "+playername[1]+"("+symbol[1]+"), "+playername[2]+"("+symbol[2]+") ");
System.out.println(" ");
}
}