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

what you are writing seems to me more a mix between pseudo code and actual code.
IMHO, if you stick to regular pseudo code, it'll be easier to write the code for the game.

what you are writing seems to me more a mix between pseudo code and actual code.

I felt the exact same thing.

ok, fair enough, maybe I have been too descriptive. How about this?

create TicTacToe class  

create enumeration
    constant X
    constant O
    constant EMPTY

while boolean variable is not true
    if 1st player's turn
        print"Player 1's turn, enter the coordinates of the square";        
    else
        print"Player 1's turn, enter the coordinates of the square";

    check the status of the square
    while the square is not empty
        choose another square 
        check the status of the square

    if the square is empty 
        if it is player n1
            then assign X to the square
        else
            assign O to the square

    print the board (array)
    check if the game is draw
        if it is draw
            print "the game is draw"
            print the board (array)
            break;
    check if any player has won
        if player1 has won
            print "player1 has won"
            print array
            break;
        else
            print "player2 has won"
            print array
            break;
    change the player's turn

thanks

well, much better.
and you used indentation as well, making it even easier.

all the blocks that start on the same level, for instance:

check if any player has won
        if player1 has won
            print "player1 has won"
            print array
            break;
        else
            print "player2 has won"
            print array
            break;
    change the player's turn

here you can see distinct blocks, that you can write one by one.
for instance:

public void playerWon(String playerName){
  System.out.println(playerName has won");
  // code to print the array
  break;
  }

and your code would start to be something like:

check if any player has won
        if player1 has won
            playerWon("Player 1");
        else
            playerWon("Player 2");
    change the player's turn

try and see first what are distinctive and repeating code, and write those as methods first, it'll spare you a lot of work.

thanks stultuske,
I must admit writing pseudocode isn't really something I am terribly good at : - ). So, just so I understand it correctly, are you suggesting to turn the pseudocode slowly into proper code? But then wouldn't I end up with what I posted first, a mixture of pseudocode and normal code?

The idea of pseudo-code is to capture/plan ypur logic without getting buried in the details of Java syntax or API method signatures. If you were writing this code for a non-programmer end user you may even be able to go through bits of the pseudo-code with him/her to confirm that you have understood the requirement properly. From your viewpoint it should be at a level of detail where you are confident that you can convert it to real code without having to re-think the overall logic.
So in some places the pseudo-code could represent a lot of simple java code:
prompt the user and get/validate name, birthday, employer, salary
but if it's some very convoluted data structure algorithm the pseudo-code may look very much like Java loops & snytax.

ok I see, so in other words what you stultuske were saying is that I need to refine the code a bit more before I start coding, correct?

Oh one thing I wanted to ask and I forgot. In this program I will have to use a class and an enumeration. Usually I have 2 java files, one for the class and one for the program to test the class: should I have 3 files this time (the 2 above plus the enumeration) or can an enumeration be inside a class?
thanks

try and see first what are distinctive and repeating code, and write those as methods first, it'll spare you a lot of work.

I did that stultuske and I came up with this code which has the class and a few methods (some of them you will find them in my original post):

/*TicTacToe.java*/
public class TicTacToe{
    private string board[];//ticTactToe 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 int playerCounter = 1;//if it is odd it's player1's turn if even it's player2's turn
    private int row;//row of the 3-by-3 array
    private int column;//column of the 3-by-3 array

    function boolean checkSquare(int number1, int number2){
        if(array[number1][number2] == EMPTY){
            return false;
        }
        else{
            return true;
        }       
    }
    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;        
        }
    }
    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;
        }       
    }
}//end of class

-checkSquare() checks that the selected box is empty (passing the 2 parameters inserted by the user);
-assignSymbol() fills up a square with the chosen symbol X or O;
-checkDraw() checks that the array is full (not sure yet how I will do that);
-checkVictory checks which player has won (again not entirely sure how I will do that)

Then I have a separate file with the enumeration in:

/*TicTacToe.java*/
public enum TicTacToeEn{
    X, O, EMPTY
}//end of enum

Is it ok for me to keep coding? One thing, if I have an enumeration TicTacToeEn{} the array I presume should be of type TicTacToeEn: private TicTacToeEn[][] board = new TicTacToeEn[3][3]; The thing is though, where should I declare it, in the enum or in the class?
thanks

This article has been dead for over six months. Start a new discussion instead.