Crossword

Joined
May 11, 2020
Messages
15
Reaction score
0
Hello I posted in the wrong place I think the last time I tried to make a crossword puzzle generator in C++. I manage to crosswords most of the time but I'd like to make it recursive.
Code:
#include <iostream>
#include <string>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <sstream>
#include <ctime>
#include <vector>
using namespace std;
std::vector<double> coord_x;
std::vector<double> coord_y;
std::vector<std::string > commonchar;
char grid[26][26] = {
        {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' }
};

int random(int from, int to)
{
    return rand() % (to - from + 1) + from;
}

std::string find_intersection(std::string &first, std::string &second)
{
    std::sort(first.begin(), first.end());
    std::sort(second.begin(), second.end());
    int length = std::min(first.length(), second.length());
    std::string result(length, ' ');
    std::set_intersection(first.begin(), first.end(), second.begin(), second.end(), result.begin());
    return result;
}
int PlaceHorizontal(int x, int y, std::string &word)
{
    coord_x.push_back(x);
    coord_y.push_back(y);
    int taille = word.length();
    for (int j = 0; j != taille; j++)
    {
        if (grid[x][y + j] == '.' || grid[x][y + j] == word[j])
        {
            grid[x][y + j] = word[j];
        }
        else {}
    }
}
int PlaceVertical(int x, int y, std::string &word)
{
    coord_x.push_back(x);
    coord_y.push_back(y);
    int taille = word.length();
    for (int j = 0; j != taille; j++)
    {
        if (grid[x + j][y] == '.' || grid[x + j][y] == word[j])
        {
            grid[x + j][y] = word[j];
        }
        else {}
    }
}
std::string randomword(int randomline)
{

    std::string word1;
    std::string ifileName = "dicoFrancais.txt";
    std::ifstream f(ifileName);

    if (!f) std::cerr << "File '" << ifileName << "' couldn't opened!\n";

    std::string s;

    for (int i = 1; i <= randomline; i++)
    {
        std::getline(f, s);
        word1 = s;
    }
    return word1;
}

int main(int argc, char *argv[])
{
    string word1;
    string word2;
    string word3;
    string word4;
    string word5;
    string word6;
    string word7;

    int x_word1_intersection;
    int x_word2_intersection;
    int x_word23_intersection;
    int x_word32_intersection;
    int x_word13_intersection;
    int x_word31_intersection;
    int x_word34_intersection;
    int x_word43_intersection;
    int x_word45_intersection;
    int x_word54_intersection;
    int x_word56_intersection;
    int x_word65_intersection;
    int x_word67_intersection;
    int x_word76_intersection;
    srand(time(0));

    int rand1 = random(1000, 2000);
    word1 = randomword(rand1);

    int rand2 = random(1000, 2000);
    word2 = randomword(rand2);

    int rand3 = random(1000, 2000);
    word3 = randomword(rand3);

    int rand4 = random(1000, 2000);
    word4 = randomword(rand4);

    int rand5 = random(1000, 2000);
    word5 = randomword(rand5);

    int rand6 = random(1000, 2000);
    word6 = randomword(rand6);

    int rand7 = random(1000, 2000);
    word7 = randomword(rand7);

    std::cout << "Word-1 : " << word1 << "\n";
    std::cout << "Word-2 : " << word2 << "\n";
    std::cout << "Word-3 : " << word3 << "\n";
    std::cout << "Word-4 : " << word4 << "\n";
    std::cout << "Word-5 : " << word5 << "\n";
    std::cout << "Word-6 : " << word6 << "\n";
    std::cout << "Word-7 : " << word7 << "\n";
    int x = 8;
    int y = 8;
    int indice1 = 0;
    int indice2 = 0;
    int indice3 = 0;
    int indice4 = 0;
    int indice5 = 0;
    int indice6 = 0;

    PlaceHorizontal(x, y, word1);
    std::string temp1 = word1;
    std::string temp2 = word2;
    std::string intersec = find_intersection(temp1, temp2);

    x_word1_intersection = word1.find_last_of(intersec[0]);
    x_word2_intersection = word2.find_last_of(intersec[0]);
    PlaceVertical(coord_x[0] - x_word2_intersection, coord_y[0] + x_word1_intersection, word2);

    std::string temp3 = word3;
    std::string intersec1 = find_intersection(temp2, temp3);
    if (intersec[0] = intersec1[0] && intersec1.length() > 0)
    {
        if (indice1 += 1 <= intersec1.length())
        {
            indice1 += 1;
        }
    }
    else
    {
        indice1 = 0;
    }
    x_word23_intersection = word2.find_last_of(intersec1[indice1]);
    x_word32_intersection = word3.find_last_of(intersec1[indice1]);

    PlaceHorizontal(coord_x[1] + x_word23_intersection, coord_y[1] - x_word32_intersection, word3);

    std::string temp4 = word4;
    std::string intersec2 = find_intersection(temp3, temp4);
    if (intersec1[indice1] = intersec2[0])
    {
        if (indice2 += 1 <= intersec2.length())
        {
            indice2 += 1;
        }
    }
    else
    {
        indice2 = 0;
    }
    x_word34_intersection = word3.find_last_of(intersec2[indice2]);
    x_word43_intersection = word4.find_last_of(intersec2[indice2]);

    PlaceVertical(coord_x[2] - x_word43_intersection, coord_y[2] + x_word34_intersection, word4);

    std::string temp5 = word5;
    std::string intersec3 = find_intersection(temp4, temp5);
    if (intersec2[indice2] = intersec3[0])
    {
        if (indice3 += 1 <= intersec3.length())
        {
            indice3 += 1;
        }
    }
    else
    {
        indice3 = 0;
    }
    x_word45_intersection = word4.find_last_of(intersec3[indice3]);
    x_word54_intersection = word5.find_last_of(intersec3[indice3]);

    PlaceHorizontal(coord_x[3] + x_word45_intersection, coord_y[3] - x_word54_intersection, word5);

    std::string temp6 = word6;
    std::string intersec4 = find_intersection(temp5, temp6);
    if (intersec3[indice3] = intersec4[0])
    {
        if (indice4 += 1 <= intersec4.length())
        {
            indice4 += 1;
        }
    }
    else
    {
        indice4 = 0;
    }
    x_word56_intersection = word5.find_last_of(intersec4[indice4]);
    x_word65_intersection = word6.find_last_of(intersec4[indice4]);

    PlaceVertical(coord_x[4] - x_word65_intersection, coord_y[4] + x_word56_intersection, word6);

    std::string temp7 = word7;
    std::string intersec5 = find_intersection(temp6, temp7);
    if (intersec4[indice4] = intersec5[0])
    {
        if (indice5 += 1 <= intersec5.length())
        {
            indice5 += 1;
        }
    }
    else
    {
        indice5 = 0;
    }
    x_word67_intersection = word6.find_last_of(intersec5[indice5]);
    x_word76_intersection = word7.find_last_of(intersec5[indice5]);
    PlaceHorizontal(coord_x[5] + x_word45_intersection, coord_y[5] - x_word54_intersection, word7);

    for (int i = 0; i < 26; i++)
    {
        for (int j = 0; j < 26; j++)
        {
            std::cout << grid[i][j];
            std::cout << " ";
        }
        std::cout << "\n";
    }
}

I thought of alternating the meaning of the words (vertical / horizontal) with even and odd numbers with a loop like this one but I couldn't figure out how to do it. Can you help me?

Code:
#include <iostream>

using namespace std;

int main()
{
    //declare variables
    int number;
    int n;

    cout << "Enter value less than 100: ";
    cin >> n; //take user input

    // print odd values
    cout << "The odd numbers are:";
    for (number = n + 1 - (n % 2); number <= 100; number += 2)
    {
        cout << " " << number;
    }
    cout << endl;

    // print even values
    cout << "The even numbers are:";
    for (number = n + (n % 2); number <= 100; number += 2)
    {
        cout << " " << number;
    }
    cout << endl;

    return 0; //end of program
}
 
Joined
May 11, 2020
Messages
15
Reaction score
0
Good morning, I figured it out tonight. It's great. There's still a few misspelled words, but the idea is there. I hope that some beginners will be able to get some inspiration from it because this code required a lot of thought from me, I'm not an expert either.
Code:
#include <iostream>
#include <string>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <sstream>
#include <ctime>
#include <vector>
using namespace std;
std::vector<double> coord_x;
std::vector<double> coord_y;
std::vector<std::string > words_hori;
std::vector<std::string > words_verti;
std::vector<std::string > words;
std::vector<double> intersections;
char grid[26][26] = {
        {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' }
};

int random(int from, int to)
{
    return rand() % (to - from + 1) + from;
}

std::string find_intersection(std::string &first, std::string &second)
{
    std::sort(first.begin(), first.end());
    std::sort(second.begin(), second.end());
    int length = std::min(first.length(), second.length());
    std::string result(length, ' ');
    std::set_intersection(first.begin(), first.end(), second.begin(), second.end(), result.begin());
    return result;
}

int PlaceHorizontal(int x, int y, std::string &word)
{
    coord_x.push_back(x);
    coord_y.push_back(y);
    int taille = word.length();
    for (int j = 0; j != taille; j++)
    {
        if (grid[x][y + j] == '.' || grid[x][y + j] == word[j])
        {
            grid[x][y + j] = word[j];
        }
        else {}
    }
}

int PlaceVertical(int x, int y, std::string &word)
{
    coord_x.push_back(x);
    coord_y.push_back(y);
    int taille = word.length();
    for (int j = 0; j != taille; j++)
    {
        if (grid[x + j][y] == '.' || grid[x + j][y] == word[j])
        {
            grid[x + j][y] = word[j];
        }
        else {}
    }
}

std::string randomword(int randomline)
{
    std::string word1;
    std::string ifileName = "dictionary.txt";
    std::ifstream f(ifileName);

    if (!f) std::cerr << "File '" << ifileName << "' couldn't opened!\n";

    std::string s;

    for (int i = 1; i <= randomline; i++)
    {
        std::getline(f, s);
        word1 = s;

    }

    return word1;
}

int main(int argc, char *argv[])
{
    string word1;
    string word2;
    string word3;
    string word4;
    string word5;
    string word6;
    string word7;
    std::string temp1;
    std::string temp2;
    std::string temp3;
    std::string temp4;
    std::string temp5;
    int x_word1_intersection;
    int x_word2_intersection;
    int x_word23_intersection;
    int x_word32_intersection;
    int x_word13_intersection;
    int x_word31_intersection;
    int x_word34_intersection;
    int x_word43_intersection;
    int x_word45_intersection;
    int x_word54_intersection;
    int x_word56_intersection;
    int x_word65_intersection;
    int x_word67_intersection;
    int x_word76_intersection;
    srand(time(0));
    int x = 8;
    int y = 8;
    int indice1 = 0;
    int indice2 = 0;
    int indice3 = 0;
    int indice4 = 0;
    int indice5 = 0;
    int indice6 = 0;
    std::string intersec1;
    std::string intersec;
    std::string intersec2;

    int n;
    std::cout << "Number of words to be placed in the grid : " << "\n";
    std::cin >> n;
    int number;

    for (int i = 0; i < n; i++)
    {
        int rand1 = random(1000, 2000);
        word1 = randomword(rand1);
        words.push_back(word1);

    }

    PlaceVertical(x, y, words[0]);

    for (int i = 1; i < words.size(); i++)
    {
        if (i % 2 == 0)
        {
            temp1 = words[i - 1];

            intersec = find_intersection(temp1, temp2);
            temp2 = words[i];
            word2 = words[i];

            words_hori.push_back(word2);
            if (i < 2)
            {
                indice2 = 0;
            }
            else
            {
                if (intersec[0] = intersec1[0] && intersec.length() > 0)
                {
                    if (indice1 += 1 <= intersec.length())
                    {
                        indice2 += 1;
                    }
                }
                else
                {
                    indice2 = 0;
                }
            }

            x_word1_intersection = word1.find_last_of(intersec[indice2]);
            x_word2_intersection = word2.find_last_of(intersec[indice2]);

            PlaceHorizontal(coord_x[i - 1] + x_word2_intersection, coord_y[i - 1] - x_word1_intersection, word2);
        }

        if (i % 2 != 0)
        {
            temp2 = words[i - 1];
            temp3 = words[i];
            word3 = words[i];
            words_verti.push_back(word3);

            intersec1 = find_intersection(temp2, temp3);
            if (intersec[0] = intersec1[0] && intersec1.length() > 0)
            {
                if (indice1 += 1 <= intersec1.length())
                {
                    indice1 += 1;
                }
            }
            else
            {
                indice1 = 0;
            }

            x_word23_intersection = word2.find_last_of(intersec1[indice1]);
            x_word32_intersection = word3.find_last_of(intersec1[indice1]);

            PlaceVertical(coord_x[i - 1] - x_word23_intersection, coord_y[i - 1] + x_word32_intersection, word3);
        }
    }

    for (int i = 0; i < 26; i++)
    {
        for (int j = 0; j < 26; j++)
        {
            std::cout << grid[i][j];
            std::cout << " ";
        }

        std::cout << "\n";
    }

    std::cout << "Words placed horizontally : " << "\n";
    for (int i = 1; i < words_hori.size(); i++)
    {
        std::cout << i << ": " << words_hori[i] << "\n";
    }

    std::cout << "Words placed vertically : " << "\n";
    for (int i = 1; i < words_verti.size(); i++)
    {
        std::cout << i << ": " << words_verti[i] << "\n";
    }
}
 
Joined
May 11, 2020
Messages
15
Reaction score
0
Hello, there is still a lot of error, I think that the loop that alternates (even number and odd number) vertical and horizontal placement does not totally solve the recursivity problem. I think that the common letter detection is not done correctly. Then the placement loop does not check the grid cells correctly and makes the errors persist despite the if loop. If you could help me that would be nice. Thanks again.
 
Joined
May 11, 2020
Messages
15
Reaction score
0
Code:
#include <iostream>
#include <string>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <sstream>
#include <ctime>
#include <vector>
using namespace std;
std::vector<double> coord_x;
std::vector<double> coord_y;
std::vector<std::string > words_hori;
std::vector<std::string > words_verti;
std::vector<std::string > words;

char grid[26][26] = {
        {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' }
};

int random(int from, int to)
{
    return rand() % (to - from + 1) + from;
}

std::string find_intersection(std::string &first, std::string &second)
{
    std::sort(first.begin(), first.end());
    std::sort(second.begin(), second.end());
    int length = std::min(first.length(), second.length());
    std::string result(length, ' ');
    std::set_intersection(first.begin(), first.end(), second.begin(), second.end(), result.begin());
    return result;
}

int PlaceHorizontal(int x, int y, std::string &word)
{
    coord_x.push_back(x);
    coord_y.push_back(y);
    int taille = word.length();
    for (int j = 0; j != taille; j++)
    {
        if (grid[x][y + j] == '.' || grid[x][y + j] == word[j])
        {
            grid[x][y + j] = word[j];
        }
        else {}
    }
}

int PlaceVertical(int x, int y, std::string &word)
{
    coord_x.push_back(x);
    coord_y.push_back(y);
    int taille = word.length();
    for (int j = 0; j != taille; j++)
    {
        if (grid[x + j][y] == '.' || grid[x + j][y] == word[j])
        {
            grid[x + j][y] = word[j];
        }
        else {}
    }
}

std::string randomword(int randomline)
{
    std::string word1;
    std::string ifileName = "dictionary.txt";
    std::ifstream f(ifileName);

    if (!f) std::cerr << "File '" << ifileName << "' couldn't opened!\n";

    std::string s;

    for (int i = 1; i <= randomline; i++)
    {
        std::getline(f, s);
        word1 = s;

    }

    return word1;
}

int main(int argc, char *argv[])
{
    string word1;
    string word2;
    string word3;

    std::string temp1;
    std::string temp2;
    std::string temp3;
    int x_word1_intersection;
    int x_word2_intersection;
    int x_word23_intersection;
    int x_word32_intersection;

    srand(time(0));
    int x = 8;
    int y = 8;
    int indice1 = 0;
    int indice2 = 0;

    std::string intersec1;
    std::string intersec;
    std::string replace;

    int n;
    std::cout << "Number of words to be placed in the grid : " << "\n";
    std::cin >> n;
    int number;

    for (int i = 0; i < n; i++)
    {
        int rand1 = random(2000, 4000);
        word1 = randomword(rand1);
        words.push_back(word1);

    }

    PlaceVertical(x, y, words[0]);

    for (int i = 1; i < words.size(); i++)
    {
        if (i % 2 == 0)
        {
            temp1 = words[i - 1];
            temp2 = words[i];
            word2 = words[i];

            intersec = find_intersection(temp1, temp2);
            if (intersec.empty())
            {
                int rand2 = random(2000, 4000);
                replace = randomword(rand2);
                word2 = replace;
                std::replace(words.begin(), words.end(), temp2, replace);
                intersec = find_intersection(temp1, temp2);
            }

            words_hori.push_back(word2);
            if (i < 2)
            {
                indice2 = 0;
            }
            else
            {
                if (intersec[0] = intersec1[0] && intersec.length() > 0)
                {
                    if (indice1 += 1 <= intersec.length())
                    {
                        indice2 += 1;
                    }
                }
                else
                {
                    indice2 = 0;
                }
            }

            x_word1_intersection = word1.find_last_of(intersec[indice2]);
            x_word2_intersection = word2.find_last_of(intersec[indice2]);

            PlaceHorizontal(coord_x[i - 1] + x_word2_intersection, coord_y[i - 1] - x_word1_intersection, word2);
        }

        if (i % 2 != 0)
        {
            temp2 = words[i - 1];
            temp3 = words[i];
            word3 = words[i];
            words_verti.push_back(word3);

            intersec1 = find_intersection(temp2, temp3);
            if (intersec1.empty())
            {
                int rand3 = random(2000, 4000);
                replace = randomword(rand3);
                word3 = replace;
                std::replace(words.begin(), words.end(), temp3, replace);
                intersec1 = find_intersection(temp2, temp3);
            }

            if (intersec[0] = intersec1[0] && intersec1.length() > 0)
            {
                if (indice1 += 1 <= intersec1.length())
                {
                    indice1 += 1;
                }
            }
            else
            {
                indice1 = 0;
            }

            x_word23_intersection = word2.find_last_of(intersec1[indice1]);
            x_word32_intersection = word3.find_last_of(intersec1[indice1]);

            PlaceVertical(coord_x[i - 1] - x_word23_intersection, coord_y[i - 1] + x_word32_intersection, word3);
        }
    }

    for (int i = 0; i < 26; i++)
    {
        for (int j = 0; j < 26; j++)
        {
            std::cout << grid[i][j];
            std::cout << " ";
        }

        std::cout << "\n";
    }

    std::cout << "Words placed horizontally : " << "\n";
    for (int i = 1; i < words_hori.size(); i++)
    {
        std::cout << i << ": " << words_hori[i] << "\n";
    }

    std::cout << "Words placed vertically : " << "\n";
    for (int i = 1; i < words_verti.size(); i++)
    {
        std::cout << i << ": " << words_verti[i] << "\n";
    }

    words.clear();
}
 
Joined
May 11, 2020
Messages
15
Reaction score
0
Hello, I've made some progress: I made a scoring of the places assigned to the current word and a search for the intersection letter if the score is bad.

Code:
#include <iostream>
#include <string>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <sstream>
#include <ctime>
#include <vector>
using namespace std;
std::vector<double> coord_x;
std::vector<double> coord_y;
std::vector<std::string > words;
//vector to know how the word is placed in the grid vertically or horizontally (v / h)
std::vector<std::string > place;
constexpr char empty = '.';
char grid[26][26] = {
        {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' }
};
//Random number generating function
int random(int from, int to)
{
    return rand() % (to - from + 1) + from;
}

//Function searching for letters common to two words
std::string find_intersection(std::string &first, std::string &second)
{
    std::sort(first.begin(), first.end());
    std::sort(second.begin(), second.end());
    int length = std::min(first.length(), second.length());
    std::string result(length, ' ');
    std::set_intersection(first.begin(), first.end(), second.begin(), second.end(), result.begin());
    return result;
}

//Horizontal word placement function
int PlaceHorizontal(int x, int y, int id_word, std::string &word)
{
    int x_word1_intersection;
    int x_word2_intersection;
    std::string intersec;
    std::string temp1 = word;
    std::string temp2;
    int taille = word.length();
    for (int j = 0; j != taille; j++)
    {
        char letter_in_grid = grid[x][y + j];
        if (letter_in_grid == empty || letter_in_grid == word[j])
        {
            grid[x][y + j] = word[j];
            coord_x.push_back(x);
            coord_y.push_back(y);
            place.push_back("h");
        }
        else {}
    }
}

//Function placing words vertically
int PlaceVertical(int x, int y, int id_word, std::string &word)
{
    int taille = word.length();
    for (int j = 0; j != taille; j++)
    {
        char letter_in_grid = grid[x + j][y];
        if (letter_in_grid == empty || letter_in_grid == word[j])
        {
            grid[x + j][y] = word[j];
            coord_x.push_back(x);
            coord_y.push_back(y);
            place.push_back("v");

        }
        else {}
    }
}
//function that assigns a score to the place chosen for the word
int can_place(int x, int y, int id_word, std::string &word)
{
    int score = 0;
    int taille = word.length();
    for (int j = 0; j != taille; j++)
    {
        if (place[id_word - 1] == "h")
        {
            char letter_in_grid = grid[x + j][y];
            if (letter_in_grid == empty || letter_in_grid == word[j])
            {
                score = score + 1;
            }
            else
            {
                score = score - 1;
            }
        }

        if (place[id_word - 1] == "v")
        {
            char letter_in_grid = grid[x][y + j];
            if (letter_in_grid == empty || letter_in_grid == word[j])
            {
                score = score + 1;
            }
            else
            {
                score = score - 1;
            }
        }
    }

    return score;

}

//Function randomly retrieving words from the dictionary
std::string randomword(int randomline)
{
    std::string word1;
    std::string ifileName = "dictionary.txt";
    std::ifstream f(ifileName);

    if (!f) std::cerr << "File '" << ifileName << "' couldn't opened!\n";

    std::string s;

    for (int i = 1; i <= randomline; i++)
    {
        std::getline(f, s);
        std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c)
        {
            return std::tolower(c);
    });
        word1 = s;

    }

    return word1;
}
int main(int argc, char *argv[])
{
    string word1;
    string word2;
    string word3;
    int score;
    int x_replace;
    int y_replace;
    std::string temp1;
    std::string temp2;
    std::string temp3;
    int x_word1_intersection;
    int x_word2_intersection;
    int x_word23_intersection;
    int x_word32_intersection;

    srand(time(0));
    int x = 8;
    int y = 8;
    int indice1 = 0;
    int indice2 = 0;

    std::string intersec1;
    std::string intersec;
    std::string replace;
    int n;
    int rand0 = random(4, 40);
    n = rand0;

    int number;
    //random word retrieval and addition to vector
    for (int i = 0; i < n; i++)
    {
        int rand1 = random(2000, 4000);
        word1 = randomword(rand1);
        words.push_back(word1);

    }

    //first word placement
    PlaceVertical(x, y, 0, words[0]);
    intersec1 = "";

    for (int i = 1; i < words.size(); i++)
    {
        temp1 = words[i - 1];
        temp2 = words[i];
        word2 = words[i];
        //if there is no common letter between the two words running to look for a new word
        intersec = find_intersection(temp1, temp2);
        if (intersec.empty())
        {
            int rand2 = random(2000, 4000);
            replace = randomword(rand2);
            word2 = replace;
            std::replace(words.begin(), words.end(), temp2, replace);
            intersec = find_intersection(temp1, temp2);
        }

        if (i < 2)
        {
            indice2 = 0;
        }
        else
        {
            //if the letters common to the two previous words are the same as for the two current words change the common letter if possible
            if (intersec[0] == intersec1[0] && intersec.length() > 0)
            {
                if (indice2 + 1 <= intersec.length())
                {
                    indice2 = indice2 + 1;
                }
            }
            else
            {
                indice2 = 0;
            }
        }

        x_word1_intersection = word1.find_last_of(intersec[indice2]);
        x_word2_intersection = word2.find_last_of(intersec[indice2]);
                //if the number i is even
        if (i % 2 == 0)
        {
            score = can_place(coord_x[i - 1] + x_word2_intersection, coord_y[i - 1] - x_word1_intersection, i, word2);
                        //if the score is equal to the size of the word place the word normally
            if (score == word2.length())
            {
                PlaceHorizontal(coord_x[i - 1] + x_word2_intersection, coord_y[i - 1] - x_word1_intersection, i, word2);
            }
                        //Otherwise look for the common letter in the grid and place the word if the score is equal to the size of the word.
            else
            {
                for (int i = 0; i < 26; i++)
                {
                    for (int j = 0; j < 26; j++)
                    {
                        if (grid[i][j] == intersec[indice2])
                        {
                            x_replace = i;
                            y_replace = j;
                        }
                    }
                }

                score = can_place(x_replace + x_word2_intersection, y_replace, i, word2);
                if (score == word2.length())
                {
                    PlaceHorizontal(x_replace + x_word2_intersection, y_replace, i, word2);
                }
            }
        }
                //if the number i is odd
        if (i % 2 != 0)
        {
            score = can_place(coord_x[i - 1] - x_word1_intersection, coord_y[i - 1] + x_word2_intersection, i, word2);
                        //if the score is equal to the size of the word place the word normally
            if (score == word2.length())
            {
                PlaceVertical(coord_x[i - 1] - x_word1_intersection, coord_y[i - 1] + x_word2_intersection, i, word2);
            }
                        //Otherwise look for the common letter in the grid and place the word if the score is equal to the size of the word.
            else
            {
                for (int i = 0; i < 26; i++)
                {
                    for (int j = 0; j < 26; j++)
                    {
                        if (grid[i][j] == intersec[indice2])
                        {
                            x_replace = i;
                            y_replace = j;
                        }
                    }
                }

                score = can_place(x_replace, y_replace + x_word2_intersection, i, word2);
                if (score == word2.length())
                {
                    PlaceVertical(x_replace, y_replace + x_word2_intersection, i, word2);
                }
            }
        }
    }

    //grid view
    for (int i = 0; i < 26; i++)
    {
        for (int j = 0; j < 26; j++)
        {
            std::cout << grid[i][j];
            std::cout << " ";
        }

        std::cout << "\n";
    }

    words.clear();

}
 
Joined
May 11, 2020
Messages
15
Reaction score
0
I think it works pretty well with a random number of words and the best placement possible. There are still some bugs, but it works overall.
Code:
#include <iostream>
#include <string>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <sstream>
#include <ctime>
#include <vector>
using namespace std;
std::vector<double> coord_x;
std::vector<double> coord_y;

std::vector<double> temporary_coord_x;
std::vector<double> temporary_coord_y;

std::vector<std::string > words;
//vector to know how the word is placed in the grid vertically or horizontally (v / h)
std::vector<std::string > place;
constexpr char empty = '.';
char grid[26][26] = {
        {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' }
};
//Random number generating function
int random(int from, int to)
{
    return rand() % (to - from + 1) + from;
}

//Function searching for letters common to two words
std::string find_intersection(std::string &first, std::string &second)
{
    std::sort(first.begin(), first.end());
    std::sort(second.begin(), second.end());
    int length = std::min(first.length(), second.length());
    std::string result(length, ' ');
    std::set_intersection(first.begin(), first.end(), second.begin(), second.end(), result.begin());
    return result;
}

//Horizontal word placement function
int PlaceHorizontal(int x, int y, int id_word, std::string &word)
{
    int x_word1_intersection;
    int x_word2_intersection;
    std::string intersec;
    std::string temp1 = word;
    std::string temp2;
    int taille = word.length();
    for (int j = 0; j <= taille; j++)
    {
        char letter_in_grid = grid[x][y + j];
        if (letter_in_grid == empty || letter_in_grid == word[j])
        {
            grid[x][y + j] = word[j];
            coord_x.push_back(x);
            coord_y.push_back(y);
            place.push_back("h");
        }
        else {}
    }
}

//Function placing words vertically
int PlaceVertical(int x, int y, int id_word, std::string &word)
{
    int taille = word.length();
    for (int j = 0; j <= taille; j++)
    {
        char letter_in_grid = grid[x + j][y];
        if (letter_in_grid == empty || letter_in_grid == word[j])
        {
            grid[x + j][y] = word[j];
            coord_x.push_back(x);
            coord_y.push_back(y);
            place.push_back("v");
        }
        else {}
    }
}
//function that assigns a score to the place chosen for the word
int can_place(int x, int y, int id_word, std::string &word)
{
    int score = 0;
    int taille = word.length();
    for (int j = 0; j != taille; j++)
    {
               //if the previous word is placed horizontally place the word vertically
        if (place[id_word - 1] == "h")
        {
            char letter_in_grid = grid[x + j][y];
            if (letter_in_grid == empty || letter_in_grid == word[j])
            {
                score = score + 1;
            }
            else
            {
                score = score - 1;
            }
        }
                //if the previous word is placed vertically place the word horizontally
        if (place[id_word - 1] == "v")
        {
            char letter_in_grid = grid[x][y + j];
            if (letter_in_grid == empty || letter_in_grid == word[j])
            {
                score = score + 1;
            }
            else
            {
                score = score - 1;
            }
        }
    }

    return score;

}

//Function randomly retrieving words from the dictionary
std::string randomword(int randomline)
{
    std::string word1;
    std::string ifileName = "dictionary.txt";
    std::ifstream f(ifileName);

    if (!f) std::cerr << "File '" << ifileName << "' couldn't opened!\n";

    std::string s;

    for (int i = 1; i <= randomline; i++)
    {
        std::getline(f, s);
        std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c)
        {
            return std::tolower(c);
    });
        word1 = s;
    }

    return word1;
}
int main(int argc, char *argv[])
{
    string word1;
    string word2;
    string word3;
    int score;
    int x_replace;
    int y_replace;
    std::string temp1;
    std::string temp2;
    std::string temp3;
    int x_word1_intersection;
    int x_word2_intersection;
    int x_word23_intersection;
    int x_word32_intersection;

    srand(time(0));
    int x = 8;
    int y = 8;
    int indice1 = 0;
    int indice2 = 0;

    std::string intersec1;
    std::string intersec;
    std::string replace;
    int n;
    int rand0 = random(4, 40);
    n = rand0;

    int number;
    //random word retrieval and addition to vector
    for (int i = 0; i < n; i++)
    {
        int rand1 = random(2000, 4000);
        word1 = randomword(rand1);
        words.push_back(word1);
    }

    //first word placement
    PlaceVertical(x, y, 0, words[0]);
    intersec1 = "";

    for (int i = 1; i < words.size(); i++)
    {
        temp1 = words[i - 1];
        temp2 = words[i];
        word2 = words[i];
        //if there is no common letter between the two words running to look for a new word
        intersec = find_intersection(temp1, temp2);
        if (intersec.empty())
        {
            int rand2 = random(2000, 4000);
            replace = randomword(rand2);
            word2 = replace;
            std::replace(words.begin(), words.end(), temp2, replace);
            intersec = find_intersection(temp1, temp2);
        }

        if (i < 2)
        {
            indice2 = 0;
        }
        else
        {
            //if the letters common to the two previous words are the same as for the two current words change the common letter if possible
            if (intersec[0] == intersec1[0] && intersec.length() > 0)
            {
                if (indice2 + 1 <= intersec.length())
                {
                    indice2 = indice2 + 1;
                }
            }
            else
            {
                indice2 = 0;
            }
        }

        x_word1_intersection = word1.find_last_of(intersec[indice2]);
        x_word2_intersection = word2.find_last_of(intersec[indice2]);
        //if the number i is even
        if (i % 2 == 0)
        {
            score = can_place(coord_x[i - 1] + x_word2_intersection, coord_y[i - 1] - x_word1_intersection, i, word2);
            //if the score is equal to the size of the word place the word normally
            if (score == word2.length())
            {
                PlaceHorizontal(coord_x[i - 1] + x_word2_intersection, coord_y[i - 1] - x_word1_intersection, i, word2);
            }
            //Otherwise search each letter of the word test the position and record the position
            else
            {
                for (int k = 0; k <= word2.length(); k++)
                {
                    for (int i = 0; i < 26; i++)
                    {
                        for (int j = 0; j < 26; j++)
                        {
                            if (grid[i][j] == word2[k])
                            {

                                temporary_coord_x.push_back(i);
                                temporary_coord_y.push_back(j);
                            }
                        }
                    }
                }
                //note all previously saved positions - if the score is equal to the size of the word place the word
                for (int l = 0; l != temporary_coord_x.size(); l++)
                {
                    score = can_place(temporary_coord_x[l] + x_word2_intersection, temporary_coord_y[l], i, word2);
                    if (score == word2.length())
                    {
                        x_replace = temporary_coord_x[l];
                        y_replace = temporary_coord_y[l];
                    }
                }
                PlaceHorizontal(x_replace + x_word2_intersection, y_replace, i, word2);
                temporary_coord_x.clear();
                temporary_coord_y.clear();
            }
        }
        //if the number i is odd
        if (i % 2 != 0)
        {
            score = can_place(coord_x[i - 1] - x_word1_intersection, coord_y[i - 1] + x_word2_intersection, i, word2);
            //if the score is equal to the size of the word place the word normally
            if (score == word2.length())
            {
                PlaceVertical(coord_x[i - 1] - x_word1_intersection, coord_y[i - 1] + x_word2_intersection, i, word2);
            }
            //Otherwise search each letter of the word test the position and record the position
            else
            {
                for (int k = 0; k <= word2.length(); k++)
                {
                    for (int i = 0; i < 26; i++)
                    {
                        for (int j = 0; j < 26; j++)
                        {
                            if (grid[i][j] == word2[k])
                            {
                                temporary_coord_x.push_back(i);
                                temporary_coord_y.push_back(j);
                            }
                        }
                    }
                }
                                //note all previously saved positions - if the score is equal to the size of the word place the word
                for (int l = 0; l != temporary_coord_x.size(); l++)
                {
                    score = can_place(temporary_coord_x[l], temporary_coord_y[l] + x_word2_intersection, i, word2);
                    if (score == word2.length())
                    {
                        x_replace = temporary_coord_x[l];
                        y_replace = temporary_coord_y[l];
                    }
                }
                PlaceVertical(x_replace, y_replace + x_word2_intersection, i, word2);
                temporary_coord_x.clear();
                temporary_coord_y.clear();
            }
        }
    }

    //grid view
    for (int i = 0; i < 26; i++)
    {
        for (int j = 0; j < 26; j++)
        {
            std::cout << grid[i][j];
            std::cout << " ";
        }

        std::cout << "\n";
    }

    words.clear();

}
 
Joined
May 11, 2020
Messages
15
Reaction score
0
Hello there's the end of the story!

Code:
#include <iostream>
#include <string>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <sstream>
#include <ctime>
#include <vector>
using namespace std;
std::vector<double> coord_x;
std::vector<double> coord_y;
std::vector<double> intersec_temp1; //I think it's unused.
std::vector<double> intersec_temp2; //I think it's unused.
std::vector<double> intersec_temp3; //I think it's unused.
std::vector<double> intersec_temp4; //I think it's unused.

//Vectors for recording word coordinates
std::vector<double> temporary_coord_x;
std::vector<double> temporary_coord_y;

std::vector<std::string > words;
//vector to know how the word is placed in the grid vertically or horizontally (v / h)
std::vector<std::string > place;
constexpr char empty = '.';
char grid[26][26] = {
        {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {
        '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' }
};
//Random number generating function
int random(int from, int to)
{
    return rand() % (to - from + 1) + from;
}

//Function searching for letters common to two words
std::string find_intersection(std::string &first, std::string &second)
{
    std::sort(first.begin(), first.end());
    std::sort(second.begin(), second.end());
    int length = std::min(first.length(), second.length());
    std::string result(length, ' ');
    std::set_intersection(first.begin(), first.end(), second.begin(), second.end(), result.begin());
    return result;
}

//Horizontal word placement function
int PlaceHorizontal(int x, int y, int id_word, std::string &word)
{
    int x_word1_intersection;
    int x_word2_intersection;
    std::string intersec;
    std::string temp1 = word;
    std::string temp2;
    int taille = word.length();
    for (int j = 0; j <= taille; j++)
    {
        char letter_in_grid = grid[x][y + j];
        if (letter_in_grid == empty || letter_in_grid == word[j])
        {
            grid[x][y + j] = word[j];
            coord_x.push_back(x);
            coord_y.push_back(y);
            place.push_back("h");
        }
        else {}
    }
}

//Function placing words vertically
int PlaceVertical(int x, int y, int id_word, std::string &word)
{
    int taille = word.length();
    for (int j = 0; j <= taille; j++)
    {
        char letter_in_grid = grid[x + j][y];
        if (letter_in_grid == empty || letter_in_grid == word[j])
        {
            grid[x + j][y] = word[j];
            coord_x.push_back(x);
            coord_y.push_back(y);
            place.push_back("v");
        }
        else {}
    }
}

//function that assigns a score to the place chosen for the word
int can_place(int x, int y, int id_word, std::string &word)
{
    int score = 0;
    int taille = word.length();
    for (int j = 0; j != taille; j++)
    {
        //if the previous word is placed horizontally place the word vertically
        if (place[id_word - 1] == "h")
        {
            char letter_in_grid_v = grid[x + j][y];
            if (letter_in_grid_v == empty || letter_in_grid_v == word[j])
            {
                score = score + 1;
            }
            else
            {
                score = score - 1;
            }
        }

        //if the previous word is placed vertically place the word horizontally
        if (place[id_word - 1] == "v")
        {
            char letter_in_grid_h = grid[x][y + j];
            if (letter_in_grid_h == empty || letter_in_grid_h == word[j])
            {
                score = score + 1;
            }
            else
            {
                score = score - 3;
            }
        }
    }

    return score;

}

//Function randomly retrieving words from the dictionary
std::string randomword(int randomline)
{
    std::string word1;
    std::string ifileName = "dictionary.txt";
    std::ifstream f(ifileName);

    if (!f) std::cerr << "File '" << ifileName << "' couldn't opened!\n";

    std::string s;

    for (int i = 1; i <= randomline; i++)
    {
        std::getline(f, s);
        std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c)
        {
            return std::tolower(c);
    });
        word1 = s;
    }

    return word1;
}

int main(int argc, char *argv[])
{
    string word1;
    string word2;
    string word3;
    int score;
    int x_replace;
    int y_replace;
    std::string temp1;
    std::string temp2;
    std::string temp3;
    int x_word1_intersection;
    int x_word2_intersection;
    int x_word23_intersection;
    int x_word32_intersection;

    srand(time(0));
    int x = 8;
    int y = 8;
    int indice1 = 0;
    int indice2 = 0;

    std::string intersec;
    std::string intersec1; //I think it's unused.
    std::string replace;

    //number of words to be placed in the grid
    int n;
    int rand0 = random(15, 40);
    n = rand0;

    int number;
    //random word retrieval and addition to vector
    for (int i = 0; i < n; i++)
    {
        //depending on the size of the dictionary
        int rand1 = random(2000, 4000);
        word1 = randomword(rand1);
        words.push_back(word1);
    }

    //first word placement
    PlaceVertical(x, y, 0, words[0]);

    for (int i = 1; i < words.size(); i++)
    {
        temp1 = words[i - 1];
        temp2 = words[i];
        word2 = words[i];

        intersec = find_intersection(temp1, temp2);
        while (intersec.empty())
        {
            int rand2 = random(2000, 4000);
            replace = randomword(rand2);
            word2 = replace;
            std::replace(words.begin(), words.end(), temp2, replace);
            intersec = find_intersection(temp1, temp2);
        }
        //for all intersecting letters, test the position
        for (int j = 0; j < intersec.length(); j++)
        {
            int temp_intersec_1 = word1.find_last_of(intersec[j]);
            int temp_intersec_2 = word2.find_last_of(intersec[j]);
            score = can_place(coord_x[i - 1] + temp_intersec_2, coord_y[i - 1] - temp_intersec_1, i, word2);
            if (score == word2.length())
            {
                x_word1_intersection = word1.find_last_of(intersec[j]);
                x_word2_intersection = word2.find_last_of(intersec[j]);
            }
        }
        //if the number i is odd
        if (i % 2 != 0)
        {
            score = can_place(coord_x[i - 1] + x_word2_intersection, coord_y[i - 1] - x_word1_intersection, i, word2);
            //if the score is equal to the size of the word place the word normally
            if (score == word2.length())
            {
                PlaceHorizontal(coord_x[i - 1] + x_word2_intersection, coord_y[i - 1] - x_word1_intersection, i, word2);
            }

            //Otherwise search each letter of the word test the position and record the position
            else
            {
                for (int k = 0; k <= word2.length(); k++)
                {
                    for (int i = 0; i < 26; i++)
                    {
                        for (int j = 0; j < 26; j++)
                        {
                            if (grid[i][j] == empty || grid[i][j] == word2[k])
                            {
                                temporary_coord_x.push_back(i);
                                temporary_coord_y.push_back(j);
                            }
                        }
                    }
                }

                //note all previously saved positions - if the score is equal to the size of the word place the word
                for (int l = 0; l != temporary_coord_x.size(); l++)
                {
                    score = can_place(temporary_coord_x[l] + x_word2_intersection, temporary_coord_y[l], i, word2);
                    if (score == word2.length())
                    {
                        x_replace = temporary_coord_x[l];
                        y_replace = temporary_coord_y[l];
                    }
                    else {}
                }
                //word placement
                PlaceHorizontal(x_replace + x_word2_intersection, y_replace, i, word2);
                temporary_coord_x.clear();
                temporary_coord_y.clear();
            }
        }
        //if the number i is even
        if (i % 2 == 0)
        {
            score = can_place(coord_x[i - 1] - x_word1_intersection, coord_y[i - 1] + x_word2_intersection, i, word2);
            //if the score is equal to the size of the word place the word normally
            if (score == word2.length())
            {
                PlaceVertical(coord_x[i - 1] - x_word1_intersection, coord_y[i - 1] + x_word2_intersection, i, word2);
            }

            //Otherwise search each letter of the word test the position and record the position
            else
            {
                for (int k = 0; k <= word2.length(); k++)
                {
                    for (int i = 0; i < 26; i++)
                    {
                        for (int j = 0; j < 26; j++)
                        {
                            if (grid[i][j] == empty || grid[i][j] == word2[k])
                            {
                                temporary_coord_x.push_back(i);
                                temporary_coord_y.push_back(j);
                            }
                        }
                    }
                }

                //note all previously saved positions - if the score is equal to the size of the word place the word
                for (int l = 0; l != temporary_coord_x.size(); l++)
                {
                    score = can_place(temporary_coord_x[l], temporary_coord_y[l] + x_word2_intersection, i, word2);
                    if (score == word2.length())
                    {
                        x_replace = temporary_coord_x[l];
                        y_replace = temporary_coord_y[l];
                    }
                    else {}
                }
                //word placement
                PlaceVertical(x_replace, y_replace + x_word2_intersection, i, word2);
                temporary_coord_x.clear();
                temporary_coord_y.clear();
            }
        }
    }

    //grid view
    for (int i = 0; i < 26; i++)
    {
        for (int j = 0; j < 26; j++)
        {
            std::cout << grid[i][j];
            std::cout << " ";
        }

        std::cout << "\n";
    }
    //cleaning the vector containing the words to prevent certain words
    //from remaining in memory the next time the program is executed
    words.clear();

}
 
