Reputation: 143
In my program, I am trying to simulate 1000 games of randomized tic tac toe. The game is only being played nine times, probably due to the inner nested do-while loop. I;m not sure how to fix this, I have tried changing the inner do-while loop to a while loop, and the outer for loop to a while loop. I know it is probably a simple mistake, but I can't pinpoint where the mistake lies. Below is my code for these two loops. Thank you in advance for any help.
for (count = 0; count < 1001; count++) {
int movecount = 0;
int row, col;
int player = 1;
do {
//pick a row
row = r.nextInt(3);
//pick a col
col = r.nextInt(3);
//check if spot is empty
if (list[row][col]>0) {continue;}
//if empty, move current player there, add to count
list[row][col] = player;
if (CheckRowWin(player, list)) {
System.out.println("Player " + player + " won");
break;
} else {
System.out.println("Tie Game");
}
movecount++;
//switch player turn
player = 3 - player;
} while (movecount < 9);
}
Upvotes: 0
Views: 120
Reputation: 1413
Your outer loop is running 1001 times, it just doesn't appear to because there's nothing else in your outer loop aside from the do{}while()
which is running only nine times and printing out stuff.
for (count = 0; count < 1001; count++) {
int movecount = 0;
int row, col;
int player = 1;
do {
//pick a row
row = r.nextInt(3);
//pick a col
col = r.nextInt(3);
//check if spot is empty
if (list[row][col]>0) {continue;}
//if empty, move current player there, add to count
list[row][col] = player;
if (CheckRowWin(player, list)) {
System.out.println("Player " + player + " won");
break;
} else {
System.out.println("Tie Game");
}
movecount++;
//switch player turn
player = 3 - player;
} while (movecount < 9);
// don't forget to reset movecount
// so that the inner loop will run again
movecount = 0;
// clear the "board" for the next game
// note: using two nested loops is slow and inefficient
// but it goes along with the theme of learning loops
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
list[r][c] = 0;
}
}
}
Upvotes: 1