TableauColumn.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "TableauColumn.h"
  2. #include "utils/RenderBuffer.h"
  3. #define max(a, b) a>b?a:b
  4. void TableauColumn::Reset() {
  5. cards->empty();
  6. }
  7. void TableauColumn::AddCard(Card *c) {
  8. cards->add(c);
  9. }
  10. void TableauColumn::AddRange(List<Card> *hand) {
  11. for (auto *item: *hand) {
  12. cards->add(item);
  13. }
  14. }
  15. void TableauColumn::Render(uint8_t x, uint8_t y, bool selected, uint8_t selection_from_end, RenderBuffer *buffer) {
  16. // If the hand is empty
  17. if (cards->count == 0) {
  18. Card::RenderEmptyCard(x, y, buffer);
  19. if (selected)
  20. buffer->draw_rbox(x+1, y+1, x + 16, y + 22, Flip);
  21. return;
  22. }
  23. uint8_t selection = cards->count - selection_from_end;
  24. if (selected) selection--;
  25. uint8_t loop_end = cards->count;
  26. uint8_t loop_start = max(loop_end - 4, 0);
  27. uint8_t position = 0;
  28. uint8_t first_non_flipped = FirstNonFlipped();
  29. bool had_top = false;
  30. // Draw the first flipped and non-flipped card with adjusted visibility
  31. if (first_non_flipped <= loop_start && selection != first_non_flipped) {
  32. // Draw a card back if it is not the first card
  33. if (first_non_flipped > 0) {
  34. Card::RenderBack(x, y + position, selection == first_non_flipped, buffer, 5);
  35. // Increment loop start index and position
  36. position += 4;
  37. loop_start++;
  38. had_top = true;
  39. }
  40. // Draw the front side of the first non-flipped card
  41. (*cards)[first_non_flipped]->Render(x, y + position, selection == first_non_flipped, buffer,
  42. cards->count == 1 ? 22 : 9);
  43. position += 8;
  44. loop_start++; // Increment loop start index
  45. }
  46. // Draw the selected card with adjusted visibility
  47. if (loop_start > selection) {
  48. if (!had_top && first_non_flipped > 0) {
  49. Card::RenderBack(x, y + position, selection == first_non_flipped, buffer, 5);
  50. position += 2;
  51. loop_start++;
  52. }
  53. // Draw the front side of the selected card
  54. (*cards)[selection]->Render(x, y + position, true, buffer, 9);
  55. position += 8;
  56. loop_start++; // Increment loop start index
  57. }
  58. //Draw the rest
  59. for (uint8_t i = loop_start; i < loop_end; i++, position += 4) {
  60. int height = 5;
  61. if((i + 1) == loop_end) height = 22;
  62. else if(i == first_non_flipped) height = 9;
  63. (*cards)[i]->Render(x, y + position, i == selection, buffer, height);
  64. if (i == selection || i == first_non_flipped) position += 4;
  65. }
  66. }
  67. int8_t TableauColumn::FirstNonFlipped() {
  68. int8_t index = -1;
  69. if (cards->count > 0) {
  70. for (auto *card: *cards) {
  71. index++;
  72. if (card && card->exposed) {
  73. break;
  74. }
  75. }
  76. }
  77. return index;
  78. }
  79. TableauColumn::TableauColumn() {
  80. cards = new List<Card>();
  81. }
  82. TableauColumn::~TableauColumn() {
  83. delete cards;
  84. }
  85. void TableauColumn::Merge(TableauColumn *other) {
  86. for (auto *item: *(other->cards)) {
  87. cards->add(item);
  88. }
  89. other->cards->soft_clear();
  90. }
  91. Card *TableauColumn::TopCard() {
  92. return (*cards)[0];
  93. }
  94. uint8_t TableauColumn::Count() {
  95. return cards->count;
  96. }
  97. Card *TableauColumn::Pop() {
  98. return cards->pop();
  99. }
  100. void TableauColumn::Reveal() {
  101. (*cards)[cards->count - 1]->exposed = true;
  102. }
  103. Card *TableauColumn::LastCard() {
  104. if (cards->count > 0) {
  105. return (*cards)[cards->count - 1];
  106. }
  107. return nullptr;
  108. }
  109. void TableauColumn::Clear() {
  110. cards->soft_clear();
  111. }
  112. bool TableauColumn::CanPlace(TableauColumn *other) {
  113. if (cards->count == 0) {
  114. if(other->TopCard()->value == 11) FURI_LOG_D("TBLC", "placing first");
  115. return other->TopCard()->value == 11;
  116. }
  117. Card *last = LastCard();
  118. Card *top = other->TopCard();
  119. int current_suit = last->suit / 2;
  120. int other_suit = top->suit / 2;
  121. if((current_suit + 1) % 2 == other_suit && (last->value + 1) % 13 == (top->value + 2) % 13) FURI_LOG_D("TBLC", "Adding at end");
  122. else
  123. FURI_LOG_D("TBLC", "CANT ADD, suit check %i, label check %i", (current_suit + 1) % 2 == other_suit, (last->value + 1) % 13 == (top->value + 2) % 13);
  124. return (current_suit + 1) % 2 == other_suit && (last->value + 1) % 13 == (top->value + 2) % 13;
  125. }