Last edited:
Joined
May 11, 2020
Messages
15
Reaction score
0
I've had a lot of difficulty, but I hope this will help those who would like to look into the problem. Code one day always code !
 
Joined
Jun 12, 2020
Messages
12
Reaction score
0
Sorry, I lost my previous logins. My code has improved a lot. It works properly.

You can decrease the number of words to be placed in the grid by decreasing the range of this line at the beginning of the main function of the program :

int rand0 = random(20, 30);

And the range to choose a word at random or to replace a word must be adapted to the size of your dictionary:

int rand3 = random(100, 20000);

Indeed the dictionary I use is about 22000 lines which explains this line.
 
Joined
Jun 12, 2020
Messages
12
Reaction score
0
Code:
#include <iostream>
#include <string>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <sstream>
#include <ctime>
#include <vector>
using namespace std;

//Vectors for recording word coordinates
std::vector<double> coord_col;
std::vector<double> coord_row;
//Vectors for recording words
std::vector<std::string > words_vect;

constexpr char empty = '.';
constexpr int GridSize = 26;
char grid[GridSize][GridSize];

//Random number generating function
int random(int from, int to)
{
    return rand() % (to - from + 1) + from;
}

//Function searching for letters common to two words
std::string find_intersection(std::string first, std::string second)
{
    std::sort(first.begin(), first.end());
    std::sort(second.begin(), second.end());
    int length = std::min(first.length(), second.length());
    std::string result(length, ' ');
    std::set_intersection(first.begin(), first.end(), second.begin(), second.end(),
        result.begin());
    return result;
}

