Repository navigation
Expand file tree
/
Copy pathflippity.cpp
More file actions
216 lines (191 loc) · 4.98 KB
/
Copy pathflippity.cpp
File metadata and controls
216 lines (191 loc) · 4.98 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
// flippity.cpp - a simple game with flipping digits
//
// Uses CImg and Allegro libraries and xemacs sounds.
#include <chrono>
#include <iostream>
#include <random>
#include <map>
// #include <allegro/sound.h>
#include <allegro.h>
#include "CImg.h"
using namespace std;
using namespace cimg_library;
class RandomInt {
public:
RandomInt(int low, int high) :dist{low, high} {}
RandomInt(int low, int high, long seed) :dist{low, high}, re(seed) {}
int operator()() { return dist(re); }
private:
uniform_int_distribution<> dist;
default_random_engine re;
};
class Sound {
public:
Sound(const char *nam) :fnam{nam} { s = load_sample(nam); }
int play() { return play_sample(s, 128, 128, 1000, false); }
~Sound() { destroy_sample(s); }
private:
SAMPLE *s;
string fnam;
};
void consequences(Sound &correct, Sound &wrong, int &n_remaining, bool is_correct)
{
if (is_correct) {
cout << "CORRECT. ";
correct.play();
--n_remaining;
} else {
cout << "INCORRECT. ";
wrong.play();
++n_remaining;
}
cout << n_remaining << " to go!" << endl;
}
int encode(int character)
{
if (character >= 'A' && character <= 'Z')
return (character - 'A') + 10;
return (character - 'a') + 10 + 26;
}
char next_alphanum(int &last_alphanum, RandomInt &ri_alphanumeric)
{
int i;
for (;;) {
i = ri_alphanumeric();
if (i == last_alphanum
|| i == encode('A')
|| i == encode('H')
|| i == encode('I')
|| i == encode('M')
|| i == encode('O')
|| i == encode('T')
|| i == encode('U')
|| i == encode('V')
|| i == encode('W')
|| i == encode('X')
|| i == encode('Y')
|| i == encode('i')
|| i == encode('l')
|| i == encode('m')
|| i == encode('o')
|| i == encode('u')
|| i == encode('v')
|| i == encode('w')
|| i == encode('x'))
continue;
switch (i) {
default:
goto out;
case 8:
case 0:
break;
}
}
out:
last_alphanum = i;
if (i <= 9)
return '0' + i;
i -= 10;
if (i < 26)
return i + 'A';
i -= 26;
return 'a' + i;
}
const char *help(int alphanum)
{
if (alphanum <= 9)
return "0123456789";
if (alphanum <= 9 + 26)
return "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
return "abcdefghijklmnopqrstuvwxyz";
}
void sndmap_destroy(map<char,Sound *> &sndmap)
{
for (auto i = 'a'; i <= 'z'; ++i)
delete sndmap[i];
}
void sndmap_init(map<char,Sound *> &sndmap)
{
for (auto i = 'a'; i <= 'z'; ++i) {
auto uc = 'A' + (i - 'a');
string fnam = string("web/static/media/original_sounds/") + i + ".wav";
sndmap[i] = sndmap[uc] = new Sound(fnam.c_str());
}
}
int main()
{
CImg < unsigned char >image(150, 250);
CImgDisplay main_disp(image, "Click Inside to Start");
unsigned char purple[] { 255, 0, 255, };
unsigned char black[] { 0, 0, 0, };
RandomInt ri_flip {0, 1};
auto start = chrono::high_resolution_clock::now();
RandomInt ri_alphanumeric {0, 9 + 26 * 2, start.time_since_epoch().count()};
int n_remaining = 15;
map<char,Sound *> sndmap;
allegro_init();
if (install_sound(DIGI_AUTODETECT, MIDI_NONE, 0) == -1) {
cerr << "allegro error" << endl;
return 1;
}
Sound correct { "/usr/share/xemacs21/xemacs-packages/etc/sounds/ding.wav" };
Sound wrong { "/usr/share/xemacs21/xemacs-packages/etc/sounds/cry.wav" };
sndmap_init(sndmap);
correct.play();
auto last_alphanum = -1;
auto i = next_alphanum(last_alphanum, ri_alphanumeric);
for (; !main_disp.is_closed() && !main_disp.is_key(cimg::keyESC); ) {
bool flip = ri_flip();
auto hoff = flip ? 30 : 20;
main_disp.wait();
if (main_disp.is_key(cimg::keySPACE)) {
auto snd = sndmap.find(i);
if (snd != sndmap.end())
snd->second->play();
if (flip)
image.fill(0)
.draw_text(hoff, 10, "%c", purple, black, 1.0, 200, i)
.mirror('x')
.blur(4)
.display(main_disp);
else
image.fill(0)
.draw_text(hoff, 10, "%c", purple, black, 1.0, 200, i)
.blur(4)
.display(main_disp);
// get the user response
for (;;) {
main_disp.wait();
if (main_disp.is_key(cimg::keyR) && snd != sndmap.end())
snd->second->play();
if (main_disp.is_key(cimg::keyH)) {
image
.draw_text(10, image.height() - 20, help(i),
purple, black, 1.0, 20)
.display(main_disp);
}
if (main_disp.is_key(cimg::keyF)) {
consequences(correct, wrong, n_remaining, !flip);
break;
}
if (main_disp.is_key(cimg::keyB)) {
consequences(correct, wrong, n_remaining, flip);
break;
}
if (main_disp.is_key(cimg::keyESC)) {
cout << "ESC" << endl;
n_remaining = 0;
break;
}
}
if (n_remaining == 0)
break;
i = next_alphanum(last_alphanum, ri_alphanumeric);
}
}
auto end = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(end - start).count();
cout << "You took " << (duration / 1000.0) << " seconds" << endl;
sndmap_destroy(sndmap);
return 0;
}