I need to create a JAVA Class TicTacToe program. There are API doc of the class TicTacToe and TicTacToeApp.java which program control TicTacToe game play. Would you please tell me how to write the Java Class program. Thanks a lot!

TicTacToeApp Java Program as shown below:

/**
*   TIC TAC TOE App
*/
public class TicTacToeApp
{


/**
*   <div>The main method (Application Entry)
*   TicTacToeApps
*   </div>
*/
public static void main (String args[])
{
// print game title
System.out.println("TicTacToe Game");
// create a TicTacToe object
TicTacToe ttt = new TicTacToe();


// display the game board
ttt.display();


// while the game is not end
while (!ttt.isGameEnded())
{
// prompt player to make a move
System.out.print("Please make a move, Player ");
if (TicTacToe.PLAYER1 == ttt.getNextMovePlayer())
System.out.println("1:");
else
System.out.println("2:");


// read x and y coordinate
System.out.print("x: ");
int x = SavitchIn.readLineInt();
System.out.print("y: ");
int y = SavitchIn.readLineInt();


// make a move
if (!ttt.addMove(x ,y))
System.out.println(">>> Invalid Move, try again! <<<\n");


// display the game board
ttt.display();
}


// the game is ended
if (ttt.isGameEnded())
{
if (ttt.getWinner() != TicTacToe.NOPLAYER)
// there is a winner
if (ttt.getWinner() == TicTacToe.PLAYER1)
System.out.println("Winner is: player 1");
else
System.out.println("Winner is: player 2");
else
// the game drawn
System.out.println("Draw Game!");
}
}


}


API shown below:


Package   Class  Tree  Deprecated  Index  Help


PREV CLASS   NEXT CLASS FRAMES    NO FRAMES     All Classes
SUMMARY: NESTED | FIELD | CONSTR | METHOD DETAIL: FIELD | CONSTR | METHOD


--------------------------------------------------------------------------------


Class TicTacToe
java.lang.Object
TicTacToe


--------------------------------------------------------------------------------


public class TicTacToeextends java.lang.ObjectTIC TAC TOE



--------------------------------------------------------------------------------


Field Summary
static int NOPLAYER


constant NOPLAYER
static int PLAYER1


constant PLAYER1
static int PLAYER2


constant PLAYER2
Constructor Summary
TicTacToe()


Default constructor for TicTacToe
Default char for p1 = '1'
Default char for p2 = '2'
TicTacToe(char p1CH, char p2CH)


Constructor for TicTacToe
Method Summary
boolean addMove(int x, int y)


Method to add a move to next player
if specified slot is occupied, it is a invalid move and the next player will not change.
void display()


Method to print the game board


Note:
empty space must be '_',
players' displays depend on constructor or setPlayerXChar()
(default: '1','2')
Example:
|1|2|_|
|2|1|_|
|_|_|1|
int getNextMovePlayer()


Method for getting the next move player
int getWinner()


Method to get the winner of the game
boolean isGameEnded()


Method to check if the game is ended
Condition for ending: the game is won or all spaces has been filled
void reset()


Method for resetting the whole game
calling reset will not change first move player
boolean setFirstMovePlayer(int player)


setting the first move player
void setPlayer1Char(char p1CH)


setting the player 1 display
void setPlayer2Char(char p2CH)


setting the player 2 display
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait



Field Detail



NOPLAYER
public static final int NOPLAYERconstant NOPLAYER


See Also:
Constant Field Values


--------------------------------------------------------------------------------


PLAYER1
public static final int PLAYER1constant PLAYER1


See Also:
Constant Field Values


--------------------------------------------------------------------------------


PLAYER2
public static final int PLAYER2constant PLAYER2


See Also:
Constant Field Values
Constructor Detail


TicTacToe
public TicTacToe()Default constructor for TicTacToe
Default char for p1 = '1'
Default char for p2 = '2'



--------------------------------------------------------------------------------


TicTacToe
public TicTacToe(char p1CH,
char p2CH)Constructor for TicTacToe


Parameters:
p1CH - player1 character
p2CH - player2 character
Method Detail


setPlayer1Char
public void setPlayer1Char(char p1CH)setting the player 1 display


Parameters:
p1CH - player1 character


--------------------------------------------------------------------------------


setPlayer2Char
public void setPlayer2Char(char p2CH)setting the player 2 display


Parameters:
p2CH - player2 character


--------------------------------------------------------------------------------


setFirstMovePlayer
public boolean setFirstMovePlayer(int player)setting the first move player


Parameters:
player - the first move player (PLAYER1 or PLAYER2)


Returns:
return false if input is NOT (PLAYER1 or PLAYER2) OR the game has begun.
otherwise, return true.


--------------------------------------------------------------------------------


reset
public void reset()Method for resetting the whole game
calling reset will not change first move player



--------------------------------------------------------------------------------


getNextMovePlayer
public int getNextMovePlayer()Method for getting the next move player


Returns:
PLAYER1 or PLAYER2


--------------------------------------------------------------------------------


addMove
public boolean addMove(int x,
int y)Method to add a move to next player
if specified slot is occupied, it is a invalid move and the next player will not change.
if the game has ended, there is no more valid move.


Parameters:
x - coordinate x (row, from top to bottom: [1-3])
y - coordinate y (column, from left to right: [1-3])
Example:
addMove(1, 1);
addMove(1, 1);
addMove(2, 2);
the first call make a move to the upper left hand corner for PLAYER1
the second call make a move for PLAYER2, but the move is INVALID (occupied)
the third valid call make a move at center for PLAYER2
resulting board:
|1|_|_|
|_|2|_|
|_|_|_|


Returns:
true / false - telling whether the move is valid or not


--------------------------------------------------------------------------------


isGameEnded
public boolean isGameEnded()Method to check if the game is ended
Condition for ending: the game is won or all spaces has been filled


Returns:
true or false.
true if game ended
false otherwise


--------------------------------------------------------------------------------


getWinner
public int getWinner()Method to get the winner of the game


Returns:
NOPLAYER or PLAYER1 or PLAYER2
if the game is ended,
|--  if the game is drawn, NOPLAYER is returned
|--  if the game is won, PLAYER1 or PLAYER2 is returned
if the game has not ended, NOPLAYER is returned


--------------------------------------------------------------------------------


display
public void display()Method to print the game board


Note:
empty space must be '_',
players' displays depend on constructor or setPlayerXChar()
(default: '1','2')
Example:
|1|2|_|
|2|1|_|
|_|_|1|



--------------------------------------------------------------------------------
Package   Class  Tree  Deprecated  Index  Help


PREV CLASS   NEXT CLASS FRAMES    NO FRAMES     All Classes
SUMMARY: NESTED | FIELD | CONSTR | METHOD DETAIL: FIELD | CONSTR | METHOD


--------------------------------------------------------------------------------

Don't use a single massive monolith of a class. That way lies disaster.

For the rest, sit down and write a design. Doesn't have to be a lot, just put some thoughts onto paper how you're going to do this thing.
Then start your text editor and start writing Java (or better expand that design document until it starts to write the code for you).

We're NOT going to write your program for you, that's your job.

If you have specific details you're stuck on be free to ask for help of course, but "write my program" is NOT a good question to ask (it's not even a question, it's an order and you're not paying us).

Thanks for your advice

I would suggest to use a 3x3 two dimensional array. Maybe something like

char player[][] = new char[3][3];

Then when player 1 wants to use cell (1,2) write:

player[1][2] = '1';

:?: For more help, www.NeedProgrammingHelp.com

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.