/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); // create and shuffle deck deck = new Deck(Content, 0, 0); deck.Shuffle(); // first player card playerHand.Add(deck.TakeTopCard()); playerHand.ElementAt(0).X = HORIZONTAL_CARD_OFFSET; playerHand.ElementAt(0).Y = TOP_CARD_OFFSET; playerHand.ElementAt(0).FlipOver(); // first dealer card dealerHand.Add(deck.TakeTopCard()); dealerHand.ElementAt(0).X = WINDOW_WIDTH - HORIZONTAL_CARD_OFFSET; dealerHand.ElementAt(0).Y = TOP_CARD_OFFSET; // second player card playerHand.Add(deck.TakeTopCard()); playerHand.ElementAt(1).X = HORIZONTAL_CARD_OFFSET; playerHand.ElementAt(1).Y = TOP_CARD_OFFSET + VERTICAL_CARD_SPACING; playerHand.ElementAt(1).FlipOver(); // second dealer card dealerHand.Add(deck.TakeTopCard()); dealerHand.ElementAt(1).X = WINDOW_WIDTH - HORIZONTAL_CARD_OFFSET; dealerHand.ElementAt(1).Y = TOP_CARD_OFFSET + VERTICAL_CARD_SPACING; dealerHand.ElementAt(1).FlipOver(); // load sprite font, create message for player score and add to list messageFont = Content.Load<SpriteFont>("Arial24"); playerScoreMessage = new Message(SCORE_MESSAGE_PREFIX + GetBlackjackScore(playerHand).ToString(), messageFont, new Vector2(HORIZONTAL_MESSAGE_OFFSET, SCORE_MESSAGE_TOP_OFFSET)); messages.Add(playerScoreMessage); // load quit button sprite for later use quitButtonSprite = Content.Load<Texture2D>("quitbutton"); // create hit button and add to list Texture2D hitButtonSprite = Content.Load<Texture2D>("hitbutton"); menuButtons.Add(new MenuButton(hitButtonSprite, new Vector2(HORIZONTAL_MENU_BUTTON_OFFSET, TOP_MENU_BUTTON_OFFSET), GameState.PlayerHitting)); // create stand button and add to list Texture2D standButtonSprite = Content.Load<Texture2D>("standbutton"); menuButtons.Add(new MenuButton(standButtonSprite, new Vector2(HORIZONTAL_MENU_BUTTON_OFFSET, TOP_MENU_BUTTON_OFFSET + VERTICAL_MENU_BUTTON_SPACING), GameState.WaitingForDealer)); }
/// <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) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // update menu buttons as appropriate MouseState mouseState = Mouse.GetState(); foreach (MenuButton btn in menuButtons) { if (currentState == GameState.WaitingForPlayer || currentState == GameState.DisplayingHandResults) { btn.Update(mouseState); } } // game state-specific processing switch (currentState) { case GameState.PlayerHitting: // get a new card for the player Card playerCard = deck.TakeTopCard(); playerCard.FlipOver(); playerCard.X = HORIZONTAL_CARD_OFFSET; playerCard.Y = TOP_CARD_OFFSET + playerHand.Count() * VERTICAL_MENU_BUTTON_SPACING; playerHand.Add(playerCard); playerHit = true; // adapt score and state playerScoreMessage.Text = SCORE_MESSAGE_PREFIX + GetBlackjackScore(playerHand).ToString(); ChangeState(GameState.WaitingForDealer); break; case GameState.WaitingForDealer: // decide for dealer to stand or hit if (GetBlackjackScore(dealerHand) < DEALER_STAND_SCORE) { ChangeState(GameState.DealerHitting); } else { ChangeState(GameState.CheckingHandOver); } break; case GameState.DealerHitting: // get a new card for the dealer Card dealerCard = deck.TakeTopCard(); dealerCard.FlipOver(); dealerCard.X = WINDOW_WIDTH - HORIZONTAL_CARD_OFFSET; dealerCard.Y = TOP_CARD_OFFSET + dealerHand.Count() * VERTICAL_MENU_BUTTON_SPACING; dealerHand.Add(dealerCard); dealerHit = true; // set next state ChangeState(GameState.CheckingHandOver); break; case GameState.CheckingHandOver: // if either player or dealer busts or both player stand if (GetBlackjackScore(playerHand) > MAX_HAND_VALUE || GetBlackjackScore(dealerHand) > MAX_HAND_VALUE || !(dealerHit || playerHit)) { string winnerText = ""; // if both players bust or have the same score, it's a tie if ((GetBlackjackScore(playerHand) > MAX_HAND_VALUE && GetBlackjackScore(dealerHand) > MAX_HAND_VALUE) || (GetBlackjackScore(playerHand) == GetBlackjackScore(dealerHand))) { winnerText = "It's a Tie!"; } else if (GetBlackjackScore(playerHand) > MAX_HAND_VALUE) { // if player busts, dealer wins winnerText = "Dealer Won!"; } else if (GetBlackjackScore(dealerHand) > MAX_HAND_VALUE) { // if dealer busts, player wins winnerText = "Player Won!"; } else if (GetBlackjackScore(playerHand) < GetBlackjackScore(dealerHand)) { // if player has less points, dealer wins winnerText = "Dealer Won!"; } else if (GetBlackjackScore(playerHand) > GetBlackjackScore(dealerHand)) { // if player has less points, dealer wins winnerText = "Player Won!"; } // flip over first dealer card dealerHand[0].FlipOver(); // create winner message Message winnerMsg = new Message(winnerText, messageFont, winnerMessageLocation); messages.Add(winnerMsg); // create dealer score Message dealerScoreMessage = new Message(SCORE_MESSAGE_PREFIX + GetBlackjackScore(dealerHand).ToString(), messageFont, new Vector2(WINDOW_WIDTH-HORIZONTAL_MESSAGE_OFFSET, SCORE_MESSAGE_TOP_OFFSET)); messages.Add(dealerScoreMessage); // remove stand and hit buttons and add quit button menuButtons.RemoveAt(0); menuButtons.RemoveAt(0); menuButtons.Add(new MenuButton(quitButtonSprite, new Vector2(HORIZONTAL_MENU_BUTTON_OFFSET, QUIT_MENU_BUTTON_OFFSET), GameState.Exiting)); ChangeState(GameState.DisplayingHandResults); } else { ChangeState(GameState.WaitingForPlayer); } // reset the hit state of playerhit and dealerhit to false playerHit = false; dealerHit = false; break; case GameState.Exiting: // exit the game Exit(); break; default: break; } base.Update(gameTime); }
/// <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) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // update menu buttons as appropriate MouseState mouse = Mouse.GetState(); if (currentState == GameState.WaitingForPlayer || currentState == GameState.DisplayingHandResults) foreach (MenuButton button in menuButtons) button.Update(mouse); // game state-specific processing switch (currentState) { case GameState.PlayerHitting: playerHit = true; // Add a card to the player's hand playerHand.Add(deck.TakeTopCard()); playerHand.Last().X = HORIZONTAL_CARD_OFFSET; playerHand.Last().Y = TOP_CARD_OFFSET + (playerHand.Count() - 1) * VERTICAL_CARD_SPACING; playerHand.Last().FlipOver(); // Update score and message messages.Remove(playerScoreMessage); playerScoreMessage = new Message(SCORE_MESSAGE_PREFIX + GetBlackjackScore(playerHand).ToString(), messageFont, new Vector2(HORIZONTAL_MESSAGE_OFFSET, SCORE_MESSAGE_TOP_OFFSET)); messages.Add(playerScoreMessage); currentState = GameState.WaitingForDealer; break; case GameState.WaitingForDealer: if (GetBlackjackScore(dealerHand) <= 16) currentState = GameState.DealerHitting; else if (GetBlackjackScore(dealerHand) >= 17) currentState = GameState.CheckingHandOver; break; case GameState.DealerHitting: dealerHit = true; // Add a card to the dealer's hand dealerHand.Add(deck.TakeTopCard()); dealerHand.Last().X = WINDOW_WIDTH - HORIZONTAL_CARD_OFFSET; dealerHand.Last().Y = TOP_CARD_OFFSET + (dealerHand.Count() - 1) * VERTICAL_CARD_SPACING; dealerHand.Last().FlipOver(); currentState = GameState.CheckingHandOver; break; case GameState.CheckingHandOver: if (!dealerHit && !playerHit) // Player and Dealer stood so game is coming to an end { if (GetBlackjackScore(dealerHand) > GetBlackjackScore(playerHand)) { //Dealer won messages.Add(new Message("Dealer Won", messageFont, winnerMessageLocation)); } else if (GetBlackjackScore(dealerHand) < GetBlackjackScore(playerHand)) { //Player won messages.Add(new Message("Player Won", messageFont, winnerMessageLocation)); } else { messages.Add(new Message("It's a Tie", messageFont, winnerMessageLocation)); } } else // Someone hit so check for busted { if (GetBlackjackScore(dealerHand) > MAX_HAND_VALUE && GetBlackjackScore(playerHand) > MAX_HAND_VALUE) { //Both the players busted messages.Add(new Message("Both players were busted", messageFont, winnerMessageLocation)); } else if (GetBlackjackScore(dealerHand) > MAX_HAND_VALUE && GetBlackjackScore(playerHand) <= MAX_HAND_VALUE) { //Dealer Busted messages.Add(new Message("Dealer Busted, Player Won", messageFont, winnerMessageLocation)); } else if (GetBlackjackScore(dealerHand) <= MAX_HAND_VALUE && GetBlackjackScore(playerHand) > MAX_HAND_VALUE) { //Player Busted messages.Add(new Message("Player Busted, Dealer Won", messageFont, winnerMessageLocation)); } else if (GetBlackjackScore(dealerHand) == GetBlackjackScore(playerHand)) { //TIE messages.Add(new Message("It's a Tie", messageFont, winnerMessageLocation)); } else // No one busted so allow another hit { if (playerHit) // If player was the one who hit, change hit flag to false and wait for input { playerHit = false; currentState = GameState.WaitingForPlayer; break; } else if (dealerHit) // If dealer was the one who hit, change hit flag to false and wait for input { dealerHit = false; currentState = GameState.WaitingForDealer; break; } } } // Display hand, scores and the end game screen dealerHand.ElementAt(0).FlipOver(); messages.Add(new Message(SCORE_MESSAGE_PREFIX + GetBlackjackScore(dealerHand).ToString(), messageFont, new Vector2(WINDOW_WIDTH - HORIZONTAL_MESSAGE_OFFSET, SCORE_MESSAGE_TOP_OFFSET))); menuButtons.Clear(); menuButtons.Add(new MenuButton(quitButtonSprite, new Vector2(HORIZONTAL_MENU_BUTTON_OFFSET, QUIT_MENU_BUTTON_OFFSET), GameState.Exiting)); currentState = GameState.DisplayingHandResults; break; case GameState.DisplayingHandResults: break; case GameState.Exiting: this.Exit(); break; } base.Update(gameTime); }
/// <summary> /// Adds the player won message. /// </summary> private void playerWon() { winnerMessage = new Message(playerWonMessage, messageFont, winnerMessageLocation); }
/// <summary> /// Adds the tie message. /// </summary> private void tie() { winnerMessage = new Message(tieMessage, messageFont, winnerMessageLocation); }
/// <summary> /// Displays the result. /// </summary> private void displayWinnerMessage() { // prepare dealer score message int dealerScore = GetBlockjuckScore(dealerHand); Message dealerScoreMessage = new Message(ScoreMessagePrefix + dealerScore, messageFont, new Vector2(WindowWidth - HorizontalMessageOffset, ScoreMessageTopOffset)); messages.Add(dealerScoreMessage); // flip dealer card dealerHand[0].FlipOver(); // add winner message messages.Add(winnerMessage); // remove playing buttons menuButtons.Remove(hitButton); menuButtons.Remove(standButton); // add quiting button menuButtons.Add(new MenuButton(quitButtonSprite, new Vector2(HorizontalMenuButtonOffset, WindowHeight - TopMenuButtonOffset), GameState.Exiting)); }
/// <summary> /// Adds the dealer won message. /// </summary> private void dealerWon() { winnerMessage = new Message(dealerWonMessage, messageFont, winnerMessageLocation); }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); // create and shuffle deck deck = new Deck(Content, 0, 0); deck.Shuffle(); // first player card addCardToPlayer(); // first dealer card Card card = deck.TakeTopCard(); card.X = WindowWidth - HorizontalCardOffset; card.Y = TopCardOffset; dealerHand.Add(card); // second player card addCardToPlayer(); // second dealer card addCardToDealer(); // load sprite font, create message for player score and add to list messageFont = Content.Load<SpriteFont>(@"fonts\Arial24"); playerScoreMessage = new Message(ScoreMessagePrefix + GetBlockjuckScore(playerHand).ToString(), messageFont, new Vector2(HorizontalMessageOffset, ScoreMessageTopOffset)); messages.Add(playerScoreMessage); // load quit button sprite for later use quitButtonSprite = Content.Load<Texture2D>("quitbutton"); // create hit button and add to list hitButtonSprite = Content.Load<Texture2D>("hitbutton"); hitButton = new MenuButton(hitButtonSprite, new Vector2(HorizontalMenuButtonOffset, TopMenuButtonOffset), GameState.PlayerHitting); menuButtons.Add(hitButton); // create stand button and add to list standButtonSprite = Content.Load<Texture2D>("standbutton"); standButton = new MenuButton(standButtonSprite, new Vector2(HorizontalMenuButtonOffset, TopMenuButtonOffset + VerticalMenuButtonSpacing), GameState.WaitingForDealer); menuButtons.Add(standButton); }
private void displayQuitButtonAndDealerScore() { Message dealerScoreMessage; menuButtons.Clear(); dealerHand[0].FlipOver(); dealerScoreMessage = new Message(SCORE_MESSAGE_PREFIX + GetBlackjackScore(dealerHand).ToString(), messageFont, new Vector2(WINDOW_WIDTH - HORIZONTAL_MESSAGE_OFFSET, SCORE_MESSAGE_TOP_OFFSET)); messages.Add(dealerScoreMessage); menuButtons.Add(new MenuButton(quitButtonSprite, new Vector2(HORIZONTAL_MENU_BUTTON_OFFSET, QUIT_MENU_BUTTON_OFFSET), GameState.Exiting)); currentState = GameState.DisplayingHandResults; }
/// <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) { MouseState mouse = Mouse.GetState(); // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // update menu buttons as appropriate if (currentState == GameState.WaitingForPlayer || currentState == GameState.DisplayingHandResults) { foreach (MenuButton menuButton in menuButtons) { menuButton.Update(mouse); } } // game state-specific processing switch (currentState) { case GameState.PlayerHitting : { Card playerNextCard = deck.TakeTopCard(); playerNextCard.FlipOver(); positionNextPlayerCard(playerNextCard); playerHand.Add(playerNextCard); playerScoreMessage.Text = SCORE_MESSAGE_PREFIX + GetBlackjackScore(playerHand).ToString(); playerHit = true; currentState = GameState.WaitingForDealer; } break; case GameState.WaitingForDealer : { if (GetBlackjackScore(dealerHand) > 16) currentState = GameState.CheckingHandOver; else currentState = GameState.DealerHitting; } break; case GameState.DealerHitting: { Card nextDealerCard = deck.TakeTopCard(); nextDealerCard.FlipOver(); positionNextDealerCard(nextDealerCard); dealerHand.Add(nextDealerCard); dealerHit = true; currentState = GameState.CheckingHandOver; } break; case GameState.CheckingHandOver: { int playerScore = GetBlackjackScore(playerHand); int dealerScore = GetBlackjackScore(dealerHand); Message winnerMessage; if (playerScore > MAX_HAND_VALUE || dealerScore > MAX_HAND_VALUE) { if (playerScore > MAX_HAND_VALUE && dealerScore > MAX_HAND_VALUE) winnerMessage = new Message("Tie", messageFont, winnerMessageLocation); else if (playerScore > MAX_HAND_VALUE) winnerMessage = new Message("Busted - You lose", messageFont, winnerMessageLocation); else winnerMessage = new Message("Dealer Busted - You win", messageFont, winnerMessageLocation); messages.Add(winnerMessage); displayQuitButtonAndDealerScore(); } else { if (playerHit && dealerHit) { currentState = GameState.WaitingForPlayer; dealerHit = false; playerHit = false; } else if (playerHit) { playerHit = false; currentState = GameState.WaitingForPlayer; } else if (dealerHit) { dealerHit = false; currentState = GameState.WaitingForDealer; } else { if (playerScore == dealerScore) winnerMessage = new Message("Tie", messageFont, winnerMessageLocation); else if (playerScore > dealerScore) winnerMessage = new Message("You win", messageFont, winnerMessageLocation); else winnerMessage = new Message("You lose", messageFont, winnerMessageLocation); messages.Add(winnerMessage); displayQuitButtonAndDealerScore(); } } } break; case GameState.Exiting: { Exit(); } break; } base.Update(gameTime); }