-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguess_square_number.cpp
95 lines (77 loc) · 2.1 KB
/
guess_square_number.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <limits>
#include <vector>
#include <ctime>
#include <algorithm>
#include <random>
class GuessSquareNumber{
private:
std::vector<int> square_number{};
int start{};
int how_many{};
private:
unsigned int acak_angka(){
std::mt19937 acak {static_cast<std::mt19937::result_type>(std::time(nullptr))};
std::uniform_int_distribution<int> batas_acak {2,4};
return batas_acak(acak);
}
void isi_vector(int many){
for(int i{1}; i<=many; ++i){
square_number.emplace_back(start * start * acak_angka());
++start;
}
}
bool input_validation(int &x)const{
std::cin>>x;
if(std::cin.fail()){
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
return false;
}
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
return true;
}
void logic(){
for(int i{how_many}; i>0; --i){
int tebak;
std::cout<<"> ";
std::cin>>tebak;
std::cout<<"> "<<tebak;
auto temu{std::find(square_number.begin(), square_number.end(),tebak)};
//need refactoring
if(temu != square_number.end()){
square_number.erase(temu);
if(i > 1){
std::cout<<"\nNice! "<<(square_number.size())<<" numbers left\n";
}else{
printf( "\nNice! You found all number, good job!\n");
}
}else{
std::cout<<'\n'<<tebak<<" is wrong! Try again next time\n";
break;
}
}
}
public:
GuessSquareNumber() = default;
void play(){
while(true){
std::cout<<"Start where? ";
if(input_validation(start))break;
}
while(true){
std::cout<<"\nHow many? ";
if(input_validation(how_many))break;
}
isi_vector(how_many);
std::cout<<"\nI generated "<<how_many<<" square numbers. Do you know what each number is after multiplying it by "<<acak_angka()<<std::endl;
std::cout<<"\n\n";
logic();
}
};
int main(int argc, char const **argv){
GuessSquareNumber g;
g.play();
return 0;
}