1,105,334 Community Members

Check if char array is empty

Member Avatar
jnthn205
Light Poster
35 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 

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!
Member Avatar
jnthn205
Light Poster
35 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
jwenting
duckman
8,330 posts since Nov 2004
Reputation Points: 1,399 [?]
Q&As Helped to Solve: 445 [?]
Skill Endorsements: 35 [?]
Infraction Points: 5
Team Colleague
 
0
 

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).

Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 

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.

Member Avatar
jnthn205
Light Poster
35 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 

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.

Member Avatar
jnthn205
Light Poster
35 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 

http://www.willamette.edu/~gorr/classes/cs231/lectures/chapter9/arrays2d.htm

Read ^. Why do you want to know if the entire array is empty? If you're playing tic tac toe, in order to put a piece somewhere, all that needs to be true is that the spot is empty. Not that the whole board is empty.

Member Avatar
jnthn205
Light Poster
35 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.
           }
Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 

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.

Member Avatar
jnthn205
Light Poster
35 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article