0% found this document useful (0 votes)
13 views

Main CPP

Uploaded by

Salah Din
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Main CPP

Uploaded by

Salah Din
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

#include "functions.

h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <random>
#include <algorithm>
#include <iomanip>
#include <unordered_set>
#include <map>
#include <chrono>

using namespace std;

int main() {
//had lfile fih 3300 word 7 flenth et 5000 word dyal 6 flength
string filename = "11300word-length-7-6-5.txt";
//string filename = "filtered_words_sorted_more_3_letters.txt";
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937 gen(seed);
int grid_size = 7;
int num_puzzles = 20;
vector<string> word_list = load_words_from_file(filename, grid_size);
word_list = filterWordsByLetters(word_list, "sareti");

/*for(int i=0;i<word_list.size();i++){
cout<<word_list[i]<<endl;
}*/

cout<<"---------------------------------"<<endl;

int total_words_used = 0;
int total_horizontal_words = 0;
int total_vertical_words = 0;

vector<int> word_lengths = {2, 3, 4}; // Lengths of words to consider


int puzzle_num =0;
while(word_list.size()>0){
puzzle_num++;
vector<WordInfo> word_info;
vector<vector<char>> puzzle_grid;
double horizontal_percentage, vertical_percentage;
bool prc=false;int i=1;
do {
puzzle_grid.clear();
word_info.clear();

vector<string> shuffled_words = word_list;


shuffle(shuffled_words.begin(), shuffled_words.end(), gen);
/*for(int i=0;i<shuffled_words.size();i++){
cout<<shuffled_words[i]<<endl;
}*/
puzzle_grid = create_word_search_puzzle(shuffled_words, grid_size,
word_info);
calculate_word_percentages(word_info, horizontal_percentage,
vertical_percentage);
prc=are_percentages_close(horizontal_percentage, vertical_percentage,
20.0);
//cout<<"************************ "<<word_list.size()<<"
**************** "<<i++<<" ******************"<<endl;
if(word_list.size()<4) prc=true;
} while (!prc);

// Fill empty spaces with words of different lengths


//fill_empty_spaces(puzzle_grid, "helpWords.txt",word_info);

display_word_search_puzzle(puzzle_grid, word_info, puzzle_num);

// Remove the words used in the current puzzle from the word_list
int words_used_in_puzzle = 0;
int horizontal_words_in_puzzle = 0;
int vertical_words_in_puzzle = 0;

for (const auto& info : word_info) {


auto it = find(word_list.begin(), word_list.end(), info.word);
if (it != word_list.end()) {
word_list.erase(it);
++words_used_in_puzzle;

if (info.direction == 0) {
++horizontal_words_in_puzzle;
} else if (info.direction == 1) {
++vertical_words_in_puzzle;
}
}
}

total_words_used += words_used_in_puzzle;
total_horizontal_words += horizontal_words_in_puzzle;
total_vertical_words += vertical_words_in_puzzle;

cout << "Words used in Puzzle " << puzzle_num << ": " <<
words_used_in_puzzle << endl;
cout << "Percentage of Horizontal Words: " << (100.0 *
horizontal_words_in_puzzle / words_used_in_puzzle) << "%" << endl;
cout << "Percentage of Vertical Words: " << (100.0 *
vertical_words_in_puzzle / words_used_in_puzzle) << "%" << endl;
cout<<"---------------------------------------------"<<endl;
}

cout << "Total words used in all puzzles: " << total_words_used << endl;
cout << "Total horizontal words used in all puzzles: " <<
total_horizontal_words << endl;
cout << "Total vertical words used in all puzzles: " << total_vertical_words <<
endl;
cout<<"Fin de Programme"<<endl;
return 0;
}
/*#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <random>
#include <iomanip>
#include <unordered_set>
#include <map>
#include <chrono>
#include <algorithm>
//#include "functions.h"

using namespace std;


struct WordInfo {
string word;
int x;
int y;
int direction; // 0 for horizontal, 1 for vertical, 2 for diagonal
};
vector<string> load_words_from_file(const string& filename, int max_word_length) {
vector<string> words;
ifstream file(filename);
if (file.is_open()) {
string word;
while (getline(file, word)) {
// Check if the word length is less than or equal to the maximum word
length
if (word.length() <= max_word_length) {
words.push_back(word);
}
}
file.close();
} else {
cout << "Failed to open the file: " << filename << endl;
}
return words;
}

std::vector<std::string> filterWordsByLetters(const std::vector<std::string>&


wordList, const std::string& targetWord) {
std::unordered_set<char> targetLetters(targetWord.begin(), targetWord.end());
std::vector<std::string> filteredWords;

for (const std::string& word : wordList) {


std::unordered_set<char> wordLetters(word.begin(), word.end());
bool isSubset = true;

for (char letter : wordLetters) {


if (targetLetters.find(letter) == targetLetters.end()) {
isSubset = false;
break;
}
}

if (isSubset) {
filteredWords.push_back(word);
}
}

return filteredWords;
}
void calculate_word_percentages(const vector<WordInfo>& word_info, double&
horizontal_percentage, double& vertical_percentage) {
int total_words = word_info.size();
int horizontal_words = 0;
int vertical_words = 0;

for (const WordInfo& info : word_info) {


if (info.direction == 0) {
++horizontal_words;
} else if (info.direction == 1) {
++vertical_words;
}
}

horizontal_percentage = (100.0 * horizontal_words) / total_words;


vertical_percentage = (100.0 * vertical_words) / total_words;
}
bool are_percentages_close(double percentage1, double percentage2, double
threshold) {
return abs(percentage1 - percentage2) <= threshold;
}
vector<vector<char>> create_word_search_puzzle(const vector<string>&
shuffled_words, int grid_size, vector<WordInfo>& word_info) {
vector<vector<char>> grid(grid_size, vector<char>(grid_size, ' '));

std::random_device rd;
std::mt19937 gen(rd());

int direction = 0; // 0: Horizontal, 1: Vertical


int num_directions = 2; // Total number of available directions

int words_tried = 0; // Counter for the number of words tried in the current
direction
for (const string& word : shuffled_words) {
bool placed = false;

for (int attempts = 0; attempts < 100; ++attempts) {


int x, y;
switch (direction) {
case 0: // Horizontal
x = uniform_int_distribution<int>(0, grid_size - word.length())
(gen);
y = uniform_int_distribution<int>(0, grid_size - 1)(gen);
placed = true;
for (int i = 0; i < word.length(); ++i) {
char intersect_char = grid[y][x + i];
if (intersect_char != ' ' && intersect_char != word[i]) {
placed = false;
break;
}
}
if (placed) {
for (int i = 0; i < word.length(); ++i) {
grid[y][x + i] = word[i];
}
word_info.push_back({word, x, y, direction});
break;
}
break;

case 1: // Vertical
x = uniform_int_distribution<int>(0, grid_size - 1)(gen);
y = uniform_int_distribution<int>(0, grid_size - word.length())
(gen);
placed = true;
for (int i = 0; i < word.length(); ++i) {
char intersect_char = grid[y + i][x];
if (intersect_char != ' ' && intersect_char != word[i]) {
placed = false;
break;
}
}
if (placed) {
for (int i = 0; i < word.length(); ++i) {
grid[y + i][x] = word[i];
}
word_info.push_back({word, x, y, direction});
break;
}
break;
}

if (placed) {
// Switch to the other direction
direction = (direction + 1) % num_directions;
words_tried = 0; // Reset the counter
break;
} else {
words_tried++;
if (words_tried > 200) {
// Switch to the other direction
direction = (direction + 1) % num_directions;
words_tried = 0; // Reset the counter
}
}
}
}

return grid;
}
void display_word_search_puzzle(const vector<vector<char>>& grid, const
vector<WordInfo>& word_info, int puzzle_num) {
int grid_size = grid.size();

// Display the puzzle number


cout << "Puzzle " << puzzle_num << ":" << endl;

// Affichage des indices des colonnes (lettres)


cout << " ";
for (int i = 0; i < grid_size; ++i) {
cout << char('A' + i) << " ";
}
cout << endl;

for (int row = 0; row < grid_size; ++row) {


// Affichage de l'indice de la ligne (nombre)
cout << setw(2) << row + 1 << " ";

for (int col = 0; col < grid_size; ++col) {


cout << grid[row][col] << " ";
}

cout << endl;


}
cout << endl;
cout << "Posistions: " << endl;
for (const WordInfo& info : word_info) {
cout << info.word << " - Position: (" << char('A' + info.x) << info.y + 1
<< ") - ";
if (info.direction == 0) cout << "Horizontal";
else if (info.direction == 1) cout << "Vertical";
else if (info.direction == 2) cout << "Diagonal";
cout << endl;
}

cout << endl;


cout << "Words:" << endl;

for (size_t i = 0; i < word_info.size(); ++i) {


const WordInfo& info = word_info[i];
int index = info.y * grid_size + info.x;
cout << setw(3) << index << ": [\"" << info.word << "\", ";
if (info.direction == 0) {
cout << "\"r\"";
} else if (info.direction == 1) {
cout << "\"b\"";
}
cout << "]" << endl;
}
}

int main() {

//had lfile fih 3300 word 7 flenth et 5000 word dyal 6 flength
string filename = "11300word-length-7-6-5.txt";
//string filename = "filtered_words_sorted_more_3_letters.txt";
//std::random_device rd;
//std::mt19937 gen(rd());
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937 gen(seed);
int grid_size = 7;
int num_puzzles = 20;
vector<string> word_list = load_words_from_file(filename, grid_size);
word_list = filterWordsByLetters(word_list, "seding");

for(int i=0;i<word_list.size();i++){
cout<<word_list[i]<<endl;
}

cout<<"---------------------------------"<<endl;

int total_words_used = 0;
int total_horizontal_words = 0;
int total_vertical_words = 0;

vector<int> word_lengths = {2, 3, 4}; // Lengths of words to consider

//for (int puzzle_num = 1; puzzle_num <= num_puzzles; ++puzzle_num) {


int puzzle_num=0;
while(word_list.size()>0){
vector<WordInfo> word_info;
vector<vector<char>> puzzle_grid;
double horizontal_percentage, vertical_percentage;
bool prc=false;
int i=0;
vector<string> shuffled_words = word_list;
shuffle(shuffled_words.begin(), shuffled_words.end(), gen);
for(int i=0;i<shuffled_words.size();i++){
cout<<shuffled_words[i]<<endl;
}
cout<<"/////////////////////"<<endl;
do {
puzzle_grid.clear();
cout<<"1"<<endl;
word_info.clear();
cout<<"2"<<endl;
puzzle_grid = create_word_search_puzzle(shuffled_words, grid_size,
word_info);
cout<<"3"<<endl;
calculate_word_percentages(word_info, horizontal_percentage,
vertical_percentage);
cout<<"4"<<endl;
int pr=20;
prc=are_percentages_close(horizontal_percentage, vertical_percentage,
pr);
cout<<"5"<<endl;
//if(word_list.size()<4) prc=false;
} while (!prc);
// Fill empty spaces with words of different lengths
//fill_empty_spaces(puzzle_grid, "filtered_words.txt",word_info);

display_word_search_puzzle(puzzle_grid, word_info, puzzle_num);

// Remove the words used in the current puzzle from the word_list
int words_used_in_puzzle = 0;
int horizontal_words_in_puzzle = 0;
int vertical_words_in_puzzle = 0;

for (size_t i = 0; i < word_info.size(); ++i) {


const WordInfo& info = word_info[i];

auto it = find(word_list.begin(), word_list.end(), info.word);

if (it != word_list.end()) {
word_list.erase(it);
++words_used_in_puzzle;

if (info.direction == 0) {
++horizontal_words_in_puzzle;
} else if (info.direction == 1) {
++vertical_words_in_puzzle;
}
}
}

total_words_used += words_used_in_puzzle;
total_horizontal_words += horizontal_words_in_puzzle;
total_vertical_words += vertical_words_in_puzzle;

cout << "Words used in Puzzle " << puzzle_num++ << ": " <<
words_used_in_puzzle << endl;
cout << "Percentage of Horizontal Words: " << (100.0 *
horizontal_words_in_puzzle / words_used_in_puzzle) << "%" << endl;
cout << "Percentage of Vertical Words: " << (100.0 *
vertical_words_in_puzzle / words_used_in_puzzle) << "%" << endl;
cout<<"---------------------------------------------"<<endl;
cout<<"Apres "<<i<<" fois"<<endl;

}
cout<<"dans la sortie word_list.size() == "<<word_list.size()<<endl;
cout << "Total words used in all puzzles: " << total_words_used << endl;
cout << "Total horizontal words used in all puzzles: " <<
total_horizontal_words << endl;
cout << "Total vertical words used in all puzzles: " << total_vertical_words <<
endl;

return 0;
}
*/

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy