deck.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. #include "deck.h"
  2. #include "space.h"
  3. #include "utils.h"
  4. #include "version.h"
  5. #include <algorithm>
  6. #include <map>
  7. #include <sstream>
  8. Deck::Deck(door::ANSIColor backcolor) : card_back_color{backcolor} {
  9. for (int i = 0; i < 52; ++i) {
  10. cards.push_back(cardOf(i));
  11. }
  12. // 0 = BLANK, 1-4 levels
  13. for (int i = 0; i < 5; ++i) {
  14. backs.push_back(backOf(i));
  15. }
  16. mark.push_back(markOf(0));
  17. mark.push_back(markOf(1));
  18. }
  19. Deck::~Deck() {
  20. /*
  21. for (auto c : cards) {
  22. delete c;
  23. }
  24. cards.clear();
  25. for (auto b : backs) {
  26. delete b;
  27. }
  28. backs.clear();
  29. for (auto m : mark) {
  30. delete m;
  31. }
  32. mark.clear();
  33. */
  34. }
  35. Deck::Deck(Deck &&ref) {
  36. card_back_color = ref.card_back_color;
  37. /*
  38. for (auto c : cards)
  39. delete c;
  40. cards.clear();
  41. */
  42. cards = ref.cards;
  43. ref.cards.clear();
  44. /*
  45. for (auto b : backs)
  46. delete b;
  47. backs.clear();
  48. */
  49. backs = ref.backs;
  50. ref.backs.clear();
  51. /*
  52. for (auto m : mark)
  53. delete m;
  54. mark.clear();
  55. */
  56. mark = ref.mark;
  57. ref.mark.clear();
  58. // card_height = ref.card_height;
  59. };
  60. Deck &Deck::operator=(Deck &&ref) {
  61. card_back_color = ref.card_back_color;
  62. /*
  63. for (auto c : cards)
  64. delete c;
  65. cards.clear();
  66. */
  67. cards = ref.cards;
  68. ref.cards.clear();
  69. /*
  70. for (auto b : backs)
  71. delete b;
  72. backs.clear();
  73. */
  74. backs = ref.backs;
  75. ref.backs.clear();
  76. /*
  77. for (auto m : mark)
  78. delete m;
  79. mark.clear();
  80. */
  81. mark = ref.mark;
  82. ref.mark.clear();
  83. // card_height = ref.card_height;
  84. return *this;
  85. }
  86. int Deck::getDeck(int c) { return c / 52; }
  87. int Deck::getSuit(int c) { return (c % 52) / 13; }
  88. int Deck::getRank(int c) { return (c % 52) % 13; }
  89. char Deck::rankSymbol(int c) {
  90. const char symbols[] = "A23456789TJQK";
  91. return symbols[c];
  92. }
  93. std::string Deck::suitSymbol(int c) {
  94. // unicode
  95. if (door::unicode) {
  96. switch (c) {
  97. case 0:
  98. return std::string("\u2665");
  99. case 1:
  100. return std::string("\u2666");
  101. case 2:
  102. return std::string("\u2663");
  103. case 3:
  104. return std::string("\u2660");
  105. }
  106. } else {
  107. if (door::full_cp437) {
  108. switch (c) {
  109. case 0:
  110. return std::string(1, '\x03');
  111. case 1:
  112. return std::string(1, '\x04');
  113. case 2:
  114. return std::string(1, '\x05');
  115. case 3:
  116. return std::string(1, '\x06');
  117. }
  118. } else {
  119. // These look horrible!
  120. switch (c) {
  121. case 0:
  122. return std::string(1, '*'); // H
  123. case 1:
  124. return std::string(1, '^'); // D
  125. case 2:
  126. return std::string(1, '%'); // C
  127. case 3:
  128. return std::string(1, '$'); // S
  129. }
  130. }
  131. }
  132. return std::string("!", 1);
  133. }
  134. shared_panel Deck::cardOf(int c) {
  135. int suit = getSuit(c);
  136. int rank = getRank(c);
  137. bool is_red = (suit < 2);
  138. door::ANSIColor color;
  139. if (is_red) {
  140. color = door::ANSIColor(door::COLOR::RED, door::COLOR::WHITE);
  141. } else {
  142. color = door::ANSIColor(door::COLOR::BLACK, door::COLOR::WHITE);
  143. }
  144. shared_panel p = std::make_shared<door::Panel>(0, 0, 5);
  145. // setColor sets border_color. NOT WHAT I WANT.
  146. // p->setColor(color);
  147. char r = rankSymbol(rank);
  148. std::string s = suitSymbol(suit);
  149. // build lines
  150. std::ostringstream oss;
  151. oss << r << s << " ";
  152. std::string str = oss.str();
  153. p->addLine(std::make_unique<door::Line>(str, 5, color));
  154. oss.str(std::string());
  155. oss.clear();
  156. if (card_height == 5)
  157. p->addLine(std::make_unique<door::Line>(" ", 5, color));
  158. oss << " " << s << " ";
  159. str = oss.str();
  160. p->addLine(std::make_unique<door::Line>(str, 5, color));
  161. oss.str(std::string());
  162. oss.clear();
  163. if (card_height == 5)
  164. p->addLine(std::make_unique<door::Line>(" ", 5, color));
  165. oss << " " << s << r;
  166. str = oss.str();
  167. p->addLine(std::make_unique<door::Line>(str, 5, color));
  168. oss.str(std::string());
  169. oss.clear();
  170. return p;
  171. }
  172. std::string Deck::backSymbol(int level) {
  173. std::string c;
  174. if (level == 0) {
  175. c = ' ';
  176. return c;
  177. }
  178. if (door::unicode) {
  179. switch (level) {
  180. case 1:
  181. c = "\u2591";
  182. break;
  183. case 2:
  184. c = "\u2592";
  185. break;
  186. case 3:
  187. c = "\u2593";
  188. break;
  189. case 4:
  190. c = "\u2588";
  191. break;
  192. }
  193. } else {
  194. switch (level) {
  195. case 1:
  196. c = "\xb0";
  197. break;
  198. case 2:
  199. c = "\xb1";
  200. break;
  201. case 3:
  202. c = "\xb2";
  203. break;
  204. case 4:
  205. c = "\xdb";
  206. break;
  207. }
  208. }
  209. return c;
  210. }
  211. shared_panel Deck::backOf(int level) {
  212. // using: \xb0, 0xb1, 0xb2, 0xdb
  213. // OR: \u2591, \u2592, \u2593, \u2588
  214. // door::ANSIColor color(door::COLOR::RED, door::COLOR::BLACK);
  215. shared_panel p = std::make_shared<door::Panel>(0, 0, 5);
  216. std::string c = backSymbol(level);
  217. std::string l = c + c + c + c + c;
  218. for (int x = 0; x < card_height; ++x) {
  219. p->addLine(std::make_unique<door::Line>(l, 5, card_back_color));
  220. };
  221. // p->addLine(std::make_unique<door::Line>(l, 5, card_back_color));
  222. // p->addLine(std::make_unique<door::Line>(l, 5, card_back_color));
  223. return p;
  224. }
  225. shared_panel Deck::markOf(int c) {
  226. shared_panel p = std::make_shared<door::Panel>(1);
  227. door::ANSIColor color = door::ANSIColor(
  228. door::COLOR::BLUE, door::COLOR::WHITE); // , door::ATTR::BOLD);
  229. std::string m;
  230. if (c == 0)
  231. m = " ";
  232. else {
  233. if (door::unicode) {
  234. m = "\u25a0";
  235. } else {
  236. m = "\xfe";
  237. }
  238. }
  239. p->addLine(std::make_unique<door::Line>(m, 1, color));
  240. return p;
  241. }
  242. shared_panel Deck::card(int c) { return cards[c]; }
  243. /**
  244. * @brief Return panel for back of card.
  245. *
  246. * - 0 = Blank
  247. * - 1 = level 1 (furthest/darkest)
  248. * - 2 = level 2
  249. * - 3 = level 3
  250. * - 4 = level 4 (closest/lightest)
  251. *
  252. * @param level
  253. * @return door::Panel*
  254. */
  255. shared_panel Deck::back(int level) { return backs[level]; }
  256. const std::array<std::pair<int, int>, 18> Deck::blocks = {
  257. make_pair(3, 4), make_pair(5, 6), make_pair(7, 8), // end row 1
  258. make_pair(9, 10), make_pair(10, 11), make_pair(12, 13),
  259. make_pair(13, 14), make_pair(15, 16), make_pair(16, 17),
  260. make_pair(18, 19), // end row 2
  261. make_pair(19, 20), make_pair(20, 21), make_pair(21, 22),
  262. make_pair(22, 23), make_pair(23, 24), make_pair(24, 25),
  263. make_pair(25, 26), make_pair(26, 27) // 27
  264. };
  265. /**
  266. * @brief Which card(s) are unblocked by this card?
  267. *
  268. * @param card
  269. * @return * int
  270. */
  271. std::vector<int> Deck::unblocks(int card) {
  272. std::vector<int> result;
  273. for (size_t i = 0; i < blocks.size(); ++i) {
  274. if ((blocks.at(i).first == card) || (blocks.at(i).second == card)) {
  275. result.push_back(i);
  276. }
  277. }
  278. return result;
  279. }
  280. /**
  281. * @brief Can this card play on this other card?
  282. *
  283. * @param card1
  284. * @param card2
  285. * @return true
  286. * @return false
  287. */
  288. bool Deck::canPlay(int card1, int card2) {
  289. int rank1, rank2;
  290. rank1 = getRank(card1);
  291. rank2 = getRank(card2);
  292. // this works %13 handles wrap-around for us.
  293. if ((rank1 + 1) % 13 == rank2)
  294. return true;
  295. if (rank1 == 0) {
  296. rank1 += 13;
  297. }
  298. if (rank1 - 1 == rank2)
  299. return true;
  300. return false;
  301. }
  302. /**
  303. * @brief Returns marker
  304. *
  305. * 0 = blank
  306. * 1 = [] symbol thing \xfe ■
  307. *
  308. * @param c
  309. * @return door::Panel*
  310. */
  311. shared_panel Deck::marker(int c) { return mark[c]; }
  312. /**
  313. * @brief removeCard
  314. *
  315. * This removes a card at a given position (c).
  316. * It needs to know if there are cards underneath
  317. * to the left or right. (If so, we restore those missing parts.)
  318. *
  319. * @param door
  320. * @param c
  321. * @param off_x
  322. * @param off_y
  323. * @param left
  324. * @param right
  325. */
  326. void Deck::removeCard(door::Door &door, int c, int off_x, int off_y, bool left,
  327. bool right) {
  328. int cx, cy, level;
  329. cardPosLevel(c, cx, cy, level);
  330. if (level > 1)
  331. --level;
  332. std::string cstr = backSymbol(level);
  333. door::Goto g(cx + off_x, cy + off_y);
  334. door << g << card_back_color;
  335. if (left)
  336. door << cstr;
  337. else
  338. door << " ";
  339. door << " ";
  340. if (right)
  341. door << cstr;
  342. else
  343. door << " ";
  344. g.set(cx + off_x, cy + off_y + 1);
  345. door << g << " ";
  346. g.set(cx + off_x, cy + off_y + 2);
  347. door << g << " ";
  348. }
  349. /*
  350. Layout spacing 1:
  351. 1 2 3 4 5 6
  352. 123456789012345678901234567890123456789012345678901234567890
  353. ░░░░░ ░░░░░ ░░░░░
  354. ░░░░░ ░░░░░ ░░░░░
  355. ▒▒▒▒▒░▒▒▒▒▒ #####░##### #####░#####
  356. ▒▒▒▒▒ ▒▒▒▒▒ ##### ##### ##### #####
  357. ▓▓▓▓▓▒▓▓▓▓▓▒▓▓▓▓▓ #####=#####=##### #####=#####=#####
  358. ▓▓▓▓▓ ▓▓▓▓▓ ▓▓▓▓▓ ##### ##### ##### ##### ##### #####
  359. █████▓█████▓█████▓#####=#####=#####=#####=#####=#####=#####
  360. █████ █████ █████ ##### ##### ##### ##### ##### ##### #####
  361. █████ █████ █████ ##### ##### ##### ##### ##### ##### #####
  362. width = 5 * 10 + (1*9) = 59 OK!
  363. Layout with spacing = 2:
  364. EEEEE
  365. ZZZZZ
  366. yyyyyZZZyyyyy
  367. yyyyy yyyyy
  368. XXXXXyyyXXXXXyyyXXXXX
  369. XXXXX XXXXX XXXXX
  370. width = 5 * 10 + (2 * 9) = 50+18 = 68 ! I could do that!
  371. */
  372. #ifdef NO
  373. /**
  374. * @brief Where does this card go in relation to everything else?
  375. *
  376. * This function is deprecated, see the other cardgo.
  377. *
  378. * @param pos
  379. * @param space
  380. * @param h
  381. * @param x
  382. * @param y
  383. * @param level
  384. */
  385. void cardgo(int pos, int space, int h, int &x, int &y, int &level) {
  386. // special cases here
  387. if (pos == 28) {
  388. // cardgo(23, space, h, x, y, level);
  389. cardgo(23, x, y, level);
  390. y += h + 1;
  391. --level;
  392. return;
  393. } else {
  394. if (pos == 29) {
  395. // cardgo(22, space, h, x, y, level);
  396. cardgo(22, x, y, level);
  397. y += h + 1;
  398. --level;
  399. return;
  400. }
  401. }
  402. const int CARD_WIDTH = 5;
  403. int HALF_WIDTH = 3;
  404. // space = 1 or 3
  405. // int space = 1;
  406. // space = 3;
  407. HALF_WIDTH += space / 2;
  408. /*
  409. int levels[4] = {3, 6, 9, 10};
  410. for (level = 0; level < 4; ++level) {
  411. if (pos < levels[level]) {
  412. level++;
  413. // we're here
  414. y = (level -1) * 2 + 1;
  415. } else {
  416. pos -= levels[level];
  417. }
  418. }
  419. */
  420. int between = CARD_WIDTH + space;
  421. if (pos < 3) {
  422. // top
  423. level = 1;
  424. y = (level - 1) * (h - 1) + 1;
  425. x = pos * (between * 3) + between + HALF_WIDTH + space; // 10
  426. return;
  427. } else {
  428. pos -= 3;
  429. }
  430. if (pos < 6) {
  431. level = 2;
  432. y = (level - 1) * (h - 1) + 1;
  433. int group = (pos) / 2;
  434. x = pos * between + (group * between) + CARD_WIDTH + space * 2;
  435. return;
  436. } else {
  437. pos -= 6;
  438. }
  439. if (pos < 9) {
  440. level = 3;
  441. y = (level - 1) * (h - 1) + 1;
  442. x = pos * between + HALF_WIDTH + space;
  443. return;
  444. } else {
  445. pos -= 9;
  446. }
  447. if (pos < 10) {
  448. level = 4;
  449. y = (level - 1) * (h - 1) + 1;
  450. x = (pos)*between + space;
  451. return;
  452. } else {
  453. // something is wrong.
  454. y = -1;
  455. x = -1;
  456. level = -1;
  457. }
  458. }
  459. #endif
  460. void cardPos(int pos, int &x, int &y) {
  461. const int space = 3;
  462. const int height = 3;
  463. // special cases here
  464. if (pos == 28) {
  465. cardPos(23, x, y);
  466. y += height + 1;
  467. return;
  468. } else {
  469. if (pos == 29) {
  470. cardPos(22, x, y);
  471. y += height + 1;
  472. return;
  473. }
  474. }
  475. const int CARD_WIDTH = 5;
  476. int HALF_WIDTH = 3;
  477. HALF_WIDTH += space / 2;
  478. int between = CARD_WIDTH + space;
  479. int level; // I still need level in my calculations
  480. if (pos < 3) {
  481. // top
  482. level = 1;
  483. y = (level - 1) * (height - 1) + 1;
  484. x = pos * (between * 3) + between + HALF_WIDTH + space; // 10
  485. return;
  486. } else {
  487. pos -= 3;
  488. }
  489. if (pos < 6) {
  490. level = 2;
  491. y = (level - 1) * (height - 1) + 1;
  492. int group = (pos) / 2;
  493. x = pos * between + (group * between) + CARD_WIDTH + space * 2;
  494. return;
  495. } else {
  496. pos -= 6;
  497. }
  498. if (pos < 9) {
  499. level = 3;
  500. y = (level - 1) * (height - 1) + 1;
  501. x = pos * between + HALF_WIDTH + space;
  502. return;
  503. } else {
  504. pos -= 9;
  505. }
  506. if (pos < 10) {
  507. level = 4;
  508. y = (level - 1) * (height - 1) + 1;
  509. x = (pos)*between + space;
  510. return;
  511. } else {
  512. // something is wrong.
  513. y = -1;
  514. x = -1;
  515. level = -1;
  516. }
  517. }
  518. void cardLevel(int pos, int &level) {
  519. /*
  520. const int space = 3;
  521. const int height = 3;
  522. */
  523. // special cases here
  524. if (pos == 28) {
  525. cardLevel(23, level);
  526. --level;
  527. return;
  528. } else {
  529. if (pos == 29) {
  530. cardLevel(22, level);
  531. --level;
  532. return;
  533. }
  534. }
  535. /*
  536. const int CARD_WIDTH = 5;
  537. int HALF_WIDTH = 3;
  538. HALF_WIDTH += space / 2;
  539. int between = CARD_WIDTH + space;
  540. */
  541. if (pos < 3) {
  542. // top
  543. level = 1;
  544. return;
  545. } else {
  546. pos -= 3;
  547. }
  548. if (pos < 6) {
  549. level = 2;
  550. return;
  551. } else {
  552. pos -= 6;
  553. }
  554. if (pos < 9) {
  555. level = 3;
  556. return;
  557. } else {
  558. pos -= 9;
  559. }
  560. if (pos < 10) {
  561. level = 4;
  562. return;
  563. } else {
  564. // something is wrong.
  565. level = -1;
  566. }
  567. }
  568. /**
  569. * @brief Given card pos, calculate x, y, and level values.
  570. *
  571. * level is used to determine the card background gradient.
  572. *
  573. * @param pos
  574. * @param x
  575. * @param y
  576. * @param level
  577. */
  578. void cardPosLevel(int pos, int &x, int &y, int &level) {
  579. const int space = 3;
  580. const int height = 3;
  581. // special cases here
  582. if (pos == 28) {
  583. cardPosLevel(23, x, y, level);
  584. y += height + 1;
  585. --level;
  586. return;
  587. } else {
  588. if (pos == 29) {
  589. cardPosLevel(22, x, y, level);
  590. y += height + 1;
  591. --level;
  592. return;
  593. }
  594. }
  595. const int CARD_WIDTH = 5;
  596. int HALF_WIDTH = 3;
  597. HALF_WIDTH += space / 2;
  598. int between = CARD_WIDTH + space;
  599. if (pos < 3) {
  600. // top
  601. level = 1;
  602. y = (level - 1) * (height - 1) + 1;
  603. x = pos * (between * 3) + between + HALF_WIDTH + space; // 10
  604. return;
  605. } else {
  606. pos -= 3;
  607. }
  608. if (pos < 6) {
  609. level = 2;
  610. y = (level - 1) * (height - 1) + 1;
  611. int group = (pos) / 2;
  612. x = pos * between + (group * between) + CARD_WIDTH + space * 2;
  613. return;
  614. } else {
  615. pos -= 6;
  616. }
  617. if (pos < 9) {
  618. level = 3;
  619. y = (level - 1) * (height - 1) + 1;
  620. x = pos * between + HALF_WIDTH + space;
  621. return;
  622. } else {
  623. pos -= 9;
  624. }
  625. if (pos < 10) {
  626. level = 4;
  627. y = (level - 1) * (height - 1) + 1;
  628. x = (pos)*between + space;
  629. return;
  630. } else {
  631. // something is wrong.
  632. y = -1;
  633. x = -1;
  634. level = -1;
  635. }
  636. }
  637. /**
  638. * @brief shuffle deck of cards
  639. *
  640. * example of seeding the deck for a given date 2/27/2021 game 1
  641. * std::seed_seq s1{2021, 2, 27, 1};
  642. * vector<int> deck1 = shuffleCards(s1, 1);
  643. * @param seed
  644. * @param decks
  645. * @return vector<int>
  646. */
  647. cards shuffleCards(std::seed_seq &seed, int decks) {
  648. std::mt19937 gen;
  649. // build deck of cards
  650. int size = decks * 52;
  651. std::vector<int> deck;
  652. deck.reserve(size);
  653. for (int x = 0; x < size; ++x) {
  654. deck.push_back(x);
  655. }
  656. // repeatable, but random
  657. gen.seed(seed);
  658. std::shuffle(deck.begin(), deck.end(), gen);
  659. return deck;
  660. }
  661. /**
  662. * @brief generate a vector of ints to track card states.
  663. *
  664. * This initializes everything to 0.
  665. *
  666. * @param decks
  667. * @return cards
  668. */
  669. cards makeCardStates(int decks) {
  670. // auto states = std::unique_ptr<std::vector<int>>(); // (decks * 52, 0)>;
  671. std::vector<int> states;
  672. states.assign(decks * 52, 0);
  673. return states;
  674. }
  675. /**
  676. * @brief Find the next card we can move the marker to.
  677. *
  678. * if left, look in the left - direction, otherwise the right + direction.
  679. * current is the current active card.
  680. * states is the card states (0 = down, 1 = in play, 2 = removed)
  681. *
  682. * updated: If we can't go any further left (or right), then
  683. * roll around to the other side.
  684. *
  685. * @param left
  686. * @param states
  687. * @param current
  688. * @return int
  689. */
  690. int findNextActiveCard(bool left, const cards &states, int current) {
  691. int cx, cy;
  692. int current_x;
  693. cardPos(current, cx, cy);
  694. current_x = cx;
  695. int x;
  696. int pos = -1;
  697. int pos_x;
  698. int max_pos = -1;
  699. int max_x = -1;
  700. int min_pos = -1;
  701. int min_x = 100;
  702. if (left)
  703. pos_x = 0;
  704. else
  705. pos_x = 100;
  706. for (x = 0; x < 28; x++) {
  707. if (states.at(x) == 1) {
  708. // possible location
  709. if (x == current)
  710. continue;
  711. cardPos(x, cx, cy);
  712. // find max and min while we're iterating here
  713. if (cx < min_x) {
  714. min_pos = x;
  715. min_x = cx;
  716. }
  717. if (cx > max_x) {
  718. max_pos = x;
  719. max_x = cx;
  720. }
  721. if (left) {
  722. if ((cx < current_x) and (cx > pos_x)) {
  723. pos_x = cx;
  724. pos = x;
  725. }
  726. } else {
  727. if ((cx > current_x) and (cx < pos_x)) {
  728. pos_x = cx;
  729. pos = x;
  730. }
  731. }
  732. }
  733. }
  734. if (pos == -1) {
  735. // we couldn't find one
  736. if (left) {
  737. // use max -- roll around to the right
  738. pos = max_pos;
  739. } else {
  740. // use min -- roll around to the left
  741. pos = min_pos;
  742. }
  743. }
  744. return pos;
  745. }
  746. /**
  747. * @brief Find the next closest card to move to.
  748. *
  749. * Given the card states, this finds the next closest card.
  750. * Uses current.
  751. *
  752. * return -1 there's no options to go to. (END OF GAME)
  753. * @param states
  754. * @param current
  755. * @return int
  756. */
  757. int findClosestActiveCard(const cards &states, int current) {
  758. int cx, cy;
  759. int current_x;
  760. cardPos(current, cx, cy);
  761. current_x = cx;
  762. int x;
  763. int pos = -1;
  764. int pos_x = -1;
  765. for (x = 0; x < 28; x++) {
  766. if (states.at(x) == 1) {
  767. // possible location
  768. if (x == current)
  769. continue;
  770. cardPos(x, cx, cy);
  771. if (pos == -1) {
  772. pos = x;
  773. pos_x = cx;
  774. } else {
  775. if (abs(current_x - cx) < abs(current_x - pos_x)) {
  776. pos = x;
  777. pos_x = cx;
  778. }
  779. }
  780. }
  781. }
  782. return pos;
  783. }
  784. vector<std::string> deck_colors = {std::string("All"), std::string("Blue"),
  785. std::string("Cyan"), std::string("Green"),
  786. std::string("Magenta"), std::string("Red")};
  787. /**
  788. * @brief menu render that sets the text color based on the color found in the
  789. * text itself.
  790. *
  791. * @param c1 [] brackets
  792. * @param c2 text within brackets
  793. * @param c3 base color give (we set the FG, we use the BG)
  794. * @return door::renderFunction
  795. */
  796. door::renderFunction makeColorRender(door::ANSIColor c1, door::ANSIColor c2,
  797. door::ANSIColor c3) {
  798. door::renderFunction render = [c1, c2,
  799. c3](const std::string &txt) -> door::Render {
  800. door::Render r(txt);
  801. bool option = true;
  802. door::ColorOutput co;
  803. // I need this mutable
  804. door::ANSIColor textColor = c3;
  805. // Color update:
  806. {
  807. std::string found;
  808. for (auto &dc : deck_colors) {
  809. if (txt.find(dc) != string::npos) {
  810. found = dc;
  811. break;
  812. }
  813. }
  814. if (!found.empty()) {
  815. if (found == "All") {
  816. // handle this some other way.
  817. textColor.setFg(door::COLOR::WHITE);
  818. } else {
  819. door::ANSIColor c = stringToANSIColor(found);
  820. textColor.setFg(c.getFg());
  821. }
  822. }
  823. }
  824. co.pos = 0;
  825. co.len = 0;
  826. co.c = c1;
  827. int tpos = 0;
  828. for (char const &c : txt) {
  829. if (option) {
  830. if (c == '[' or c == ']') {
  831. if (co.c != c1)
  832. if (co.len != 0) {
  833. r.outputs.push_back(co);
  834. co.reset();
  835. co.pos = tpos;
  836. }
  837. co.c = c1;
  838. if (c == ']')
  839. option = false;
  840. } else {
  841. if (co.c != c2)
  842. if (co.len != 0) {
  843. r.outputs.push_back(co);
  844. co.reset();
  845. co.pos = tpos;
  846. }
  847. co.c = c2;
  848. }
  849. } else {
  850. if (co.c != textColor)
  851. if (co.len != 0) {
  852. r.outputs.push_back(co);
  853. co.reset();
  854. co.pos = tpos;
  855. }
  856. co.c = textColor;
  857. }
  858. co.len++;
  859. tpos++;
  860. }
  861. if (co.len != 0) {
  862. r.outputs.push_back(co);
  863. }
  864. return r;
  865. };
  866. return render;
  867. }
  868. // convert a string to an option
  869. // an option to the string to store
  870. // This needs to be updated to use deck_colors.
  871. door::ANSIColor stringToANSIColor(std::string colorCode) {
  872. std::map<std::string, door::ANSIColor> codeMap = {
  873. {std::string("BLUE"), door::ANSIColor(door::COLOR::BLUE)},
  874. {std::string("RED"), door::ANSIColor(door::COLOR::RED)},
  875. {std::string("CYAN"), door::ANSIColor(door::COLOR::CYAN)},
  876. {std::string("GREEN"), door::ANSIColor(door::COLOR::GREEN)},
  877. {std::string("MAGENTA"), door::ANSIColor(door::COLOR::MAGENTA)}};
  878. std::string code = colorCode;
  879. string_toupper(code);
  880. auto iter = codeMap.find(code);
  881. if (iter != codeMap.end()) {
  882. return iter->second;
  883. }
  884. // And if it doesn't match, and isn't ALL ... ?
  885. // if (code.compare("ALL") == 0) {
  886. std::random_device dev;
  887. std::mt19937_64 rng(dev());
  888. std::uniform_int_distribution<size_t> idDist(0, codeMap.size() - 1);
  889. iter = codeMap.begin();
  890. std::advance(iter, idDist(rng));
  891. return iter->second;
  892. // }
  893. }
  894. std::string stringFromColorOptions(int opt) { return deck_colors[opt]; }
  895. door::Panel make_about(void) {
  896. const int W = 60;
  897. door::Panel about(W);
  898. about.setStyle(door::BorderStyle::DOUBLE_SINGLE);
  899. about.setColor(door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  900. door::ATTR::BOLD));
  901. about.addLine(std::make_unique<door::Line>("About This Door", W));
  902. about.addLine(std::make_unique<door::Line>(
  903. "---------------------------------", W,
  904. door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLUE, door::ATTR::BOLD)));
  905. /*
  906. 123456789012345678901234567890123456789012345678901234567890-60
  907. This door was written by Bugz.
  908. It is written in c++, only supports Linux, and replaces
  909. opendoors.
  910. It's written in c++, and replaces the outdated opendoors
  911. library.
  912. */
  913. about.addLine(
  914. std::make_unique<door::Line>(SPACEACE " v" SPACEACE_VERSION, W));
  915. std::string copyright = SPACEACE_COPYRIGHT;
  916. if (door::unicode) {
  917. replace(copyright, "(C)", "\u00a9");
  918. }
  919. about.addLine(std::make_unique<door::Line>(copyright, W));
  920. about.addLine(std::make_unique<door::Line>("", W));
  921. about.addLine(
  922. std::make_unique<door::Line>("This door was written by Bugz.", W));
  923. about.addLine(std::make_unique<door::Line>("", W));
  924. about.addLine(std::make_unique<door::Line>(
  925. "It is written in door++, using c++, only support Linux.", W));
  926. /*
  927. door::updateFunction updater = [](void) -> std::string {
  928. std::string text = "Currently: ";
  929. text.append(return_current_time_and_date());
  930. return text;
  931. };
  932. std::string current = updater();
  933. door::Line active(current, 60);
  934. active.setUpdater(updater);
  935. active.setRender(statusValue(
  936. door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE,
  937. door::ATTR::BOLD), door::ANSIColor(door::COLOR::YELLOW,
  938. door::COLOR::BLUE, door::ATTR::BOLD)));
  939. about.addLine(std::make_unique<door::Line>(active));
  940. */
  941. return about;
  942. }
  943. void display_starfield(door::Door &door, std::mt19937 &rng) {
  944. door << door::reset << door::cls;
  945. int mx = door.width;
  946. int my = door.height;
  947. // display starfield
  948. const char *stars[2];
  949. stars[0] = ".";
  950. if (door::unicode) {
  951. stars[1] = "\u2219"; // "\u00b7";
  952. } else {
  953. stars[1] = "\xf9"; // "\xfa";
  954. };
  955. {
  956. // Make uniform random distribution between 1 and MAX screen size X/Y
  957. std::uniform_int_distribution<int> uni_x(1, mx);
  958. std::uniform_int_distribution<int> uni_y(1, my);
  959. door::ANSIColor white(door::COLOR::WHITE);
  960. door::ANSIColor dark(door::COLOR::BLACK, door::ATTR::BRIGHT);
  961. // 10 is too many, 100 is too few. 40 looks ok.
  962. int MAX_STARS = ((mx * my) / 40);
  963. // door.log() << "Generating starmap using " << mx << "," << my << " : "
  964. // << MAX_STARS << " stars." << std::endl;
  965. for (int x = 0; x < MAX_STARS; x++) {
  966. door::Goto star_at(uni_x(rng), uni_y(rng));
  967. door << star_at;
  968. if (x % 5 < 2)
  969. door << dark;
  970. else
  971. door << white;
  972. if (x % 2 == 0)
  973. door << stars[0];
  974. else
  975. door << stars[1];
  976. }
  977. }
  978. }
  979. void display_space_ace(door::Door &door) {
  980. int mx = door.width;
  981. int my = door.height;
  982. // space_ace is 72 chars wide, 6 high
  983. int sa_x = (mx - 72) / 2;
  984. int sa_y = (my - 6) / 2;
  985. // output the SpaceAce logo -- centered!
  986. for (const auto s : space) {
  987. door::Goto sa_at(sa_x, sa_y);
  988. door << sa_at;
  989. if (door::unicode) {
  990. std::string unicode;
  991. door::cp437toUnicode(s, unicode);
  992. door << unicode; // << door::nl;
  993. } else
  994. door << s; // << door::nl;
  995. sa_y++;
  996. }
  997. // pause 5 seconds so they can enjoy our awesome logo -- if they want.
  998. door.sleep_key(5);
  999. }
  1000. void display_starfield_space_ace(door::Door &door, std::mt19937 &rng) {
  1001. // mx = door.width;
  1002. // my = door.height;
  1003. display_starfield(door, rng);
  1004. display_space_ace(door);
  1005. door << door::reset;
  1006. }
  1007. door::Panel make_timeout(int mx, int my) {
  1008. door::ANSIColor yellowred =
  1009. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::RED, door::ATTR::BOLD);
  1010. std::string line_text("Sorry, you've been inactive for too long.");
  1011. int msgWidth = line_text.length() + (2 * 3); // + padding * 2
  1012. door::Panel timeout((mx - (msgWidth)) / 2, my / 2 + 4, msgWidth);
  1013. // place.setTitle(std::make_unique<door::Line>(title), 1);
  1014. timeout.setStyle(door::BorderStyle::DOUBLE);
  1015. timeout.setColor(yellowred);
  1016. door::Line base(line_text);
  1017. base.setColor(yellowred);
  1018. std::string pad1(3, ' ');
  1019. /*
  1020. std::string pad1(3, '\xb0');
  1021. if (door::unicode) {
  1022. std::string unicode;
  1023. door::cp437toUnicode(pad1.c_str(), unicode);
  1024. pad1 = unicode;
  1025. }
  1026. */
  1027. base.setPadding(pad1, yellowred);
  1028. // base.setColor(door::ANSIColor(door::COLOR::GREEN, door::COLOR::BLACK));
  1029. std::unique_ptr<door::Line> stuff = std::make_unique<door::Line>(base);
  1030. timeout.addLine(std::make_unique<door::Line>(base));
  1031. return timeout;
  1032. }
  1033. door::Panel make_notime(int mx, int my) {
  1034. door::ANSIColor yellowred =
  1035. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::RED, door::ATTR::BOLD);
  1036. std::string line_text("Sorry, you've used up all your time for today.");
  1037. int msgWidth = line_text.length() + (2 * 3); // + padding * 2
  1038. door::Panel timeout((mx - (msgWidth)) / 2, my / 2 + 4, msgWidth);
  1039. timeout.setStyle(door::BorderStyle::DOUBLE);
  1040. timeout.setColor(yellowred);
  1041. door::Line base(line_text);
  1042. base.setColor(yellowred);
  1043. std::string pad1(3, ' ');
  1044. /*
  1045. std::string pad1(3, '\xb0');
  1046. if (door::unicode) {
  1047. std::string unicode;
  1048. door::cp437toUnicode(pad1.c_str(), unicode);
  1049. pad1 = unicode;
  1050. }
  1051. */
  1052. base.setPadding(pad1, yellowred);
  1053. std::unique_ptr<door::Line> stuff = std::make_unique<door::Line>(base);
  1054. timeout.addLine(std::make_unique<door::Line>(base));
  1055. return timeout;
  1056. }
  1057. door::Menu make_main_menu(void) {
  1058. door::Menu m(5, 5, 25);
  1059. door::Line mtitle(SPACEACE " Main Menu");
  1060. door::ANSIColor border_color(door::COLOR::CYAN, door::COLOR::BLUE);
  1061. door::ANSIColor title_color(door::COLOR::CYAN, door::COLOR::BLUE,
  1062. door::ATTR::BOLD);
  1063. m.setColor(border_color);
  1064. mtitle.setColor(title_color);
  1065. mtitle.setPadding(" ", title_color);
  1066. m.setTitle(std::make_unique<door::Line>(mtitle), 1);
  1067. // m.setColorizer(true,
  1068. m.setRender(true, door::Menu::makeRender(
  1069. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
  1070. door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD),
  1071. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
  1072. door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD)));
  1073. // m.setColorizer(false,
  1074. m.setRender(false, door::Menu::makeRender(
  1075. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  1076. door::ATTR::BOLD),
  1077. door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE,
  1078. door::ATTR::BOLD),
  1079. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  1080. door::ATTR::BOLD),
  1081. door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLUE,
  1082. door::ATTR::BOLD)));
  1083. m.addSelection('P', "Play Cards");
  1084. m.addSelection('S', "View Scores");
  1085. m.addSelection('C', "Configure");
  1086. m.addSelection('H', "Help");
  1087. m.addSelection('A', "About this game");
  1088. m.addSelection('Q', "Quit");
  1089. return m;
  1090. }
  1091. door::Menu make_config_menu(void) {
  1092. door::Menu m(5, 5, 31);
  1093. door::Line mtitle(SPACEACE " Configuration Menu");
  1094. door::ANSIColor border_color(door::COLOR::CYAN, door::COLOR::BLUE);
  1095. door::ANSIColor title_color(door::COLOR::CYAN, door::COLOR::BLUE,
  1096. door::ATTR::BOLD);
  1097. m.setColor(border_color);
  1098. mtitle.setColor(title_color);
  1099. mtitle.setPadding(" ", title_color);
  1100. m.setTitle(std::make_unique<door::Line>(mtitle), 1);
  1101. // m.setColorizer(true,
  1102. m.setRender(true, door::Menu::makeRender(
  1103. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
  1104. door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD),
  1105. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
  1106. door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD)));
  1107. // m.setColorizer(false,
  1108. m.setRender(false, door::Menu::makeRender(
  1109. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  1110. door::ATTR::BOLD),
  1111. door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE,
  1112. door::ATTR::BOLD),
  1113. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  1114. door::ATTR::BOLD),
  1115. door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLUE,
  1116. door::ATTR::BOLD)));
  1117. m.addSelection('D', "Deck Colors");
  1118. m.addSelection('V', "View Settings");
  1119. m.addSelection('Q', "Quit");
  1120. return m;
  1121. }
  1122. /*
  1123. // all the possible deck colors
  1124. vector<std::string> deck_colors = {std::string("All"), std::string("Blue"),
  1125. std::string("Cyan"), std::string("Green"),
  1126. std::string("Magenta"), std::string("Red")};
  1127. */
  1128. door::Menu make_deck_menu(void) {
  1129. door::Menu m(5, 5, 31);
  1130. door::Line mtitle(SPACEACE " Deck Menu");
  1131. door::ANSIColor border_color(door::COLOR::CYAN, door::COLOR::BLUE);
  1132. door::ANSIColor title_color(door::COLOR::CYAN, door::COLOR::BLUE,
  1133. door::ATTR::BOLD);
  1134. m.setColor(border_color);
  1135. mtitle.setColor(title_color);
  1136. mtitle.setPadding(" ", title_color);
  1137. m.setTitle(std::make_unique<door::Line>(mtitle), 1);
  1138. m.setRender(true, makeColorRender(
  1139. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
  1140. door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD),
  1141. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD)));
  1142. m.setRender(false, makeColorRender(
  1143. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  1144. door::ATTR::BOLD),
  1145. door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE,
  1146. door::ATTR::BOLD),
  1147. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  1148. door::ATTR::BOLD)));
  1149. // build the menu options from the colors. First character = single letter
  1150. // option trigger.
  1151. for (auto iter = deck_colors.begin(); iter != deck_colors.end(); ++iter) {
  1152. char c = (*iter)[0];
  1153. m.addSelection(c, (*iter).c_str());
  1154. }
  1155. /*
  1156. m.addSelection('A', "All");
  1157. m.addSelection('B', "Blue");
  1158. m.addSelection('C', "Cyan");
  1159. m.addSelection('G', "Green");
  1160. m.addSelection('M', "Magenta");
  1161. m.addSelection('R', "Red");
  1162. */
  1163. return m;
  1164. }