Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <fstream>
- #include <string>
- void crswrd_fill(char **array1, bool **array2, int ROW, int COL) {
- for (int i = 0; i < ROW; i++)
- for (int j = 0; j < COL; j++) {
- array1[i][j] = char(65 + rand() % 26);
- array2[i][j] = false;
- }
- }
- void crswrd_lay(char **array1, bool **array2, std::string *term, std::string *find, int ROW, int COL, int NUM, int DEX) {
- int row, column, orientation, selection, term_dex, find_dex, count;
- bool repeat;
- find_dex = count = 0;
- while (count < NUM) {
- orientation = rand() % 3;
- do {
- selection = rand() % DEX;
- }
- while (term[selection] == "");
- term_dex = 0;
- repeat = false;
- switch (orientation) {
- case 0: column = rand() % COL;
- row = ROW - term[selection].length() - (rand() % (ROW - term[selection].length()));
- for (int i = row; i < (row + term[selection].length()); i++)
- if (array2[i][column]) {
- repeat = true; break;
- }
- if (!repeat)
- for (int i = row; i < (row + term[selection].length()); i++) {
- array1[i][column] = (term[selection])[term_dex++];
- array2[i][column] = true;
- }
- break;
- case 1: row = rand() % ROW;
- column = COL - (term[selection].length()) - (rand() % (COL - (term[selection].length())));
- for (int i = column; i < (column + term[selection].length()); i++)
- if (array2[row][i]) {
- repeat = true; break;
- }
- if (!repeat)
- for (int i = column; i < (column + term[selection].length()); i++) {
- array1[row][i] = (term[selection])[term_dex++];
- array2[row][i] = true;
- }
- break;
- case 2: row = ROW - term[selection].length() - (rand() % (ROW - term[selection].length()));
- column = COL - term[selection].length() - (rand() % (COL - term[selection].length()));
- for (int i = 0; i < (row + term[selection].length()) && !repeat;) {
- for (int j = 0; j < (column + term[selection].length()); i++, j++) {
- if (array2[i][j]) {
- repeat = true;
- break;
- }
- }
- }
- if (!repeat)
- for (int i = row; i < (row + term[selection].length());)
- for (int j = column; j < (column + term[selection].length()); i++, j++) {
- array1[i][j] = (term[selection])[term_dex++];
- array2[i][j] = true;
- }
- break;
- }
- if (!repeat) {
- count++;
- find[find_dex++] = term[selection];
- term[selection] = "";
- }
- }
- }
- void crswrd_print(char **array, int ROW, int COL) {
- for (int i = 0; i < ROW; i++, std::cout << '\n')
- for (int j = 0; j < COL; j++)
- std::cout << array[i][j] << ' ';
- std::cout << '\n';
- }
- bool validation(std::string str) {
- bool pr = true;
- for(int i = 0;i < str.length();i++){
- if(!isdigit(str.[i])){
- pr = false;
- std::cout << "\nНеверное значение!\n";
- return pr;
- }
- }
- return pr;
- }
- int main() {
- int ROW = 0,COL = 0,NUM = 0,DEX = 0;
- while (1) {
- std::string str;
- std::cout << "Input row\n";
- std::cin >> str;
- if(validation(str))
- {
- ROW = atoi(str.c_str());
- break;
- }
- }
- std::cout << "Input col\n";
- std::cin >> COL;
- std::cout << "Input num\n";
- std::cin >> NUM;
- std::cout << "Input dex\n";
- std::cin >> DEX;
- srand((unsigned int)time(0));
- char **maze = new char* [ROW];
- for (int i = 0; i < ROW; i++)
- maze[i] = new char[COL];
- bool **mark = new bool* [ROW];
- for (int i = 0; i < ROW; i++)
- mark[i] = new bool[COL];
- std::string *find = new std::string[NUM];
- std::string *term = new std::string[DEX];
- std::ifstream file("words.txt");
- std::string tmpstr;
- for (int i = 0; i < DEX ; i++) {
- getline(file, tmpstr);
- term[i] = tmpstr;
- }
- crswrd_fill(maze, mark, ROW, COL);
- crswrd_lay(maze, mark, term, find, ROW, COL, NUM, DEX);
- crswrd_print(maze, ROW, COL);
- delete []term;
- for (int i = 0; i < ROW ;i++){
- delete []maze[i];
- }
- delete []maze;
- for (int i = 0; i < ROW ;i++){
- delete []mark[i];
- }
- delete []mark;
- std::cout << "Words used list:\n";
- for (int i = 0; i < NUM ; i++)
- std::cout << i+1 << ". " << find[i] << '\n';
- delete[] find;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement