Hey guys. I have a tic tac toe game that plots random X's and O's on a 2d matrix array called theBoard[row][col].

I want to check to see if one X is placed on 1 spot, and if another point is plotted in the same spot, to check if it is occupied or empty, and whether to put a piece down.

Mostly I want to know how to check if char[][] theBoard is empty.

Here is the code I have so far.

Random myRand = new Random();

        int myRow = myRand.nextInt(3);
        int myCol = myRand.nextInt(3);       
            
                
            if (theBoard[myRow][myCol]==theBoard[' '][' ']){// if the array is empty, go ahead and mark myPiece
                  theBoard[myRow][myCol] = myPiece; //this marks myPiece.
            }
            else {
                //otherwise, do nothing.
            }

When I run this, oddly I receive an array out of bounds error...
char[][] theBoard is set to length 3...so I don't know why I am getting this error.

You can only refer to array indexes with integers, not ' ' as you have done. To see if an array is completely empty, you need to use a for loop. I believe array elements are automatically initialized to null, so if you want to check if some element of a 2d array is null try something like this. Oh, and information on arrays is pretty easily found on google. Try it.

if (array[0][0] == null) // put your piece in the array!

well i cant use null. Its a char array. null does not work with char arrays...

indeed it doesn't.
There is no such concept as an "empty array" of primitives.
Every element of an array is always initialised to something.
In case of object types, that's null.
In case of primitive types, that's 0 (zero).

Sorry man, I knew that, but I was thinking about Objects, not about primitive types. Anyway, what I said about only using integers isn't entirely true. You can index the array based on a char but really it is going to convert it into an int first. For example:

char[] chars = new char[1000];
		int theAchar = 'a';
		chars[theAchar] = 'b';
		System.out.println(chars['a']);

Of course, this isn't really helpful for your application whatsoever, especially what you mentioned about checking to see if the board was blank. Again, sorry about the prior misinformation.

So should
I have the if statement check if theBoard[myRow][myCol]==theBoard. I just want to check if the spot that the random array is going to is taken or not

No. Re-read, then run, my previous example. If you try to check theBoard it is going to convert X and O to their integer representations, then it is going to look at the board slot at those integer indexes. I think you have a fundamental misunderstanding of how arrays work.

theBoard[index][otherIndex] holds one character, not two. Therefore, in order to check if the board at indexes 1,2 is empty, and taking jwenting's advice, you would put "if theBoard[1][2] == 0". If you want to check multiple slots on the board, you need to use a for loop.

Oh ok. Yea sorry I'm still new to arrays. So can I use a for loop and use for theBoard[index][index] ==0? that will check if the array has a character there or not?

OK thanks. I think I've got it. Once a spot is chosen, either an 'X' or an 'O' gets put there. So if the spot on the array that the random generator is choosing, equals a an 'X' or an 'O'. Then don't do anything. Otherwise, put a piece down.

Random myRand = new Random();

        int myRow = myRand.nextInt(3);
        int myCol = myRand.nextInt(3);       
            
                
            if (theBoard[myRow][myCol] == 'X' || theBoard[myRow][myCol]=='O') {// if the array is not empty.
                   //do nothing.
           }
            else {
               theBoard[myRow][myCol] = myPiece;  //otherwise, place piece.
           }

Yes! Exactly. You might want to further modify that logic so that if the spot the random number generator is choosing is already taken, then you pick a new spot. You could use a while loop to do that. For example:

Random myRand = new Random();

        int myRow = myRand.nextInt(3);
        int myCol = myRand.nextInt(3);       
            
            while (theBoard[myRow][myCol] == 'X' || theBoard[myRow][myCol]=='O') {
               myRow = myRand.nextInt(3);
               myCol = myRand.nextInt(3);
           }

//At this point, it is guaranteed that theBoard[myRow][myCol] 
//contains neither an 'X' nor an 'O' (it is empty)

....

Keep in mind that if every spot on the board is taken, then the while loop above will never end. So you would also need to add some more logic to keep track of how many times a turn has been taken, and act appropriately (for example, print out 'draw') if the game is over.

Thank you. Yes there is more code that I didn't post that tells if someone wins or if it is a tie. Thanks again, I'll come back if I have any more questions

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.