#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <random>
using namespace std;
string filename = "words.txt";
ifstream ifile {filename};
void error(const string&);
vector<string> load_vector(const string& filename) {
vector<string> vi;
if (!ifile)
error("load_vector cannot open the file " + filename);
copy(istream_iterator<string>(ifile), istream_iterator<string>(), back_inserter(vi));
return vi;
}
void error(const string& s) {
cerr << "Error: " << s << endl;
exit(EXIT_FAILURE);
}
void aleatorio () {
int HEIGHT = 15;
int WIDTH = 15;
int wordsize = 0;
int wordPos = 0;
char puzzle[HEIGHT][WIDTH];
bool okWord;
srand(time(NULL)); //inicializamos semilla
char random;
int filarand = 0;
vector<string> load_vector(const string&);
vector<string> words = load_vector(filename);
vector<string> auxWords;
string enteredWord;
string aux;
//Inicializamos matriz
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
puzzle[i][j] = ' ';
}
}
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
random = 'A' + (rand() % 26);
puzzle[i][j] = random;
}
}
for (int i = 0; i < HEIGHT; i++) {
filarand = rand() % 15;
wordsize = WIDTH - words[i].size() - 1;
wordPos = rand() % wordsize;
for (int j = 0; j < words[i].size(); j++) {
if (words[i].size() < 10) {
aux = "";
aux = words[i].substr(j,1);
puzzle[filarand][wordPos + j] = aux[0];
}
}
}
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
cout << puzzle[i][j];
cout << " ";
}
cout << endl;
}
auxWords = words;
do {
cout << "Introduce una palabra: ";
cin >> enteredWord;
for (int i = 0; i < auxWords.size(); i++) {
if (enteredWord == auxWords[i]) {
okWord = true;
auxWords.erase(auxWords.begin() + i);
break;
}
else {
okWord = false;
}
}
if (okWord == true) {
cout << "Palabra " << enteredWord << " correcta" << endl;
}
else if (enteredWord != "FIN") {
cout << "Palabra " << enteredWord << " incorrecta" << endl;
}
} while (enteredWord != "FIN");
}
/*char wordToMatrix(int HEIGHT, int WIDTH, char puzzle) {
int filarand = 0;
vector<string> load_vector(const string&);
vector<string> words = load_vector(filename);
string aux;
char empty = ' ';
const char* aux2;
char random;
char espacio = ' ';
char puzzle[HEIGHT][WIDTH];
filarand = rand() % 10;
//cout << filarand << endl;
for (int i = 0; i < 10; i++) {
filarand = rand() % 10;
cout << "" << endl;
for (int j = 0; j < words[i].size(); j++) {
if (words[i].size() < 10) {
//aux2 = words[i].substr(j,1);
//aux = words[i].substr(j,1);
//puzzle[i][j] = aux[0];
puzzle[i][j] = words[i].substr(j,1).c_str();
cout << puzzle[i][j];
}
}
}
for (int i = 0; i < 10; i++) {
//cout << endl;
for (int j = 0; j < 10; j++ ) {
if (puzzle[i][j] == '1') {
random = 'A' + (rand() % 26);
puzzle[i][j] = random;
//cout << puzzle[i][j];
}
}
}
for (int i = 0; i < HEIGHT; i++) {
cout << endl;
for (int j = 0; j < WIDTH; j++) {
random = 'A' + (rand() % 26);
if (puzzle[i][j] == espacio) {
puzzle[i][j] = random;
cout << puzzle[i][j];
}
}
}
return puzzle[HEIGHT][WIDTH];
}*/
int main() {
int HEIGHT = 10;
int WIDTH = 10;
char puzzle[HEIGHT][WIDTH];
srand(time(NULL));
aleatorio();
//wordToMatrix(10, 10, puzzle[HEIGHT][WIDTH]);
/*for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
cout << puzzle[i][j] << endl;
}
}*/
return 0;
}