1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
#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
Place(int row, int col,
int dr, int dc,
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[row][col] = word[j];
col += dc;
row += dr;
}
}
//function that assigns a score to the place chosen for the word
bool
can_place(int row, int col,
int dr, int dc,
std::string & word)
{
int score = 0;
unsigned taille = word.length();
std::cout << "Can " << word << " go at " << row << ',' << col << " -> " << dr << ',' << dc << '?';
if (col < 0 || col + dc*taille >= GridSize ||
row < 0 || row + dr*taille >= GridSize) {
std::cout << " no\n";
return false;
}
for (unsigned int j = 0; j < taille; j++) {
char word_letter = word[j];
if (grid[row][col] == empty) {
score = score + 1;
} else if (grid[row][col] != word_letter) {
std::cout << " no\n";
return false;
}
row += dr;
col += dc;
}
if (score < taille) {
std::cout << " yes\n";
return true;
} else {
std::cout << " no\n";
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
std::cout << " ";
for (int col = 0; col < GridSize; col++) {
std::cout << col % 10 << ' ';
}
std::cout << '\n';
for (unsigned int row = 0; row < GridSize; row++) {
std::cout << row%10 << ' ';
for (int col = 0; col < GridSize; col++) {
std::cout << grid[row][col];
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 place_v = false;
bool place_h = false;
//number of words to place
int rand0 = random(10, 15);
int num = rand0;
//position of the horizontal and vertical intersection letter
std::string prec_word;
std::string current_word;
int cur_intersec;
int prev_intersec;
//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);
Place(pos_word1, (GridSize / 2), 1, 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]
<< "because it doesn't intersect with " << words_vect[k-1] << "\n";
replace_word(current_word);
goto back;
} else {
//for each intersecting letter of the word in the vector
std::cout << "Last word started at " << coord_row[k-1] << ',' << coord_col[k-1] << '\n';
for (unsigned int j = 0; j < intersec.length(); j++) {
//temporary position of the selected intersection letter
if (k % 2 != 0) {
// Place it horizontally
cur_intersec = current_word.find_last_of(intersec[j]);
prev_intersec = prec_word.find_last_of(intersec[j]);
score_h =
can_place(coord_row[k - 1] + prev_intersec,
coord_col[k - 1] - cur_intersec,
0, 1, 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";
goto placing_word;
} else {
place_h = false;
}
}
if (k % 2 == 0) {
// Place it vertically
cur_intersec = current_word.find_last_of(intersec[j]);
prev_intersec = prec_word.find_last_of(intersec[j]);
score_v =
can_place(coord_row[k - 1] - cur_intersec,
coord_col[k - 1] + prev_intersec,
1, 0, 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";
goto placing_word;
} else {
place_v = false;
}
}
}
}
placing_word:
if (place_h && k % 2 != 0) {
Place(coord_row[k - 1] + prev_intersec,
coord_col[k - 1] - cur_intersec,
0, 1, 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) {
Place(coord_row[k - 1] - cur_intersec,
coord_col[k - 1] + prev_intersec,
1, 0, 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();
}
|