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="codingd, post: 5167741, member: 85948"] [code=C++]#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 ¤t_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(); }[/code] [/QUOTE]
Verification
Post reply
Forums
Programming Languages
C, C++ and C#
Crossword
Top