For a module in college I have to make a game. I don't have much experience with coding but basically the game is a 3x3 board, with numbers 0-8 and they are initially in a random order. The aim of the game is to move the '0' and get the board back in to order. I can't have duplicate numbers in the boards 2D array.
This is the code for my board constructor:
cout << "Enter your PlayerName for the leaderboard" << endl;
cin >> playerName;
limit = 10000;
int alreadySeen[9];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int r = rand() % 8 + 0;
bool inAlreadySeen = false;
while (inAlreadySeen == false) {
int k = 0;
for (; k < 9; k++) {
if (alreadySeen[k] == r) {
break;
}
}
if (k == 9) {
inAlreadySeen = true;
}
else {
r = rand() % 8 + 0;
}
}
board[i][j] = r;
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == 0) {
row = i;
column = j;
}
}
}
}
The output is giving me duplicate numbers. I think this is because I'm never actually adding anything to the alreadySeen array? But I have messed about with it, tried to add the random number to it and it doesnt seem to work. Can anyone tell me where I should add to the already seen array?
You don't want random numbers. You want numbers 0-8. One of each. What you do want to be random is their order. A deck of cards has one of each. The deck is shuffled. An example: