# Thread: Tic Tac Toe Problem

1. Member
Join Date
Oct 2009
Posts
5
Rep Power
0

## Tic Tac Toe Problem

I have created a Java Program to run tic tac toe between the computer and a user. The problem i have is the program overwrites a space when it is taken already and I can not figure out what to do to tell the program to skip that space if already taken.
Last edited by shtinky; 10-17-2009 at 06:18 AM. Reason: Code tags added to aid readability

2. Forget Java for a minute. Logically, what must be done to make sure that the computer doesn't select a spot already selected?

3. Member
Join Date
Oct 2009
Posts
5
Rep Power
0
It has to keep check of what spots are being used?

4. Originally Posted by shtinky
It has to keep check of what spots are being used?
Exactly. And where is this information stored in your program?

5. Member
Join Date
Oct 2009
Posts
5
Rep Power
0
Java Code:
```
/**
* Fill a given two-dimensional character array with a given
* character
*
* @param a  the array to fill
* @param c  the character to use
*/
public static void fill2D(char[][] a, char c) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = c;
}
}
}```

6. Well which class variable holds this information? What gets filled via the fill2D method?

7. Member
Join Date
Oct 2009
Posts
5
Rep Power
0
the board gets filled via fill2d... correct? the spaces in the board of 2d arrays
Last edited by shtinky; 10-17-2009 at 06:04 AM.

8. right. so you must check that the spot you want to fill isn't already occupied. You'll likely want to create a while loop to keep checking that spot until an empty one is found.

9. Member
Join Date
Oct 2009
Posts
5
Rep Power
0
where would i go inserting the while loop?

10. Originally Posted by shtinky
where would i go inserting the while loop?
Rather than just give you the answer for this, it is far better for you to think logically about your program and try to figure out where would be the best spot. If you apply yourself, you'll figure it out. Best of luck!