deck.cpp 41 KB

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