-
Notifications
You must be signed in to change notification settings - Fork 0
/
bullsandcows.cpp
177 lines (172 loc) · 4.57 KB
/
bullsandcows.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <ctime>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <utility>
#include <time.h>
int read_amount() {
std::string input;
int res = 0;
std::cout << "Enter the number of digits: ";
while (true) {
std::cin >> input;
if (input.length() > 2) {
std::cout << "Error: input string is too large. Try again: " << std::endl;
continue;
}
if (input.length() == 1) {
if (input[0] < '0' || input[0] > '9') {
std::cout << "Error: input string is not a number. Try again: " << std::endl;
continue;
}
if (input[0] > '9' || input[0] < '2') {
std::cout << "Error: input is out of range. Valid range is from 2 to 10. Try again: " << std::endl;
continue;
}
}
if (input.length() == 2) {
if (input[0] < '0' || input[0] > '9') {
std::cout << "Error: input string is not a number. Try again: " << std::endl;
continue;
}
if (input[1] < '0' || input[1] > '9') {
std::cout << "Error: input string is not a number. Try again: " << std::endl;
continue;
}
if (input[0] > '1' || input[1] != '0') {
std::cout << "Error: input is out of range. Valid range is from 2 to 10. Try again: " << std::endl;
continue;
}
}
input.length() == 2 ? res = 10 : res = static_cast<int>(input[0] - 48);
break;
}
return res;
}
std::pair<int, int> game() {
int num = read_amount();
std::vector<int> arr;
for (auto i = 0; i < num; ++i) {
int dig;
for (bool flag = true; flag != false;) {
dig = rand() % 10;
flag = false;
for (auto j : arr) {
if (dig == j) {
flag = true;
break;
}
}
}
arr.push_back(dig);
}
int cows;
int bulls;
std::vector<int> guess;
int guess_cnt = 0;
while (true) {
cows = 0;
bulls = 0;
guess_cnt++;
std::cout << "Make a guess: ";
std::string input;
bool flag = true;
while (flag) {
flag = false;
std::cin >> input;
if (input.length() > static_cast<size_t>(num)) {
std::cout << "Error: input string is too large. Try again: " << std::endl;
flag = true;
continue;
}
for (auto i = 0; i < num; ++i) {
if (input[i] > '9' || input[i] < '0') {
std::cout << "Error: invalid character found in the input. Try again: ";
flag = true;
break;
}
}
if (flag) continue;
for (auto i = 0; i < num; ++i) {
for (auto j = 0; j < i; ++j) {
if (input[i] == input[j]) {
std::cout << "Error: repeating character found in the input. Try again: ";
flag = true;
break;
}
}
if (flag) break;
}
}
for (auto i = 0; i < num; ++i) {
guess.push_back(static_cast<int>(input[i] - 48));
}
for (auto i = 0; i < num; ++i) {
for (auto j = 0; j < num; ++j) {
if (arr.at(i) == guess.at(j)) {
if (i == j) bulls++;
else cows++;
}
}
}
std::cout << bulls << " bulls, " << cows << " cows" << std::endl;
if (bulls == num) break;
guess.clear();
}
std::cout << "Congratulation, you won! It took you " << guess_cnt << " guesses!" << std::endl;
return std::pair<int, int>(num, guess_cnt);
}
void get_answer(bool &b) {
std::cout << "Do you want to play again? (Y/N)" << std::endl;
std::string input;
for (auto answer = 'N';;) {
std::cin >> input;
if (input.length() == 1) {
answer = input.at(0);
}
else {
std::cout << "Error: input string is too large. Valid inputs are Y and N. Try again: " << std::endl;
continue;
}
if (toupper(answer) == 'Y') {
b = true;
return;
}
else if (toupper(answer) == 'N') {
b = false;
return;
}
else {
std::cout << "Error: invalid input. Valid inputs are Y and N. Try again: " << std::endl;
continue;
}
}
}
void save_result(std::pair<int, int> game_result) {
std::ofstream fout("game_results.txt", std::ios_base::app);
char time_output[40];
tm current_time;
const time_t timer = time(NULL);
localtime_s(¤t_time, &timer);
strftime(time_output, 40, "%d.%m.%Y %H:%M:%S : ", ¤t_time);
if (fout.is_open())
{
fout << time_output << "game was completed in " << game_result.second <<
" guesses for " << game_result.first << " digits." << std::endl;
fout.close();
}
else {
std::cout << "Warning: the result of this game will not be saved as the file for storing results could not be opened/created" << std::endl;
}
}
int main() {
srand(static_cast<int>(time(NULL)));
std::pair<int, int> game_result;
for (auto answer = true; answer;) {
game_result = game();
save_result(game_result);
get_answer(answer);
}
return 0;
}