forked from doctorlove/BootstrapCpp
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
442 lines (389 loc) · 12.1 KB
/
main.cpp
File metadata and controls
442 lines (389 loc) · 12.1 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include <cassert>
#include <concepts>
#include <coroutine>
#include <iostream>
#include <optional>
#include <functional>
#include <map>
#include <memory>
#include <random>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
// Listing 8.1 Read an optional zero or one
std::optional<int> read_number(std::istream& in)
{
std::string line;
std::getline(in, line);
if (line == "0") {
return { 0 };
}
else if (line == "1") {
return { 1 };
}
return {};
}
// Listing 8.2 A pennies game
void pennies_game()
{
int player_wins = 0;
int turns = 0;
std::mt19937 gen{ std::random_device{}() };
std::uniform_int_distribution dist(0, 1);
std::cout << "Select 0 or 1 at random and press enter.\n";
std::cout << "If the computer predicts your guess it wins.\n";
while (true)
{
const int prediction = dist(gen);
auto input = read_number(std::cin);
if (!input)
{
break;
}
const int player_choice = input.value();
++turns;
std::cout << "You pressed " << player_choice << ", I guessed " << prediction << '\n';
if (player_choice != prediction)
{
++player_wins;
}
}
std::cout << "you win " << player_wins << '\n'
<< "I win " << turns - player_wins << '\n';
}
// Listing 8.3 Three possible choices and outcomes
enum class Choice
{
Same,
Change,
Shrug,
};
enum class Outcome
{
Lose,
Win,
Unset,
};
using state_t = std::tuple<Outcome, Choice, Outcome>;
using last_choices_t = std::pair<Choice, Choice>;
// Listing 8.5 Specializing std::hash for our state tuple
template<>
struct std::hash<state_t>
{
std::size_t operator()(state_t const& state) const noexcept
{
std::size_t h1 = std::hash<Outcome>{}(std::get<0>(state));
std::size_t h2 = std::hash<Choice>{}(std::get<1>(state));
std::size_t h3 = std::hash<Outcome>{}(std::get<2>(state));
return h1 + (h2 << 1) + (h3 << 2);
}
};
// Listing 8.6 An initial state table
std::unordered_map<state_t, last_choices_t> initial_state()
{
const auto unset = std::pair<Choice, Choice>{ Choice::Shrug, Choice::Shrug };
return {
{ {Outcome::Lose, Choice::Same, Outcome::Lose}, unset },
{ {Outcome::Lose, Choice::Same, Outcome::Win}, unset },
{ {Outcome::Lose, Choice::Change, Outcome::Lose}, unset },
{ {Outcome::Lose, Choice::Change, Outcome::Win}, unset },
{ {Outcome::Win, Choice::Same, Outcome::Lose}, unset },
{ {Outcome::Win, Choice::Same, Outcome::Win}, unset },
{ {Outcome::Win, Choice::Change, Outcome::Lose}, unset },
{ {Outcome::Win, Choice::Change, Outcome::Win}, unset },
};
}
// Listing 8.7 Class to track the game's state
class State
{
std::unordered_map<state_t, last_choices_t> state_lookup = initial_state();
public:
// Listing 8.8 Find the choices or return two shrugs
last_choices_t choices(const state_t& key) const
{
if (auto it = state_lookup.find(key); it!=state_lookup.end())
{
return it->second;
}
else
{
return { Choice::Shrug, Choice::Shrug };
}
}
// Listing 8.9 Update choices for valid keys
void update(const state_t& key, const Choice& turn_changed)
{
if (auto it = state_lookup.find(key); it != state_lookup.end())
{
const auto [prev2, prev1] = it->second;
last_choices_t value{ prev1, turn_changed };
it->second = value;
}
}
};
// Listing 8.10 Choice from state
Choice prediction_method(const last_choices_t& choices)
{
if (choices.first == choices.second)
{
return choices.first;
}
else
{
return Choice::Shrug;
}
}
// Listing 8.11 A mind-reading class
template <std::invocable<> T, typename U>
class MindReader {
State state_table;
T generator;
U distribution;
int prediction = flip();
state_t state{Outcome::Unset, Choice::Shrug, Outcome::Unset};
int previous_go = -1;
// Listing 8.12 Update the prediction
bool update_prediction(int player_choice)
{
bool guessing = false;
Choice option = prediction_method(state_table.choices(state));
switch (option)
{
case Choice::Shrug:
prediction = flip();
guessing = true;
break;
case Choice::Change:
prediction = player_choice ^ 1;
break;
case Choice::Same:
prediction = player_choice;
break;
}
return guessing;
}
int flip()
{
return distribution(generator);
}
public:
MindReader(T gen, U dis)
: generator(gen), distribution(dis)
{
}
int get_prediction() const
{
return prediction;
}
// Listing 8.13 The mind reader's update method
bool update(int player_choice)
{
const Choice turn_changed = player_choice == previous_go ? Choice::Same : Choice::Change;
state_table.update(state, turn_changed);
previous_go = player_choice;
state = {std::get<2>(state), turn_changed, (player_choice != prediction) ? Outcome::Win : Outcome::Lose};
return update_prediction(player_choice);
}
};
// Listing 8.6 Check we have no hash collisions (and more besides)
void check_properties()
{
// No bucket clashes
std::unordered_map<state_t, last_choices_t> states = initial_state();
for (size_t bucket = 0; bucket < states.bucket_count(); bucket++)
{
auto bucket_size = states.bucket_size(bucket);
assert(bucket_size <= 1);
}
{
MindReader mr([]() { return 0; }, [](auto gen) { return gen(); });
assert(mr.update(0)); // guesses first
assert(mr.update(0)); // second is a guess too
}
assert(prediction_method({ Choice::Shrug, Choice::Shrug }) == Choice::Shrug);
assert(prediction_method({ Choice::Shrug, Choice::Change }) == Choice::Shrug);
assert(prediction_method({ Choice::Shrug, Choice::Same }) == Choice::Shrug);
assert(prediction_method({ Choice::Change, Choice::Shrug }) == Choice::Shrug);
assert(prediction_method({ Choice::Same, Choice::Shrug }) == Choice::Shrug);
assert(prediction_method({ Choice::Change, Choice::Change }) == Choice::Change);
assert(prediction_method({ Choice::Same, Choice::Same }) == Choice::Same);
assert(prediction_method({ Choice::Change, Choice::Same }) == Choice::Shrug);
assert(prediction_method({ Choice::Same, Choice::Change }) == Choice::Shrug);
State s;
auto got1 = s.choices({ Outcome::Unset, Choice::Shrug, Outcome::Unset });
assert(got1.first == Choice::Shrug);
assert(got1.second == Choice::Shrug);
auto got2 = s.choices({ Outcome::Lose, Choice::Same, Outcome::Unset });
assert(got2.first == Choice::Shrug);
assert(got2.second == Choice::Shrug);
auto got3 = s.choices({ Outcome::Win, Choice::Change, Outcome::Unset });
assert(got3.first == Choice::Shrug);
assert(got3.second == Choice::Shrug);
{
// Listing 6.12 had a RandomBlob we tested, using a lambda to stub out the random function
// This always returns 0
MindReader mr([]() { return 0; }, [](auto gen) { return gen(); });
assert( mr.update(0)); //flip first two goes
assert( mr.update(0)); //flip first two goes
// The random generator always returns zero
// it guesses first
// state is
// lose, same, lose
// but without two matching next choices
assert( mr.update(0));
// now
// lose, same, lose -> -1 ,lose
// so when we decide 0 it stops guessing
// now the state is
// lose, same, lose -> lose ,lose
// so it will predict a 0 next
assert(!mr.update(0));
assert( mr.get_prediction() == 0);
assert(!mr.update(0));
assert( mr.get_prediction() == 0);
assert(!mr.update(0));
assert( mr.get_prediction() == 0);
assert(!mr.update(0));
assert( mr.get_prediction() == 0);
assert(!mr.update(0));
assert( mr.get_prediction() == 0);
}
}
// Listing 8.14 A mind reading game
void mind_reader()
{
int turns = 0;
int player_wins = 0;
int guessing = 0;
std::mt19937 gen{ std::random_device{}() };
std::uniform_int_distribution dist{ 0, 1 };
MindReader mr(gen, dist);
std::cout << "Select 0 or 1 at random and press enter.\n";
std::cout << "If the computer predicts your guess it wins\n";
std::cout << "and it can now read your mind.\n";
while (true)
{
const int prediction = mr.get_prediction();
auto input = read_number(std::cin);
if (!input)
{
break;
}
const int player_choice = input.value();
++turns;
std::cout << "You pressed " << player_choice
<< ", I guessed " << prediction << '\n';
if (player_choice != prediction)
{
++player_wins;
}
if (mr.update(player_choice))
{
++guessing;
}
}
std::cout << "you win " << player_wins << '\n'
<< "machine guessed " << guessing << " times" << '\n'
<< "machine won " << (turns - player_wins) << '\n';
}
// Listing 8.18 Customer "deleter"
template<typename Promise>
struct coro_deleter
{
void operator()(Promise* promise) const noexcept
{
auto handle = std::coroutine_handle<Promise>::from_promise(*promise);
if (handle)
handle.destroy();
}
};
template<typename T>
using promise_ptr = std::unique_ptr<T, coro_deleter<T>>;
// Listing 8.17, 8.20 and 8.21 The coroutine's Task and promise_type
struct Task
{
// Listing 8.19 Our promise type
struct promise_type
{
std::pair<int, int> choice_and_prediction;
Task get_return_object()
{
return Task(this);
}
std::suspend_never initial_suspend() noexcept { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
void unhandled_exception() {}
std::suspend_always yield_value(std::pair<int, int> got)
{
choice_and_prediction = got;
return {};
}
void return_void() { }
};
std::pair<int, int> choice_and_prediction() const
{
return promise->choice_and_prediction;
}
bool done() const
{
auto handle = std::coroutine_handle<promise_type>::from_promise(*promise);
return handle.done();
}
void next()
{
auto handle = std::coroutine_handle<promise_type>::from_promise(*promise);
handle();
}
private:
promise_ptr<promise_type> promise;
Task(promise_type* p) : promise(p) {}
};
// Listing 8.16 Our first coroutine
Task coroutine_game()
{
std::mt19937 gen{ std::random_device{}() };
std::uniform_int_distribution dist{ 0, 1 };
MindReader mr(gen, dist);
while (true)
{
auto input = read_number(std::cin);
if (!input)
{
co_return;
}
int player_choice = input.value();
co_yield{ player_choice , mr.get_prediction() };
mr.update(player_choice);
}
}
// Listing 8.22 A coroutine version of a mind reader
void coroutine_minder_reader()
{
int turns = 0;
int player_wins = 0;
std::cout << "Select 0 or 1 at random and press enter.\n";
std::cout << "If the computer predicts your guess it wins\nand it can now read your mind.\n";
Task game = coroutine_game();
while (!game.done())
{
auto [player_choice, prediction] = game.choice_and_prediction();
++turns;
std::cout << "You pressed " << player_choice << ", I guessed " << prediction << '\n';
if (player_choice != prediction)
{
++player_wins;
}
game.next();
}
std::cout << "you win " << player_wins << '\n'
<< "machine won " << (turns - player_wins) << '\n';
}
int main()
{
check_properties();
pennies_game();
// Choose one version of the mind reader (or play both if you want):
mind_reader();
//coroutine_minder_reader();
}