Domanda

I am developing a wordsearch generator to learn c++ better and I am stuck on preventing non-overlapping words from overlapping, such as a side-to-side word writing over a letter in a top-down word. Here is the code snippet:

else if (random_choice == 1 && random_word.size() <= 10-j && words_vector.size() != 0) {
    flag = true;
    for (int x = 0; x < random_word.size(); x++) {
        if (wordsearch[i][j+x] != '0') {
            flag = false;
            break;
        }
    }
    if (flag = true) {
        for (int x = 0; x < random_word.size(); x++) {
            wordsearch[i][j] = random_word[x];
            j += 1;
        }
        j -= 1;
        words_found_vector.insert(words_found_vector.begin(),words_vector[random_word_number]);
        //words_vector.erase(words_vector.begin()+random_word_number);
    }
    else {
        wordsearch[i][j] = '1';
    }
}

What I have done was create a two dimensional array [10][11] filled with the 0 (zero) character so when I iterate through it all spaces are filled with 0 except for the 11th space in each line with a newline character to make a 10X10 grid. In my else if loop, the first part already has a word chosen and it tests if the word will fit in its proper space by checking if a 0 is present. If it runs into a non-zero character (such as if it runs into a letter from a top-down or diagonal word) the inner loop terminates, sets the boolean flag, and inputs a 1 (or any random letter) instead of the whole word. What happens is that the whole word is inserted anyways and overwrites one letter from the top down word. What am I doing wrong? Here is the rest of the code:

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    srand(time(NULL));
    const char* const a_to_z = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;

    int random_char;

    char wordsearch [10][11] = {0};

    bool flag;

    string words_array[] = {"CAT", "HELLO", "GOODBYE", "DOG", "BAT", "NEW", "SAY", "MAY", "DAY", "HAY"};
    vector<string> words_vector (words_array, words_array + sizeof(words_array) / sizeof(string));

    string words_found_array[] = {};
    vector<string> words_found_vector (words_found_array, words_found_array + sizeof(words_found_array) / sizeof(string));

    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 11; j++) {
            int random_choice = rand() % 5;
            int random_word_number = rand() % words_vector.size();
            string random_word = words_vector[random_word_number];

            if (j == 10) {
                wordsearch[i][j] = '\n';
            }
            else if (random_choice == 1 && random_word.size() <= 10-j && words_vector.size() != 0) {
                flag = true;
                for (int x = 0; x < random_word.size(); x++) {
                    if (wordsearch[i][j+x] != '0') {
                        flag = false;
                        break;
                    }

                }
                if (flag = true) {
                    for (int x = 0; x < random_word.size(); x++) {
                        wordsearch[i][j] = random_word[x];
                        j += 1;
                    }
                    j -= 1;
                    words_found_vector.insert(words_found_vector.begin(),words_vector[random_word_number]);
                    //words_vector.erase(words_vector.begin()+random_word_number);
                }
                else {
                    wordsearch[i][j] = '1';
                }
            }
            else if (random_choice == 2 && random_word.size() <= 10-i && words_vector.size() != 0) {
                int temp_i = i;
                flag = true;
                for (int x = 0; x < random_word.size(); x++) {
                    if (wordsearch[i+x][j] != '0') {
                        flag = false;
                        break;
                    }
                }
                if (flag = true) {
                    for (int x = 0; x < random_word.size(); x++) {
                        wordsearch[i][j] = random_word[x];
                        i += 1;
                    }
                    i = temp_i;
                    words_found_vector.insert(words_found_vector.begin(),words_vector[random_word_number]);
                    //words_vector.erase(words_vector.begin()+random_word_number);
                }
                else {
                    wordsearch[i][j] = '1';
                }
            }
            else {
                int random_char = rand() % 26 + 0;
                wordsearch[i][j] = a_to_z[random_char];
            }
        }
    }
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 11; j++) {
                cout<<wordsearch[i][j];
        }
    }

    cout<<"Your words are:"<<endl;
    for (int x = 0; x < words_found_vector.size(); x++) {
        cout<<words_found_vector[x]<<endl;
    }
}

One more thing:

//words_vector.erase(words_vector.begin()+random_word_number);

crashes my program. I think it is a scoping issue with this:

int random_choice = rand() % 5;
int random_word_number = rand() % words_vector.size();
string random_word = words_vector[random_word_number];

What I want to do is eventually have the user give me a list of words they want to search for and this function chooses some of them and presents it to the user when playing the game. This not functioning correctly also causes duplicates to appear in the crossword and words-to-find-list.

Thank you for your help!

È stato utile?

Soluzione

You have this error twice in your code:

if (flag = true)

That is not a condition, it's an assignment. It assigns true to flag, and the if-block will always execute. You need to make it a comparison condition by using ==

if (flag == true)

A more common way to write that in C++ would be just

if (flag)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top