-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.cpp
executable file
·301 lines (263 loc) · 7.93 KB
/
loader.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
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
#include "loader.h"
#include "libxl.h"
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::string;
using namespace libxl;
// using a separate validate method allows more flexibility
// in the spreadsheet format. It allows any combination of
// tables to be read in, whereas to make the code more
// efficient (validate itself as it loads) would require
// posting the tables in the spreadsheet in specific order
bool Loader::validate() const
{
bool ret = true;
// 1) make sure every reel symbol appears in the list of symbols
for(auto const & reel : m_reels_str)
{
for(auto const & sym : reel)
{
if(m_symbols.end() == std::find(m_symbols.begin(), m_symbols.end(), sym))
{
cout << "Error: reel sym " << sym << " not found in symbols list" << endl;
ret = false;
}
}
}
// 2) every payout symbol must be included in the symbols list
for(auto const & payout : m_payout_str)
{
int count = 0; // only care about first 3 syms
for(auto const &sym : payout)
{
if(m_symbols.end() == std::find(m_symbols.begin(), m_symbols.end(), sym) && count < 3)
{
cout << "Error: payout sym " << sym << " not found in symbols list" << endl;
ret = false;
}
++count;
}
}
// 3) (skipped) to be _very_ technical, you would verify that
// each pay was physically capable of being paid, i.e. each
// winning symbol must appear at least once on the reel
return ret;
}
// convert strings to enumerated symbols, ready to be evaluated
bool Loader::load()
{
// converts string BAR_2X to int, e.g. 2
auto encode = [this](auto const &sym) -> int {
return (int)std::distance(m_symbols.begin(),
std::find(m_symbols.begin(), m_symbols.end(), sym));
};
int c = 0;
for(auto const & reel : m_reels_str)
{
std::vector<int> reel_int;
//cout << "Reel: " << c << endl;
++ c;
for(auto const & sym : reel)
{
auto num = encode(sym);
//cout << "mapping " << sym << " to " << num << endl;
reel_int.push_back(num);
if(num > 9)
{
cout << "--- Loader Error: reel > 9: " << num << " / " << sym << endl;
throw 0;
}
}
cout << endl;
m_reels.emplace_back(reel_int);
}
for(auto const & pay : m_payout_str)
{
WinTuple win;
win.win = std::stoi(pay[3]);
win.symbols = { encode(pay[0]), encode(pay[1]), encode(pay[2]) };
m_pays.push_back(win);
}
return true;
}
Loader::Loader(string fname)
{
auto book = xlCreateXMLBook();
if(book->loadSheet(fname.c_str(), 0))
{
auto sheet = book->getSheet(0);
if(sheet)
{
for(int i =0 ;i < 100; ++ i)
{
for(int j = 0; j < 200; ++ j)
{
char const * text = sheet->readStr(i,j);
if(nullptr != text)
{
auto cellText {std::string(text) };
if("Symbols:" == cellText)
loadSymbols(sheet, i,j);
else if("Reels:" == cellText)
loadReels(sheet,i,j);
else if("Pay Schedule:" == cellText)
loadPays(sheet,i+2,j);
else if("Paylines:" == cellText)
loadPaylines(sheet,i+2,j);
}
}
}
}
book->release();
}
}
void Loader::loadPaylines(Sheet *sheet, int row, int col)
{
cout << "Loading Paylines: " << endl;
// assumes a 3-reel machine, although generalization is possible
string reel_window[3][3];
for(int reel = 0; reel < 3; ++ reel)
{
for(int j = 0; j < 3; ++ j)
{
auto text = sheet->readStr(row+j, col+reel);
if(nullptr != text)
reel_window[reel][j] = text;
else
throw std::invalid_argument("Paylines must be 3x3 integers");
}
}
// helper function
auto contains = [](string const &heystack, int needle) -> bool {
bool ret = (needle < 10);
ret &= (string::npos != heystack.find(char(needle + '0')));
return ret;
};
// paylines are written graphically in excel, concatenated
// digit-wise with other payline geometries. they must start
// at 1 and increment sequentially. e.g.
// 1 X X
// X 1 X
// X X 1
//
// 1 X X
// 2 21 2
// X X 1
//
// decode up to 9 paylines. this could be extended to 35 if you used
// letters + numbers, 61 if CAPS letters
for(int pay_idx = 1; pay_idx < 10; ++pay_idx)
{
std::vector<int> pl(3,0);
int found = 0;
// find this payline's position in the window
// relative to the centerline
for(int reel=0; reel <3; ++reel)
{
for(int pos=0; pos <3; ++pos)
{
if(contains(reel_window[reel][pos],pay_idx))
{
pl[reel] = pos;
++ found;
break;
}
}
}
cout << "pl: " << pl[0] << " " << pl[1] << " " << pl[2] << endl;
if(3 == found)
{
m_paylines[pay_idx] = pl;
}
else
{
if(0 == found)
break;
else
throw std::invalid_argument("Invalid Payline definition: " + std::to_string(pay_idx));
}
}
}
void Loader::loadPays(Sheet *sheet, int row, int col)
{
cout << "Loading Pays: " << endl;
std::vector<string> payline(4,"");
for(int i = 0; i < 1000; ++ i)
{
auto text = sheet->readStr(row+i, col);
if(nullptr != text)
{
// record the 3 syms + payout (as string)
for(int j = 0; j < 3; ++ j)
{
payline[j] = text;
text = sheet->readStr(row+i, col+1+j);
if(nullptr == text)
throw std::invalid_argument("Payout Schedule must have 4 entries");
}
// the payout amount (as string)
payline[3] = text;
m_payout_str.push_back(payline);
}
else if (nullptr == sheet->readStr(row+i+1,col))
{
// quit after two successive blank rows
break;
}
}
cout << "Total pays: " << m_payout_str.size() << endl;
}
void Loader::loadReels(Sheet *sheet, int row, int col)
{
cout << "Loading reels: " << endl;
// right now, hard-coded to 3 reels, can easily be generalized
for(int reel = 0; reel < 3; ++ reel)
{
std::vector<string> reel_text;
for(int i = 2; i < 40; ++ i)
{
auto text = sheet->readStr(row+i, col + reel);
if(nullptr != text)
{
string label = text;
reel_text.push_back(label);
//cout << "lbl = " << label << endl;
// always add a blank
reel_text.push_back("BL");
}
else
{
// cancel after this section is read
break;
}
}
m_reels_str.emplace_back(std::move(reel_text));
}
}
void Loader::loadSymbols(Sheet *sheet, int row, int col)
{
cout << "Loading symbols: " << endl;
for(int i = 2; i < 20; ++ i)
{
auto text = sheet->readStr(row+i, col);
if(nullptr != text)
{
string label = text;
text = sheet->readStr(row+i, col+1);
if(nullptr == text)
throw std::invalid_argument("Symbol " + label + " must have short alias in next column.");
string sym = text;
m_symbols.push_back(sym);
cout << "Sym: " << label << " / " << sym << endl;
}
else
{
// cancel after this section is read
break;
}
}
}