protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } // Process auto-move card and perhaps initiate next auto-move bool checkForNextAutoMove = false; foreach (List <CardInfo> final in finals) { foreach (CardInfo cardInfo in final) { if (cardInfo.AutoMoveTime > TimeSpan.Zero) { cardInfo.AutoMoveTime -= gameTime.ElapsedGameTime; if (cardInfo.AutoMoveTime <= TimeSpan.Zero) { cardInfo.AutoMoveTime = TimeSpan.Zero; checkForNextAutoMove = true; } cardInfo.AutoMoveInterpolation = (float)cardInfo.AutoMoveTime.Ticks / AutoMoveDuration.Ticks; } } } if (checkForNextAutoMove && !AnalyzeForAutoMove() && HasWon()) { congratsComponent.Enabled = true; } while (TouchPanel.IsGestureAvailable) { GestureSample gesture = TouchPanel.ReadGesture(); // Adjust position and delta for compressed image Vector2 position = Vector2.Transform(gesture.Position, inverseMatrix); Vector2 delta = position - Vector2.Transform(gesture.Position - gesture.Delta, inverseMatrix); switch (gesture.GestureType) { case GestureType.Tap: // Check if Replay is pressed if ((position - centerReplay).Length() < radiusReplay) { congratsComponent.Enabled = false; Replay(); } break; case GestureType.FreeDrag: // Continue to move a dragged card if (touchedCard != null) { touchedCardPosition += delta; } // Try to pick up a card else if (firstDragInGesture) { TryPickUpCard(position); } firstDragInGesture = false; break; case GestureType.DragComplete: if (touchedCard != null && TryPutDownCard(touchedCard)) { CalculateDisplayMatrix(); if (!AnalyzeForAutoMove() && HasWon()) { congratsComponent.Enabled = true; } } firstDragInGesture = true; touchedCard = null; break; } } base.Update(gameTime); }
protected override void Draw(GameTime gameTime) { spriteBatch.Begin(SpriteSortMode.Immediate, null, null, null, null, null, displayMatrix); spriteBatch.Draw(surface, Vector2.Zero, Color.White); // Draw holds for (int hold = 0; hold < 4; hold++) { CardInfo cardInfo = holds[hold]; if (cardInfo != null) { Rectangle source = GetCardTextureSource(cardInfo); Vector2 destination = new Vector2(cardSpots[hold].X, cardSpots[hold].Y); spriteBatch.Draw(cards, destination, source, Color.White); } } // Draw piles for (int pile = 0; pile < 8; pile++) { Rectangle cardSpot = cardSpots[pile + 8]; for (int card = 0; card < piles[pile].Count; card++) { CardInfo cardInfo = piles[pile][card]; Rectangle source = GetCardTextureSource(cardInfo); Vector2 destination = new Vector2(cardSpot.X, cardSpot.Y + card * yOverlay); spriteBatch.Draw(cards, destination, source, Color.White); } } // Draw finals including all previous cards (for auto-move) for (int pass = 0; pass < 2; pass++) { for (int final = 0; final < 4; final++) { for (int card = 0; card < finals[final].Count; card++) { CardInfo cardInfo = finals[final][card]; if (pass == 0 && cardInfo.AutoMoveInterpolation == 0 || pass == 1 && cardInfo.AutoMoveInterpolation != 0) { Rectangle source = GetCardTextureSource(cardInfo); Vector2 destination = new Vector2(cardSpots[final + 4].X, cardSpots[final + 4].Y) + cardInfo.AutoMoveInterpolation * cardInfo.AutoMoveOffset; spriteBatch.Draw(cards, destination, source, Color.White); } } } } // Draw touched card if (touchedCard != null) { Rectangle source = GetCardTextureSource(touchedCard); spriteBatch.Draw(cards, touchedCardPosition, source, Color.White); } spriteBatch.End(); base.Draw(gameTime); }
protected override void Initialize() { // Initialize deck for (int suit = 0; suit < 4; suit++) for (int rank = 0; rank < 13; rank++) { CardInfo cardInfo = new CardInfo(suit, rank); deck[suit * 13 + rank] = cardInfo; } // Create the List objects for the 8 piles for (int pile = 0; pile < 8; pile++) piles[pile] = new List<CardInfo>(); // Create the List objects for the 4 finals for (int final = 0; final < 4; final++) finals[final] = new List<CardInfo>(); // Create congratulations component congratsComponent = new CongratulationsComponent(this); congratsComponent.Enabled = false; this.Components.Add(congratsComponent); base.Initialize(); }
protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // Process auto-move card and perhaps initiate next auto-move bool checkForNextAutoMove = false; foreach (List<CardInfo> final in finals) foreach (CardInfo cardInfo in final) { if (cardInfo.AutoMoveTime > TimeSpan.Zero) { cardInfo.AutoMoveTime -= gameTime.ElapsedGameTime; if (cardInfo.AutoMoveTime <= TimeSpan.Zero) { cardInfo.AutoMoveTime = TimeSpan.Zero; checkForNextAutoMove = true; } cardInfo.AutoMoveInterpolation = (float)cardInfo.AutoMoveTime.Ticks / AutoMoveDuration.Ticks; } } if (checkForNextAutoMove && !AnalyzeForAutoMove() && HasWon()) { congratsComponent.Enabled = true; } while (TouchPanel.IsGestureAvailable) { GestureSample gesture = TouchPanel.ReadGesture(); // Adjust position and delta for compressed image Vector2 position = Vector2.Transform(gesture.Position, inverseMatrix); Vector2 delta = position - Vector2.Transform(gesture.Position - gesture.Delta, inverseMatrix); switch (gesture.GestureType) { case GestureType.Tap: // Check if Replay is pressed if ((position - centerReplay).Length() < radiusReplay) { congratsComponent.Enabled = false; Replay(); } break; case GestureType.FreeDrag: // Continue to move a dragged card if (touchedCard != null) { touchedCardPosition += delta; } // Try to pick up a card else if (firstDragInGesture) { TryPickUpCard(position); } firstDragInGesture = false; break; case GestureType.DragComplete: if (touchedCard != null && TryPutDownCard(touchedCard)) { CalculateDisplayMatrix(); if (!AnalyzeForAutoMove() && HasWon()) { congratsComponent.Enabled = true; } } firstDragInGesture = true; touchedCard = null; break; } } base.Update(gameTime); }
bool CheckForAutoMove(CardInfo cardInfo) { if (cardInfo.Rank == 0) // ie, ace { for (int final = 0; final < 4; final++) if (finals[final].Count == 0) { finals[final].Add(cardInfo); cardInfo.AutoMoveOffset = -new Vector2(cardSpots[final + 4].X, cardSpots[final + 4].Y); return true; } } else if (cardInfo.Rank == 1) // ie, deuce { for (int final = 0; final < 4; final++) { CardInfo topCardInfo = TopCard(finals[final]); if (topCardInfo != null && topCardInfo.Suit == cardInfo.Suit && topCardInfo.Rank == 0) { finals[final].Add(cardInfo); cardInfo.AutoMoveOffset = -new Vector2(cardSpots[final + 4].X, cardSpots[final + 4].Y); return true; } } } else { int slot = -1; int count = 0; for (int final = 0; final < 4; final++) { CardInfo topCardInfo = TopCard(finals[final]); if (topCardInfo != null) { if (topCardInfo.Suit == cardInfo.Suit && topCardInfo.Rank == cardInfo.Rank - 1) { slot = final; } else if (topCardInfo.Suit < 2 != cardInfo.Suit < 2 && topCardInfo.Rank >= cardInfo.Rank - 1) { count++; } } } if (slot >= 0 && count == 2) { cardInfo.AutoMoveOffset = -new Vector2(cardSpots[slot + 4].X, cardSpots[slot + 4].Y); finals[slot].Add(cardInfo); return true; } } return false; }
bool TryPutDownCard(CardInfo touchedCard) { Vector2 cardCenter = new Vector2(touchedCardPosition.X + wCard / 2, touchedCardPosition.Y + hCard / 2); for (int cardSpot = 0; cardSpot < 16; cardSpot++) { Rectangle rect = cardSpots[cardSpot]; // Greatly expand the card-spot rectangle for the piles if (cardSpot >= 8) rect.Inflate(0, hSurface - rect.Bottom); if (IsWithinRectangle(cardCenter, rect)) { // Check if the hold is empty if (cardSpot < 4) { int hold = cardSpot; if (holds[hold] == null) { holds[hold] = touchedCard; return true; } } else if (cardSpot < 8) { int final = cardSpot - 4; if (TopCard(finals[final]) == null) { if (touchedCard.Rank == 0) // ie, an ace { finals[final].Add(touchedCard); return true; } } else if (touchedCard.Suit == TopCard(finals[final]).Suit && touchedCard.Rank == TopCard(finals[final]).Rank + 1) { finals[final].Add(touchedCard); return true; } } else { int pile = cardSpot - 8; if (piles[pile].Count == 0) { piles[pile].Add(touchedCard); return true; } else { CardInfo topCard = TopCard(piles[pile]); if (touchedCard.Suit < 2 != topCard.Suit < 2 && touchedCard.Rank == topCard.Rank - 1) { piles[pile].Add(touchedCard); return true; } } } // The card was in a card-spot rectangle but wasn't a legal drop break; } } // Restore the card to its original place if (touchedCardOrigin is CardInfo[]) { (touchedCardOrigin as CardInfo[])[touchedCardOriginIndex] = touchedCard; } else { ((touchedCardOrigin as List<CardInfo>[])[touchedCardOriginIndex]).Add(touchedCard); } return false; }
static void ShuffleDeck(CardInfo[] deck) { Random rand = new Random(); for (int card = 0; card < 52; card++) { int random = rand.Next(52); CardInfo swap = deck[card]; deck[card] = deck[random]; deck[random] = swap; } }
static Rectangle GetCardTextureSource(CardInfo cardInfo) { return new Rectangle(wCard * cardInfo.Rank, hCard * cardInfo.Suit, wCard, hCard); }
bool TryPutDownCard(CardInfo touchedCard) { Vector2 cardCenter = new Vector2(touchedCardPosition.X + wCard / 2, touchedCardPosition.Y + hCard / 2); for (int cardSpot = 0; cardSpot < 16; cardSpot++) { Rectangle rect = cardSpots[cardSpot]; // Greatly expand the card-spot rectangle for the piles if (cardSpot >= 8) { rect.Inflate(0, hSurface - rect.Bottom); } if (IsWithinRectangle(cardCenter, rect)) { // Check if the hold is empty if (cardSpot < 4) { int hold = cardSpot; if (holds[hold] == null) { holds[hold] = touchedCard; return(true); } } else if (cardSpot < 8) { int final = cardSpot - 4; if (TopCard(finals[final]) == null) { if (touchedCard.Rank == 0) // ie, an ace { finals[final].Add(touchedCard); return(true); } } else if (touchedCard.Suit == TopCard(finals[final]).Suit&& touchedCard.Rank == TopCard(finals[final]).Rank + 1) { finals[final].Add(touchedCard); return(true); } } else { int pile = cardSpot - 8; if (piles[pile].Count == 0) { piles[pile].Add(touchedCard); return(true); } else { CardInfo topCard = TopCard(piles[pile]); if (touchedCard.Suit < 2 != topCard.Suit < 2 && touchedCard.Rank == topCard.Rank - 1) { piles[pile].Add(touchedCard); return(true); } } } // The card was in a card-spot rectangle but wasn't a legal drop break; } } // Restore the card to its original place if (touchedCardOrigin is CardInfo[]) { (touchedCardOrigin as CardInfo[])[touchedCardOriginIndex] = touchedCard; } else { ((touchedCardOrigin as List <CardInfo>[])[touchedCardOriginIndex]).Add(touchedCard); } return(false); }
bool CheckForAutoMove(CardInfo cardInfo) { if (cardInfo.Rank == 0) // ie, ace { for (int final = 0; final < 4; final++) { if (finals[final].Count == 0) { finals[final].Add(cardInfo); cardInfo.AutoMoveOffset = -new Vector2(cardSpots[final + 4].X, cardSpots[final + 4].Y); return(true); } } } else if (cardInfo.Rank == 1) // ie, deuce { for (int final = 0; final < 4; final++) { CardInfo topCardInfo = TopCard(finals[final]); if (topCardInfo != null && topCardInfo.Suit == cardInfo.Suit && topCardInfo.Rank == 0) { finals[final].Add(cardInfo); cardInfo.AutoMoveOffset = -new Vector2(cardSpots[final + 4].X, cardSpots[final + 4].Y); return(true); } } } else { int slot = -1; int count = 0; for (int final = 0; final < 4; final++) { CardInfo topCardInfo = TopCard(finals[final]); if (topCardInfo != null) { if (topCardInfo.Suit == cardInfo.Suit && topCardInfo.Rank == cardInfo.Rank - 1) { slot = final; } else if (topCardInfo.Suit < 2 != cardInfo.Suit < 2 && topCardInfo.Rank >= cardInfo.Rank - 1) { count++; } } } if (slot >= 0 && count == 2) { cardInfo.AutoMoveOffset = -new Vector2(cardSpots[slot + 4].X, cardSpots[slot + 4].Y); finals[slot].Add(cardInfo); return(true); } } return(false); }
static Rectangle GetCardTextureSource(CardInfo cardInfo) { return(new Rectangle(wCard * cardInfo.Rank, hCard * cardInfo.Suit, wCard, hCard)); }