- 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.
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";
}
}