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: 5167749, member: 85948"] A little crazier! [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; } //Function placing words vertically void Place(int col, int row, int id_word, int dr, int dc, std::string & word) { words_vect.push_back(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] = word[j]; col += dc; row += dr; } } //function that assigns a score to the place chosen for the word bool can_place(int col, int row, int id_word, int dr, int dc, std::string & word) { int score = 0; unsigned taille = word.length(); if (col < 0 || col + dc * taille >= GridSize || row < 0 || row + dr * taille >= GridSize) return false; for (unsigned int j = 0; j < taille; j++) { char word_letter = word[j]; if (grid[col][row] == empty) { score = score + 1; } else if (grid[col][row] != word_letter) { return false; } row += dr; col += dc; } if (score < taille) { return true; } else { return false; } } //Function randomly retrieving words from the dictionary std::string randomword() { // 22000 words in the dictionary. "static" means doesn't get destroyed each time the function returns static std::vector < std::string > words; std::string s; if (words.size() == 0) { // If the size is zero then this must be the first time it's called. Read the dictionary // file into the words array. This only happens the first time the function is called std::string ifileName = "dictionary.txt"; std::ifstream f(ifileName); if (!f) { std::cerr << "File '" << ifileName << "' couldn't opened!\n"; exit(1); // fatal error } for (int i = 0; i <= 22000; i++) { std::getline(f, s); std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); }); words.push_back(s); // save the word } } // When you get here, the dictionary file has been read into the words vector. int randomline = random(20, 22000); // pick a random number 20 - 22,000 return words[randomline]; // return that word from the dictionary } 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 / 2; return pos; } int main(int argc, char * argv[]) { // x-axis direction table int x[] = { -1, -1, -1, 0, 0, 1, 1, 1 }; // y-axis direction table int y[] = { -1, 0, 1, -1, 1, -1, 0, 1 }; srand(time(0)); memset(grid, '.', sizeof(grid)); bool score; //number of words to place int rand0 = random(15, 30); int num = rand0; //position of the horizontal and vertical intersection letter int cur_word_pos_inter; int prec_word_pos_inter; std::string prec_word; std::string current_word; int prec_intersec; int cur_intersec; int index_direction; std::string intersec; std::string word1 = randomword(); unsigned len_word1 = word1.length(); int pos_word1 = Align(len_word1, GridSize); Place(pos_word1, (GridSize / 2), 0, 0, 1, word1); // For each word to place for (int k = 1; k < num; k++) { // Loop to try to place random words do { current_word = randomword(); prec_word = words_vect[k - 1]; intersec = find_intersection(words_vect[k - 1], current_word); score = false; for (unsigned int j = 0; !score && j < intersec.length(); j++) { //temporary position of the selected intersection letter cur_intersec = current_word.find_last_of(intersec[j]); prec_intersec = prec_word.find_last_of(intersec[j]); // test every possible direction to place the word for (unsigned int i = 0; !score && i < 8; i++) { score = can_place(coord_col[k - 1] + cur_intersec, coord_row[k - 1] - prec_intersec, k, x[i], y[i], current_word); if (score) { cur_word_pos_inter = cur_intersec; prec_word_pos_inter = prec_intersec; // if placement in the direction is possible keep the index of the chosen direction index_direction = i; } } } } while (!score); // place the word in the chosen direction Place(coord_col[k - 1] + cur_word_pos_inter, coord_row[k - 1] - prec_word_pos_inter, k, x[index_direction], y[index_direction], current_word); } showGrid(); // Show the word list std::cout << "\n Word List:\n"; for (auto & word: words_vect) { std::cout << word << '\n'; } }[/code] [/QUOTE]
Verification
Post reply
Forums
Programming Languages
C, C++ and C#
Crossword
Top