Deck.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "Deck.h"
  2. #include "utils/Buffer.h"
  3. #include "utils/RenderBuffer.h"
  4. Deck::Deck(uint8_t count) : deck_count(count) {
  5. }
  6. void Deck::Generate() {
  7. waste_pile.clear();
  8. stock_pile.clear();
  9. //generate and shuffle deck
  10. int cards_count = 52 * deck_count;
  11. uint8_t cards[cards_count];
  12. for (int i = 0; i < cards_count; i++) cards[i] = i % 52;
  13. srand(DWT->CYCCNT);
  14. for (int i = 0; i < cards_count; i++) {
  15. int r = i + (rand() % (cards_count - i));
  16. uint8_t card = cards[i];
  17. cards[i] = cards[r];
  18. cards[r] = card;
  19. }
  20. //Init deck list
  21. for (int i = 0; i < 52; i++) {
  22. int letter = cards[i] % 13;
  23. int suit = cards[i] / 13;
  24. stock_pile.add(new Card(suit, letter));
  25. }
  26. FURI_LOG_D("Deck count", "%li", stock_pile.count);
  27. }
  28. void Deck::Cycle() {
  29. if (stock_pile.count > 0) {
  30. auto *c = stock_pile.pop();
  31. check_pointer(c);
  32. c->exposed = true;
  33. waste_pile.add(c);
  34. } else {
  35. while (waste_pile.count > 0) {
  36. auto *c = waste_pile.pop();
  37. check_pointer(c);
  38. c->exposed = false;
  39. stock_pile.add(c);
  40. }
  41. }
  42. }
  43. Card *Deck::GetLastWaste() {
  44. return waste_pile.pop();
  45. }
  46. void Deck::AddToWaste(Card *c) {
  47. check_pointer(c);
  48. waste_pile.add(c);
  49. }
  50. Card *Deck::Extract() {
  51. return stock_pile.pop();
  52. }
  53. void Deck::Render(RenderBuffer *buffer, bool stockpileSelect, bool wasteSelect) {
  54. if (stock_pile.count == 0) {
  55. Card::RenderEmptyCard(1, 1, buffer);
  56. if (stockpileSelect) {
  57. buffer->draw_rbox(2, 2, 17, 23, Flip);
  58. }
  59. } else {
  60. if (stock_pile.count > 1) {
  61. buffer->draw_rbox_frame(1, 1, 17, 23, Black);
  62. stock_pile.last()->Render(0, 0, stockpileSelect, buffer, 22);
  63. } else {
  64. stock_pile.last()->Render(1, 1, stockpileSelect, buffer, 22);
  65. }
  66. }
  67. if (waste_pile.count == 0) {
  68. Card::RenderEmptyCard(19, 1, buffer);
  69. if (wasteSelect) {
  70. buffer->draw_rbox(20, 2, 35, 23, Flip);
  71. }
  72. } else {
  73. waste_pile.last()->Render(19, 1, wasteSelect, buffer, 22);
  74. }
  75. }