static void suffleDeck(CardInfo[] deck) { Random rand = new Random(); int cardCount=CardInfo.ranks.Length*CardInfo.suits.Length; for (int card = 0; card < cardCount; card++) { int random = rand.Next(cardCount); CardInfo swap = deck[card]; deck[card] = deck[random]; deck[random] = swap; } }
private 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]; if (cardspot >= 8) { rect.Inflate(0, hSurface - rect.Bottom); } if (isWithinRectangle(cardCenter, rect)) { 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) { 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 topcardp = topCard(piles[pile]); if (touchedCard.Suit < 2 != topcardp.Suit < 2 && touchedCard.Rank == topcardp.Rank - 1 ) { piles[pile].Add(touchedCard); return true; } } } break; } } if (touchedCardOrigin is CardInfo[]) { (touchedCardOrigin as CardInfo[])[touchedCardOriginIndex] = touchedCard; } else if(touchedCardOrigin is List<CardInfo>[]) { (touchedCardOrigin as List<CardInfo>[])[touchedCardOriginIndex].Add(touchedCard); } return false; }
private Rectangle GetCardTextureSource(CardInfo cardInfo) { return new Rectangle(wCard * cardInfo.Rank, hCard * cardInfo.Suit, wCard, hCard); }
private bool checkForAutoMove(CardInfo cardinfo) { if (cardinfo.Rank == 0) { for (int final = 0; final < 4; final++) { finals[final].Add(cardinfo); cardinfo.AutoMoveOffset = -new Vector2(cardSpots[final + 4].X, cardSpots[final + 4].Y); return true; } } else if (cardinfo.Rank == 1) { 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; }
/// <summary> /// 允许游戏运行逻辑,例如更新全部内容、 /// 检查冲突、收集输入信息以及播放音频。 /// </summary> /// <param name="gameTime">提供计时值的快照。</param> protected override void Update(GameTime gameTime) { // 允许游戏退出 if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // TODO: 在此处添加更新逻辑 bool checkForNextAutoMove = false; foreach (List<CardInfo> final in finals) { foreach (CardInfo card in final) { if (card.AutoMoveTime > TimeSpan.Zero) { card.AutoMoveTime -= gameTime.ElapsedGameTime; if (card.AutoMoveTime <= TimeSpan.Zero) { card.AutoMoveTime = TimeSpan.Zero; checkForNextAutoMove = true; } card.AutoMoveInterpolation = (float)card.AutoMoveTime.Ticks / AutoMoveDuration.Ticks; } } if (checkForNextAutoMove && !AnalyzeForAntoMove() && hasWon()) { congratsComponent.Enabled = true; } } while (TouchPanel.IsGestureAvailable) { GestureSample gesture = TouchPanel.ReadGesture(); Vector2 position = Vector2.Transform(gesture.Position,inverseMatrix); Vector2 delta = position - Vector2.Transform(gesture.Position-gesture.Delta,inverseMatrix); switch (gesture.GestureType) { case GestureType.Tap: if ((position - centerReplay).Length() < radiusReplay) { congratsComponent.Enabled = false; replay(); } break; case GestureType.FreeDrag: if (touchedCard != null) { touchedCardPosition += delta; } else if (firstDragInGesture) { tryPickUpCard(position); } firstDragInGesture = false; break; case GestureType.DragComplete: if (touchedCard != null && tryPutDownCard(touchedCard)) { calculateDisplayMatrix(); if (!AnalyzeForAntoMove() && hasWon()) { congratsComponent.Enabled = true; } } firstDragInGesture = true; touchedCard = null; break; } } base.Update(gameTime); }
/// <summary> /// 允许游戏在开始运行之前执行其所需的任何初始化。 /// 游戏能够在此时查询任何所需服务并加载任何非图形 /// 相关的内容。调用 base.Initialize 将枚举所有组件 /// 并对其进行初始化。 /// </summary> protected override void Initialize() { // TODO: 在此处添加初始化逻辑 for (int suit = 0; suit < CardInfo.suits.Length; suit++) { for (int rank = 0; rank < CardInfo.ranks.Length; rank++) { CardInfo card = new CardInfo(suit, rank); deck[suit * CardInfo.ranks.Length + rank] = card; } } for (int pile = 0; pile < 8; pile++) { piles[pile] = new List<CardInfo>(); } for (int final = 0; final < 4; final++) { finals[final] = new List<CardInfo>(); } congratsComponent = new CongratulationsComponent(this); congratsComponent.Enabled = false; this.Components.Add(congratsComponent); base.Initialize(); }