- Submission for Object Oriented Software Design Semester 1 CA3
- In the game of Blackjack, players aim to achieve a hand value as close to 21 as possible without exceeding it.
- Each player is dealt two cards initially.
- Face cards have a value of 10.
- An Ace can be either 1 or 11.
- Players can choose to "hit" to receive additional cards or "stand" to keep their current hand.
- If a player exceeds 21, they bust and lose the round.
- The dealer follows a set of rules for drawing cards.
- The game involves strategic decisions based on the player's hand and the dealer's visible card.
- The ultimate goal is to beat the dealer without busting
- Program emulates the game of Blackjack.
Game flow:
- Create 2 players - user and dealer. Note: Game is restricted to two players only.
- Create a standard deck of 52 cards and shuffle before play.
- When game starts, deal two (2) cards to each player in alternating sequence, starting with user.
- Dealer's first card is the 'hole card' i.e. it's value is hidden.
- Evaluate scores to see if there is a winner after initial deal - if so, end game.
- Prompt user for decision - hit (deal another card) or stand (keep current hand and end turn).
- Evaluate scores to check if user is bust or hits 21 - if so, end game.
- Dealer's turn:
- Reveal hole card and evaluate score.
- Dealer must hit until score of >= 17 is reached, then stand.
- Evaluate scores to check for winner.
- Prompt user to play again.
Handling Aces:
- Ace is worth 1 or 11 - value is automatically set throughout the game to whichever is in favour of the player.
- The value of any Aces in a player's hand is re-evaluated after each "hit" by checking if the new score is greater than 21.
- Ace value can change throughout the game i.e. Ace may initially be 11, but if the user hits the Ace may change to 1 if required.
The following classes are implemented:
- Represents a player in the game
- Properties and variables:
string Name- Name of playerList<Card> Hand = [];- Player's hand, containing cardsint Score- Player's score
- Methods
AddCardToHand- Adds a card to the player's hand. Adjusts score. Evaluates value of Ace.HitOrStand- Prompts user to hit or stand.PrintPlayerHand- Prints cards in player's hand to console.
- Inherits from
Player. - Class created to handle logic for Dealer's rules for drawing cards.
- Hit if score < 17, else stand.
- Represents a standard deck of 52 cards.
- Properties and variables:
Card CardsInDeck[]- The cards remaining in the deck. An array of length 52.
- Methods
Shuffle- Shuffles the deck into random order.RemoveTopCard- Gets the last ('top') card in the deck, removes it, and updatesCardsInDeck.
- Represents a single card.
- Properties and variables:
CardValue- The point value of a card.IsAce- A boolean used to determine theCardValueof an Ace.IsFacecard- A boolean used to determine theCardValueof a face card.Rank- Stores the card's rank in relation to theCardRankenumeration.Suit- Stores the card's suit in relation to theCardSuitenumeration.
- Methods:
ToString- Prints the rank, suit and value of a card.
- Represents a single game/round.
- Properties and variables:
Player Players[]- An array containing players in the game.
- Methods:
Deal- Deals a card to a player and prints card to console.EvaluateScores- Checks if there's a winner.
Program.cs contains the following methods:
Mainmethod which handles the overall game flow.PlayAgainPrompt- Prompts player to play again or exit.InitialiseGame- Sets up game with players and deck.PlayOutPlayerTurn- Handles the logic for a player's turn, evaluating the score after each action.
- An Array is created as a variable of the
Gameclass to store the two (2) players in the game (User and Dealer). - A fifty-two (52) element Array stores 52 cards in a standard deck of cards used in the game.
- A list stores the cards of each player's hand. A list was used over an array as the amount of elements contained in a hand can vary.
- A feature of .NET 8, the Random.Shared.Shuffle() method is used to shuffle the deck of cards array in a random order.
Loops are used throughout the project, including for, foreach, while and do...while. Some notable examples include:
- The Main method is contained in a
do...whileloop which loops until the player decides to not play again. - Nested
forloops are used in theDeckconstructor to create a full deck of 52 cards - 4 suits with 13 cards per suit. - A
whileloop is used in thePlayer.AddCardToHandmethod to iterate through each Ace in the player's hand while their score is > 21, and to change the value to 1. whileloops are used throughout the program to get valid user input, for example in thePlayer.HitOrStandandPlayAgainPromptmethods.- A
whileloop is used in conjunction with theGame.EvaluateScoresmethod throughout the program to check if the game has a winner after each action.
- A
switchstatement is used in thePlayAgainPromptmethod when checking user input. ifstatements are used throughout the program to determine logic to follow if the game has a winner and what to output to the console for each result, such as in theGame.EvaluateScoresmethod- An
ifstatement is used in thePlayer.PrintPlayerHandandGame.Dealmethods to ensure that the Dealer's first card is concealed from the player (the 'hole card').
enums are used in theCardclass and used in conjunction with anifstatement to determine the score value for each card in the deck.
- The program correctly handles the Dealer's "Hole card" in line with Blackjack rules, by dealing this card face down initially to add an element of uncertainty to the game, and then revealing the card when it's the Dealer's turn.