Android Open Source - AndroSol Card Stack






From Project

Back to project page AndroSol.

License

The source code is released under:

MIT License

If you think the Android project AndroSol listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package lib.cards.models;
/*from   w  w  w. j a  v a  2  s  . c  om*/
import java.util.ArrayList;
import java.util.Locale;

@SuppressWarnings("serial")
public class CardStack extends ArrayList<Card> {
    CardStack() {
    }

    CardStack(int minimumCapacity) {
        this.ensureCapacity(minimumCapacity);
    }

    private int index;

    // Will not be unique for plain CardStacks.
    public CardStackId getCardStackId() {
        CardStackId id = new CardStackId();
        id.index = getIndex();
        id.stackType = getStackType();
        return id;
    }

    public int getIndex() {
        return index;
    }

    protected void setIndex(int index) {
        this.index = index;
    }

    public StackType getStackType() {
        // StackType type = StackType.STACK;

        String typeName = this.getClass().getSimpleName()
                .toUpperCase(Locale.US);
        return StackType.valueOf(typeName);
        /*
         * for (StackType p : StackType.values()) { if
         * (p.name().equals(typeName)) { return p; } }
         * 
         * return type;
         */
    }

    public void init(Iterable<CardState> cards) {
        this.clear();
        for (CardState cardState : cards) {
            this.add(new Card(cardState.getId(), cardState.isFaceUp()));
        }
    }

    public static boolean buildsDown(Card topCard, Card card) {
        if (topCard != null) {
            return topCard.getValue().toInt() - 1 == card.getValue().toInt();
        }
        return false;
    }

    public static boolean buildsUp(Card topCard, Card card) {
        if (topCard != null) {
            return topCard.getValue().toInt() + 1 == card.getValue().toInt();
        }
        return false;
    }

    boolean canFillEmptyStackWith(Card card) {
        return true;
    }

    boolean couldBuildStackWith(Card topCard, Card card) {
        return true;
    }

    boolean canBuildStackWith(Card card) {
        return couldBuildStackWith(peek(), card);
    }

    public boolean canPush(Card card) {
        if (card == null) {
            return false;
        }

        if (peek() == null) {
            return canFillEmptyStackWith(card);
        }

        return canBuildStackWith(card);
    }

    public void push(Card card) {
        if (canPush(card)) {
            this.add(card);
        }
    }

    public Card pop() {
        if (size() == 0) {
            return null;
        }
        Card popped = this.get(size() - 1);
        this.remove(size() - 1);
        return popped;
    }

    public Card peek() {
        return size() == 0 ? null : this.get(size() - 1);
    }

    public String dumpString() {
        String dumpString = "";
        for (int i = 0; i < size(); i++) {
            if (i > 0) {
                dumpString += "  ";
            }
            dumpString += this.get(i).getShortName();
        }
        return dumpString;
    }
}




Java Source Code List

com.example.androsol.AndroidDeck.java
com.example.androsol.AndroidGameBoard.java
com.example.androsol.CardSpriteImpl.java
com.example.androsol.DeckTheme.java
com.example.androsol.DisplayMessageActivity.java
com.example.androsol.GameSurface.java
com.example.androsol.MainActivity.java
com.example.androsol.SpriteImpl.java
com.example.androsol.StackSpriteImpl.java
com.example.androsol.StandardDeck.java
lib.cards.controllers.Actions.java
lib.cards.controllers.DealAction.java
lib.cards.controllers.GameActionState.java
lib.cards.controllers.GameAction.java
lib.cards.controllers.GameController.java
lib.cards.controllers.NewGameActionState.java
lib.cards.controllers.NewGameAction.java
lib.cards.controllers.StackMoveAction.java
lib.cards.controllers.StackMoveState.java
lib.cards.models.CardColor.java
lib.cards.models.CardStackId.java
lib.cards.models.CardStack.java
lib.cards.models.CardState.java
lib.cards.models.CardSuit.java
lib.cards.models.CardValue.java
lib.cards.models.Card.java
lib.cards.models.CardsMovedEventHandler.java
lib.cards.models.CardsMovedEventObject.java
lib.cards.models.EmptyTableauPileFilledBy.java
lib.cards.models.EventHandler.java
lib.cards.models.FoundationBaseCard.java
lib.cards.models.FoundationSequence.java
lib.cards.models.Foundation.java
lib.cards.models.FreeCell.java
lib.cards.models.GameEventObject.java
lib.cards.models.GameOverEventHandler.java
lib.cards.models.GameOverEventObject.java
lib.cards.models.GameProperties.java
lib.cards.models.GameState.java
lib.cards.models.Game.java
lib.cards.models.MoveGroupsOfCardsAsAUnit.java
lib.cards.models.NewGameEventHandler.java
lib.cards.models.NumberOfCardsDealtFromStock.java
lib.cards.models.RedealsAllowed.java
lib.cards.models.RestoreGameEventHandler.java
lib.cards.models.ScoreChangedEventHandler.java
lib.cards.models.ScoreChangedEventObject.java
lib.cards.models.StackType.java
lib.cards.models.Stock.java
lib.cards.models.SubStackMovedEventHandler.java
lib.cards.models.SubStackMovedEventObject.java
lib.cards.models.SubStack.java
lib.cards.models.TableauSequence.java
lib.cards.models.TableauType.java
lib.cards.models.Tableau.java
lib.cards.models.Waste.java
lib.cards.utilities.CollectionUtils.java
lib.cards.utilities.CommandImpl.java
lib.cards.utilities.Command.java
lib.cards.utilities.Point.java
lib.cards.utilities.Rect.java
lib.cards.utilities.Size.java
lib.cards.utilities.UndoStack.java
lib.cards.views.CardSprite.java
lib.cards.views.DeckMetrics.java
lib.cards.views.Deck.java
lib.cards.views.GameBoardImpl.java
lib.cards.views.GameBoardMetrics.java
lib.cards.views.GameBoard.java
lib.cards.views.SpriteAddedEventHandler.java
lib.cards.views.SpriteDefaultActionEventHandler.java
lib.cards.views.SpriteEventObject.java
lib.cards.views.SpriteRemovedEventHandler.java
lib.cards.views.SpriteSelectedEventHandler.java
lib.cards.views.Sprite.java
lib.cards.views.StackSprite.java