Menu
Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Programming Languages
C, C++ and C#
Crossword
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="bryonmajor, post: 5167713, member: 85918"] Hello there's the end of the story! [code=C++]#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(); }[/code] [/QUOTE]
Verification
Post reply
Forums
Programming Languages
C, C++ and C#
Crossword
Top