/// <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 the deck object and shuffle Deck deck = new Deck(Content, 0, 0); deck.Shuffle(); // create the player hands and fully deal the deck into the hands // WINDOW_HEIGHT / 6 and WINDOW_HEIGHT * 5/6 handPlayer1 = new WarHand(WINDOW_WIDTH / 2, WINDOW_HEIGHT - 500); handPlayer2 = new WarHand(WINDOW_WIDTH / 2, WINDOW_HEIGHT - 100); //while (!deck.Empty) for (int i = 0; i < 2; i++) { handPlayer1.AddCard(deck.TakeTopCard()); handPlayer2.AddCard(deck.TakeTopCard()); } // create the player battle piles // WINDOW_HEIGHT / 3 and WINDOW_HEIGHT * 2/3 pilePlayer1 = new WarBattlePile(WINDOW_WIDTH / 2, WINDOW_HEIGHT - 380); pilePlayer2 = new WarBattlePile(WINDOW_WIDTH / 2, WINDOW_HEIGHT - 220); // create the player winner messages // WINDOW_WIDHT * 3 / 4, WINDOW_HEIGHT / 6 AND WINDOW_HEIGHT * 5 / 6 winnerPlayer1 = new WinnerMessage(Content, WINDOW_WIDTH - 200, WINDOW_HEIGHT - 500); winnerPlayer2 = new WinnerMessage(Content, WINDOW_WIDTH - 200, WINDOW_HEIGHT - 100); // create the menu buttons // WINDOW_WIDTH / 5, WINDOW_HEIGHT / 4 flipCards = new MenuButton(Content, "flipbutton", WINDOW_WIDTH - 650, WINDOW_HEIGHT - 450, GameState.Flip); // WINDOW_WIDTH / 5, WINDOW_HEIGHT * 3 / 4 quitGame = new MenuButton(Content, "quitbutton", WINDOW_WIDTH - 650, WINDOW_HEIGHT - 150, GameState.Quit); // WINDOW_WIDTH / 5, WINDOW_HEIGHT / 2 collectWinnings = new MenuButton(Content, "collectwinningsbutton", WINDOW_WIDTH - 650, WINDOW_HEIGHT - 300, GameState.Play); // GameState.CollectWinnings collectWinnings.Visible = false; }
/// <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 the deck object and shuffle deck = new Deck(Content, 100, 100); deck.Shuffle(); for (int i = 1; i < 45; i++) { deck.TakeTopCard(); } // create the player hands and fully deal the deck into the hands player1 = new WarHand((int)(graphics.PreferredBackBufferWidth / 2), (int)(graphics.PreferredBackBufferHeight * 0.2)); player2 = new WarHand((int)(graphics.PreferredBackBufferWidth / 2), (int)(graphics.PreferredBackBufferHeight * 0.8)); while (!deck.Empty) { player1.AddCard(deck.TakeTopCard()); player2.AddCard(deck.TakeTopCard()); } // create the player battle piles battlePile1 = new WarBattlePile((int)(graphics.PreferredBackBufferWidth / 2), (int)(graphics.PreferredBackBufferHeight * 0.4)); battlePile2 = new WarBattlePile((int)(graphics.PreferredBackBufferWidth / 2), (int)(graphics.PreferredBackBufferHeight * 0.6)); // create the player winner messages playerOneWin = new WinnerMessage(Content, (int)(graphics.PreferredBackBufferWidth * 0.7), (int)(graphics.PreferredBackBufferHeight * 0.2)); playerTwoWin = new WinnerMessage(Content, (int)(graphics.PreferredBackBufferWidth * 0.7), (int)(graphics.PreferredBackBufferHeight * 0.8)); // create the menu buttons flipButton = new MenuButton(Content, "flipbutton", (int)(graphics.PreferredBackBufferWidth * 0.2), (int)(graphics.PreferredBackBufferHeight * 0.3), GameState.Flip); collectButton = new MenuButton(Content, "collectwinningsbutton", (int)(graphics.PreferredBackBufferWidth * 0.2), (int)(graphics.PreferredBackBufferHeight * 0.5), GameState.CollectWinnings); quitButton = new MenuButton(Content, "quitbutton", (int)(graphics.PreferredBackBufferWidth * 0.2), (int)(graphics.PreferredBackBufferHeight * 0.7), GameState.Quit); flipButton.Visible = false; collectButton.Visible = false; quitButton.Visible = false; }
/// <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 the deck object and shuffle deck = new Deck(Content, 0, 0); deck.Shuffle(); // create the player hands and fully deal the deck into the hands playerOneHand = new WarHand(graphics.PreferredBackBufferWidth / 2, graphics.PreferredBackBufferHeight / 6); playerTwoHand = new WarHand(graphics.PreferredBackBufferWidth / 2, graphics.PreferredBackBufferHeight / 6 * 5); while (!deck.Empty) { playerOneHand.AddCard(deck.TakeTopCard()); playerTwoHand.AddCard(deck.TakeTopCard()); } // create the player battle piles playerOnePile = new WarBattlePile(graphics.PreferredBackBufferWidth / 2, graphics.PreferredBackBufferHeight / 3); playerTwoPile = new WarBattlePile(graphics.PreferredBackBufferWidth / 2, graphics.PreferredBackBufferHeight / 3 * 2); // create the player winner messages playerOneMessage = new WinnerMessage(Content, graphics.PreferredBackBufferWidth / 4 * 3, graphics.PreferredBackBufferHeight / 3); playerTwoMessage = new WinnerMessage(Content, graphics.PreferredBackBufferWidth / 4 * 3, graphics.PreferredBackBufferHeight / 3 * 2); // create the menu buttons flipButton = new MenuButton(Content, "flipbutton", 200, 150, GameState.Flip); quitButton = new MenuButton(Content, "quitbutton", 200 ,450 , GameState.Quit); collectButton = new MenuButton(Content, "collectwinningsbutton", 200, 150, GameState.CollectWinnings); // load font spriteFont = Content.Load<SpriteFont>("SpriteFont1"); }
/// <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 currentMouseState = Mouse.GetState(); // update menu buttons as appropriate foreach (MenuButton thisButton in menuButtons) { if (currentState == GameState.WaitingForPlayer || currentState == GameState.DisplayingHandResults) { if (currentState == GameState.WaitingForPlayer) { playerHit = false; } thisButton.Update(currentMouseState); } } if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } int newPlayerScore = GetBlockjuckScore(playerHand); int newDealerScore = GetBlockjuckScore(dealerHand); // game state-specific processing if ((newDealerScore < MaxHandValue) && (newPlayerScore < MaxHandValue)) { if (currentState == GameState.PlayerHitting) { Card thisCard = deck.TakeTopCard(); int cardPosition = playerHand.Count; thisCard.X = HorizontalCardOffset; thisCard.Y = TopCardOffset + (VerticalCardSpacing * cardPosition); if (!thisCard.FaceUp) { thisCard.FlipOver(); } playerHand.Add(thisCard); newPlayerScore = GetBlockjuckScore(playerHand); playerScoreMessage.Text = ScoreMessagePrefix + newPlayerScore.ToString(); playerHit = true; currentState = GameState.WaitingForDealer; } if (currentState == GameState.WaitingForDealer) { if (newPlayerScore > newDealerScore) { currentState = GameState.DealerHitting; } else { dealerHit = false; currentState = GameState.CheckingHandOver; } } if (currentState == GameState.DealerHitting) { Card thisCard = deck.TakeTopCard(); int cardPosition = dealerHand.Count; thisCard.X = HorizontalCardOffset * 4; thisCard.Y = TopCardOffset + (VerticalCardSpacing * cardPosition); dealerHand.Add(thisCard); newDealerScore = GetBlockjuckScore(dealerHand); dealerHit = true; currentState = GameState.CheckingHandOver; } if (currentState == GameState.CheckingHandOver) { if (!playerHit && !dealerHit) { dealerScoreMessage = new Message(ScoreMessagePrefix + GetBlockjuckScore(dealerHand).ToString(), messageFont, new Vector2(HorizontalMessageOffset * 4, ScoreMessageTopOffset)); messages.Add(dealerScoreMessage); if (newPlayerScore > newDealerScore) { currentState = GameState.DisplayingHandResults; menuButtons.Clear(); winnerMessage = new Message("You Won!", messageFont, winnerMessageLocation); messages.Add(winnerMessage); MenuButton quitButton = new MenuButton(quitButtonSprite, new Vector2(WindowWidth / 2, QuitMenuButtonOffset), GameState.Exiting); menuButtons.Add(quitButton); dealerScoreMessage = new Message(ScoreMessagePrefix + GetBlockjuckScore(dealerHand).ToString(), messageFont, new Vector2(HorizontalMessageOffset * 4, ScoreMessageTopOffset)); messages.Add(dealerScoreMessage); } else if (newPlayerScore == newDealerScore) { currentState = GameState.DisplayingHandResults; menuButtons.Clear(); winnerMessage = new Message("Tie", messageFont, winnerMessageLocation); messages.Add(winnerMessage); MenuButton quitButton = new MenuButton(quitButtonSprite, new Vector2(WindowWidth / 2, QuitMenuButtonOffset), GameState.Exiting); menuButtons.Add(quitButton); dealerScoreMessage = new Message(ScoreMessagePrefix + GetBlockjuckScore(dealerHand).ToString(), messageFont, new Vector2(HorizontalMessageOffset * 4, ScoreMessageTopOffset)); messages.Add(dealerScoreMessage); } else { currentState = GameState.DisplayingHandResults; menuButtons.Clear(); winnerMessage = new Message("Dealer Won!", messageFont, winnerMessageLocation); messages.Add(winnerMessage); MenuButton quitButton = new MenuButton(quitButtonSprite, new Vector2(WindowWidth / 2, QuitMenuButtonOffset), GameState.Exiting); menuButtons.Add(quitButton); dealerScoreMessage = new Message(ScoreMessagePrefix + GetBlockjuckScore(dealerHand).ToString(), messageFont, new Vector2(HorizontalMessageOffset * 4, ScoreMessageTopOffset)); messages.Add(dealerScoreMessage); } currentState = GameState.DisplayingHandResults; } else { if (newPlayerScore >= MaxHandValue && newDealerScore >= MaxHandValue) { currentState = GameState.DisplayingHandResults; menuButtons.Clear(); winnerMessage = new Message("Tie", messageFont, winnerMessageLocation); messages.Add(winnerMessage); MenuButton quitButton = new MenuButton(quitButtonSprite, new Vector2(WindowWidth / 2, QuitMenuButtonOffset), GameState.Exiting); menuButtons.Add(quitButton); dealerScoreMessage = new Message(ScoreMessagePrefix + GetBlockjuckScore(dealerHand).ToString(), messageFont, new Vector2(HorizontalMessageOffset * 4, ScoreMessageTopOffset)); messages.Add(dealerScoreMessage); } else if (newPlayerScore >= MaxHandValue) { currentState = GameState.DisplayingHandResults; menuButtons.Clear(); winnerMessage = new Message("Dealer Won!", messageFont, winnerMessageLocation); messages.Add(winnerMessage); MenuButton quitButton = new MenuButton(quitButtonSprite, new Vector2(WindowWidth / 2, QuitMenuButtonOffset), GameState.Exiting); menuButtons.Add(quitButton); dealerScoreMessage = new Message(ScoreMessagePrefix + GetBlockjuckScore(dealerHand).ToString(), messageFont, new Vector2(HorizontalMessageOffset * 4, ScoreMessageTopOffset)); messages.Add(dealerScoreMessage); } else if (newDealerScore >= MaxHandValue) { currentState = GameState.DisplayingHandResults; menuButtons.Clear(); winnerMessage = new Message("You Won!", messageFont, winnerMessageLocation); messages.Add(winnerMessage); MenuButton quitButton = new MenuButton(quitButtonSprite, new Vector2(WindowWidth / 2, QuitMenuButtonOffset), GameState.Exiting); menuButtons.Add(quitButton); dealerScoreMessage = new Message(ScoreMessagePrefix + GetBlockjuckScore(dealerHand).ToString(), messageFont, new Vector2(HorizontalMessageOffset * 4, ScoreMessageTopOffset)); messages.Add(dealerScoreMessage); } else { currentState = GameState.WaitingForPlayer; } } foreach (Card thisCard in playerHand) { if (!thisCard.FaceUp) { thisCard.FlipOver(); } } foreach (Card thisCard in dealerHand) { if (!thisCard.FaceUp) { thisCard.FlipOver(); } } } } if (currentState == GameState.DisplayingHandResults) { } if (currentState == GameState.Exiting) { playerHit = true; dealerHit = true; deck = new Deck(Content, 0, 0); playerHand.Clear(); dealerHand.Clear(); newPlayerScore = GetBlockjuckScore(playerHand); newDealerScore = GetBlockjuckScore(dealerHand); messages.Clear(); menuButtons.Clear(); Vector2 hitCenter = new Vector2(HorizontalCardOffset * 2 + 75, TopCardOffset); MenuButton hitButton = new MenuButton(hitButtonSprite, hitCenter, GameState.PlayerHitting); menuButtons.Add(hitButton); Vector2 standCenter = new Vector2(HorizontalCardOffset * 2 + 75, TopCardOffset + VerticalCardSpacing); MenuButton standButton = new MenuButton(standButtonSprite, standCenter, GameState.WaitingForDealer); menuButtons.Add(standButton); currentState = GameState.WaitingForPlayer; deck.Shuffle(); // first player card playerHand.Add(deck.TakeTopCard()); playerHand[0].X = HorizontalCardOffset; playerHand[0].Y = TopCardOffset; if (!playerHand[0].FaceUp) { playerHand[0].FlipOver(); } // first dealer card dealerHand.Add(deck.TakeTopCard()); dealerHand[0].X = HorizontalCardOffset * 4; dealerHand[0].Y = TopCardOffset; if (dealerHand[0].FaceUp) { dealerHand[0].FlipOver(); } // second player card playerHand.Add(deck.TakeTopCard()); playerHand[1].X = HorizontalCardOffset; playerHand[1].Y = TopCardOffset + VerticalCardSpacing; if (!playerHand[1].FaceUp) { playerHand[1].FlipOver(); } // second dealer card dealerHand.Add(deck.TakeTopCard()); dealerHand[1].X = HorizontalCardOffset * 4; dealerHand[1].Y = TopCardOffset + VerticalCardSpacing; if (!dealerHand[1].FaceUp) { dealerHand[1].FlipOver(); } playerScoreMessage = new Message(ScoreMessagePrefix + GetBlockjuckScore(playerHand).ToString(), messageFont, new Vector2(HorizontalMessageOffset, ScoreMessageTopOffset)); messages.Add(playerScoreMessage); } base.Update(gameTime); }
/// <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);//Placement of deck didn't matter since deck is not going to be drawn. deck.Shuffle(); // first player card Card firstPlayerCard = deck.TakeTopCard(); firstPlayerCard.X = HorizontalCardOffset; firstPlayerCard.Y = TopCardOffset; firstPlayerCard.FlipOver(); playerHand.Add(firstPlayerCard); // first dealer card Card firstDealerCard = deck.TakeTopCard(); firstDealerCard.X = HorizontalCardOffset + HorizontalMenuButtonOffset + TopCardOffset;//Just to make the layout seem symmetrical without magic number. firstDealerCard.Y = TopCardOffset; dealerHand.Add(firstDealerCard); // second player card Card secondPlayerCard = deck.TakeTopCard(); secondPlayerCard.X = HorizontalCardOffset; secondPlayerCard.Y = TopCardOffset + VerticalCardSpacing; secondPlayerCard.FlipOver(); playerHand.Add(secondPlayerCard); // second dealer card Card secondDealerCard = deck.TakeTopCard(); secondDealerCard.X = HorizontalCardOffset + HorizontalMenuButtonOffset + TopCardOffset;//Just to make the layout seem symmetrical without magic number. secondDealerCard.Y = TopCardOffset + VerticalCardSpacing; secondDealerCard.FlipOver(); dealerHand.Add(secondDealerCard); // 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>(@"graphics\quitbutton"); backButtonSprite = Content.Load <Texture2D>(@"graphics\Back"); // create hit button and add to list hitButtonSprite = Content.Load <Texture2D>(@"graphics\hitbutton"); Vector2 hitPlacement = new Vector2(HorizontalMenuButtonOffset, TopMenuButtonOffset); MenuButton hitButton = new MenuButton(hitButtonSprite, hitPlacement, GameState.PlayerHitting); menuButtons.Add(hitButton); // create stand button and add to list standButtonSprite = Content.Load <Texture2D>(@"graphics\standbutton"); Vector2 standPlacement = new Vector2(HorizontalMenuButtonOffset, VerticalMenuButtonSpacing + TopMenuButtonOffset); MenuButton standButton = new MenuButton(standButtonSprite, standPlacement, GameState.WaitingForDealer); menuButtons.Add(standButton); }
/// <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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } // update menu buttons as appropriate int offsetPlayerCounter = playerHand.Count; //Used as a multiplier for vertical card offset. Already started off with 2 cards. int offsetDealerCounter = dealerHand.Count; //Used as a multiplier for vertical card offset. Already started off with 2 cards. MouseState mouse = Mouse.GetState(); foreach (MenuButton buttons in menuButtons) { if (currentState == GameState.WaitingForPlayer || currentState == GameState.DisplayingHandResults) { buttons.Update(mouse); } } // game state-specific processing switch (currentState) { case GameState.PlayerHitting: playerHit = true; //Used for later. Card newPlayerCard = deck.TakeTopCard(); newPlayerCard.X = HorizontalCardOffset; newPlayerCard.Y = TopCardOffset + (offsetPlayerCounter * VerticalCardSpacing); newPlayerCard.FlipOver(); playerHand.Add(newPlayerCard); playerScoreMessage.Text = ScoreMessagePrefix + GetBlockjuckScore(playerHand).ToString(); //Changes to new score. ChangeState(GameState.WaitingForDealer); //Changes to Dealer's state. break; case GameState.WaitingForDealer: if (GetBlockjuckScore(dealerHand) <= 16) { ChangeState(GameState.DealerHitting); } else { ChangeState(GameState.CheckingHandOver); } break; case GameState.DealerHitting: dealerHit = true; //Used for later. Card newDealerCard = deck.TakeTopCard(); newDealerCard.X = HorizontalCardOffset + HorizontalMenuButtonOffset + TopCardOffset; newDealerCard.Y = TopCardOffset + (offsetDealerCounter * VerticalCardSpacing); newDealerCard.FlipOver(); dealerHand.Add(newDealerCard); ChangeState(GameState.CheckingHandOver); break; case GameState.CheckingHandOver: int playerScore = GetBlockjuckScore(playerHand); int dealerScore = GetBlockjuckScore(dealerHand); if (playerScore > MaxHandValue || dealerScore > MaxHandValue || (dealerHit == false && playerHit == false)) { dealerHand[0].FlipOver(); //First dealer card is flipped. dealerScoreMessage = new Message(ScoreMessagePrefix + dealerScore.ToString(), messageFont, new Vector2(HorizontalMessageOffset + HorizontalMenuButtonOffset + TopCardOffset, ScoreMessageTopOffset)); messages.Add(dealerScoreMessage); menuButtons.Clear(); //Clears all previous menu buttons. if (playerScore > MaxHandValue && dealerScore <= MaxHandValue) { winnerMessage = new Message("Dealer Won!", messageFont, winnerMessageLocation); messages.Add(winnerMessage); } else if (playerScore == MaxHandValue) { winnerMessage = new Message("Player Won!", messageFont, winnerMessageLocation); messages.Add(winnerMessage); } else if (dealerScore == MaxHandValue) { winnerMessage = new Message("Dealer Won!", messageFont, winnerMessageLocation); messages.Add(winnerMessage); } else if (dealerScore > MaxHandValue && playerScore <= MaxHandValue) { winnerMessage = new Message("Player Won!", messageFont, winnerMessageLocation); messages.Add(winnerMessage); } else { winnerMessage = new Message("It's a Draw!", messageFont, winnerMessageLocation); messages.Add(winnerMessage); } MenuButton quitButton = new MenuButton(quitButtonSprite, new Vector2(HorizontalMenuButtonOffset, QuitMenuButtonOffset), GameState.Exiting); menuButtons.Add(quitButton); ChangeState(GameState.DisplayingHandResults); } else { playerHit = false; dealerHit = false; ChangeState(GameState.WaitingForPlayer); } break; case GameState.Exiting: Exit(); 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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } MouseState mouse = Mouse.GetState(); // update menu buttons as appropriate foreach (MenuButton button in menuButtons) { if (currentState == GameState.WaitingForPlayer || currentState == GameState.DisplayingHandResults) { button.Update(mouse); } } // game state-specific processing switch (currentState) { //player hits, drawing one card case (GameState.PlayerHitting): { playerHit = true; playerHand.Add(deck.TakeTopCard()); playerHand[playerHand.Count - 1].X = HorizontalCardOffset; playerHand[playerHand.Count - 1].Y = TopCardOffset + VerticalCardSpacing * (playerHand.Count - 1); playerHand[playerHand.Count - 1].FlipOver(); //update score messages.Remove(playerScoreMessage); playerScoreMessage = new Message( ScoreMessagePrefix + GetBlockjuckScore(playerHand).ToString(), messageFont, new Vector2(HorizontalMessageOffset, ScoreMessageTopOffset)); messages.Add(playerScoreMessage); currentState = GameState.WaitingForDealer; break; } // determines if dealer should hit case (GameState.WaitingForDealer): { if (GetBlockjuckScore(dealerHand) < 17) { currentState = GameState.DealerHitting; break; } else { currentState = GameState.CheckingHandOver; break; } } // dealer hits case (GameState.DealerHitting): { dealerHit = true; dealerHand.Add(deck.TakeTopCard()); dealerHand[dealerHand.Count - 1].X = WindowWidth - HorizontalCardOffset; dealerHand[dealerHand.Count - 1].Y = TopCardOffset + VerticalCardSpacing * (dealerHand.Count - 1); dealerHand[dealerHand.Count - 1].FlipOver(); currentState = GameState.CheckingHandOver; break; } // Did either bust? or did both stand? case (GameState.CheckingHandOver): { if (GetBlockjuckScore(playerHand) > MaxHandValue || GetBlockjuckScore(dealerHand) > MaxHandValue || (!playerHit && !dealerHit)) { //draw if ((GetBlockjuckScore(playerHand) > MaxHandValue && GetBlockjuckScore(dealerHand) > MaxHandValue) || (GetBlockjuckScore(playerHand) == GetBlockjuckScore(dealerHand))) { winnerMessage = new Message("Draw!", messageFont, winnerMessageLocation); } //dealer wins else if (GetBlockjuckScore(playerHand) > MaxHandValue || (GetBlockjuckScore(playerHand) < GetBlockjuckScore(dealerHand) && GetBlockjuckScore(dealerHand) <= MaxHandValue)) { winnerMessage = new Message("Dealer Wins!", messageFont, winnerMessageLocation); } //player wins else if ((GetBlockjuckScore(dealerHand) > MaxHandValue) || ((GetBlockjuckScore(playerHand) > GetBlockjuckScore(dealerHand)) && (GetBlockjuckScore(playerHand) <= MaxHandValue))) { winnerMessage = new Message("You Win!", messageFont, winnerMessageLocation); } messages.Add(winnerMessage); // displays dealers hand and score dealerHand[0].FlipOver(); dealerScoreMessage = new Message(ScoreMessagePrefix + GetBlockjuckScore(dealerHand).ToString(), messageFont, new Vector2(WindowWidth - HorizontalMessageOffset, ScoreMessageTopOffset)); messages.Add(dealerScoreMessage); // update buttons to show quit button menuButtons.Remove(hitting); menuButtons.Remove(standing); menuButtons.Add(quit = new MenuButton(quitButtonSprite, new Vector2(WindowWidth / 2, TopCardOffset), GameState.Exiting)); currentState = GameState.DisplayingHandResults; break; } // no one busted continue else { playerHit = false; dealerHit = false; currentState = GameState.WaitingForPlayer; break; } } // quitting case (GameState.Exiting): { Exit(); break; } } base.Update(gameTime); }
/// <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 the deck object and shuffle Deck deck = new Deck(Content, 100, 100); deck.Shuffle(); // create the player hands and fully deal the deck into the hands playerOneHand = new WarHand(WINDOW_WIDTH / 2, 100); playerTwoHand = new WarHand(WINDOW_WIDTH / 2, WINDOW_HEIGHT - 100); for (int i = 0; i < 2; i++) { playerOneHand.AddCard(deck.TakeTopCard()); playerTwoHand.AddCard(deck.TakeTopCard()); } // create the player battle piles playerOneBP = new WarBattlePile(WINDOW_WIDTH / 2, 200); playerTwoBP = new WarBattlePile(WINDOW_WIDTH / 2, WINDOW_HEIGHT - 200); // create the player winner messages playerOneWM = new WinnerMessage(Content, WINDOW_WIDTH / 2 + 150, 100); playerTwoWM = new WinnerMessage(Content, WINDOW_WIDTH / 2 + 150, WINDOW_HEIGHT - 100); // create the menu buttons flipButton = new MenuButton(Content, "flipbutton", 150, 150, GameState.Flip); quitButton = new MenuButton(Content, "quitbutton", 150, 450, GameState.Quit); collectWinnings = new MenuButton(Content, "collectwinningsbutton", 150, 300, GameState.CollectWinnings); }
/// <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 Card firstPlayerCard = deck.TakeTopCard(); firstPlayerCard = new Card(Content, firstPlayerCard.Rank, firstPlayerCard.Suit, HorizontalCardOffset, TopCardOffset); playerHand.Add(firstPlayerCard); firstPlayerCard.FlipOver(); // first dealer card Card firstDealerCard = deck.TakeTopCard(); firstDealerCard = new Card(Content, firstDealerCard.Rank, firstDealerCard.Suit, WindowWidth - HorizontalCardOffset, TopCardOffset); dealerHand.Add(firstDealerCard); // second player card Card secondPlayerCard = deck.TakeTopCard(); secondPlayerCard = new Card(Content, secondPlayerCard.Rank, secondPlayerCard.Suit, HorizontalCardOffset, TopCardOffset + VerticalCardSpacing); playerHand.Add(secondPlayerCard); secondPlayerCard.FlipOver(); // second dealer card Card secondDealerCard = deck.TakeTopCard(); secondDealerCard = new Card(Content, secondDealerCard.Rank, secondDealerCard.Suit, WindowWidth - HorizontalCardOffset, TopCardOffset + VerticalCardSpacing); dealerHand.Add(secondDealerCard); secondDealerCard.FlipOver(); // 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>(@"graphics/quitbutton"); // create hit button and add to list hitButtonSprite = Content.Load <Texture2D>(@"graphics/hitbutton"); Vector2 hitButtonCenter = new Vector2(WindowWidth / 2, TopCardOffset); MenuButton hitButton = new MenuButton(hitButtonSprite, hitButtonCenter, GameState.PlayerHitting); menuButtons.Add(hitButton); // create stand button and add to list standButtonSprite = Content.Load <Texture2D>(@"graphics/standbutton"); Vector2 standButtonCenter = new Vector2(WindowWidth / 2, TopCardOffset + VerticalCardSpacing); MenuButton standButton = new MenuButton(standButtonSprite, standButtonCenter, GameState.WaitingForDealer); menuButtons.Add(standButton); }
/// <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 deck = new Deck(Content, 0, 0); deck.Shuffle(); // first player card playerHand.Add(deck.TakeTopCard()); playerHand[0].FlipOver(); playerHand[0].X = HorizontalCardOffset; playerHand[0].Y = TopCardOffset; // first dealer card dealerHand.Add(deck.TakeTopCard()); dealerHand[0].X = WindowWidth - HorizontalCardOffset; dealerHand[0].Y = TopCardOffset; // second player card playerHand.Add(deck.TakeTopCard()); playerHand[1].FlipOver(); playerHand[1].X = HorizontalCardOffset; playerHand[1].Y = TopCardOffset + VerticalCardSpacing; // second dealer card dealerHand.Add(deck.TakeTopCard()); dealerHand[1].FlipOver(); dealerHand[1].X = WindowWidth - HorizontalCardOffset; dealerHand[1].Y = TopCardOffset + VerticalCardSpacing; // 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); dealerScoreMessage = new Message(ScoreMessagePrefix + GetBlockjuckScore(dealerHand).ToString(), messageFont, new Vector2(WindowWidth - HorizontalCardOffset, ScoreMessageTopOffset)); //create finals game messages winnerMessage = new Message(" won!", messageFont, winnerMessageLocation); drawMessage = new Message("Draw!", messageFont, winnerMessageLocation); // load quit button sprite for later use dn create exit button quitButtonSprite = Content.Load <Texture2D>(@"graphics\quitbutton"); quitButton = new MenuButton(quitButtonSprite, quitButtonPlacement, GameState.Exiting); // create hit button and add to list hitButtonSprite = Content.Load <Texture2D>(@"graphics\hitbutton"); hitButton = new MenuButton(hitButtonSprite, hitButtonPlacement, GameState.PlayerHitting); menuButtons.Add(hitButton); // create stand button and add to list standButtonSprite = Content.Load <Texture2D>(@"graphics\standbutton"); standButton = new MenuButton(standButtonSprite, standButtonPlacement, GameState.WaitingForDealer); menuButtons.Add(standButton); }
/// <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 Card card = deck.TakeTopCard(); card.FlipOver(); card.X = HORIZONTAL_CARD_OFFSET; card.Y = TOP_CARD_OFFSET + (VERTICAL_CARD_SPACING * playerHand.Count); playerHand.Add(card); // first dealer card card = deck.TakeTopCard(); card.X = WINDOW_WIDTH - HORIZONTAL_CARD_OFFSET; card.Y = TOP_CARD_OFFSET + (VERTICAL_CARD_SPACING * dealerHand.Count); dealerHand.Add(card); // second player card card = deck.TakeTopCard(); card.FlipOver(); card.X = HORIZONTAL_CARD_OFFSET; card.Y = TOP_CARD_OFFSET + (VERTICAL_CARD_SPACING * playerHand.Count); playerHand.Add(card); // second dealer card card = deck.TakeTopCard(); card.FlipOver(); card.X = WINDOW_WIDTH - HORIZONTAL_CARD_OFFSET; card.Y = TOP_CARD_OFFSET + (VERTICAL_CARD_SPACING * dealerHand.Count); dealerHand.Add(card); // 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 MenuButton hitButton = new MenuButton( Content.Load <Texture2D>("hitbutton"), new Vector2(HORIZONTAL_MENU_BUTTON_OFFSET, TOP_MENU_BUTTON_OFFSET), GameState.PlayerHitting); menuButtons.Add(hitButton); // create stand button and add to list MenuButton standButton = new MenuButton( Content.Load <Texture2D>("standbutton"), new Vector2(HORIZONTAL_MENU_BUTTON_OFFSET, TOP_MENU_BUTTON_OFFSET + VERTICAL_MENU_BUTTON_SPACING), GameState.WaitingForDealer); menuButtons.Add(standButton); }
/// <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(); } MouseState mouse = Mouse.GetState(); // update menu buttons as appropriate if (currentState == GameState.WaitingForPlayer) { foreach (MenuButton button in menuButtons) { button.Update(mouse); } } else if (currentState == GameState.DisplayingHandResults) { foreach (MenuButton button in menuButtons) { button.Update(mouse); } } // game state-specific processing switch (currentState) { case GameState.PlayerHitting: Card cardPlayer = deck.TakeTopCard(); cardPlayer.FlipOver(); cardPlayer.X = HORIZONTAL_CARD_OFFSET; cardPlayer.Y = TOP_CARD_OFFSET + (VERTICAL_CARD_SPACING * playerHand.Count); playerHand.Add(cardPlayer); playerScoreMessage.Text = SCORE_MESSAGE_PREFIX + GetBlackjackScore(playerHand).ToString(); playerHit = true; Game1.ChangeState(GameState.WaitingForDealer); break; case GameState.WaitingForDealer: if (GetBlackjackScore(dealerHand) <= 16) { Game1.ChangeState(GameState.DealerHitting); } else { Game1.ChangeState(GameState.CheckingHandOver); } break; case GameState.DealerHitting: Card cardDealer = deck.TakeTopCard(); cardDealer.FlipOver(); cardDealer.X = WINDOW_WIDTH - HORIZONTAL_CARD_OFFSET; cardDealer.Y = TOP_CARD_OFFSET + (VERTICAL_CARD_SPACING * dealerHand.Count); dealerHand.Add(cardDealer); dealerHit = true; Game1.ChangeState(GameState.CheckingHandOver); break; case GameState.CheckingHandOver: int playerScore = GetBlackjackScore(playerHand); int dealerScore = GetBlackjackScore(dealerHand); if (playerScore > MAX_HAND_VALUE || dealerScore > MAX_HAND_VALUE || (!playerHit && !dealerHit)) { String resultText; //Player and dealer busted if (playerScore > MAX_HAND_VALUE && dealerScore > MAX_HAND_VALUE) { resultText = "It's a tie"; } //Dealer busted else if (dealerScore > MAX_HAND_VALUE) { resultText = "Player won"; } //Player busted else if (playerScore > MAX_HAND_VALUE) { resultText = "Dealer won"; } // No one was busted, so we check the scores else if (playerScore > dealerScore) { resultText = "Player won"; } else if (playerScore < dealerScore) { resultText = "Dealer won"; } else //playerScore == dealerScore { resultText = "It's a tie"; } messageFont = Content.Load <SpriteFont>("Arial24"); Message resultMessage = new Message(resultText, messageFont, winnerMessageLocation); messages.Add(resultMessage); //Flip over the dealer first card dealerHand[0].FlipOver(); //Create message for 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 hit and stand buttons menuButtons.Clear(); //Add quit button MenuButton hitButton = new MenuButton( quitButtonSprite, new Vector2(HORIZONTAL_MENU_BUTTON_OFFSET, QUIT_MENU_BUTTON_OFFSET), GameState.Exiting); menuButtons.Add(hitButton); //change game state Game1.ChangeState(GameState.DisplayingHandResults); } else { playerHit = false; dealerHit = false; Game1.ChangeState(GameState.WaitingForPlayer); } break; case GameState.Exiting: this.Exit(); break; } base.Update(gameTime); }
/// <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); }
/// <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 Card firstPlayerCard = deck.TakeTopCard(); firstPlayerCard.Y = TOP_CARD_OFFSET; firstPlayerCard.X = HORIZONTAL_CARD_OFFSET; firstPlayerCard.FlipOver(); playerHand.Add(firstPlayerCard); // first dealer card Card firstDealerCard = deck.TakeTopCard(); //firstDealerCard.FlipOver(); firstDealerCard.Y = TOP_CARD_OFFSET; firstDealerCard.X = WINDOW_WIDTH - HORIZONTAL_CARD_OFFSET; dealerHand.Add(firstDealerCard); // second player card Card secondPlayerCard = deck.TakeTopCard(); positionNextPlayerCard(secondPlayerCard); secondPlayerCard.FlipOver(); playerHand.Add(secondPlayerCard); // second dealer card Card secondDealerCard = deck.TakeTopCard(); positionNextDealerCard(secondDealerCard); secondDealerCard.FlipOver(); dealerHand.Add(secondDealerCard); // 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 MenuButton hitButton = new MenuButton(Content.Load<Texture2D>("hitbutton"), new Vector2(HORIZONTAL_MENU_BUTTON_OFFSET, TOP_MENU_BUTTON_OFFSET), GameState.PlayerHitting); menuButtons.Add(hitButton); // create stand button and add to list MenuButton standButton = new MenuButton(Content.Load<Texture2D>("standbutton"), new Vector2(HORIZONTAL_MENU_BUTTON_OFFSET, hitButton.DrawRectangle.Bottom + VERTICAL_MENU_BUTTON_SPACING), GameState.WaitingForDealer); menuButtons.Add(standButton); }
/// <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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } // update menu buttons as appropriate if (currentState == GameState.WaitingForPlayer || currentState == GameState.DisplayingHandResults) { foreach (MenuButton button in menuButtons) { MouseState mouse = Mouse.GetState(); button.Update(mouse); } } Card card; switch (currentState) { case GameState.PlayerHitting: card = deck.TakeTopCard(); playerHand.Add(card); card.FlipOver(); card.X = HorizontalCardOffset; card.Y = (playerHand.Count - 1) * VerticalCardSpacing + TopCardOffset; currentState = GameState.WaitingForDealer; playerHit = true; messages.Remove(playerScoreMessage); playerScoreMessage = new Message(ScoreMessagePrefix + GetBlockjuckScore(playerHand).ToString(), messageFont, new Vector2(HorizontalMessageOffset, ScoreMessageTopOffset)); messages.Add(playerScoreMessage); break; case GameState.WaitingForDealer: // Dealer have less than 16 points and player is not busted if ((GetBlockjuckScore(dealerHand) <= 16) && (GetBlockjuckScore(playerHand) <= MaxHandValue)) { currentState = GameState.DealerHitting; } else { currentState = GameState.CheckingHandOver; } break; case GameState.DealerHitting: card = deck.TakeTopCard(); dealerHand.Add(card); card.FlipOver(); card.X = WindowWidth - HorizontalCardOffset; card.Y = (dealerHand.Count - 1) * VerticalCardSpacing + TopCardOffset; currentState = GameState.CheckingHandOver; dealerHit = true; break; case GameState.CheckingHandOver: // If some of us are busted or we both stand if ((GetBlockjuckScore(dealerHand) > MaxHandValue || GetBlockjuckScore(playerHand) > MaxHandValue) || (!dealerHit && !playerHit)) { // Player stand and was not busted and have more points than the dealer if (!playerHit && (GetBlockjuckScore(playerHand) <= MaxHandValue) && (GetBlockjuckScore(playerHand) > GetBlockjuckScore(dealerHand))) { currentState = GameState.DealerHitting; break; } // If we both are busted or we both have the same points nobody wins else if ((GetBlockjuckScore(dealerHand) > MaxHandValue && GetBlockjuckScore(playerHand) > MaxHandValue) || (GetBlockjuckScore(dealerHand) == GetBlockjuckScore(playerHand))) { winnerMessage = new Message("Nobody won!", messageFont, winnerMessageLocation); } // Else if dealer get busted or the dealer dont get busted and player have more points, he wins else if ((GetBlockjuckScore(dealerHand) > MaxHandValue) || ((GetBlockjuckScore(dealerHand) <= MaxHandValue) && (GetBlockjuckScore(playerHand) <= MaxHandValue) && (GetBlockjuckScore(playerHand) > GetBlockjuckScore(dealerHand)))) { winnerMessage = new Message("Player won!", messageFont, winnerMessageLocation); } // Else player get busted else { winnerMessage = new Message("Dealer won!", messageFont, winnerMessageLocation); } dealerHand[0].FlipOver(); messages.Remove(dealerScoreMessage); dealerScoreMessage = new Message(ScoreMessagePrefix + GetBlockjuckScore(dealerHand).ToString(), messageFont, new Vector2(4 * HorizontalMessageOffset, ScoreMessageTopOffset)); messages.Add(dealerScoreMessage); messages.Add(winnerMessage); currentState = GameState.DisplayingHandResults; // remove and add the appropiate buttons menuButtons.Remove(hitButton); menuButtons.Remove(standButton); Texture2D quitButtonSprite = Content.Load <Texture2D>(@"graphics/quitbutton"); MenuButton quitButton = new MenuButton(quitButtonSprite, new Vector2(HorizontalMenuButtonOffset, WindowHeight - VerticalMenuButtonSpacing), GameState.Exiting); menuButtons.Add(quitButton); } // No one is busted and player hit again else { dealerHit = false; playerHit = false; currentState = GameState.WaitingForPlayer; } break; case GameState.Exiting: Exit(); break; } // game state-specific processing base.Update(gameTime); }
/// <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[0].X = HorizontalCardOffset; playerHand[0].Y = TopCardOffset; if (!playerHand[0].FaceUp) { playerHand[0].FlipOver(); } // first dealer card dealerHand.Add(deck.TakeTopCard()); dealerHand[0].X = HorizontalCardOffset * 4; dealerHand[0].Y = TopCardOffset; if (dealerHand[0].FaceUp) { dealerHand[0].FlipOver(); } // second player card playerHand.Add(deck.TakeTopCard()); playerHand[1].X = HorizontalCardOffset; playerHand[1].Y = TopCardOffset + VerticalCardSpacing; if (!playerHand[1].FaceUp) { playerHand[1].FlipOver(); } // second dealer card dealerHand.Add(deck.TakeTopCard()); dealerHand[1].X = HorizontalCardOffset * 4; dealerHand[1].Y = TopCardOffset + VerticalCardSpacing; if (!dealerHand[1].FaceUp) { dealerHand[1].FlipOver(); } // 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>(@"graphics\quitbutton"); // create hit button and add to list hitButtonSprite = Content.Load <Texture2D>("graphics//hitButton"); Vector2 hitCenter = new Vector2(HorizontalCardOffset * 2 + 75, TopCardOffset); MenuButton hitButton = new MenuButton(hitButtonSprite, hitCenter, GameState.PlayerHitting); menuButtons.Add(hitButton); // create stand button and add to list standButtonSprite = Content.Load <Texture2D>("graphics//standbutton"); Vector2 standCenter = new Vector2(HorizontalCardOffset * 2 + 75, TopCardOffset + VerticalCardSpacing); MenuButton standButton = new MenuButton(standButtonSprite, standCenter, GameState.WaitingForDealer); menuButtons.Add(standButton); }
/// <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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { 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; Card nextPlayerCard = deck.TakeTopCard(); nextPlayerCard = new Card(Content, nextPlayerCard.Rank, nextPlayerCard.Suit, HorizontalCardOffset, TopCardOffset + VerticalCardSpacing * (playerHand.Count)); playerHand.Add(nextPlayerCard); nextPlayerCard.FlipOver(); playerScoreMessage.Text = ScoreMessagePrefix + GetBlockjuckScore(playerHand).ToString(); currentState = GameState.WaitingForDealer; break; case GameState.WaitingForDealer: if (GetBlockjuckScore(dealerHand) <= GetBlockjuckScore(playerHand)) { currentState = GameState.DealerHitting; } else { currentState = GameState.WaitingForPlayer; } break; case GameState.DealerHitting: dealerHit = true; Card nextDealerCard = deck.TakeTopCard(); nextDealerCard = new Card(Content, nextDealerCard.Rank, nextDealerCard.Suit, WindowWidth - HorizontalCardOffset, TopCardOffset + VerticalCardSpacing * (dealerHand.Count)); dealerHand.Add(nextDealerCard); nextDealerCard.FlipOver(); currentState = GameState.CheckingHandOver; break; case GameState.CheckingHandOver: if (!dealerHit && !playerHit) { if (GetBlockjuckScore(dealerHand) > GetBlockjuckScore(playerHand)) { const string dealerWinMessage = "Dealer win !"; Message resultMessage = new Message(dealerWinMessage, messageFont, winnerMessageLocation); messages.Add(resultMessage); dealerHand[0].FlipOver(); currentState = GameState.DisplayingHandResults; } else { const string playerWinMessage = "Player win !"; Message resultMessage = new Message(playerWinMessage, messageFont, winnerMessageLocation); messages.Add(resultMessage); dealerHand[0].FlipOver(); menuButtons.Clear(); Vector2 quitButtonCenter = new Vector2(WindowWidth / 2, TopCardOffset + VerticalCardSpacing * 3); MenuButton quitButton = new MenuButton(quitButtonSprite, quitButtonCenter, GameState.Exiting); menuButtons.Add(quitButton); currentState = GameState.DisplayingHandResults; } } if (GetBlockjuckScore(dealerHand) > MaxHandValue || GetBlockjuckScore(playerHand) > MaxHandValue) { if (GetBlockjuckScore(dealerHand) <= MaxHandValue) { const string dealerWinMessage = "Dealer win !"; Message resultMessage = new Message(dealerWinMessage, messageFont, winnerMessageLocation); messages.Add(resultMessage); } else { const string playerWinMessage = "Player win !"; Message resultMessage = new Message(playerWinMessage, messageFont, winnerMessageLocation); messages.Add(resultMessage); dealerHand[0].FlipOver(); } menuButtons.Clear(); Vector2 quitButtonCenter = new Vector2(WindowWidth / 2, TopCardOffset + VerticalCardSpacing * 3); MenuButton quitButton = new MenuButton(quitButtonSprite, quitButtonCenter, GameState.Exiting); menuButtons.Add(quitButton); currentState = GameState.DisplayingHandResults; } else { currentState = GameState.WaitingForPlayer; } break; case GameState.DisplayingHandResults: break; case GameState.Exiting: this.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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } // update menu buttons as appropriate MouseState mouse = Mouse.GetState(); foreach (MenuButton button in menuButtons) { if (currentState == GameState.WaitingForPlayer || currentState == GameState.DisplayingHandResults) { button.Update(mouse); } } // game state-specific processing switch (currentState) { // case if player hitting case (GameState.PlayerHitting): numberOfCardsPlayerHand = playerHand.Count; Card playerCard2 = deck.TakeTopCard(); playerCard2.FlipOver(); playerCard2.X = HorizontalCardOffset; playerCard2.Y = TopCardOffset + numberOfCardsPlayerHand * VerticalCardSpacing; playerHand.Add(playerCard2); playerScoreMessage.Text = ScoreMessagePrefix + GetBlockjuckScore(playerHand).ToString(); playerHit = true; Game1.ChangeState(GameState.WaitingForDealer); break; // case of player is passing case (GameState.PlayerPassing): playerDone = true; if (dealerDone) { Game1.ChangeState(GameState.CheckingHandOver); } else { Game1.ChangeState(GameState.WaitingForDealer); } break; // case if waiting for dealer case (GameState.WaitingForDealer): // dealer decide to hit if (GetBlockjuckScore(dealerHand) < MaxDealerHitValue) { Game1.ChangeState(GameState.DealerHitting); } // dealer decide to stand or he got 21 else { dealerHit = false; dealerDone = true; Game1.ChangeState(GameState.CheckingHandOver); } break; // case if dealer hitting case (GameState.DealerHitting): numberOfCardsDealerHand = dealerHand.Count; Card dealerCard2 = deck.TakeTopCard(); dealerCard2.FlipOver(); dealerCard2.X = WindowWidth - HorizontalCardOffset; dealerCard2.Y = TopCardOffset + numberOfCardsDealerHand * VerticalCardSpacing; dealerHand.Add(dealerCard2); dealerHit = true; if (GetBlockjuckScore(dealerHand) >= MaxDealerHitValue && GetBlockjuckScore(dealerHand) <= MaxHandValue) { dealerDone = true; } else if (GetBlockjuckScore(dealerHand) > MaxHandValue) { dealerDone = true; playerDone = true; } Game1.ChangeState(GameState.CheckingHandOver); break; // case checking hand over case (GameState.CheckingHandOver): if (!playerDone && GetBlockjuckScore(playerHand) <= MaxHandValue) { dealerHit = false; playerHit = false; Game1.ChangeState(GameState.WaitingForPlayer); } else if (playerDone && !dealerDone) { dealerHit = false; playerHit = false; Game1.ChangeState(GameState.WaitingForDealer); } else { if (GetBlockjuckScore(playerHand) <= MaxHandValue && (GetBlockjuckScore(dealerHand) > MaxHandValue || dealerDone && playerDone && GetBlockjuckScore(playerHand) > GetBlockjuckScore(dealerHand))) { winnerText = "YOU WON!"; winnerMessage = new Message(winnerText, messageFont, winnerMessageLocation); messages.Add(winnerMessage); } else if (GetBlockjuckScore(dealerHand) <= MaxHandValue && (GetBlockjuckScore(playerHand) > MaxHandValue || dealerDone && playerDone && GetBlockjuckScore(playerHand) < GetBlockjuckScore(dealerHand))) { winnerText = "DEALER WON!"; winnerMessage = new Message(winnerText, messageFont, winnerMessageLocation); messages.Add(winnerMessage); } else if (GetBlockjuckScore(dealerHand) > MaxHandValue && GetBlockjuckScore(playerHand) > MaxHandValue || dealerDone && playerDone && GetBlockjuckScore(dealerHand) == GetBlockjuckScore(playerHand)) { winnerText = "TIE!"; winnerMessage = new Message(winnerText, messageFont, winnerMessageLocation); messages.Add(winnerMessage); } dealerHand[0].FlipOver(); dealerScoreMessage = new Message(ScoreMessagePrefix + GetBlockjuckScore(dealerHand).ToString(), messageFont, new Vector2(WindowWidth - HorizontalMessageOffset, ScoreMessageTopOffset)); messages.Add(dealerScoreMessage); for (int i = 0; i < menuButtons.Count; i++) { menuButtons.RemoveAt(i); } MenuButton exitButton0 = new MenuButton(quitButtonSprite, new Vector2(HorizontalMenuButtonOffset, TopMenuButtonOffset + VerticalMenuButtonSpacing), GameState.Exiting); menuButtons.Add(exitButton0); Game1.ChangeState(GameState.DisplayingHandResults); } break; // case for exit case (GameState.Exiting): this.Exit(); break; } base.Update(gameTime); }
/// <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 the deck object and shuffle Deck deck = new Deck(Content, 0, 0); deck.Shuffle(); // create the player hands and fully deal the deck into the hands for(int i = 0; i < 3;i ++ )/*while (deck.Empty != true)*/ { player1.AddCard(deck.TakeTopCard()); if (deck.Empty != true) { player2.AddCard(deck.TakeTopCard()); } } // create the player battle piles wbp1 = new WarBattlePile(400, 200); wbp2 = new WarBattlePile(400, 400); // create the player winner messages wmessage1 = new WinnerMessage(Content, 600, 100); wmessage2 = new WinnerMessage(Content,600,500); // create the menu buttons quitbutton = new MenuButton(Content, "quitbutton", 180, 450,GameState.Quit); flipbutton = new MenuButton(Content, "flipbutton", 180, 150,GameState.Flip); collectwinningsbutton = new MenuButton(Content, "collectwinningsbutton", 180, 300, GameState.CollectWinnings); // initialized the menu buttons collectwinningsbutton.Visible = false; flipbutton.Visible = true; quitbutton.Visible = true; }
/// <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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } MouseState mouse = Mouse.GetState(); // update menu buttons as appropriate if (currentState == GameState.WaitingForPlayer || currentState == GameState.DisplayingHandResults) { foreach (MenuButton button in menuButtons) { button.Update(mouse); } } // game state-specific processing switch (currentState) { case GameState.PlayerHitting: // giving player a new card DealNewCard(playerHand); // updating players score playerScoreMessage.Text = GetBlockjuckScore(playerHand).ToString(); // moving to the next game state currentState = GameState.WaitingForDealer; // memorising players turn playerHit = true; break; case GameState.WaitingForDealer: // deciding wheather dealer hits or not if (GetBlockjuckScore(dealerHand) < 17) { currentState = GameState.DealerHitting; } else { currentState = GameState.CheckingHandOver; } break; case GameState.DealerHitting: // giving dealer a new card DealNewCard(dealerHand); // changing the game state currentState = GameState.CheckingHandOver; // memorising dealers turn dealerHit = true; break; case GameState.CheckingHandOver: // deciding whether the game is over or not if (GetBlockjuckScore(dealerHand) > MaxHandValue || GetBlockjuckScore(playerHand) > MaxHandValue || (playerHit == false && dealerHit == false)) { // if it is fliping over dealers first card dealerHand[0].FlipOver(); // showing dealers score dealerScoreMessage = new Message(ScoreMessagePrefix + GetBlockjuckScore(dealerHand).ToString(), messageFont, new Vector2(WindowWidth - HorizontalMessageOffset, ScoreMessageTopOffset)); messages.Add(dealerScoreMessage); // removing hit and stand buttons menuButtons.RemoveRange(0, 2); // creating quit button MenuButton quitButton = new MenuButton(quitButtonSprite, new Vector2((float)(HorizontalMenuButtonOffset), (float)(WindowHeight - TopMenuButtonOffset)), GameState.Exiting); menuButtons.Add(quitButton); // deciding who has won and creating congadulations message if ((GetBlockjuckScore(dealerHand) > GetBlockjuckScore(playerHand) || GetBlockjuckScore(playerHand) > MaxHandValue) && (GetBlockjuckScore(dealerHand) <= MaxHandValue)) { winnerMessage = new Message("Dealer Won!", messageFont, winnerMessageLocation); } else if ((GetBlockjuckScore(dealerHand) < GetBlockjuckScore(playerHand) || GetBlockjuckScore(dealerHand) > MaxHandValue) && (GetBlockjuckScore(playerHand) <= MaxHandValue)) { winnerMessage = new Message("You Won!", messageFont, winnerMessageLocation); } else { winnerMessage = new Message("It's A Tie!", messageFont, winnerMessageLocation); } messages.Add(winnerMessage); // changing game state currentState = GameState.DisplayingHandResults; } else { // if the game continues we return to the first game state playerHit = false; dealerHit = false; currentState = GameState.WaitingForPlayer; } break; case GameState.DisplayingHandResults: // Empty game state to display results break; case GameState.Exiting: // quiting from the game Exit(); break; } base.Update(gameTime); }