//Horizontal word placement function
void PlaceHorizontal(int col, int row, int id_word, std::string &word)
{
    coord_col.push_back(col);
    coord_row.push_back(row);
    unsigned taille = word.length();
    for (unsigned int j = 0; j < taille; j++)
    {
        grid[col][row + j] = word[j];
    }
}

//Function placing words vertically
void PlaceVertical(int col, int row, int id_word, std::string &word)
{
    coord_col.push_back(col);
    coord_row.push_back(row);
    unsigned taille = word.length();
    for (unsigned int j = 0; j < taille; j++)
    {
        grid[col + j][row] = word[j];
    }
}

//function that assigns a score to the place chosen for the word
bool can_place(int col, int row, int id_word, std::string &word)
{
    int score = 0;
    unsigned taille = word.length();

    if (id_word % 2 != 0)
    {
        if (col < 0 || col >= GridSize || row < 0 || row + taille >= GridSize) return false;
        for (unsigned int j = 0; j < taille; j++)
        {
            char word_letter = word[j];
            if (grid[col][row + j] == empty)
            {
                score = score + 1;
            }
            else if (grid[col][row + j] != word_letter)
            {
                return false;
            }
        }
    }
    else if (id_word % 2 == 0)
    {
        if (col < 0 || col + taille >= GridSize || row < 0 || row >= GridSize) return false;

        for (unsigned int j = 0; j < taille; j++)
        {
            char word_letter = word[j];
            if (grid[col + j][row] == empty)
            {
                score = score + 1;
            }
            else if (grid[col + j][row] != word_letter)
            {
                return false;
            }
        }
    }

    if (score < taille)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//Function randomly retrieving words from the dictionary
std::string randomword(int randomline)
{
    std::string word1;
    std::string ifileName = "dictionary.txt";
    std::ifstream f(ifileName);

    if (!f)
        std::cerr << "File '" << ifileName << "' couldn't opened!\n";

    std::string s;

    for (int i = 1; i < randomline; i++)
    {
        std::getline(f, s);
        std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c)
        {
            return std::tolower(c);
    });
        word1 = s;
    }

    return word1;
}

