예제 #1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            KeyboardState state = Keyboard.GetState();

            // Quit the game if Escape is pressed.
            if (state.IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            if (state.IsKeyDown(Keys.M)
                & !previousState.IsKeyDown(Keys.M))
            {
                MuteUnmuteSound();
                previousState = state;
                return;
            }

            // Start the game if Space is pressed.
            if (!gameStarted)
            {
                if (state.IsKeyDown(Keys.Space)
                    & !previousState.IsKeyDown(Keys.Space))
                {
                    PrepareNewGame();
                }
                previousState = state;
                return;
            }

            if (firstHand && (p1Hand.GetNumCardsInHand() == p2Hand.GetNumCardsInHand()) &&
                p1Hand.GetNumCardsInHand() == 2)
            {
                UpdateScore(p1Hand, "p1");
                UpdateScore(p2Hand, "p2");
                CheckForWinner();
                firstHand = false;
            }
            if (winner != "")
            {
                if (state.IsKeyDown(Keys.Space)
                    & !previousState.IsKeyDown(Keys.Space))
                {
                    int i;
                    // explicitly turn the cards to back face
                    for (i = 0; i < p1Hand.GetNumCardsInHand(); i++)
                    {
                        p1Hand.GetCard(i).setTurned(false);
                    }
                    for (i = 0; i < p2Hand.GetNumCardsInHand(); i++)
                    {
                        p2Hand.GetCard(i).setTurned(false);
                    }

                    gameOver  = true;
                    spaceDown = false;

                    PrepareNewGame();
                }
                else
                {
                    var timer = (float)gameTime.ElapsedGameTime.TotalSeconds;
                    cpuHandAction.Update(timer);
                }

                previousState = state;
                return;
            }
            if (isAskingToDraw)
            {
                if (state.IsKeyDown(Keys.S)
                    & !previousState.IsKeyDown(Keys.S))
                {
                    // P1: draw a new card from the deck
                    Card drawn = m_deck.RemoveLast();
                    drawn.setTurned(true);
                    p1Hand.AddCard(drawn);
                    p1HasDrawn = true;
                }
                if (state.IsKeyDown(Keys.N)
                    & !previousState.IsKeyDown(Keys.N))
                {
                    p1HasDrawn     = false;
                    isAskingToDraw = false;
                }
            }

            // update the score
            UpdateScore(p1Hand, "p1");

            // the cpu could draw too
            if (isAskingToDraw && p1HasDrawn)
            {
                if (CpuHasToDraw())
                {
                    // CPU: draw a new card from the deck
                    Card drawn = m_deck.RemoveLast();
                    p2Hand.AddCard(drawn);
                }
                p1HasDrawn = false;
            }
            else
            {
                if (!isAskingToDraw && !cpuHasStopped)
                {
                    if (CpuHasToDraw())
                    {
                        // CPU: draw a new card from the deck
                        Card drawn = m_deck.RemoveLast();
                        drawn.setTurned(false);
                        p2Hand.AddCard(drawn);
                    }
                }
            }
            UpdateScore(p2Hand, "p2");
            if (!isAskingToDraw && ((scoreP2 >= scoreP1) || (scoreP1 >= 20)))
            {
                cpuHasStopped = true;
            }

            CheckForWinner();

            previousState = state;

            base.Update(gameTime);
        }