Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using XnaCards;
- namespace ProgrammingAssignment6
- {
- /// <summary>
- /// This is the main type for your game.
- /// </summary>
- public class Game1 : Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- const int WindowWidth = 800;
- const int WindowHeight = 600;
- // max valid blockjuck score for a hand
- const int MaxHandValue = 21;
- // deck and hands
- Deck deck;
- List<Card> dealerHand = new List<Card>();
- List<Card> playerHand = new List<Card>();
- // hand placement
- const int TopCardOffset = 100;
- const int HorizontalCardOffset = 150;
- const int VerticalCardSpacing = 125;
- // messages
- SpriteFont messageFont;
- const string ScoreMessagePrefix = "Score: ";
- Message playerScoreMessage;
- Message dealerScoreMessage;
- Message winnerMessage;
- List<Message> messages = new List<Message>();
- // message placement
- const int ScoreMessageTopOffset = 25;
- const int HorizontalMessageOffset = HorizontalCardOffset;
- Vector2 winnerMessageLocation = new Vector2(WindowWidth / 2,
- WindowHeight / 2);
- // menu buttons
- Texture2D quitButtonSprite;
- List<MenuButton> menuButtons = new List<MenuButton>();
- // menu button placement
- const int TopMenuButtonOffset = TopCardOffset;
- const int QuitMenuButtonOffset = WindowHeight - TopCardOffset;
- const int HorizontalMenuButtonOffset = WindowWidth / 2;
- const int VerticalMenuButtonSpacing = 125;
- // use to detect hand over when player and dealer didn't hit
- bool playerHit = false;
- bool dealerHit = false;
- // game state tracking. Start by setting current state to WaitingForPlayer because this input is what will
- //trigger the game into action
- static GameState currentState = GameState.WaitingForPlayer;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- graphics.PreferredBackBufferWidth = WindowWidth;
- graphics.PreferredBackBufferHeight = WindowHeight;
- IsMouseVisible = true;
- }
- /// <summary>
- /// Allows the game to perform any initialization it needs to before starting to run.
- /// This is where it can query for any required services and load any non-graphic
- /// related content. Calling base.Initialize will enumerate through any components
- /// and initialize them as well.
- /// </summary>
- protected override void Initialize()
- {
- // TODO: Add your initialization logic here
- base.Initialize();
- }
- /// <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 playerCard1 = new Card(Content, deck.TakeTopCard().Rank,
- deck.TakeTopCard().Suit, HorizontalCardOffset, TopCardOffset);
- playerCard1.FlipOver();
- playerHand.Add(playerCard1);
- // first dealer card
- Card dealerCard1 = new Card(Content, deck.TakeTopCard().Rank,
- deck.TakeTopCard().Suit, WindowWidth - HorizontalCardOffset, TopCardOffset);
- dealerHand.Add(dealerCard1);
- // second player card
- Card playerCard2 = new Card(Content, deck.TakeTopCard().Rank,
- deck.TakeTopCard().Suit, HorizontalCardOffset, TopCardOffset * 2);
- playerCard2.FlipOver();
- playerHand.Add(playerCard2);
- // second dealer card
- Card dealerCard2 = new Card(Content, deck.TakeTopCard().Rank,
- deck.TakeTopCard().Suit, WindowWidth - HorizontalCardOffset, TopCardOffset * 2);
- dealerCard2.FlipOver();
- dealerHand.Add(dealerCard2);
- // 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
- Texture2D hitButtonImage = Content.Load<Texture2D>(@"graphics/hitbutton");
- Vector2 center = new Vector2(WindowWidth / 2, TopCardOffset);
- MenuButton hitButton = new MenuButton(hitButtonImage, center, GameState.PlayerHitting);
- menuButtons.Add(hitButton);
- // create stand button and add to list
- Texture2D standButtonImage = Content.Load<Texture2D>(@"graphics/standbutton");
- Vector2 center2 = new Vector2(WindowWidth / 2, TopCardOffset + VerticalCardSpacing);
- MenuButton standButton = new MenuButton(standButtonImage, center2, GameState.WaitingForDealer);
- menuButtons.Add(standButton);
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// game-specific content.
- /// </summary>
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- /// <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 if WaitingForPlayer or DisplayingHandResults
- if (currentState == GameState.WaitingForPlayer || currentState == GameState.DisplayingHandResults)
- {
- foreach (MenuButton button in menuButtons)
- {
- button.Update(mouse);
- }
- }
- // =============== game states ===============
- //PlayerHitting
- if (currentState == GameState.PlayerHitting)
- {
- Card anotherCardForPlayer = new Card(Content, deck.TakeTopCard().Rank,
- deck.TakeTopCard().Suit, HorizontalCardOffset, TopCardOffset * (playerHand.Count + 1));
- anotherCardForPlayer.FlipOver();
- playerHand.Add(anotherCardForPlayer);
- playerScoreMessage.Text = ScoreMessagePrefix + GetBlockjuckScore(playerHand).ToString();
- playerHit = true;
- ChangeState(GameState.WaitingForDealer);
- }
- // WaitingForDealer
- else if (currentState == GameState.WaitingForDealer)
- {
- if (GetBlockjuckScore(dealerHand) <= 16) // dealer can and WILL HIT
- {
- ChangeState(GameState.DealerHitting);
- }
- else
- {
- // dealer DIDN'T HIT
- ChangeState(GameState.CheckingHandOver);
- }
- }
- // DealerHitting
- else if (currentState == GameState.DealerHitting)
- {
- Card anotherCardForDealer = new Card(Content, deck.TakeTopCard().Rank,
- deck.TakeTopCard().Suit, WindowWidth - HorizontalCardOffset,
- TopCardOffset * (dealerHand.Count + 1));
- anotherCardForDealer.FlipOver();
- dealerHand.Add(anotherCardForDealer);
- dealerHit = true;
- ChangeState(GameState.CheckingHandOver);
- }
- // CheckingHandOver
- else if (currentState == GameState.CheckingHandOver)
- {
- // if either party goes over 21 or both decided to STAND the HAND IS OVER.
- if ((GetBlockjuckScore(playerHand) > MaxHandValue) ||
- (GetBlockjuckScore(dealerHand) > MaxHandValue) ||
- (playerHit == false && dealerHit == false))
- {
- dealerScoreMessage = new Message(ScoreMessagePrefix +
- GetBlockjuckScore(dealerHand).ToString(),
- messageFont, new Vector2(WindowWidth -
- HorizontalMessageOffset, ScoreMessageTopOffset));
- messages.Add(dealerScoreMessage);
- // create quit button and add to list
- Vector2 quitButCenter = new Vector2(WindowWidth / 2,
- WindowHeight - TopCardOffset - VerticalCardSpacing);
- MenuButton quitButton = new MenuButton(quitButtonSprite, quitButCenter,
- GameState.Exiting);
- dealerHand[0].FlipOver();
- menuButtons.RemoveRange(0, 2);
- menuButtons.Add(quitButton);
- ChangeState(GameState.DisplayingHandResults);
- // game logic
- if (GetBlockjuckScore(dealerHand) > GetBlockjuckScore(playerHand) &&
- GetBlockjuckScore(dealerHand )<= MaxHandValue)
- {
- string message = "Dealer Wins!";
- winnerMessage = new Message(message, messageFont, winnerMessageLocation);
- messages.Add(winnerMessage);
- ChangeState(GameState.DisplayingHandResults);
- }
- else if (GetBlockjuckScore(playerHand) > GetBlockjuckScore(dealerHand) &&
- GetBlockjuckScore(playerHand) <= MaxHandValue)
- {
- string message = "Player Wins!";
- winnerMessage = new Message(message, messageFont, winnerMessageLocation);
- messages.Add(winnerMessage);
- ChangeState(GameState.DisplayingHandResults);
- }
- else if (GetBlockjuckScore(playerHand) == GetBlockjuckScore(dealerHand))
- {
- string message = "It's a TIE!";
- winnerMessage = new Message(message, messageFont, winnerMessageLocation);
- messages.Add(winnerMessage);
- ChangeState(GameState.DisplayingHandResults);
- }
- else if (GetBlockjuckScore(playerHand) > MaxHandValue &&
- GetBlockjuckScore(dealerHand) > MaxHandValue)
- {
- string message = "Both Busted!!!";
- winnerMessage = new Message(message, messageFont, winnerMessageLocation);
- messages.Add(winnerMessage);
- ChangeState(GameState.DisplayingHandResults);
- }
- else if (GetBlockjuckScore(playerHand) > MaxHandValue &&
- GetBlockjuckScore(dealerHand) <= MaxHandValue)
- {
- string message = "Dealer Wins";
- winnerMessage = new Message(message, messageFont, winnerMessageLocation);
- messages.Add(winnerMessage);
- ChangeState(GameState.DisplayingHandResults);
- }
- else if (GetBlockjuckScore(dealerHand) > MaxHandValue &&
- GetBlockjuckScore(playerHand) <= MaxHandValue)
- {
- string message = "Player Wins";
- winnerMessage = new Message(message, messageFont, winnerMessageLocation);
- messages.Add(winnerMessage);
- ChangeState(GameState.DisplayingHandResults);
- }
- }
- else
- {
- ChangeState(GameState.WaitingForPlayer);
- playerHit = false;
- dealerHit = false;
- }
- }
- // QUIT GAME
- else if (currentState == GameState.Exiting)
- {
- Exit();
- }
- base.Update(gameTime);
- }
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.Goldenrod);
- spriteBatch.Begin();
- // draw hands
- foreach (Card card in dealerHand)
- {
- card.Draw(spriteBatch);
- }
- foreach (Card card in playerHand)
- {
- card.Draw(spriteBatch);
- }
- // draw messages
- foreach (Message message in messages)
- {
- message.Draw(spriteBatch);
- }
- // draw menu buttons
- foreach (MenuButton button in menuButtons)
- {
- button.Draw(spriteBatch);
- }
- spriteBatch.End();
- base.Draw(gameTime);
- }
- /// <summary>
- /// Calculates the Blockjuck score for the given hand
- /// </summary>
- /// <param name="hand">the hand</param>
- /// <returns>the Blockjuck score for the hand</returns>
- private int GetBlockjuckScore(List<Card> hand)
- {
- // add up score excluding Aces
- int numAces = 0;
- int score = 0;
- foreach (Card card in hand)
- {
- if (card.Rank != Rank.Ace)
- {
- score += GetBlockjuckCardValue(card);
- }
- else
- {
- numAces++;
- }
- }
- // if more than one ace, only one should ever be counted as 11
- if (numAces > 1)
- {
- // make all but the first ace count as 1
- score += numAces - 1;
- numAces = 1;
- }
- // if there's an Ace, score it the best way possible
- if (numAces > 0)
- {
- if (score + 11 <= MaxHandValue)
- {
- // counting Ace as 11 doesn't bust
- score += 11;
- }
- else
- {
- // count Ace as 1
- score++;
- }
- }
- return score;
- }
- /// <summary>
- /// Gets the Blockjuck value for the given card
- /// </summary>
- /// <param name="card">the card</param>
- /// <returns>the Blockjuck value for the card</returns>
- private int GetBlockjuckCardValue(Card card)
- {
- switch (card.Rank)
- {
- case Rank.Ace:
- return 11;
- case Rank.King:
- case Rank.Queen:
- case Rank.Jack:
- case Rank.Ten:
- return 10;
- case Rank.Nine:
- return 9;
- case Rank.Eight:
- return 8;
- case Rank.Seven:
- return 7;
- case Rank.Six:
- return 6;
- case Rank.Five:
- return 5;
- case Rank.Four:
- return 4;
- case Rank.Three:
- return 3;
- case Rank.Two:
- return 2;
- default:
- return 0;
- }
- }
- /// <summary>
- /// Changes the state of the game
- /// </summary>
- /// <param name="newState">the new game state</param>
- public static void ChangeState(GameState newState)
- {
- currentState = newState;
- }
- }
- }
Add Comment
Please, Sign In to add comment