|
34 | 34 | #include "dealer.h" |
35 | 35 |
|
36 | 36 | namespace lbj { |
37 | | -Dealer::Dealer(Configuration &conf) : rng(dev_random()), fiftyTwoCards(1, 52), fakeshoe(0, RAND_MAX) { |
| 37 | +Dealer::Dealer(Configuration &conf) : rng(dev_random()), fakeshoe(0, RAND_MAX) { |
| 38 | + |
| 39 | + // TODO: remove |
| 40 | + srand(dev_random()); |
| 41 | + //srand(1); |
38 | 42 |
|
39 | 43 | conf.set(&error_standard_deviations, {"error_standard_deviations"}); |
40 | 44 | conf.set(report_file_path, {"report", "report_file", "report_file_path"}); |
41 | 45 | conf.set(&report_verbosity, {"report_verbosity", "report_level"}); |
42 | 46 |
|
43 | 47 | // TODO: explain |
44 | | - conf.set(&real_shoe, {"real_shoe"}); |
45 | 48 | conf.set(&n_players, {"n_players"}); |
46 | 49 |
|
47 | 50 |
|
@@ -337,12 +340,6 @@ Dealer::Dealer(Configuration &conf) : rng(dev_random()), fiftyTwoCards(1, 52), f |
337 | 340 |
|
338 | 341 | // initialize shoe and perform initial shuffle |
339 | 342 | if (n_decks > 0) { |
340 | | - shoe.reserve(52*n_decks); |
341 | | - for (unsigned int deck = 0; deck < n_decks; deck++) { |
342 | | - for (unsigned int tag = 1; tag <= 52; tag++) { |
343 | | - shoe.push_back(tag); |
344 | | - } |
345 | | - } |
346 | 343 | shuffle(); |
347 | 344 | cut_card_position = static_cast<size_t>(penetration * 52 * n_decks); |
348 | 345 | } |
@@ -504,7 +501,9 @@ void Dealer::deal(void) { |
504 | 501 | shuffle(); |
505 | 502 |
|
506 | 503 | // burn as many cards as asked |
507 | | - pos += number_of_burnt_cards; |
| 504 | + for (int i = 0; i < number_of_burnt_cards; i++) { |
| 505 | + draw(); |
| 506 | + } |
508 | 507 | last_pass = false; |
509 | 508 | } |
510 | 509 | info(lbj::Info::NewHand, n_hand, 1e3*playerStats.bankroll); |
@@ -560,6 +559,9 @@ void Dealer::deal(void) { |
560 | 559 | std::cout << "up card " << card[dealer_up_card].utf8() << std::endl; |
561 | 560 | #endif |
562 | 561 | player->value_dealer = hand.value(); |
| 562 | + if (player->value_dealer == 0) { |
| 563 | + std::cout << "momento" << std::endl; |
| 564 | + } |
563 | 565 |
|
564 | 566 | // step 5. deal the second card to each player |
565 | 567 | player_second_card = draw(&(*playerStats.currentHand)); |
@@ -1180,110 +1182,89 @@ int Dealer::process(void) { |
1180 | 1182 | return 0; |
1181 | 1183 | } |
1182 | 1184 |
|
| 1185 | +unsigned int Dealer::count_shoe_cards() { |
| 1186 | + unsigned int total_cards = 0; |
| 1187 | + for (auto i = 1; i <= 52; i++) { |
| 1188 | + total_cards += shoe_cards[i]; |
| 1189 | + } |
| 1190 | + return total_cards; |
| 1191 | +} |
1183 | 1192 |
|
1184 | 1193 | void Dealer::shuffle() { |
1185 | 1194 |
|
1186 | 1195 | // for infinite decks there is no need to shuffle (how would one do it?) |
1187 | 1196 | // we just pick a random card when we need to deal and that's it |
1188 | 1197 | if (n_decks > 0) { |
1189 | | - if (real_shoe == false) { |
1190 | | - for (int i = 2; i <= 9; i++) { |
1191 | | - shoe_cards[i] = 4 * n_decks; |
1192 | | - } |
1193 | | - shoe_cards[10] = 16 * n_decks; // 10, J, Q, K |
1194 | | - shoe_cards[11] = 4 * n_decks; // Ace |
1195 | | - } else { |
1196 | | - std::shuffle(shoe.begin(), shoe.end(), rng); |
1197 | | - pos = 0; |
| 1198 | + for (int i = 1; i <= 52; i++) { |
| 1199 | + shoe_cards[i] = n_decks; |
1198 | 1200 | } |
1199 | 1201 |
|
1200 | 1202 | i_arranged_cards = 0; |
| 1203 | + pos = 0; |
1201 | 1204 | n_shuffles++; |
1202 | 1205 | } |
1203 | 1206 |
|
1204 | 1207 | return; |
1205 | 1208 | } |
1206 | 1209 |
|
1207 | | - |
1208 | | -unsigned int Dealer::draw(Hand *hand) { |
| 1210 | +unsigned int Dealer::draw_tag(void) { |
1209 | 1211 |
|
1210 | | - unsigned int tag = 0; |
1211 | | - |
1212 | 1212 | if (n_decks == 0) { |
1213 | | - |
1214 | | - if (n_arranged_cards == 0 || i_arranged_cards >= n_arranged_cards) { |
1215 | | - tag = fiftyTwoCards(rng); |
1216 | | - } else { |
1217 | | - // negative (or invalid) values are placeholder for random cards |
1218 | | - if ((tag = arranged_cards[i_arranged_cards++]) <= 0 || tag > 52) { |
1219 | | - tag = fiftyTwoCards(rng); |
1220 | | - } |
1221 | | - |
1222 | | - if (quit_when_arranged_cards_run_out && i_arranged_cards == n_arranged_cards) { |
1223 | | - finished(true); |
1224 | | - } |
1225 | | - } |
1226 | | - |
| 1213 | + return 1 + fakeshoe(rng) % 52; |
1227 | 1214 | } else { |
1228 | | - if (real_shoe == false) { |
1229 | | - if (n_arranged_cards == 0 || i_arranged_cards >= n_arranged_cards) { |
1230 | | - int total_cards = 0; |
1231 | | - for (int i = 2; i <= 11; i++) { |
1232 | | - total_cards += shoe_cards[i]; |
1233 | | - } |
1234 | | - |
1235 | | -// int rand_pos = rand() % total_cards; |
1236 | | - unsigned int rand_pos = fakeshoe(rng) % total_cards; |
1237 | | - unsigned cumulative = 0; |
| 1215 | + int total_cards = count_shoe_cards(); |
| 1216 | + if (total_cards == 0) { |
| 1217 | + std::cout << "mamoncho" << std::endl; |
| 1218 | + shuffle(); |
| 1219 | + total_cards = count_shoe_cards(); |
| 1220 | + } |
1238 | 1221 |
|
1239 | | - for (int card = 2; card <= 11; card++) { |
1240 | | - cumulative += shoe_cards[card]; |
1241 | | - if (rand_pos < cumulative) { |
1242 | | - shoe_cards[card]--; |
1243 | | - tag = (card == 11) ? 1 : card; |
1244 | | - break; |
1245 | | - } |
1246 | | - } |
| 1222 | + pos++; |
| 1223 | +// unsigned int rand_pos = rand() % total_cards; |
| 1224 | + unsigned int rand_pos = fakeshoe(rng) % total_cards; |
| 1225 | + unsigned cumulative = 0; |
1247 | 1226 |
|
1248 | | - } else { |
1249 | | - tag = arranged_cards[i_arranged_cards++]; |
1250 | | - auto arranged_card = (tag == 11) ? 1 : tag; |
1251 | | - shoe_cards[arranged_card]--; |
| 1227 | + for (int tag = 1; tag <= 52; tag++) { |
| 1228 | + cumulative += shoe_cards[tag]; |
| 1229 | + if (rand_pos < cumulative) { |
| 1230 | + shoe_cards[tag]--; |
| 1231 | + return tag; |
1252 | 1232 | } |
1253 | | - |
1254 | | - } else { |
1255 | | - |
1256 | | - if (n_arranged_cards == 0 || i_arranged_cards >= n_arranged_cards) { |
1257 | | - last_pass = (pos >= cut_card_position) || shuffle_every_hand; |
1258 | | - if (pos >= 52 * n_decks) { |
1259 | | - shuffle(); |
1260 | | - } |
1261 | | - |
1262 | | - } else { |
1263 | | - |
1264 | | - if ((tag = arranged_cards[i_arranged_cards++]) > 0 && tag < 52) { |
| 1233 | + } |
| 1234 | + } |
1265 | 1235 |
|
1266 | | - // find the original position of the card tag |
1267 | | - auto it = std::find(shoe.begin() + pos, shoe.end(), tag); |
| 1236 | + return 0; |
| 1237 | +} |
| 1238 | + |
| 1239 | +unsigned int Dealer::draw(Hand *hand) { |
| 1240 | + |
| 1241 | + if (n_decks > 0) { |
| 1242 | + last_pass = (pos >= cut_card_position) || shuffle_every_hand; |
| 1243 | + if (pos >= 52 * n_decks) { |
| 1244 | + shuffle(); |
| 1245 | + } |
| 1246 | + } |
1268 | 1247 |
|
1269 | | - // Check if 'tag' was found and pos is valid |
1270 | | - if (it != shoe.end()) { |
1271 | | - // Get the index of the first occurrence of 'tag' |
1272 | | - size_t tag_index = std::distance(shoe.begin(), it); |
1273 | | - |
1274 | | - // Only swap if they're different positions |
1275 | | - if (pos != tag_index) { |
1276 | | - std::swap(shoe[pos], shoe[tag_index]); |
1277 | | - } |
1278 | | - } else { |
1279 | | - std::cerr << "error: no more cards " << tag << " in the shoe" << std::endl; |
1280 | | - exit(1); |
1281 | | - } |
1282 | | - } |
| 1248 | + unsigned int tag = 0; |
| 1249 | + if (n_arranged_cards == 0 || i_arranged_cards >= n_arranged_cards) { |
| 1250 | + tag = draw_tag(); |
| 1251 | + } else { |
| 1252 | + if ((tag = arranged_cards[i_arranged_cards++]) == 0) { |
| 1253 | + // zero means random |
| 1254 | + tag = draw_tag(); |
| 1255 | + } |
| 1256 | + if (n_decks > 0) { |
| 1257 | + if (shoe_cards[tag] == 0) { |
| 1258 | + std::cerr << "error: no more cards " << tag << " in the shoe" << std::endl; |
| 1259 | + exit(1); |
1283 | 1260 | } |
1284 | | - tag = shoe[pos++]; |
| 1261 | + shoe_cards[tag]--; |
1285 | 1262 | } |
1286 | 1263 | } |
| 1264 | + |
| 1265 | + if (quit_when_arranged_cards_run_out && i_arranged_cards == n_arranged_cards) { |
| 1266 | + finished(true); |
| 1267 | + } |
1287 | 1268 |
|
1288 | 1269 | if (hand != nullptr) { |
1289 | 1270 | hand->cards.push_back(tag); |
|
0 commit comments