Deck.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.empty();
  8. stock_pile.empty();
  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. }
  27. void Deck::Cycle() {
  28. if (stock_pile.count > 0) {
  29. auto *c = stock_pile.pop();
  30. c->exposed = true;
  31. waste_pile.add(c);
  32. } else {
  33. while (waste_pile.count > 0) {
  34. auto *c = waste_pile.pop();
  35. c->exposed = false;
  36. stock_pile.add(c);
  37. }
  38. }
  39. }
  40. Card *Deck::GetLastWaste() {
  41. return waste_pile.pop();
  42. }
  43. void Deck::AddToWaste(Card *c) {
  44. waste_pile.add(c);
  45. }
  46. Card *Deck::Extract() {
  47. return stock_pile.pop();
  48. }
  49. void Deck::Render(RenderBuffer *buffer, bool stockpileSelect, bool wasteSelect) {
  50. if (stock_pile.count == 0) {
  51. Card::RenderEmptyCard(1, 1, buffer);
  52. if (stockpileSelect) {
  53. buffer->draw_rbox(2, 2, 17, 23, Flip);
  54. }
  55. } else {
  56. if (stock_pile.count > 1) {
  57. buffer->draw_rbox_frame(1, 1, 17, 23, Black);
  58. stock_pile.last()->Render(0, 0, stockpileSelect, buffer, 22);
  59. } else {
  60. stock_pile.last()->Render(1, 1, stockpileSelect, buffer, 22);
  61. }
  62. }
  63. if (waste_pile.count == 0) {
  64. Card::RenderEmptyCard(19, 1, buffer);
  65. if (wasteSelect) {
  66. buffer->draw_rbox(20, 2, 35, 23, Flip);
  67. }
  68. } else {
  69. waste_pile.last()->Render(19, 1, wasteSelect, buffer, 22);
  70. }
  71. }