private void UpdateScore(CardHand hand, string playerId) { int sub_points = 0; int ace_count = 0; Card[] theCards = new Card[hand.GetNumCardsInHand()]; BlackJackData[] theAces = new BlackJackData[4]; BlackJackData[] theHand = new BlackJackData[hand.GetNumCardsInHand()]; for (int i = 0; i < hand.GetNumCardsInHand(); i++) { theCards[i] = hand.GetCard(i); bjData = new BlackJackData(); bjData.AddCardData(theCards[i], 10); theHand[i] = bjData; } var sorted = theHand.OrderBy(i => i.m_value).ToArray <BlackJackData>(); if (theCards.Length >= 2) { foreach (BlackJackData c in sorted) { if (c.m_id != 1) { sub_points += c.m_value; } else { theAces[ace_count] = c; ++ace_count; } } if (ace_count > 0) { while (ace_count > 0) { if (sub_points + 11 > 21) { if (theAces[ace_count - 1].m_id == 1) { sub_points += 1; } } else { sub_points += 11; } --ace_count; } } } if (playerId == "p1") { scoreP1 = sub_points; } else //"p2" { scoreP2 = sub_points; } }
/// <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); }