/// <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 int playerScore = GetBlockjuckScore(playerHand); int dealerScore = GetBlockjuckScore(dealerHand); if (currentState == GameState.PlayerHitting) { Card newPlayerCard = deck.TakeTopCard(); newPlayerCard.X = HorizontalCardOffset; newPlayerCard.Y = TopCardOffset + VerticalCardSpacing * (playerHand.Count); newPlayerCard.FlipOver(); playerHand.Add(newPlayerCard); playerScoreMessage.Text = ScoreMessagePrefix + GetBlockjuckScore(playerHand).ToString(); playerHit = true; currentState = GameState.WaitingForDealer; } else if (currentState == GameState.WaitingForDealer) { if (dealerScore < 17) { currentState = GameState.DealerHitting; } else { currentState = GameState.CheckingHandOver; } } else if (currentState == GameState.DealerHitting) { Card newDealerCard = deck.TakeTopCard(); newDealerCard.X = WindowWidth - HorizontalCardOffset; newDealerCard.Y = TopCardOffset + VerticalCardSpacing * (dealerHand.Count); newDealerCard.FlipOver(); dealerHand.Add(newDealerCard); dealerHit = true; currentState = GameState.CheckingHandOver; } else if (currentState == GameState.CheckingHandOver) { if (playerScore >= MaxHandValue || dealerScore >= MaxHandValue || (playerHit == false && dealerHit == false)) { if ((playerScore >= MaxHandValue && dealerScore < MaxHandValue) || (playerHit == false && dealerHit == false && playerScore > dealerScore)) { winnerMessage = new Message("Player won !", messageFont, winnerMessageLocation); messages.Add(winnerMessage); } else if ((dealerScore >= MaxHandValue && playerScore < MaxHandValue) || (playerHit == false && dealerHit == false && dealerScore > playerScore)) { winnerMessage = new Message("Dealer won !", messageFont, winnerMessageLocation); messages.Add(winnerMessage); } else { winnerMessage = new Message("It's a draw.", messageFont, winnerMessageLocation); messages.Add(winnerMessage); } currentState = GameState.DisplayingHandResults; } else { playerHit = false; dealerHit = false; currentState = GameState.WaitingForPlayer; } } else if (currentState == GameState.DisplayingHandResults) { if (!dealerHand[0].FaceUp) { dealerHand[0].FlipOver(); } if (!messages.Contains(dealerScoreMessage)) { dealerScoreMessage = new Message(ScoreMessagePrefix + GetBlockjuckScore(dealerHand).ToString(), messageFont, new Vector2(WindowWidth - HorizontalMessageOffset, ScoreMessageTopOffset)); messages.Add(dealerScoreMessage); } menuButtons.Clear(); MenuButton quitButton = new MenuButton(Content.Load <Texture2D>(@"graphics\quitbutton"), new Vector2(HorizontalMenuButtonOffset, QuitMenuButtonOffset), GameState.Exiting); menuButtons.Add(quitButton); quitButton.Update(mouse); } else if (currentState == GameState.Exiting) { Exit(); } 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 the menu buttons MouseState mouse = Mouse.GetState(); flipButton.Update(mouse); collectWinningsButton.Update(mouse); quitButton.Update(mouse); // update based on game state if (gameState == GameState.Flip) { // flip cards into battle piles Card player1Card = player1Hand.TakeTopCard(); player1Card.FlipOver(); player1BattlePile.AddCard(player1Card); Card player2Card = player2Hand.TakeTopCard(); player2Card.FlipOver(); player2BattlePile.AddCard(player2Card); // figure out winner and show the message if (player1Card.WarValue > player2Card.WarValue) { player1Message.Visible = true; currentWinner = Player.Player1; } else if (player2Card.WarValue > player1Card.WarValue) { player2Message.Visible = true; currentWinner = Player.Player2; } else { currentWinner = Player.None; } // adjust button visibility flipButton.Visible = false; collectWinningsButton.Visible = true; // wait for players to collect winnings gameState = GameState.CollectWinnings; } else if (gameState == GameState.Play) { // distribute battle piles into appropriate hands and hide message if (currentWinner == Player.Player1) { player1Hand.AddCards(player1BattlePile); player1Hand.AddCards(player2BattlePile); player1Message.Visible = false; } else if (currentWinner == Player.Player2) { player2Hand.AddCards(player1BattlePile); player2Hand.AddCards(player2BattlePile); player2Message.Visible = false; } else { player1Hand.AddCards(player1BattlePile); player2Hand.AddCards(player2BattlePile); } currentWinner = Player.None; // set button visibility flipButton.Visible = true; collectWinningsButton.Visible = false; // check for game over if (player1Hand.Empty) { flipButton.Visible = false; player2Message.Visible = true; gameState = GameState.GameOver; } else if (player2Hand.Empty) { player2Message.Visible = true; flipButton.Visible = false; gameState = GameState.GameOver; } } else if (gameState == GameState.Quit) { Exit(); } 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(); } MouseState mouse = Mouse.GetState(); // update the menu buttons flipButton.Update(mouse); quitButton.Update(mouse); collectWinnings.Update(mouse); // update based on game state if (gameState == GameState.Play) { playerOneWM.Visible = false; playerTwoWM.Visible = false; flipButton.Visible = true; collectWinnings.Visible = false; if (playerOneHand.Empty) { flipButton.Visible = false; playerTwoWM.Visible = true; } else if (playerTwoHand.Empty) { flipButton.Visible = false; playerOneWM.Visible = true; } } if (gameState == GameState.Flip && playerOneBP.Empty && playerTwoBP.Empty) { Card cardToAddToPlayerOne = playerOneHand.TakeTopCard(); cardToAddToPlayerOne.FlipOver(); playerOneBP.AddCard(cardToAddToPlayerOne); Card cardToAddToPlayerTwo = playerTwoHand.TakeTopCard(); cardToAddToPlayerTwo.FlipOver(); playerTwoBP.AddCard(cardToAddToPlayerTwo); if (playerOneBP.GetTopCard().WarValue > playerTwoBP.GetTopCard().WarValue) { currentWinner = Player.Player1; playerOneWM.Visible = true; playerTwoWM.Visible = false; } else if (playerOneBP.GetTopCard().WarValue < playerTwoBP.GetTopCard().WarValue) { currentWinner = Player.Player2; playerTwoWM.Visible = true; playerOneWM.Visible = false; } else { currentWinner = Player.None; playerOneWM.Visible = false; playerTwoWM.Visible = false; } flipButton.Visible = false; collectWinnings.Visible = true; } if (gameState == GameState.CollectWinnings) { if (currentWinner == Player.Player1) { playerOneHand.AddCards(playerOneBP); playerOneHand.AddCards(playerTwoBP); } else if (currentWinner == Player.Player2) { playerTwoHand.AddCards(playerOneBP); playerTwoHand.AddCards(playerTwoBP); } else { playerOneHand.AddCards(playerOneBP); playerTwoHand.AddCards(playerTwoBP); } gameState = GameState.Play; } if (gameState == GameState.Quit) { this.Exit(); } 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 the menu buttons MouseState mouse = Mouse.GetState(); quitButton.Update(mouse); flipButton.Update(mouse); collectButton.Update(mouse); // update based on game state if (gameState == GameState.Play) { flipButton.Visible = true; quitButton.Visible = true; collectButton.Visible = false; } if (gameState == GameState.Flip) { flipButton.Visible = false; collectButton.Visible = true; if (playerOnePile.Empty) { playerOnePile.AddCard(playerOneHand.TakeTopCard()); playerTwoPile.AddCard(playerTwoHand.TakeTopCard()); playerOnePile.GetTopCard().FlipOver(); playerTwoPile.GetTopCard().FlipOver(); } else if (playerOnePile.GetTopCard().WarValue > playerTwoPile.GetTopCard().WarValue) { playerOneMessage.Visible = true; } else if (playerTwoPile.GetTopCard().WarValue > playerOnePile.GetTopCard().WarValue) { playerTwoMessage.Visible = true; } else { playerOnePile.AddCard(playerOneHand.TakeTopCard()); playerTwoPile.AddCard(playerTwoHand.TakeTopCard()); playerOnePile.GetTopCard().FlipOver(); playerTwoPile.GetTopCard().FlipOver(); } } if (gameState == GameState.CollectWinnings) { flipButton.Visible = true; playerOneMessage.Visible = false; playerTwoMessage.Visible = false; gameState = GameState.Play; if (playerOnePile.GetTopCard().WarValue > playerTwoPile.GetTopCard().WarValue) { playerOneHand.AddCards(playerOnePile); playerOneHand.AddCards(playerTwoPile); } else if (playerTwoPile.GetTopCard().WarValue > playerOnePile.GetTopCard().WarValue) { playerTwoHand.AddCards(playerOnePile); playerTwoHand.AddCards(playerTwoPile); } } if (gameState == GameState.CollectWinnings) { playerOneHand.AddCards(playerOnePile); playerTwoHand.AddCards(playerTwoPile); } if (gameState == GameState.Quit) { this.Exit(); } // update game stats string0 = playerOneHand.Count.ToString(); string1 = playerTwoHand.Count.ToString(); 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 the menu buttons MouseState mouse = Mouse.GetState(); flipCards.Update(mouse); collectWinnings.Update(mouse); quitGame.Update(mouse); // update based on game state if (gameState == GameState.Flip) { // FLIP CARDS INTO BATTLE PILES Card player1Card = handPlayer1.TakeTopCard(); player1Card.FlipOver(); pilePlayer1.AddCard(player1Card); Card player2Card = handPlayer2.TakeTopCard(); player2Card.FlipOver(); pilePlayer2.AddCard(player2Card); //Otherwise //pilePlayer1.AddCard(handPlayer1.TakeTopCard()); //pilePlayer1.GetTopCard().FlipOver(); //pilePlayer2.AddCard(handPlayer2.TakeTopCard()); //pilePlayer2.GetTopCard().FlipOver(); // FIGURE OUT WINNER AND SHOW MESSAGE if (player1Card.WarValue > player2Card.WarValue) { winnerPlayer1.Visible = true; currentWinner = Player.Player1; } else if (player1Card.WarValue < player2Card.WarValue) { winnerPlayer2.Visible = true; currentWinner = Player.Player2; } else { currentWinner = Player.None; } // Otherwise //if (pilePlayer1.GetTopCard().WarValue > pilePlayer2.GetTopCard().WarValue) //{ // winnerPlayer1.Visible = true; //} //else if (pilePlayer1.GetTopCard().WarValue < pilePlayer2.GetTopCard().WarValue) //{ // winnerPlayer2.Visible = true; //} // gameState = GameState.Play; // adjust button visibility flipCards.Visible = false; collectWinnings.Visible = true; // wait for player to collect winnings gameState = GameState.CollectWinnings; } else if (gameState == GameState.Play) { // distribute battle piles into appropiate hands and hide message if (currentWinner == Player.Player1) { handPlayer1.AddCards(pilePlayer1); handPlayer1.AddCards(pilePlayer2); winnerPlayer1.Visible = false; } else if (currentWinner == Player.Player2) { handPlayer2.AddCards(pilePlayer1); handPlayer2.AddCards(pilePlayer2); winnerPlayer2.Visible = false; } else { handPlayer1.AddCards(pilePlayer1); handPlayer2.AddCards(pilePlayer2); } currentWinner = Player.None; //Otherwise //if (pilePlayer1.GetTopCard().WarValue > pilePlayer2.GetTopCard().WarValue) //{ // handPlayer1.AddCards(pilePlayer1); // handPlayer1.AddCards(pilePlayer2); // winnerPlayer1.Visible = false; //} //else if (pilePlayer1.GetTopCard().WarValue < pilePlayer2.GetTopCard().WarValue) //{ // handPlayer2.AddCards(pilePlayer1); // handPlayer2.AddCards(pilePlayer2); // winnerPlayer2.Visible = false; //} //else //{ // handPlayer1.AddCards(pilePlayer1); // handPlayer2.AddCards(pilePlayer2); //} // set flip button visibility flipCards.Visible = true; collectWinnings.Visible = false; //gameState = GameState.Play; // check for game over if (handPlayer1.Empty || handPlayer2.Empty) { flipCards.Visible = false; gameState = GameState.GameOver; if (handPlayer1.Empty) { winnerPlayer2.Visible = true; } else { winnerPlayer1.Visible = true; } } } //Otherwise //if (gameState == GameState.GameOver) //{ // collectWinnings.Visible = false; // flipCards.Visible = false; // if (handPlayer1.Empty) // { // winnerPlayer2.Visible = true; // } // else // { // winnerPlayer1.Visible = true; // } //} else if (gameState == GameState.Quit) { Exit(); } //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(); } MouseState mouse = Mouse.GetState(); // update the menu buttons flipButton.Update(mouse); collectButton.Update(mouse); quitButton.Update(mouse); switch (gameState) { case (GameState.Play): flipButton.Visible = true; collectButton.Visible = false; playerOneWin.Visible = false; playerTwoWin.Visible = false; quitButton.Visible = true; break; case (GameState.Flip): flipButton.Visible = false; collectButton.Visible = true; break; case (GameState.Quit): Exit(); break; } // update based on game state switch (gameState) { case (GameState.Flip): if (battlePile1.Empty && battlePile2.Empty) { takeCardsForBattle(); while (playCard1.WarValue == playCard2.WarValue) { takeCardsForBattle(); } } if (playCard1.WarValue > playCard2.WarValue) { //player1 win playerOneWin.Visible = true; } else { //player2 win playerTwoWin.Visible = true; } break; case (GameState.CollectWinnings): if (playCard1.WarValue > playCard2.WarValue) { player1.AddCards(battlePile1); player1.AddCards(battlePile2); } else { player2.AddCards(battlePile1); player2.AddCards(battlePile2); } if (player1.Empty || player2.Empty) { flipButton.Visible = false; collectButton.Visible = false; } else { ChangeState(GameState.Play); } break; } base.Update(gameTime); }