Hi all it's that time again, java exercise! Mindful of what happened last time http://www.daniweb.com/software-development/java/threads/449525/building-a-simple-airline-reservation-system-exercise/ , this time I have decided that I want to have all the pseudocode done before I even start thinking about how to code the whole thing.
This is what the exercise requires:
Create a TicTacToe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 2 dimensional arrays. Use an enumeration to represent the values in each square of the array. The enumeration's constants should be named X, 0 and EMPTY (for a position that doesn't contain an X or 0).
The constructor should initialize the board elements to EMPTY. Allow 2 human players. Wherever the first player moves place an X in the specified square amd place an 0 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game has been won and whether it's a draw.
If anybody is interested this exercise is taken from the Deitel and deitel book "Java How to program" p393.
Ok, this is my pseudocode:
There are plenty of comments so it should be clear what's going on:
/*PSEUDOCODE*/
create TicTacToe class
private 3-by-3 two_dimensional_arrays;//holds the board
private boolean isFull = false//if true all the squares have been assigned.
private boolean isDraw;//to check if the game is draw
private boolean squareStatus;//returns the status of a square, if false the square is full if true the square is empty
private boolean victory;//if returns true one of the players has won
private playerCounter = 1;//if it is odd it's player1's turn if even it's player2's turn
private row;//row of the 3-by-3 array
private column;//column of the 3-by-3 array
create enum{
constant X
constant O
constant EMPTY
}
while(!isFull){
if(playerCounter is odd){//if it's player1's turn
print("Player 1's turn, enter the coordinates of the square");
}
else{it's player2's turn
print("Player 2's turn, enter the coordinates of the square");
}
type number from 0 to array.length for the row
row = userInputRow;
type number from 0 to array[row].lenght for columns
column = userInputCol;
boolean squareStatus = checkSquare(num1, num2);//check if square chosen is empty (returns false) or full (returns true)
while(squareStatus){//if the chosen box is full
input again row and column
squareStatus = checkSquare(num1, num2);//check again the status of the new chosen square
}
//if the square is empty then assign the X or O to that box
assignSymbol(row, column, playerCounter);
print the array;
isDraw = checkDraw();//check if game is draw
if(isDraw){//if it is draw
print "the game is draw"
print array;
break;//end of the game
}
victory = checkVictory();//check if one of the players has won
if(victory && playerCounter is odd){//if player1 has won
print "player1 has won"
print array
break;
}
else if(victory && playerCounter is even){//if player2 has won
print "player2 has won"
print array
break;
}
playerCounter++; increase the counter variable to change player's turn
}
function boolean checkSquare(int number1, int number2){
if(array[number1][number2] == EMPTY){
return false;
}
else{
return true;
}
}
function boolean checkDraw(){
if (all the squares are full){
return true;
}
else{
return false;
}
}
function boolean checkVictory(){
//check if anybody has won;
if(there are 3 X or O in horizontal, vertical, or diagonal row){
return true;
}
else{
return false;
}
}
function void assignSymbol(int number1, int number2, int counter){
if(counter is odd){//it's player1
array[number1][number2] = X;
}
else{//it's player2
array[number1][number2] = O;
}
}
/*END OF PSEUDOCODE*/
Could somebody kindly let me know if it all makes sense? Now, I know that there are probably better ways to do this, but I have to stick to the exercise as much as I can.
thanks