deck.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. #include "deck.h"
  2. #include <algorithm>
  3. #include <map>
  4. #include <sstream>
  5. Deck::Deck(int size) {
  6. cardback = door::ANSIColor(door::COLOR::RED);
  7. card_height = size;
  8. init();
  9. }
  10. Deck::Deck(door::ANSIColor backcolor, int size) : cardback{backcolor} {
  11. card_height = size;
  12. init();
  13. }
  14. void Deck::init(void) {
  15. if (card_height != 3) {
  16. if (card_height != 5) {
  17. card_height = 3;
  18. }
  19. }
  20. for (int i = 0; i < 52; ++i) {
  21. cards.push_back(card_of(i));
  22. }
  23. // 0 = BLANK, 1-4 levels
  24. for (int i = 0; i < 5; ++i) {
  25. backs.push_back(back_of(i));
  26. }
  27. mark.push_back(mark_of(0));
  28. mark.push_back(mark_of(1));
  29. }
  30. Deck::~Deck() {
  31. for (auto c : cards) {
  32. delete c;
  33. }
  34. cards.clear();
  35. for (auto b : backs) {
  36. delete b;
  37. }
  38. backs.clear();
  39. for (auto m : mark) {
  40. delete m;
  41. }
  42. mark.clear();
  43. }
  44. Deck::Deck(Deck &&ref) {
  45. cardback = ref.cardback;
  46. for (auto c : cards)
  47. delete c;
  48. cards.clear();
  49. cards = ref.cards;
  50. ref.cards.clear();
  51. for (auto b : backs)
  52. delete b;
  53. backs.clear();
  54. backs = ref.backs;
  55. ref.backs.clear();
  56. for (auto m : mark)
  57. delete m;
  58. mark.clear();
  59. mark = ref.mark;
  60. ref.mark.clear();
  61. card_height = ref.card_height;
  62. };
  63. Deck &Deck::operator=(Deck &&ref) {
  64. cardback = ref.cardback;
  65. for (auto c : cards)
  66. delete c;
  67. cards.clear();
  68. cards = ref.cards;
  69. ref.cards.clear();
  70. for (auto b : backs)
  71. delete b;
  72. backs.clear();
  73. backs = ref.backs;
  74. ref.backs.clear();
  75. for (auto m : mark)
  76. delete m;
  77. mark.clear();
  78. mark = ref.mark;
  79. ref.mark.clear();
  80. card_height = ref.card_height;
  81. return *this;
  82. }
  83. int Deck::is_deck(int c) { return c / 52; }
  84. int Deck::is_suit(int c) { return (c % 52) / 13; }
  85. int Deck::is_rank(int c) { return (c % 52) % 13; }
  86. char Deck::rank_symbol(int c) {
  87. const char symbols[] = "A23456789TJQK";
  88. return symbols[c];
  89. }
  90. std::string Deck::suit_symbol(int c) {
  91. // unicode
  92. if (door::unicode) {
  93. switch (c) {
  94. case 0:
  95. return std::string("\u2665");
  96. case 1:
  97. return std::string("\u2666");
  98. case 2:
  99. return std::string("\u2663");
  100. case 3:
  101. return std::string("\u2660");
  102. }
  103. } else {
  104. if (door::full_cp437) {
  105. switch (c) {
  106. case 0:
  107. return std::string(1, '\x03');
  108. case 1:
  109. return std::string(1, '\x04');
  110. case 2:
  111. return std::string(1, '\x05');
  112. case 3:
  113. return std::string(1, '\x06');
  114. }
  115. } else {
  116. // These look horrible!
  117. switch (c) {
  118. case 0:
  119. return std::string(1, '*'); // H
  120. case 1:
  121. return std::string(1, '^'); // D
  122. case 2:
  123. return std::string(1, '%'); // C
  124. case 3:
  125. return std::string(1, '$'); // S
  126. }
  127. }
  128. }
  129. return std::string("!", 1);
  130. }
  131. door::Panel *Deck::card_of(int c) {
  132. int suit = is_suit(c);
  133. int rank = is_rank(c);
  134. bool is_red = (suit < 2);
  135. door::ANSIColor color;
  136. if (is_red) {
  137. color = door::ANSIColor(door::COLOR::RED, door::COLOR::WHITE);
  138. } else {
  139. color = door::ANSIColor(door::COLOR::BLACK, door::COLOR::WHITE);
  140. }
  141. door::Panel *p = new door::Panel(0, 0, 5);
  142. // setColor sets border_color. NOT WHAT I WANT.
  143. // p->setColor(color);
  144. char r = rank_symbol(rank);
  145. std::string s = suit_symbol(suit);
  146. // build lines
  147. std::ostringstream oss;
  148. oss << r << s << " ";
  149. std::string str = oss.str();
  150. p->addLine(std::make_unique<door::Line>(str, 5, color));
  151. oss.str(std::string());
  152. oss.clear();
  153. if (card_height == 5)
  154. p->addLine(std::make_unique<door::Line>(" ", 5, color));
  155. oss << " " << s << " ";
  156. str = oss.str();
  157. p->addLine(std::make_unique<door::Line>(str, 5, color));
  158. oss.str(std::string());
  159. oss.clear();
  160. if (card_height == 5)
  161. p->addLine(std::make_unique<door::Line>(" ", 5, color));
  162. oss << " " << s << r;
  163. str = oss.str();
  164. p->addLine(std::make_unique<door::Line>(str, 5, color));
  165. oss.str(std::string());
  166. oss.clear();
  167. return p;
  168. }
  169. std::string Deck::back_char(int level) {
  170. std::string c;
  171. if (level == 0) {
  172. c = ' ';
  173. return c;
  174. }
  175. if (door::unicode) {
  176. switch (level) {
  177. case 1:
  178. c = "\u2591";
  179. break;
  180. case 2:
  181. c = "\u2592";
  182. break;
  183. case 3:
  184. c = "\u2593";
  185. break;
  186. case 4:
  187. c = "\u2588";
  188. break;
  189. }
  190. } else {
  191. switch (level) {
  192. case 1:
  193. c = "\xb0";
  194. break;
  195. case 2:
  196. c = "\xb1";
  197. break;
  198. case 3:
  199. c = "\xb2";
  200. break;
  201. case 4:
  202. c = "\xdb";
  203. break;
  204. }
  205. }
  206. return c;
  207. }
  208. door::Panel *Deck::back_of(int level) {
  209. // using: \xb0, 0xb1, 0xb2, 0xdb
  210. // OR: \u2591, \u2592, \u2593, \u2588
  211. // door::ANSIColor color(door::COLOR::RED, door::COLOR::BLACK);
  212. door::Panel *p = new door::Panel(0, 0, 5);
  213. std::string c = back_char(level);
  214. std::string l = c + c + c + c + c;
  215. for (int x = 0; x < card_height; ++x) {
  216. p->addLine(std::make_unique<door::Line>(l, 5, cardback));
  217. };
  218. // p->addLine(std::make_unique<door::Line>(l, 5, cardback));
  219. // p->addLine(std::make_unique<door::Line>(l, 5, cardback));
  220. return p;
  221. }
  222. door::Panel *Deck::mark_of(int c) {
  223. door::Panel *p = new door::Panel(1);
  224. door::ANSIColor color = door::ANSIColor(
  225. door::COLOR::BLUE, door::COLOR::WHITE); // , door::ATTR::BOLD);
  226. std::string m;
  227. if (c == 0)
  228. m = " ";
  229. else {
  230. if (door::unicode) {
  231. m = "\u25a0";
  232. } else {
  233. m = "\xfe";
  234. }
  235. }
  236. p->addLine(std::make_unique<door::Line>(m, 1, color));
  237. return p;
  238. }
  239. void Deck::part(int x, int y, door::Door &d, int level, bool left) {
  240. // Render part of the back of a card.
  241. y += 2;
  242. if (!left) {
  243. x += 2;
  244. }
  245. std::string c = back_char(level);
  246. std::string l = c + c + c;
  247. door::Goto g(x, y);
  248. d << g << cardback << l;
  249. }
  250. door::Panel *Deck::card(int c) { return cards[c]; }
  251. door::Panel *Deck::back(int level) { return backs[level]; }
  252. const std::array<std::pair<int, int>, 18> Deck::blocks = {
  253. make_pair(3, 4), make_pair(5, 6), make_pair(7, 8), // end row 1
  254. make_pair(9, 10), make_pair(10, 11), make_pair(12, 13),
  255. make_pair(13, 14), make_pair(15, 16), make_pair(16, 17),
  256. make_pair(18, 19), // end row 2
  257. make_pair(19, 20), make_pair(20, 21), make_pair(21, 22),
  258. make_pair(22, 23), make_pair(23, 24), make_pair(24, 25),
  259. make_pair(25, 26), make_pair(26, 27) // 27
  260. };
  261. /**
  262. * @brief Which card (if any) is unblocked by this card
  263. *
  264. * @param c
  265. * @return * int
  266. */
  267. std::vector<int> Deck::unblocks(int c) {
  268. std::vector<int> result;
  269. for (size_t i = 0; i < blocks.size(); ++i) {
  270. if ((blocks.at(i).first == c) || (blocks.at(i).second == c)) {
  271. result.push_back(i);
  272. }
  273. }
  274. return result;
  275. }
  276. bool Deck::can_play(int c1, int c2) {
  277. int s1, s2;
  278. s1 = is_rank(c1);
  279. s2 = is_rank(c2);
  280. // this works %13 handles wrap-around for us.
  281. if ((s1 + 1) % 13 == s2)
  282. return true;
  283. if (s1 == 0) {
  284. s1 += 13;
  285. }
  286. if (s1 - 1 == s2)
  287. return true;
  288. return false;
  289. }
  290. door::Panel *Deck::marker(int c) { return mark[c]; }
  291. /**
  292. * @brief remove_card
  293. *
  294. * This removes a card at a given position (c).
  295. * It needs to know if there are cards underneath
  296. * to the left or right. (If so, we restore those missing parts.)
  297. *
  298. * @param door
  299. * @param c
  300. * @param off_x
  301. * @param off_y
  302. * @param left
  303. * @param right
  304. */
  305. void Deck::remove_card(door::Door &door, int c, int off_x, int off_y, bool left,
  306. bool right) {
  307. int cx, cy, level;
  308. cardgo(c, cx, cy, level);
  309. if (level > 1)
  310. --level;
  311. std::string cstr = back_char(level);
  312. door::Goto g(cx + off_x, cy + off_y);
  313. door << g << cardback;
  314. if (left)
  315. door << cstr;
  316. else
  317. door << " ";
  318. door << " ";
  319. if (right)
  320. door << cstr;
  321. else
  322. door << " ";
  323. g.set(cx + off_x, cy + off_y + 1);
  324. door << g << " ";
  325. g.set(cx + off_x, cy + off_y + 2);
  326. door << g << " ";
  327. }
  328. /*
  329. Layout spacing 1:
  330. 1 2 3 4 5 6
  331. 123456789012345678901234567890123456789012345678901234567890
  332. ░░░░░ ░░░░░ ░░░░░
  333. ░░░░░ ░░░░░ ░░░░░
  334. ▒▒▒▒▒░▒▒▒▒▒ #####░##### #####░#####
  335. ▒▒▒▒▒ ▒▒▒▒▒ ##### ##### ##### #####
  336. ▓▓▓▓▓▒▓▓▓▓▓▒▓▓▓▓▓ #####=#####=##### #####=#####=#####
  337. ▓▓▓▓▓ ▓▓▓▓▓ ▓▓▓▓▓ ##### ##### ##### ##### ##### #####
  338. █████▓█████▓█████▓#####=#####=#####=#####=#####=#####=#####
  339. █████ █████ █████ ##### ##### ##### ##### ##### ##### #####
  340. █████ █████ █████ ##### ##### ##### ##### ##### ##### #####
  341. width = 5 * 10 + (1*9) = 59 OK!
  342. Layout with spacing = 2:
  343. EEEEE
  344. ZZZZZ
  345. yyyyyZZZyyyyy
  346. yyyyy yyyyy
  347. XXXXXyyyXXXXXyyyXXXXX
  348. XXXXX XXXXX XXXXX
  349. width = 5 * 10 + (2 * 9) = 50+18 = 68 ! I could do that!
  350. */
  351. /**
  352. * @brief Where does this card go in relation to everything else?
  353. *
  354. * This function is deprecated, see the other cardgo.
  355. *
  356. * @param pos
  357. * @param space
  358. * @param h
  359. * @param x
  360. * @param y
  361. * @param level
  362. */
  363. void cardgo(int pos, int space, int h, int &x, int &y, int &level) {
  364. // special cases here
  365. if (pos == 28) {
  366. // cardgo(23, space, h, x, y, level);
  367. cardgo(23, x, y, level);
  368. y += h + 1;
  369. --level;
  370. return;
  371. } else {
  372. if (pos == 29) {
  373. // cardgo(22, space, h, x, y, level);
  374. cardgo(22, x, y, level);
  375. y += h + 1;
  376. --level;
  377. return;
  378. }
  379. }
  380. const int CARD_WIDTH = 5;
  381. int HALF_WIDTH = 3;
  382. // space = 1 or 3
  383. // int space = 1;
  384. // space = 3;
  385. HALF_WIDTH += space / 2;
  386. /*
  387. int levels[4] = {3, 6, 9, 10};
  388. for (level = 0; level < 4; ++level) {
  389. if (pos < levels[level]) {
  390. level++;
  391. // we're here
  392. y = (level -1) * 2 + 1;
  393. } else {
  394. pos -= levels[level];
  395. }
  396. }
  397. */
  398. int between = CARD_WIDTH + space;
  399. if (pos < 3) {
  400. // top
  401. level = 1;
  402. y = (level - 1) * (h - 1) + 1;
  403. x = pos * (between * 3) + between + HALF_WIDTH + space; // 10
  404. return;
  405. } else {
  406. pos -= 3;
  407. }
  408. if (pos < 6) {
  409. level = 2;
  410. y = (level - 1) * (h - 1) + 1;
  411. int group = (pos) / 2;
  412. x = pos * between + (group * between) + CARD_WIDTH + space * 2;
  413. return;
  414. } else {
  415. pos -= 6;
  416. }
  417. if (pos < 9) {
  418. level = 3;
  419. y = (level - 1) * (h - 1) + 1;
  420. x = pos * between + HALF_WIDTH + space;
  421. return;
  422. } else {
  423. pos -= 9;
  424. }
  425. if (pos < 10) {
  426. level = 4;
  427. y = (level - 1) * (h - 1) + 1;
  428. x = (pos)*between + space;
  429. return;
  430. } else {
  431. // something is wrong.
  432. y = -1;
  433. x = -1;
  434. level = -1;
  435. }
  436. }
  437. /**
  438. * @brief Given card pos, calculate x, y, and level values.
  439. *
  440. * level is used to determine the card background gradient.
  441. *
  442. * @param pos
  443. * @param x
  444. * @param y
  445. * @param level
  446. */
  447. void cardgo(int pos, int &x, int &y, int &level) {
  448. const int space = 3;
  449. const int h = 3;
  450. // special cases here
  451. if (pos == 28) {
  452. cardgo(23, x, y, level);
  453. y += h + 1;
  454. --level;
  455. return;
  456. } else {
  457. if (pos == 29) {
  458. cardgo(22, x, y, level);
  459. y += h + 1;
  460. --level;
  461. return;
  462. }
  463. }
  464. const int CARD_WIDTH = 5;
  465. int HALF_WIDTH = 3;
  466. HALF_WIDTH += space / 2;
  467. int between = CARD_WIDTH + space;
  468. if (pos < 3) {
  469. // top
  470. level = 1;
  471. y = (level - 1) * (h - 1) + 1;
  472. x = pos * (between * 3) + between + HALF_WIDTH + space; // 10
  473. return;
  474. } else {
  475. pos -= 3;
  476. }
  477. if (pos < 6) {
  478. level = 2;
  479. y = (level - 1) * (h - 1) + 1;
  480. int group = (pos) / 2;
  481. x = pos * between + (group * between) + CARD_WIDTH + space * 2;
  482. return;
  483. } else {
  484. pos -= 6;
  485. }
  486. if (pos < 9) {
  487. level = 3;
  488. y = (level - 1) * (h - 1) + 1;
  489. x = pos * between + HALF_WIDTH + space;
  490. return;
  491. } else {
  492. pos -= 9;
  493. }
  494. if (pos < 10) {
  495. level = 4;
  496. y = (level - 1) * (h - 1) + 1;
  497. x = (pos)*between + space;
  498. return;
  499. } else {
  500. // something is wrong.
  501. y = -1;
  502. x = -1;
  503. level = -1;
  504. }
  505. }
  506. cards card_shuffle(std::seed_seq &seed, int decks) {
  507. std::mt19937 gen;
  508. // build deck of cards
  509. int size = decks * 52;
  510. std::vector<int> deck;
  511. deck.reserve(size);
  512. for (int x = 0; x < size; ++x) {
  513. deck.push_back(x);
  514. }
  515. // repeatable, but random
  516. gen.seed(seed);
  517. std::shuffle(deck.begin(), deck.end(), gen);
  518. return deck;
  519. }
  520. /**
  521. * @brief generate a vector of ints to track card states.
  522. *
  523. * This initializes everything to 0.
  524. *
  525. * @param decks
  526. * @return cards
  527. */
  528. cards card_states(int decks) {
  529. // auto states = std::unique_ptr<std::vector<int>>(); // (decks * 52, 0)>;
  530. std::vector<int> states;
  531. states.assign(decks * 52, 0);
  532. return states;
  533. }
  534. /**
  535. * @brief Find the next card we can move the marker to.
  536. *
  537. * if left, look in the left - direction, otherwise the right + direction.
  538. * current is the current active card.
  539. * states is the card states (0 = down, 1 = in play, 2 = removed)
  540. *
  541. * updated: If we can't go any further left (or right), then
  542. * roll around to the other side.
  543. *
  544. * @param left
  545. * @param states
  546. * @param current
  547. * @return int
  548. */
  549. int find_next(bool left, const cards &states, int current) {
  550. int cx, cy, level;
  551. int current_x;
  552. cardgo(current, cx, cy, level);
  553. current_x = cx;
  554. int x;
  555. int pos = -1;
  556. int pos_x;
  557. int max_pos = -1;
  558. int max_x = -1;
  559. int min_pos = -1;
  560. int min_x = 100;
  561. if (left)
  562. pos_x = 0;
  563. else
  564. pos_x = 100;
  565. for (x = 0; x < 28; x++) {
  566. if (states.at(x) == 1) {
  567. // possible location
  568. if (x == current)
  569. continue;
  570. cardgo(x, cx, cy, level);
  571. // find max and min while we're iterating here
  572. if (cx < min_x) {
  573. min_pos = x;
  574. min_x = cx;
  575. }
  576. if (cx > max_x) {
  577. max_pos = x;
  578. max_x = cx;
  579. }
  580. if (left) {
  581. if ((cx < current_x) and (cx > pos_x)) {
  582. pos_x = cx;
  583. pos = x;
  584. }
  585. } else {
  586. if ((cx > current_x) and (cx < pos_x)) {
  587. pos_x = cx;
  588. pos = x;
  589. }
  590. }
  591. }
  592. }
  593. if (pos == -1) {
  594. // we couldn't find one
  595. if (left) {
  596. // use max -- roll around to the right
  597. pos = max_pos;
  598. } else {
  599. // use min -- roll around to the left
  600. pos = min_pos;
  601. }
  602. }
  603. return pos;
  604. }
  605. int find_next_closest(const cards &states, int current) {
  606. int cx, cy, level;
  607. int current_x;
  608. cardgo(current, cx, cy, level);
  609. current_x = cx;
  610. int x;
  611. int pos = -1;
  612. int pos_x = -1;
  613. for (x = 0; x < 28; x++) {
  614. if (states.at(x) == 1) {
  615. // possible location
  616. if (x == current)
  617. continue;
  618. cardgo(x, cx, cy, level);
  619. if (pos == -1) {
  620. pos = x;
  621. pos_x = cx;
  622. } else {
  623. if (abs(current_x - cx) < abs(current_x - pos_x)) {
  624. pos = x;
  625. pos_x = cx;
  626. }
  627. }
  628. }
  629. }
  630. return pos;
  631. }
  632. vector<std::string> deck_colors = {std::string("All"), std::string("Blue"),
  633. std::string("Cyan"), std::string("Green"),
  634. std::string("Magenta"), std::string("Red")};
  635. /**
  636. * @brief menu render that sets the text color based on the color found in the
  637. * text itself.
  638. *
  639. * @param c1 [] brackets
  640. * @param c2 text within brackets
  641. * @param c3 base color give (we set the FG, we use the BG)
  642. * @return door::renderFunction
  643. */
  644. door::renderFunction makeColorRender(door::ANSIColor c1, door::ANSIColor c2,
  645. door::ANSIColor c3) {
  646. door::renderFunction render = [c1, c2,
  647. c3](const std::string &txt) -> door::Render {
  648. door::Render r(txt);
  649. bool option = true;
  650. door::ColorOutput co;
  651. // I need this mutable
  652. door::ANSIColor textColor = c3;
  653. // Color update:
  654. {
  655. std::string found;
  656. for (auto &dc : deck_colors) {
  657. if (txt.find(dc) != string::npos) {
  658. found = dc;
  659. break;
  660. }
  661. }
  662. if (!found.empty()) {
  663. if (found == "All") {
  664. // handle this some other way.
  665. textColor.setFg(door::COLOR::WHITE);
  666. } else {
  667. door::ANSIColor c = from_string(found);
  668. textColor.setFg(c.getFg());
  669. }
  670. }
  671. }
  672. co.pos = 0;
  673. co.len = 0;
  674. co.c = c1;
  675. int tpos = 0;
  676. for (char const &c : txt) {
  677. if (option) {
  678. if (c == '[' or c == ']') {
  679. if (co.c != c1)
  680. if (co.len != 0) {
  681. r.outputs.push_back(co);
  682. co.reset();
  683. co.pos = tpos;
  684. }
  685. co.c = c1;
  686. if (c == ']')
  687. option = false;
  688. } else {
  689. if (co.c != c2)
  690. if (co.len != 0) {
  691. r.outputs.push_back(co);
  692. co.reset();
  693. co.pos = tpos;
  694. }
  695. co.c = c2;
  696. }
  697. } else {
  698. if (co.c != textColor)
  699. if (co.len != 0) {
  700. r.outputs.push_back(co);
  701. co.reset();
  702. co.pos = tpos;
  703. }
  704. co.c = textColor;
  705. }
  706. co.len++;
  707. tpos++;
  708. }
  709. if (co.len != 0) {
  710. r.outputs.push_back(co);
  711. }
  712. return r;
  713. };
  714. return render;
  715. }
  716. // convert a string to an option
  717. // an option to the string to store
  718. // This needs to be updated to use deck_colors.
  719. door::ANSIColor from_string(std::string colorCode) {
  720. std::map<std::string, door::ANSIColor> codeMap = {
  721. {std::string("BLUE"), door::ANSIColor(door::COLOR::BLUE)},
  722. {std::string("RED"), door::ANSIColor(door::COLOR::RED)},
  723. {std::string("CYAN"), door::ANSIColor(door::COLOR::CYAN)},
  724. {std::string("GREEN"), door::ANSIColor(door::COLOR::GREEN)},
  725. {std::string("MAGENTA"), door::ANSIColor(door::COLOR::MAGENTA)}};
  726. std::string code = colorCode;
  727. string_toupper(code);
  728. auto iter = codeMap.find(code);
  729. if (iter != codeMap.end()) {
  730. return iter->second;
  731. }
  732. // And if it doesn't match, and isn't ALL ... ?
  733. // if (code.compare("ALL") == 0) {
  734. std::random_device dev;
  735. std::mt19937_64 rng(dev());
  736. std::uniform_int_distribution<size_t> idDist(0, codeMap.size() - 1);
  737. iter = codeMap.begin();
  738. std::advance(iter, idDist(rng));
  739. return iter->second;
  740. // }
  741. }
  742. std::string from_color_option(int opt) { return deck_colors[opt]; }
  743. void string_toupper(std::string &str) {
  744. std::transform(str.begin(), str.end(), str.begin(), ::toupper);
  745. }