Crossword

Joined
May 11, 2020
Messages
15
Reaction score
0
Hi, I've been working on a crossword puzzle generator project for some time. But I'm having a hard time finishing it recursively. Could you help me, please?
Coming soon.

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;

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++)
    {
        grid[x][y + j] = word[j];
    }
}
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++)
    {
        grid[x + j][y] = word[j];
    }
}

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

    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;
    srand(time(0));

    std::string ifileName = "dicoFrancais.txt";
    if (argc > 1)
        ifileName = argv[1];

    std::ifstream f(ifileName);

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

    std::string s;

    int rand1 = random(1000, 2000);
    for (int i = 1; i <= rand1; i++)
    {
        std::getline(f, s);
        word1 = s;
    }

    for (int i = 1; i <= rand1; i++)
    {
        std::getline(f, s);
        word2 = s;
    }

    for (int i = 1; i <= rand1; i++)
    {
        std::getline(f, s);
        word3 = s;
    }

    for (int i = 1; i <= rand1; i++)
    {
        std::getline(f, s);
        word4 = s;
    }

    for (int i = 1; i <= rand1; i++)
    {
        std::getline(f, s);
        word5 = s;
    }
    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";
    int x = 8;
    int y = 8;

    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);
    x_word23_intersection = word2.find_last_of(intersec1[0]);
    x_word32_intersection = word3.find_last_of(intersec1[0]);
    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);
    x_word34_intersection = word3.find_last_of(intersec2[0]);
    x_word43_intersection = word4.find_last_of(intersec2[0]);
    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);
    x_word45_intersection = word4.find_last_of(intersec3[0]);
    x_word54_intersection = word5.find_last_of(intersec3[0]);
    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);
    x_word56_intersection = word5.find_last_of(intersec4[0]);
    x_word65_intersection = word6.find_last_of(intersec4[0]);
    PlaceVertical(coord_x[3] - x_word65_intersection, coord_y[3] + x_word56_intersection, word6);

    for (int i = 0; i < 26; i++)
    {
        for (int j = 0; j < 26; j++)
        {
            std::cout << grid[i][j];
            std::cout << " ";
        }
        std::cout << "\n";
    }
}
 
Joined
May 11, 2020
Messages
15
Reaction score
0
Hi, I've made a little progress on my idea. Yesterday I forgot to specify the text file is a dictionary of words to put in the grid. What I would like to do is to make the placement as recursive as possible according to a number of words chosen by the user. If you have any ideas to improve or go a little more towards my idea I'm interested.
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++)
    {
        grid[x][y + j] = word[j];
    }
}
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++)
    {
        grid[x + j][y] = word[j];
    }
}
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;

    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;
    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);

    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";
    int x = 8;
    int y = 8;
    int indice1;
    int indice2;
    int indice3;
    int indice4;
    int indice5;

    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] && intersec2.length() > 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] && intersec3.length() > 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] && intersec4.length() > 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[3] - x_word65_intersection, coord_y[3] + x_word56_intersection, word6);

    for (int i = 0; i < 26; i++)
    {
        for (int j = 0; j < 26; j++)
        {
            std::cout << grid[i][j];
            std::cout << " ";
        }
        std::cout << "\n";
    }
}
 
Joined
May 11, 2020
Messages
15
Reaction score
0
Hi, sorry, some of the loops were incoherent. I corrected it.
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;

    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;
    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);

    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";
    int x = 8;
    int y = 8;
    int indice1 = 0;
    int indice2 = 0;
    int indice3 = 0;
    int indice4 = 0;
    int indice5 = 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[3] - x_word65_intersection, coord_y[3] + x_word56_intersection, word6);

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

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top