void replace_word(std::string &current_word)
{
    int rand3 = random(100, 20000);
    std::string replacement = randomword(rand3);
    std::replace(words_vect.begin(), words_vect.end(), current_word, replacement);

}

void showGrid()
{
    //grid view
    for (unsigned int i = 0; i < GridSize; i++)
    {
        for (int j = 0; j < GridSize; j++)
        {
            std::cout << grid[i][j];
            std::cout << " ";
        }

        std::cout << "\n";
    }
}

int Align(float value, float size)
{
    int pos = (size / 2) - value;
    return pos;
}

int main(int argc, char *argv[])
{
    srand(time(0));
    memset(grid, '.', sizeof(grid));
    bool place_v = false;
    bool place_h = false;
    int n;
    //number of words to place
    int rand0 = random(20, 30);
    int num = rand0;

    //position of the horizontal and vertical intersection letter
    int x_word2_intersection_h;
    int x_word1_intersection_h;
    int x_word2_intersection_v;
    int x_word1_intersection_v;
    std::string prec_word;
    std::string current_word;
    int temp_intersec_2;
    int temp_intersec_1;
    //score for horizontal and vertical positioning
    bool score_h = false;
    bool score_v = false;

    //random word retrieval and addition to vector
    for (unsigned int i = 0; i < num; i++)
    {
        //depending on the size of the dictionary
        int rand1 = random(100, 20000);
        std::string word1 = randomword(rand1);
        words_vect.push_back(word1);
    }

    std::string word1 = words_vect[0];
    unsigned len_word1 = word1.length();

    int pos_word1 = Align(len_word1, GridSize);
    PlaceVertical(pos_word1, (GridSize / 2), 0, words_vect[0]);

    for (unsigned int k = 1; k < words_vect.size(); k++)
    {
        back:
            // start of testing
            prec_word = words_vect[k - 1];
        current_word = words_vect[k];
        std::string intersec = find_intersection(words_vect[k - 1], words_vect[k]);

        if (intersec.empty())
        {
            std::cout << " Replacing current word " << words_vect[k] << "\n";
            replace_word(current_word);
            goto back;
        }
        else
        {
            //for each intersecting letter of the word in the vector
            for (unsigned int j = 0; j < intersec.length(); j++)
            {
                 //temporary position of the selected intersection letter

                if (k % 2 != 0)
                {
                    temp_intersec_2 = current_word.find_last_of(intersec[j]);
                    temp_intersec_1 = prec_word.find_last_of(intersec[j]);
                    score_h = can_place(coord_col[k - 1] + temp_intersec_2, coord_row[k - 1] - temp_intersec_1, k, current_word);
                    //if the horizontal score of the position is equal to the size of the word

                    if (score_h && k % 2 != 0)
                    {
                        place_h = true;
                        std::cout << " Testing word " << k << "/" << num << "\n";
                        std::cout << " Current word1 " << prec_word << " Current word2 " << current_word << "\n";
                        std::cout << "-----" << "\n";
                        x_word2_intersection_h = temp_intersec_2;
                        x_word1_intersection_h = temp_intersec_1;
                        goto placing_word;
                    }
                    else
                    {
                        place_h = false;
                    }
                }

                if (k % 2 == 0)
                {
                    temp_intersec_2 = current_word.find_last_of(intersec[j]);
                    temp_intersec_1 = prec_word.find_last_of(intersec[j]);
                    score_v = can_place(coord_col[k - 1] - temp_intersec_1, coord_row[k - 1] + temp_intersec_2, k, current_word);

                    //if the vertical score of the position is equal to the size of the word

                    if (score_v && k % 2 == 0)
                    {
                        place_v = true;
                        std::cout << " Testing word " << k << "/" << num << "\n";
                        std::cout << " Current word1 " << prec_word << " Current word2 " << current_word << "\n";
                        std::cout << "-----" << "\n";
                        x_word2_intersection_v = temp_intersec_2;
                        x_word1_intersection_v = temp_intersec_1;
                        goto placing_word;
                    }
                    else
                    {
                        place_v = false;
                    }
                }
            }
        }

        placing_word:

            if (place_h && k % 2 != 0)
            {
                PlaceHorizontal(coord_col[k - 1] + x_word2_intersection_h, coord_row[k - 1] - x_word1_intersection_h, k, current_word);
            }
        else if (!place_h && k % 2 != 0)
        {
            std::cout << " Replacing current word " << words_vect[k] << "\n";
            replace_word(words_vect[k]);
            goto back;
        }

        if (place_v && k % 2 == 0)
        {
            PlaceVertical(coord_col[k - 1] - x_word1_intersection_v, coord_row[k - 1] + x_word2_intersection_v, k, current_word);
        }
        else if (!place_v && k % 2 == 0)
        {
            std::cout << " Replacing current word " << words_vect[k] << "\n";
            replace_word(words_vect[k]);
            goto back;
        }
    }

    showGrid();

    words_vect.clear();
}
 
