Hello ,
I would like share this beginner code in order to get any advises.
( if you have time , thanks)
Description:
Choose a level. Level number determines the maximum numbers of words to turn into an hidden word
Every hidden word is overwrote by "_" char and identified by a progressive number
You have to choice a number and guess what words is back before to press enter
Example:
original text : Students compile a collection of their texts in a variety of genres
<<Difficulty level 1-10 ( 1 = harder): ? 5>>
Students 1______ a collection of 2____ texts 3__ a variety 4__ genres
<<What number you choose ? 2>>
speak loudly what word you have guessed..
Here is : their
<<What number you choose ? >>
and so on...
Code:
#include "pch.h"
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <set>
#include <ctime>
#include <random>
using namespace std;
vector <wstring> countWords(wstring text);
vector<int> mersenne(int seed, int start, int end, size_t howManyNumb);
vector<wstring> quizIt(vector <wstring> words, vector <int> rnd);
const unsigned int seed = time(0);
int main()
{
wstring text( // Odissey
L"Narrami, o musa , l'uomo dall'agile mente che a lungo andó vagando, "
"poi che cadde troia ,la forte città e di molte genti vide le terre "
"e così conobbe la natura dell'anima, molti dolori patì nel suo cuore "
" lungo le vie del mare lottando per tornare in patria coi compagni. "
" Ma per loro follià (come simili a fanciulli !) non li potè sottrarre "
" alla morte. Poi che mangiarono i buoi del sole , figlio del cielo , "
" tolse loro il tempo di ritorno. Questo narrami,o dea , figlia di Zeus "
" e comincia di dove tu vuoi ");
vector <wstring> quiz;
vector <wstring> Words;
vector <int> rnd;
size_t level = 1;
int difficulty = 1;
cout << "Difficulty level 1-10 ( 1 = harder):> ";
cin >> difficulty;
Words = countWords(text);
level = (Words.size() / difficulty)-1;
cout << level << " " << Words.size();
rnd = mersenne(seed, 1, Words.size() - 1, level);
quiz = quizIt(Words,rnd);
int wordNumber = 0;
while (1) {
cout << "Completa numero:> " ;
cin >> wordNumber;
wcout << quiz.at(wordNumber) << endl;
}// ctrl-c to exit
return 0;
}
vector <wstring> countWords(wstring text) {
wstring wordExtracted;
vector <wstring> words;
wistringstream iss(text);
while (iss >> wordExtracted) { words.push_back(wordExtracted); }
return words;
}
vector<wstring> quizIt(vector <wstring> words, vector <int> rnd) {
vector <wstring> hiddenWords;
vector <wstring>::iterator itP;
vector <int>::iterator it;
int newLine = 0;
int contator=0;
std::cout << "Hello World!\n";
for (it = rnd.begin(); it < rnd.end(); it++) {
hiddenWords.push_back(words[*it]); // saves the hidden words choices by random numb generator
// overwrites the hiddenwords by '_' char and add an id number
words.at(*it) = to_wstring(contator++) + wstring(words[*it].size(), '_');
}
setlocale(LC_ALL, ".437");
for (itP = words.begin(); itP < words.end(); itP++) {
if (!(newLine = (newLine + 1) % 12)) cout << endl;
wcout << *itP << ' ';
}
cout << endl;
return hiddenWords;
}
vector<int> mersenne(int seed, int start, int end, size_t howManyNumb) {
set<int> numb;
vector<int> result;
int rnd = 0;
mt19937_64 rndGen(seed);
uniform_int_distribution<int> rndDist(1, end);
while (numb.size() < howManyNumb) {
rnd = rndDist(rndGen);
if (numb.insert(rnd).second) { // If doesn't fail ,saves rnd to result
result.push_back(rnd);
}
}
return result;
}