// given a card, see if any of the home piles will accept it protected CardContainer FindHome(Card cardToMove) { // the home that will allow the card CardContainer home = null; if (cardToMove != null) { // determine potential home, based on suit switch (cardToMove.Suit) { case Card.SUITS.Spade: home = m_HomeSpade; break; case Card.SUITS.Club: home = m_HomeClub; break; case Card.SUITS.Diamond: home = m_HomeDiamond; break; case Card.SUITS.Heart: home = m_HomeHeart; break; } // the top card in the selected home pile Card cardToHost = home.TopCard; // the rank of the card that is moving int rankMove = (int)cardToMove.Rank; if (cardToHost != null) { // see if the cards have the proper ranks int rankHost = (int)cardToHost.Rank; if (rankHost != rankMove - 1) { home = null; } } else { // player may be trying to move an ace home if (rankMove != (int)Card.RANKS.Ace) { home = null; } } } // return our findings return home; }
// receive a new card into this container public void AcceptCard(Card card) { if (card != null) { m_Cards.Add(card); } }
// populate the main deck public void FillMainDeck() { if (Texture == null) { // the texture MUST be loaded before this method is called // this method inspects the texture to determine the locations // of each card, card back, and the cursor images throw new Exception("Card.Texture must be set before SolitareGame.Init is called."); } // helper variables to save some typing int w = Texture.Width; int h = Texture.Height; float dx = Card.CardSize.X; float dy = Card.CardSize.Y; // current position in the texture float x = 0; float y = 0; // for each suit for (int suit = 0; suit < 4; suit++) { // for each rank for (int rank = 0; rank < 13; rank++) { // create and configure a new card instance Card card = new Card(); card.TextureRect = new Rectangle((int)x, (int)y, (int)dx, (int)dy); card.IsFaceUp = false; card.Rank = (Card.RANKS)rank; card.Suit = (Card.SUITS)suit; // add the new card to the deck m_Deck.AcceptCard(card); // determine where the next card image will be x += dx; if (x > w || x + dx > w) { y += dy; x = 0; } } } // beyond the 52 standard cards, there are 14 other "card" images for (int back = -2; back < 12; back++) { if (back == -2) { // the black joker image Card.TextureRectBlackJoker = new Rectangle((int)x, (int)y, (int)dx, (int)dy); } else if (back == -1) { // the red joker image Card.TextureRectRedJoker = new Rectangle((int)x, (int)y, (int)dx, (int)dy); } else if (back == 10) { // the "no card here" image Card.TextureRectEmpty = new Rectangle((int)x, (int)y, (int)dx, (int)dy); } else if (back == 11) { // the card cursor, split into four seperate images int cw = Card.CURSOR_SIZE; int ch = Card.CURSOR_SIZE; Card.TextureRectCursorNW = new Rectangle((int)x, (int)y, cw, ch); Card.TextureRectCursorNE = new Rectangle((int)x + (int)dx - Card.CURSOR_SIZE, (int)y, cw, ch); Card.TextureRectCursorSE = new Rectangle((int)x + (int)dx - Card.CURSOR_SIZE, (int)y + (int)dy - Card.CURSOR_SIZE, cw, ch); Card.TextureRectCursorSW = new Rectangle((int)x, (int)y + (int)dy - Card.CURSOR_SIZE, cw, ch); } else { // one of the 10 card back images CardBack.AddTextureRect(new Rectangle((int)x, (int)y, (int)dx, (int)dy)); } // determine the location of the next card image x += dx; if (x > w || x + dx > w) { y += dy; x = 0; } } }