Joined
Jun 12, 2020
Messages
12
Reaction score
0
Even better! lol Don't worry, I have help... Because I'm a beginner too. But I think it's important to share the code! My superhero is dhayden, whom I thank very much.

Code:
#include <iostream>

#include <string>

#include <algorithm>

#include <bits/stdc++.h>

#include <stdlib.h>

#include <sstream>

#include <ctime>

#include <vector>

using namespace std;

//Vectors for recording word coordinates
std::vector < double > coord_col;
std::vector < double > coord_row;
//Vectors for recording words
std::vector < std::string > words_vect;

constexpr char empty = '.';
constexpr int GridSize = 26;
char grid[GridSize][GridSize];

//Random number generating function
int random(int from, int to) {
  return rand() % (to - from + 1) + from;
}

//Function searching for letters common to two words
std::string find_intersection(std::string first, std::string second) {
  std::sort(first.begin(), first.end());
  std::sort(second.begin(), second.end());
  int length = std::min(first.length(), second.length());
  std::string result(length, ' ');
  std::set_intersection(first.begin(), first.end(), second.begin(), second.end(),
    result.begin());
  return result;
}

//Horizontal word placement function
//Horizontal word placement function
void PlaceHorizontal(int col, int row, int id_word, std::string & word) {
  words_vect.push_back(word);
  coord_col.push_back(col);
  coord_row.push_back(row);
  unsigned taille = word.length();
  for (unsigned int j = 0; j < taille; j++) {
    grid[col][row + j] = word[j];
  }
}

