Exemple #1
0
        private void StartGame()
        {
            scoreP1 = scoreP2 = 0;
            m_rootZ = 0;

            firstHand      = true;
            showP2Score    = false;
            isAskingToDraw = true;
            p1HasDrawn     = false;
            cpuHasStopped  = false;
            winner         = "";

            p1Hand = new CardHand();
            p2Hand = new CardHand();

            m_dataList = new BlackJackDataList();
            bjData     = new BlackJackData();

            // prepare the deck
            m_deck.RemoveAllCards();

            m_allCards = m_allCards_cpy.ToList();
            m_random   = new Random();

            // Source deck
            for (int i = 0; i < m_allCards.Count; i++)
            {
                m_deck.AddCard(GetRandomCard());
                i--;
            }

            // the card under the pile
            m_deck.GetCard(0).setTurned(false);

            m_dataList.AddDeckData(m_deck);

            Card drawn;

            // draw the first 2 cards
            for (int i = 0; i < 2; i++)
            {
                drawn = m_deck.RemoveLast();
                drawn.setTurned(true);
                p1Hand.AddCard(drawn);
                //System.Diagnostics.Debug.WriteLine("Giocatore 1 ha pescato : " + drawn.CardId() + " " + drawn.Suit());

                drawn = m_deck.RemoveLast();
                drawn.setTurned((i % 2 == 0) ? true : false);
                p2Hand.AddCard(drawn);
                //System.Diagnostics.Debug.WriteLine("Avversario ha pescato : " + drawn.CardId() + " " + drawn.Suit());
            }
        }
Exemple #2
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);
        }