Selaa lähdekoodia

Updated cardgo to cardPos, cardPosLevel.

cardLevel is never used by itself, might delete that.
Steve Thielemann 4 vuotta sitten
vanhempi
commit
be32857367
4 muutettua tiedostoa jossa 175 lisäystä ja 65 poistoa
  1. 138 29
      deck.cpp
  2. 7 6
      deck.h
  3. 3 3
      main.cpp
  4. 27 27
      play.cpp

+ 138 - 29
deck.cpp

@@ -243,18 +243,6 @@ door::Panel *Deck::markOf(int c) {
   return p;
 }
 
-void Deck::part(int x, int y, door::Door &d, int level, bool left) {
-  // Render part of the back of a card.
-  y += 2;
-  if (!left) {
-    x += 2;
-  }
-  std::string c = backSymbol(level);
-  std::string l = c + c + c;
-  door::Goto g(x, y);
-  d << g << card_back_color << l;
-}
-
 door::Panel *Deck::card(int c) { return cards[c]; }
 
 /**
@@ -354,7 +342,7 @@ door::Panel *Deck::marker(int c) { return mark[c]; }
 void Deck::removeCard(door::Door &door, int c, int off_x, int off_y, bool left,
                       bool right) {
   int cx, cy, level;
-  cardgo(c, cx, cy, level);
+  cardPosLevel(c, cx, cy, level);
   if (level > 1)
     --level;
   std::string cstr = backSymbol(level);
@@ -404,6 +392,7 @@ XXXXX   XXXXX   XXXXX
 width = 5 * 10 + (2 * 9) = 50+18 = 68   !  I could do that!
 */
 
+#ifdef NO
 /**
  * @brief Where does this card go in relation to everything else?
  *
@@ -494,6 +483,126 @@ int levels[4] = {3, 6, 9, 10};
     level = -1;
   }
 }
+#endif
+
+void cardPos(int pos, int &x, int &y) {
+  const int space = 3;
+  const int height = 3;
+
+  // special cases here
+  if (pos == 28) {
+    cardPos(23, x, y);
+    y += height + 1;
+    return;
+  } else {
+    if (pos == 29) {
+      cardPos(22, x, y);
+      y += height + 1;
+      return;
+    }
+  }
+
+  const int CARD_WIDTH = 5;
+  int HALF_WIDTH = 3;
+  HALF_WIDTH += space / 2;
+
+  int between = CARD_WIDTH + space;
+  int level; // I still need level in my calculations
+
+  if (pos < 3) {
+    // top
+    level = 1;
+    y = (level - 1) * (height - 1) + 1;
+    x = pos * (between * 3) + between + HALF_WIDTH + space; // 10
+    return;
+  } else {
+    pos -= 3;
+  }
+  if (pos < 6) {
+    level = 2;
+    y = (level - 1) * (height - 1) + 1;
+    int group = (pos) / 2;
+    x = pos * between + (group * between) + CARD_WIDTH + space * 2;
+    return;
+  } else {
+    pos -= 6;
+  }
+  if (pos < 9) {
+    level = 3;
+    y = (level - 1) * (height - 1) + 1;
+    x = pos * between + HALF_WIDTH + space;
+    return;
+  } else {
+    pos -= 9;
+  }
+  if (pos < 10) {
+    level = 4;
+    y = (level - 1) * (height - 1) + 1;
+    x = (pos)*between + space;
+    return;
+  } else {
+    // something is wrong.
+    y = -1;
+    x = -1;
+    level = -1;
+  }
+}
+
+void cardLevel(int pos, int &level) {
+  /*
+  const int space = 3;
+  const int height = 3;
+  */
+
+  // special cases here
+  if (pos == 28) {
+    cardLevel(23, level);
+    --level;
+    return;
+  } else {
+    if (pos == 29) {
+      cardLevel(22, level);
+      --level;
+      return;
+    }
+  }
+
+  /*
+  const int CARD_WIDTH = 5;
+  int HALF_WIDTH = 3;
+  HALF_WIDTH += space / 2;
+  int between = CARD_WIDTH + space;
+  */
+
+  if (pos < 3) {
+    // top
+    level = 1;
+    return;
+  } else {
+    pos -= 3;
+  }
+
+  if (pos < 6) {
+    level = 2;
+    return;
+  } else {
+    pos -= 6;
+  }
+
+  if (pos < 9) {
+    level = 3;
+    return;
+  } else {
+    pos -= 9;
+  }
+  if (pos < 10) {
+    level = 4;
+    return;
+  } else {
+    // something is wrong.
+    level = -1;
+  }
+}
 
 /**
  * @brief Given card pos, calculate x, y, and level values.
@@ -505,20 +614,20 @@ int levels[4] = {3, 6, 9, 10};
  * @param y
  * @param level
  */
-void cardgo(int pos, int &x, int &y, int &level) {
+void cardPosLevel(int pos, int &x, int &y, int &level) {
   const int space = 3;
-  const int h = 3;
+  const int height = 3;
 
   // special cases here
   if (pos == 28) {
-    cardgo(23, x, y, level);
-    y += h + 1;
+    cardPosLevel(23, x, y, level);
+    y += height + 1;
     --level;
     return;
   } else {
     if (pos == 29) {
-      cardgo(22, x, y, level);
-      y += h + 1;
+      cardPosLevel(22, x, y, level);
+      y += height + 1;
       --level;
       return;
     }
@@ -533,7 +642,7 @@ void cardgo(int pos, int &x, int &y, int &level) {
   if (pos < 3) {
     // top
     level = 1;
-    y = (level - 1) * (h - 1) + 1;
+    y = (level - 1) * (height - 1) + 1;
     x = pos * (between * 3) + between + HALF_WIDTH + space; // 10
     return;
   } else {
@@ -541,7 +650,7 @@ void cardgo(int pos, int &x, int &y, int &level) {
   }
   if (pos < 6) {
     level = 2;
-    y = (level - 1) * (h - 1) + 1;
+    y = (level - 1) * (height - 1) + 1;
     int group = (pos) / 2;
     x = pos * between + (group * between) + CARD_WIDTH + space * 2;
     return;
@@ -550,7 +659,7 @@ void cardgo(int pos, int &x, int &y, int &level) {
   }
   if (pos < 9) {
     level = 3;
-    y = (level - 1) * (h - 1) + 1;
+    y = (level - 1) * (height - 1) + 1;
     x = pos * between + HALF_WIDTH + space;
     return;
   } else {
@@ -558,7 +667,7 @@ void cardgo(int pos, int &x, int &y, int &level) {
   }
   if (pos < 10) {
     level = 4;
-    y = (level - 1) * (h - 1) + 1;
+    y = (level - 1) * (height - 1) + 1;
     x = (pos)*between + space;
     return;
   } else {
@@ -627,9 +736,9 @@ cards makeCardStates(int decks) {
  * @return int
  */
 int findNextActiveCard(bool left, const cards &states, int current) {
-  int cx, cy, level;
+  int cx, cy;
   int current_x;
-  cardgo(current, cx, cy, level);
+  cardPos(current, cx, cy);
   current_x = cx;
   int x;
   int pos = -1;
@@ -650,7 +759,7 @@ int findNextActiveCard(bool left, const cards &states, int current) {
       // possible location
       if (x == current)
         continue;
-      cardgo(x, cx, cy, level);
+      cardPos(x, cx, cy);
       // find max and min while we're iterating here
       if (cx < min_x) {
         min_pos = x;
@@ -698,9 +807,9 @@ int findNextActiveCard(bool left, const cards &states, int current) {
  * @return int
  */
 int findClosestActiveCard(const cards &states, int current) {
-  int cx, cy, level;
+  int cx, cy;
   int current_x;
-  cardgo(current, cx, cy, level);
+  cardPos(current, cx, cy);
   current_x = cx;
   int x;
   int pos = -1;
@@ -711,7 +820,7 @@ int findClosestActiveCard(const cards &states, int current) {
       // possible location
       if (x == current)
         continue;
-      cardgo(x, cx, cy, level);
+      cardPos(x, cx, cy);
       if (pos == -1) {
         pos = x;
         pos_x = cx;

+ 7 - 6
deck.h

@@ -89,10 +89,11 @@ class Deck {
 private:
   // We assume for this game that there's only one deck back color.
   door::ANSIColor card_back_color;
-  // shared_ptr<door::Panel> for the win?
+
   vector<door::Panel *> cards;
   vector<door::Panel *> backs;
   vector<door::Panel *> mark;
+
   door::Panel *cardOf(int c);
   std::string backSymbol(int level);
   door::Panel *backOf(int level);
@@ -103,6 +104,7 @@ private:
 
 public:
   enum SUIT { HEART, DIAMOND, CLUBS, SPADE };
+  const static std::array<std::pair<int, int>, 18> blocks;
 
   Deck(door::ANSIColor backcolor = door::ANSIColor(door::COLOR::RED));
   Deck(Deck &&);
@@ -113,21 +115,20 @@ public:
   int getRank(int c);
   int getSuit(int c);
   int getDeck(int c);
-
   bool canPlay(int card1, int card2);
   door::Panel *card(int c);
   door::Panel *back(int level);
   door::Panel *marker(int c);
-
-  void part(int x, int y, door::Door &d, int level, bool left);
   std::vector<int> unblocks(int card);
-  const static std::array<std::pair<int, int>, 18> blocks;
 
   void removeCard(door::Door &door, int c, int off_x, int off_y, bool left,
                   bool right);
 };
 
-void cardgo(int pos, int &x, int &y, int &level);
+void cardPos(int pos, int &x, int &y);
+void cardLevel(int pos, int &level);
+void cardPosLevel(int pos, int &x, int &y, int &level);
+
 cards shuffleCards(std::seed_seq &seed, int decks = 1);
 cards makeCardStates(int decks = 1);
 int findNextActiveCard(bool left, const cards &states, int current);

+ 3 - 3
main.cpp

@@ -958,7 +958,7 @@ int main(int argc, char *argv[]) {
   int game_width;
   {
     int cx, cy, level;
-    cardgo(27, space, height, cx, cy, level);
+    cardPosLevel(27, space, height, cx, cy, level);
     game_width = cx + 5; // card width
   }
   int off_x = (mx - game_width) / 2;
@@ -979,7 +979,7 @@ int main(int argc, char *argv[]) {
   for (int x = 0; x < 28; x++) {
     int cx, cy, level;
 
-    cardgo(x, space, height, cx, cy, level);
+    cardPosLevel(x, space, height, cx, cy, level);
     // This is hardly visible.
     // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
     std::this_thread::sleep_for(std::chrono::milliseconds(75));
@@ -999,7 +999,7 @@ int main(int argc, char *argv[]) {
     // usleep(1000 * 20);
 
     state.at(x) = 1;
-    cardgo(x, space, height, cx, cy, level);
+    cardPosLevel(x, space, height, cx, cy, level);
     // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
     std::this_thread::sleep_for(std::chrono::milliseconds(200));
 

+ 27 - 27
play.cpp

@@ -93,8 +93,8 @@ int PlayCards::play_cards(void) {
   int game_width;
   int game_height = 20;
   {
-    int cx, cy, level;
-    cardgo(27, cx, cy, level);
+    int cx, cy;
+    cardPos(27, cx, cy);
     game_width = cx + 5;
   }
 
@@ -137,12 +137,12 @@ next_hand:
 
   {
     int off_yp = off_y + 11;
-    int cxp, cyp, levelp;
+    int cxp, cyp;
     int left_panel_x, right_panel_x;
     // find position of card, to position the panels
-    cardgo(18, cxp, cyp, levelp);
+    cardPos(18, cxp, cyp);
     left_panel_x = cxp;
-    cardgo(15, cxp, cyp, levelp);
+    cardPos(15, cxp, cyp);
     right_panel_x = cxp;
     score_panel->set(left_panel_x + off_x, off_yp);
     streak_panel->set(right_panel_x + off_x, off_yp);
@@ -194,13 +194,13 @@ next_hand:
           int cx, cy, level;
 
           if (play_card == 51) {
-            cardgo(29, cx, cy, level);
+            cardPosLevel(29, cx, cy, level);
             level = 0; // out of cards
             c = dp.back(level);
             c->set(cx + off_x, cy + off_y);
             door << *c;
           }
-          cardgo(28, cx, cy, level);
+          cardPos(28, cx, cy);
           c = dp.card(deck.at(play_card));
           c->set(cx + off_x, cy + off_y);
           door << *c;
@@ -257,7 +257,7 @@ next_hand:
             deck.at(select_card) = deck.at(play_card);
             deck.at(play_card) = temp;
             // select_card is -- invalidated here!  find "new" card.
-            int cx, cy, level;
+            int cx, cy;
 
             // erase/clear select_card
             std::vector<int> check = dp.unblocks(select_card);
@@ -273,14 +273,14 @@ next_hand:
             dp.removeCard(door, select_card, off_x, off_y, left, right);
 
             /*   // old way of doing this that leaves holes.
-            cardgo(select_card, cx, cy, level);
+            cardPosLevel(select_card, cx, cy, level);
             c = d.back(0);
             c->set(cx + off_x, cy + off_y);
             door << *c;
             */
 
             // redraw play card #28. (Which is the "old" select_card)
-            cardgo(28, cx, cy, level);
+            cardPos(28, cx, cy);
             c = dp.card(deck.at(play_card));
             c->set(cx + off_x, cy + off_y);
             door << *c;
@@ -310,7 +310,7 @@ next_hand:
                   get_logger() << "showing: " << to_check << std::endl;
                   */
                   state.at(to_check) = 1;
-                  cardgo(to_check, cx, cy, level);
+                  cardPos(to_check, cx, cy);
                   c = dp.card(deck.at(to_check));
                   c->set(cx + off_x, cy + off_y);
                   door << *c;
@@ -321,8 +321,8 @@ next_hand:
               // top card cleared
               // get_logger() << "top card cleared?" << std::endl;
               // display something at select_card position
-              int cx, cy, level;
-              cardgo(select_card, cx, cy, level);
+              int cx, cy;
+              cardPos(select_card, cx, cy);
               door << door::Goto(cx + off_x, cy + off_y);
               bonus();
 
@@ -371,7 +371,7 @@ next_hand:
               }
             }
             // update the select_card marker!
-            cardgo(select_card, cx, cy, level);
+            cardPos(select_card, cx, cy);
             c = dp.marker(1);
             c->set(cx + off_x + 2, cy + off_y + 2);
             door << *c;
@@ -390,13 +390,13 @@ next_hand:
         }*/
         if (new_select >= 0) {
 
-          int cx, cy, level;
-          cardgo(select_card, cx, cy, level);
+          int cx, cy;
+          cardPos(select_card, cx, cy);
           c = dp.marker(0);
           c->set(cx + off_x + 2, cy + off_y + 2);
           door << *c;
           select_card = new_select;
-          cardgo(select_card, cx, cy, level);
+          cardPos(select_card, cx, cy);
           c = dp.marker(1);
           c->set(cx + off_x + 2, cy + off_y + 2);
           door << *c;
@@ -414,13 +414,13 @@ next_hand:
         }
         */
         if (new_select >= 0) { //(new_active < 28) {
-          int cx, cy, level;
-          cardgo(select_card, cx, cy, level);
+          int cx, cy;
+          cardPos(select_card, cx, cy);
           c = dp.marker(0);
           c->set(cx + off_x + 2, cy + off_y + 2);
           door << *c;
           select_card = new_select;
-          cardgo(select_card, cx, cy, level);
+          cardPos(select_card, cx, cy);
           c = dp.marker(1);
           c->set(cx + off_x + 2, cy + off_y + 2);
           door << *c;
@@ -452,7 +452,7 @@ void PlayCards::redraw(bool dealing) {
     // step 1:
     // draw the deck "source"
     int cx, cy, level;
-    cardgo(29, cx, cy, level);
+    cardPosLevel(29, cx, cy, level);
 
     if (play_card == 51)
       level = 0; // out of cards!
@@ -476,7 +476,7 @@ void PlayCards::redraw(bool dealing) {
   for (int x = 0; x < (dealing ? 28 : 29); x++) {
     int cx, cy, level;
 
-    cardgo(x, cx, cy, level);
+    cardPosLevel(x, cx, cy, level);
     // This is hardly visible.
     // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
     if (dealing)
@@ -497,7 +497,7 @@ void PlayCards::redraw(bool dealing) {
         door << *c;
         break;
       case 1:
-        // cardgo(x, space, height, cx, cy, level);
+        // cardPosLevel(x, space, height, cx, cy, level);
         if (x == 28)
           c = dp.card(deck.at(play_card));
         else
@@ -519,10 +519,10 @@ void PlayCards::redraw(bool dealing) {
 
   if (dealing)
     for (int x = 18; x < 29; x++) {
-      int cx, cy, level;
+      int cx, cy;
 
       state.at(x) = 1;
-      cardgo(x, cx, cy, level);
+      cardPos(x, cx, cy);
       // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
       std::this_thread::sleep_for(std::chrono::milliseconds(200));
 
@@ -532,8 +532,8 @@ void PlayCards::redraw(bool dealing) {
     }
 
   {
-    int cx, cy, level;
-    cardgo(select_card, cx, cy, level);
+    int cx, cy;
+    cardPos(select_card, cx, cy);
     c = dp.marker(1);
     c->set(cx + off_x + 2, cy + off_y + 2);
     door << *c;