//Function placing words vertically
void PlaceVertical(int col, int row, int id_word, std::string & word) {
  words_vect.push_back(word);
  coord_col.push_back(col);
  coord_row.push_back(row);
  unsigned taille = word.length();
  for (unsigned int j = 0; j < taille; j++) {
    grid[col + j][row] = word[j];
  }
}

//function that assigns a score to the place chosen for the word
bool can_place(int col, int row, int id_word, std::string & word) {
  int score = 0;
  unsigned taille = word.length();

  if (id_word % 2 != 0) {
    if (col < 0 || col >= GridSize || row < 0 || row + taille >= GridSize) return false;
    for (unsigned int j = 0; j < taille; j++) {
      char word_letter = word[j];
      if (grid[col][row + j] == empty) {
        score = score + 1;
      } else if (grid[col][row + j] != word_letter) {
        return false;
      }
    }
  } else if (id_word % 2 == 0) {
    if (col < 0 || col + taille >= GridSize || row < 0 || row >= GridSize) return false;

    for (unsigned int j = 0; j < taille; j++) {
      char word_letter = word[j];
      if (grid[col + j][row] == empty) {
        score = score + 1;
      } else if (grid[col + j][row] != word_letter) {
        return false;
      }
    }
  }

  if (score < taille) {
    return true;
  } else {
    return false;
  }
}

//Function randomly retrieving words from the dictionary
std::string randomword() {
  // 22000 words in the dictionary. "static" means doesn't get destroyed each time the function returns
  static std::vector < std::string > words;
  std::string s;

  if (words.size() == 0) {
    // If the size is zero then this must be the first time it's called. Read the dictionary
    // file into the words array. This only happens the first time the function is called
    std::string ifileName = "dictionary.txt";
    std::ifstream f(ifileName);

    if (!f) {
      std::cerr << "File '" << ifileName << "' couldn't opened!\n";
      exit(1); // fatal error
    }
    for (int i = 0; i <= 22000; i++) {
      std::getline(f, s);
      std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) {
        return std::tolower(c);
      });
      words.push_back(s); // save the word
    }
  }

  // When you get here, the dictionary file has been read into the words vector.
  int randomline = random(20, 22000); // pick a random number 20 - 22,000
  return words[randomline]; // return that word from the dictionary
}

void showGrid() {
  //grid view
  for (unsigned int i = 0; i < GridSize; i++) {
    for (int j = 0; j < GridSize; j++) {
      std::cout << grid[i][j];
      std::cout << " ";
    }

    std::cout << "\n";
  }
}

