public void Update(GameTime gameTime)
        {
            KeyboardState kbState = Keyboard.GetState();

            Keys[] pressedKeys = kbState.GetPressedKeys();
            foreach (Keys key in pressedKeys)
            {
                if (key == Keys.Escape)
                {
                    Screens.ScreenManager.GotoScreen("menu");
                    return;
                }
            }
            CardPile.Update(gameTime);
            foreach (Player player in Players)
            {
                player.Update(gameTime);
            }
            if (CurrentPlayState == PlayState.DEAL)
            {
                elapsedTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
                // Handle deal state
                if (elapsedTime > 0.3f)
                {
                    foreach (Player player in Players)
                    {
                        if (player.HandCardCount() < 3)
                        {
                            player.AddHandCards(Deck.DrawCard());
                        }
                        else if (player.TableFlippedCardCount() < 3)
                        {
                            player.AddTableFlippedCards(Deck.DrawCard());
                        }
                        else
                        {
                            player.AddTableCards(Deck.DrawCard());
                        }
                    }
                    if (Players.ElementAt(0).TableCardCount() == 3)
                    {
                        CurrentPlayState = PlayState.SWAP;
                        SwapState();
                    }
                    elapsedTime = 0f;
                }
            }
            else if (CurrentPlayState == PlayState.SWAP)
            {
                elapsedTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (elapsedTime > 15f)
                {
                    elapsedTime      = 0f;
                    CurrentPlayState = PlayState.PLAY;
                    AddPlayedCards(Deck.DrawCard());
                }
                else
                {
                    foreach (Player player in Players)
                    {
                        PlayerAction action = player.HandleInput(gameTime, this);
                        if (action != null && action.Type == PlayerActionType.SWAP_CARD && action.Cards.Count > 1)
                        {
                            Card handCard = action.Cards[0];
                            Card swapCard = action.Cards[1];
                            player.RemoveCard(handCard);
                            player.RemoveCard(swapCard);
                            player.AddHandCards(swapCard);
                            player.AddTableCards(handCard);
                        }
                    }
                }
            }
            else if (CurrentPlayState == PlayState.PLAY)
            {
                // Handle play state
                if (CurrentPlayer != null)
                {
                    if (CurrentPlayer.HasNoCardsLeft())
                    {
                        CurrentPlayState = PlayState.WON;
                        PlayerWinner();
                    }
                    else
                    {
                        PlayerAction action = CurrentPlayer.HandleInput(gameTime, this);
                        if (action != null)
                        {
                            if (action.Type == PlayerActionType.PLAY_CARD)
                            {
                                if (!PlayCards(action.Cards.ToArray()))
                                {
                                    if (CurrentPlayer.HandCardCount() == 0 || CurrentPlayer.HandCardCount() == 0 && CurrentPlayer.TableCardCount() == 0)
                                    {
                                        // Trying to play a flipped card
                                        foreach (Card card in action.Cards)
                                        {
                                            CurrentPlayer.RemoveCard(card);
                                            CurrentPlayer.AddHandCards(card);
                                        }
                                        PlayedCards(action.Cards.ToArray());
                                        List <Card> pickedUpCards = new List <Card>();
                                        while (CardPile.Pile.Count > 0)
                                        {
                                            Card pickedUpCard = CardPile.Pile.Pop();
                                            pickedUpCards.Add(pickedUpCard);
                                            CurrentPlayer.AddHandCards(pickedUpCard);
                                        }
                                        PickedUpCards(pickedUpCards.ToArray());
                                        NextPlayer();
                                    }
                                    foreach (Card card in action.Cards)
                                    {
                                        card.ResetPosition();
                                    }
                                }
                                else
                                {
                                    // Played a normal hand
                                    while (Deck.CanDrawCard() && CurrentPlayer.HandCardCount() < 3)
                                    {
                                        CurrentPlayer.AddHandCards(Deck.DrawCard());
                                    }
                                    PlayedCards(action.Cards.ToArray());
                                    if (!EvaluateCards(action.Cards.ElementAt(0)))
                                    {
                                        NextPlayer();
                                    }
                                }
                            }
                            else if (action.Type == PlayerActionType.PICK_UP_CARDS)
                            {
                                List <Card> pickedUpCards = new List <Card>();
                                while (CardPile.Pile.Count > 0)
                                {
                                    Card pickedUpCard = CardPile.Pile.Pop();
                                    pickedUpCards.Add(pickedUpCard);
                                    CurrentPlayer.AddHandCards(pickedUpCard);
                                }
                                PickedUpCards(pickedUpCards.ToArray());
                                NextPlayer();
                            }
                            else if (action.Type == PlayerActionType.TRY_CARD)
                            {
                                Card card = DrawCard();
                                if (card != null)
                                {
                                    CurrentPlayer.AddHandCards(card);
                                    TriedFromDeck();
                                }
                            }
                        }
                    }
                }
                else
                {
                    // Set random player as the current player
                    Random random = new Random();
                    CurrentPlayer = Players.ElementAt(random.Next(Players.Count - 1));
                    NewTurn();
                }
            }
            else if (CurrentPlayState == PlayState.WON)
            {
                elapsedTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (elapsedTime > 5f)
                {
                    foreach (Player player in Players)
                    {
                        player.ResetCards();
                    }
                    Deck = Deck.CreateFullDeck(Deck.Position.X, Deck.Position.Y);
                    Deck.Shuffle();
                    CardPile.Pile.Clear();
                    CurrentPlayState = PlayState.DEAL;
                    elapsedTime      = 0f;
                }
            }
        }
예제 #2
0
 public override PlayerAction HandleInput(GameTime gameTime, GameState state)
 {
     // Local input
     if (GameCursor.LeftMouseState(gameTime) == MouseAction.CLICK)
     {
         // Check if you can't play
         if (state.GetDeck().Contains(GameCursor.MousePosition()) && state.GetDeck().CanDrawCard() && GameCursor.SelectedCards.Count == 0)
         {
             return(new PlayerAction(PlayerActionType.TRY_CARD));
         }
         else if (state.GetPile().Contains(GameCursor.MousePosition()) && GameCursor.SelectedCards.Count == 0 && state.GetPile().Pile.Count > 0)
         {
             return(new PlayerAction(PlayerActionType.PICK_UP_CARDS));
         }
         else
         {
             Card card = SelectedCard(GameCursor.MousePosition());
             if (card != null && GameCursor.SelectedCards.Count == 0 && (selectedCards.Count == 0 || selectedCards.ElementAt(0).CompareTo(card) == 0))
             {
                 List <Card> tempCards = new List <Card>();
                 tempCards.AddRange(selectedCards);
                 if (!selectedCards.Contains(card))
                 {
                     tempCards.Add(card);
                 }
                 selectedCards.Clear();
                 GameCursor.SelectedCards.AddRange(tempCards);
             }
         }
     }
     else if (GameCursor.RightMouseState(gameTime) == MouseAction.CLICK)
     {
         Card card = SelectedCard(GameCursor.MousePosition());
         if (card != null && !GameCursor.SelectedCards.Contains(card))
         {
             if (selectedCards.Contains(card))
             {
                 selectedCards.Remove(card);
             }
             else if (selectedCards.Count == 0)
             {
                 selectedCards.Add(card);
             }
             else if (selectedCards.ElementAt(0).CompareTo(card) == 0)
             {
                 selectedCards.Add(card);
             }
         }
     }
     else if (GameCursor.LeftMouseState(gameTime) == MouseAction.RELEASE)
     {
         if (state.GetPile().Contains(GameCursor.MousePosition()) && GameCursor.SelectedCards.Count > 0)
         {
             Card[] cards = GameCursor.SelectedCards.ToArray();
             foreach (Card card in cards)
             {
                 card.ResetPosition();
             }
             GameCursor.SelectedCards.Clear();
             return(new PlayerAction(PlayerActionType.PLAY_CARD, cards));
         }
         else
         {
             PlayerAction action = null;
             foreach (Card tableCard in tableCards)
             {
                 if (tableCard.Contains(GameCursor.MousePosition()) && GameCursor.SelectedCards.Count > 0)
                 {
                     action = new PlayerAction(PlayerActionType.SWAP_CARD, GameCursor.SelectedCards[0], tableCard);
                 }
             }
             foreach (Card card in GameCursor.SelectedCards)
             {
                 card.ResetPosition();
             }
             GameCursor.SelectedCards.Clear();
             return(action);
         }
     }
     return(null);
 }