Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
161 views
Welcome To Ask or Share your Answers For Others

1 Answer

When you decide to place the word in a location then first scan everywhere you are placing the word and see if there is another word. If there is then abort and try a new location.

You might want to have some sort of "reset" option as well if it fails too often just in case there is no valid place.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...