int Align(float value, float size) {
  int pos = (size / 2) - value / 2;
  return pos;
}

int main(int argc, char * argv[]) {
  srand(time(0));
  memset(grid, '.', sizeof(grid));
  bool score;
  //number of words to place
  int rand0 = random(20, 30);
  int num = rand0;

  //position of the horizontal and vertical intersection letter
  int cur_word_pos_inter;
  int prec_word_pos_inter;

  std::string prec_word;
  std::string current_word;
  int prec_intersec;
  int cur_intersec;

  std::string intersec;

  std::string word1 = randomword();
  unsigned len_word1 = word1.length();
  int pos_word1 = Align(len_word1, GridSize);
  PlaceVertical(pos_word1, (GridSize / 2), 0, word1);

  // For each word to place
  for (int k = 1; k < num; k++) {

    std::cout << " Testing word " << k << "/" << num << "\n";
    // Loop to try to place random words
    do {

      current_word = randomword();
      prec_word = words_vect[k - 1];

      intersec = find_intersection(words_vect[k - 1], current_word);
      score = false;

      for (unsigned int j = 0; !score && j < intersec.length(); j++) {
        //temporary position of the selected intersection letter
        cur_intersec = current_word.find_last_of(intersec[j]);
        prec_intersec = prec_word.find_last_of(intersec[j]);
        //if the horizontal score of the position is equal to the size of the word
        if (k % 2 != 0) {
          score = can_place(coord_col[k - 1] + cur_intersec, coord_row[k - 1] - prec_intersec, k, current_word);
          if (score) {
            cur_word_pos_inter = cur_intersec;
            prec_word_pos_inter = prec_intersec;
          }
        } else if (k % 2 == 0) {
          score = can_place(coord_col[k - 1] - prec_intersec, coord_row[k - 1] + cur_intersec, k, current_word);
          if (score) {
            cur_word_pos_inter = cur_intersec;
            prec_word_pos_inter = prec_intersec;
          }
        }

      }

    } while (!score);

    if (k % 2 != 0) {
      PlaceHorizontal(coord_col[k - 1] + cur_word_pos_inter, coord_row[k - 1] - prec_word_pos_inter, k, current_word);
    } else if (k % 2 == 0) {
      PlaceVertical(coord_col[k - 1] - prec_word_pos_inter, coord_row[k - 1] + cur_word_pos_inter, k, current_word);
    }
  }
  showGrid();

}
 
Joined
Jun 12, 2020
Messages
12
Reaction score
0
A little crazier!

Code:
#include <iostream>

#include <string>

#include <algorithm>

#include <bits/stdc++.h>

#include <stdlib.h>

#include <sstream>

#include <ctime>

#include <vector>

using namespace std;

//Vectors for recording word coordinates
std::vector < double > coord_col;
std::vector < double > coord_row;
//Vectors for recording words
std::vector < std::string > words_vect;

constexpr char empty = '.';
constexpr int GridSize = 26;
char grid[GridSize][GridSize];

//Random number generating function
int random(int from, int to) {
  return rand() % (to - from + 1) + from;
}

//Function searching for letters common to two words
std::string find_intersection(std::string first, std::string second) {
  std::sort(first.begin(), first.end());
  std::sort(second.begin(), second.end());
  int length = std::min(first.length(), second.length());
  std::string result(length, ' ');
  std::set_intersection(first.begin(), first.end(), second.begin(), second.end(),
    result.begin());
  return result;
}

//Function placing words vertically
void Place(int col, int row, int id_word, int dr, int dc, std::string & word) {
  words_vect.push_back(word);
  coord_col.push_back(col);
  coord_row.push_back(row);
  unsigned taille = word.length();
  for (unsigned int j = 0; j < taille; j++) {
    grid[col][row] = word[j];
    col += dc;
    row += dr;
  }
}
//function that assigns a score to the place chosen for the word
bool can_place(int col, int row, int id_word, int dr, int dc, std::string & word) {
  int score = 0;
  unsigned taille = word.length();

  if (col < 0 || col + dc * taille >= GridSize || row < 0 || row + dr * taille >= GridSize) return false;
  for (unsigned int j = 0; j < taille; j++) {
    char word_letter = word[j];

    if (grid[col][row] == empty) {
      score = score + 1;
    } else if (grid[col][row] != word_letter) {
      return false;
    }
    row += dr;
    col += dc;
  }

  if (score < taille) {
    return true;
  } else {
    return false;
  }
}

//Function randomly retrieving words from the dictionary
std::string randomword() {
  // 22000 words in the dictionary. "static" means doesn't get destroyed each time the function returns
  static std::vector < std::string > words;
  std::string s;

  if (words.size() == 0) {
    // If the size is zero then this must be the first time it's called. Read the dictionary
    // file into the words array. This only happens the first time the function is called
    std::string ifileName = "dictionary.txt";
    std::ifstream f(ifileName);

    if (!f) {
      std::cerr << "File '" << ifileName << "' couldn't opened!\n";
      exit(1); // fatal error
    }
    for (int i = 0; i <= 22000; i++) {
      std::getline(f, s);
      std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) {
        return std::tolower(c);
      });
      words.push_back(s); // save the word
    }
  }

  // When you get here, the dictionary file has been read into the words vector.
  int randomline = random(20, 22000); // pick a random number 20 - 22,000
  return words[randomline]; // return that word from the dictionary
}

void showGrid() {
  //grid view
  for (unsigned int i = 0; i < GridSize; i++) {
    for (int j = 0; j < GridSize; j++) {
      std::cout << grid[i][j];
      std::cout << " ";
    }

    std::cout << "\n";
  }
}

int Align(float value, float size) {
  int pos = (size / 2) - value / 2;
  return pos;
}

int main(int argc, char * argv[]) {
// x-axis direction table
  int x[] = { -1,  -1,  -1,  0,  0,  1,  1,  1 };
// y-axis direction table
  int y[] = {  -1,  0,  1,  -1,  1,  -1,  0,  1 };

  srand(time(0));
  memset(grid, '.', sizeof(grid));
  bool score;
  //number of words to place
  int rand0 = random(15, 30);
  int num = rand0;

  //position of the horizontal and vertical intersection letter
  int cur_word_pos_inter;
  int prec_word_pos_inter;

  std::string prec_word;
  std::string current_word;
  int prec_intersec;
  int cur_intersec;
  int index_direction;
  std::string intersec;

  std::string word1 = randomword();
  unsigned len_word1 = word1.length();
  int pos_word1 = Align(len_word1, GridSize);
  Place(pos_word1, (GridSize / 2), 0, 0, 1, word1);

  // For each word to place
  for (int k = 1; k < num; k++) {

    // Loop to try to place random words
    do {

      current_word = randomword();
      prec_word = words_vect[k - 1];

      intersec = find_intersection(words_vect[k - 1], current_word);
      score = false;

      for (unsigned int j = 0; !score && j < intersec.length(); j++) {
        //temporary position of the selected intersection letter
        cur_intersec = current_word.find_last_of(intersec[j]);
        prec_intersec = prec_word.find_last_of(intersec[j]);
       
         // test every possible direction to place the word
        for (unsigned int i = 0; !score && i < 8; i++) {
          score = can_place(coord_col[k - 1] + cur_intersec, coord_row[k - 1] - prec_intersec, k, x[i], y[i], current_word);

          if (score) {
            cur_word_pos_inter = cur_intersec;
            prec_word_pos_inter = prec_intersec;
           // if placement in the direction is possible keep the index of the chosen direction
            index_direction = i;

          }
        }

      }

    } while (!score);
    // place the word in the chosen direction
    Place(coord_col[k - 1] + cur_word_pos_inter, coord_row[k - 1] - prec_word_pos_inter, k, x[index_direction], y[index_direction], current_word);

  }
  showGrid();
  // Show the word list
  std::cout << "\n Word List:\n";
  for (auto & word: words_vect) {
    std::cout << word << '\n';
  }

}
 
Joined
Jul 4, 2020
Messages
5
Reaction score
0
Good morning, I figured it out tonight. It's great. There's still a few misspelled words, but the idea is there. I hope that some beginners will be able to get some inspiration from it because this code required a lot of thought from me, I'm not an expert either.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top