Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import unit4.collectionsLib.Stack;
- public class Column {
- Stack<Card> cards;
- int faceUpCount;
- public Column(Card[] cardsArr) {
- this.cards = new Stack<Card>();
- for (Card c : cardsArr) {
- cards.push(c);
- }
- if (cards.isEmpty())
- this.faceUpCount = 0;
- else
- this.faceUpCount = 1;
- }
- boolean add(Card newCard) {
- if (cards.isEmpty()) {
- if (newCard.getValue() == 13) {
- cards.push(newCard);
- faceUpCount = 1;
- return true;
- }
- } else {
- if (!cards.top().getColor().equals(newCard) && cards.top().getValue() == newCard.getValue() + 1) {
- cards.push(newCard);
- faceUpCount++;
- return true;
- }
- }
- return false;
- }
- public boolean moveToColumn(Column targetColumn, int cardsToMove) {
- // cardsToMove >= 1
- if (cardsToMove < 1)
- cardsToMove = 1;
- if (cardsToMove > faceUpCount)
- return false;
- Stack<Card> tmp = new Stack<Card>();
- for (int i = 0; i < cardsToMove; i++) {
- tmp.push(cards.pop());
- }
- if (targetColumn.add(tmp.top())) {
- // move all cards to target
- tmp.pop(); // already moved
- while (!tmp.isEmpty())
- targetColumn.cards.push(tmp.pop());
- faceUpCount -= cardsToMove;
- targetColumn.faceUpCount += cardsToMove - 1;
- if (faceUpCount == 0 && !cards.isEmpty())
- faceUpCount = 1;
- return true;
- } else {
- // cann't move cards
- while (!tmp.isEmpty())
- cards.push(tmp.pop());
- return false;
- }
- }
- // move first card to final deck
- public boolean moveToFinal(FinalDeck finalDeck) {
- if (faceUpCount > 0) {
- if (finalDeck.add(cards.top())) {
- cards.pop();
- faceUpCount--;
- if (faceUpCount == 0 && !cards.isEmpty()) {
- faceUpCount = 1;
- }
- return true;
- }
- }
- return false;
- }
- @Override
- public String toString() {
- if (cards.isEmpty())
- return "[ ]";
- String res = "";
- Stack<Card> temp = new Stack<Card>();
- int i = 0;
- while (!cards.isEmpty()) {
- if (i < faceUpCount)
- res = cards.top().toString() + res;
- else
- res = "( )" + res;
- temp.push(cards.pop());
- i++;
- }
- while (!temp.isEmpty()) {
- cards.push(temp.pop());
- }
- return "[" + res + "]";
- }
- public String[] toStringArray() {
- if (cards.isEmpty())
- return new String[0];
- Stack<Card> temp = new Stack<Card>();
- int count = 0;
- while (!cards.isEmpty()) {
- temp.push(cards.pop());
- count++;
- }
- String[] res = new String[count];
- int i = 0;
- while (!temp.isEmpty()) {
- if (count - i <= faceUpCount)
- res[i] = temp.top().toString();
- else
- res[i] = "( )";
- cards.push(temp.pop());
- i++;
- }
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement