Advertisement
mmayoub

Solitaire, Column

Jul 30th, 2018
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 KB | None | 0 0
  1. import unit4.collectionsLib.Stack;
  2.  
  3. public class Column {
  4.     Stack<Card> cards;
  5.     int faceUpCount;
  6.  
  7.     public Column(Card[] cardsArr) {
  8.         this.cards = new Stack<Card>();
  9.         for (Card c : cardsArr) {
  10.             cards.push(c);
  11.         }
  12.  
  13.         if (cards.isEmpty())
  14.             this.faceUpCount = 0;
  15.         else
  16.             this.faceUpCount = 1;
  17.     }
  18.  
  19.     boolean add(Card newCard) {
  20.         if (cards.isEmpty()) {
  21.             if (newCard.getValue() == 13) {
  22.                 cards.push(newCard);
  23.                 faceUpCount = 1;
  24.                 return true;
  25.             }
  26.         } else {
  27.             if (!cards.top().getColor().equals(newCard) && cards.top().getValue() == newCard.getValue() + 1) {
  28.                 cards.push(newCard);
  29.                 faceUpCount++;
  30.                 return true;
  31.             }
  32.         }
  33.         return false;
  34.     }
  35.  
  36.     public boolean moveToColumn(Column targetColumn, int cardsToMove) {
  37.         // cardsToMove >= 1
  38.         if (cardsToMove < 1)
  39.             cardsToMove = 1;
  40.  
  41.         if (cardsToMove > faceUpCount)
  42.             return false;
  43.  
  44.         Stack<Card> tmp = new Stack<Card>();
  45.         for (int i = 0; i < cardsToMove; i++) {
  46.             tmp.push(cards.pop());
  47.         }
  48.  
  49.         if (targetColumn.add(tmp.top())) {
  50.             // move all cards to target
  51.             tmp.pop(); // already moved
  52.             while (!tmp.isEmpty())
  53.                 targetColumn.cards.push(tmp.pop());
  54.             faceUpCount -= cardsToMove;
  55.             targetColumn.faceUpCount += cardsToMove - 1;
  56.  
  57.             if (faceUpCount == 0 && !cards.isEmpty())
  58.                 faceUpCount = 1;
  59.  
  60.             return true;
  61.         } else {
  62.             // cann't move cards
  63.             while (!tmp.isEmpty())
  64.                 cards.push(tmp.pop());
  65.  
  66.             return false;
  67.         }
  68.  
  69.     }
  70.  
  71.     // move first card to final deck
  72.     public boolean moveToFinal(FinalDeck finalDeck) {
  73.         if (faceUpCount > 0) {
  74.             if (finalDeck.add(cards.top())) {
  75.                 cards.pop();
  76.                 faceUpCount--;
  77.  
  78.                 if (faceUpCount == 0 && !cards.isEmpty()) {
  79.                     faceUpCount = 1;
  80.                 }
  81.  
  82.                 return true;
  83.             }
  84.         }
  85.  
  86.         return false;
  87.     }
  88.  
  89.     @Override
  90.     public String toString() {
  91.         if (cards.isEmpty())
  92.             return "[     ]";
  93.  
  94.         String res = "";
  95.         Stack<Card> temp = new Stack<Card>();
  96.  
  97.         int i = 0;
  98.         while (!cards.isEmpty()) {
  99.             if (i < faceUpCount)
  100.                 res = cards.top().toString() + res;
  101.             else
  102.                 res = "(   )" + res;
  103.  
  104.             temp.push(cards.pop());
  105.             i++;
  106.         }
  107.  
  108.         while (!temp.isEmpty()) {
  109.             cards.push(temp.pop());
  110.         }
  111.  
  112.         return "[" + res + "]";
  113.     }
  114.  
  115.     public String[] toStringArray() {
  116.         if (cards.isEmpty())
  117.             return new String[0];
  118.  
  119.         Stack<Card> temp = new Stack<Card>();
  120.         int count = 0;
  121.  
  122.         while (!cards.isEmpty()) {
  123.             temp.push(cards.pop());
  124.             count++;
  125.         }
  126.  
  127.         String[] res = new String[count];
  128.         int i = 0;
  129.         while (!temp.isEmpty()) {
  130.             if (count - i <= faceUpCount)
  131.                 res[i] = temp.top().toString();
  132.             else
  133.                 res[i] = "(   )";
  134.  
  135.             cards.push(temp.pop());
  136.             i++;
  137.         }
  138.  
  139.         return res;
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement