In this function the program gets the four random words generated previously from a list and randomly puts them in the 10x10 matrix.
The problem is that sometimes words from horizontal/vertical/slanting overlap each other (however words from the same category don't). I don't want them to overlap in this way
void outputMatrix() {
char matrix[10][10];
for (int i = 0; i<10; i++) {
for (int j = 0; j<10; j++) {
matrix[i][j] = '-';
}
}
getRandomWord();
srand(time(NULL));
int randPosY = (rand() % 9);
int randPosX = (rand() % 9);
int incr = 0;
bool amend;
char r;
for (int x = 0; x < 4; x++) {
char randCase = (rand() % 3) + 65;
switch (randCase) {
case 'A': //to input words horizontally
if (incr == 1) {
r1 = r2;
}
if (incr == 2) {
r1 = r3;
}
if (incr == 3) {
r1 = r4;
}
reCheck:
amend = false;
if ((randPosX + strlen(dictionary[r1]) > 10)) {
randPosX--;
goto reCheck;
}
for (int i = 0; i < strlen(dictionary[r1]); i++) {
matrix[randPosY][randPosX + i] = dictionary[r1][i];
}
for (int i = 0; i < strlen(dictionary[r1]); i++) {
reRand:
if (matrix[randPosY][randPosX + i] != '-') {
randPosY = (rand() % 9);
randPosX = (rand() % 9);
goto reRand;
amend = true;
}if ((i == (strlen(dictionary[r1]) - 1)) && (amend == true)) goto reCheck;
}
break;
case 'B': //to input words vertically
if (incr == 1) {
r1 = r2;
}
if (incr == 2) {
r1 = r3;
}
if (incr == 3) {
r1 = r4;
}
reCheck2:
amend = false;
if ((randPosY + strlen(dictionary[r1]) > 10)) {
randPosY--;
goto reCheck2;
}
for (int i = 0; i < strlen(dictionary[r1]); i++) {
matrix[randPosY + i][randPosX] = dictionary[r1][i];
}
for (int i = 0; i < strlen(dictionary[r1]); i++) {
reRand2:
if (matrix[randPosY + i][randPosX] != '-') {
randPosY = (rand() % 9);
randPosX = (rand() % 9);
goto reRand2;
amend = true;
}if ((i == (strlen(dictionary[r1]) - 1)) && (amend == true)) goto reCheck2;
}
break;
case 'C': //to input words slanting
if (incr == 1) {
r1 = r2;
}
if (incr == 2) {
r1 = r3;
}
if (incr == 3) {
r1 = r4;
}
reCheck3:
amend = false;
if ((randPosY + strlen(dictionary[r1]) > 10)) {
randPosY--;
goto reCheck3;
}
reCheck4:
if ((randPosX + strlen(dictionary[r1]) > 10)) {
randPosX--;
goto reCheck4;
}
for (int i = 0; i < strlen(dictionary[r1]); i++) {
matrix[randPosY + i][randPosX + i] = dictionary[r1][i];
}
for (int i = 0; i < strlen(dictionary[r1]); i++) {
reRand3:
if (matrix[randPosY + i][randPosX + i] != '-') {
randPosY = (rand() % 9);
randPosX = (rand() % 9);
amend == true;
goto reRand3;
}if ((i == (strlen(dictionary[r1]) - 1)) && (amend == true)) goto reCheck3;
}
break;
} incr++;
}
//for (int i = 0; i<10; i++) {
//for (int j = 0; j < 10; j++) {
//if (matrix[i][j] == '-') {
//r = (rand() % 26) + 65;
//matrix[i][j] = r;
//}
//}
//}
printf("
A B C D E F G H I J
");
for (int a = 0; a <10; a++) {
printf("%d", a);
printf(" ");
for (int b = 0; b < 10; b++) {
printf("%c ", matrix[a][b]);
}
printf("
");
}
}
See Question&Answers more detail:os