Designing a Blackjack App
Software design practices and good habits!
When you are just starting out as a new software engineer it can be tempting to skip over some of the things that not only make your job easier , but also tend to make the quality of your code a little higher. As a freshman engineer I would often find myself skipping right to coding without even planning what I was going to code. So in the spirit of teaching you the RIGHT way to do things let’s talk about the bare minimum we need to do , and then let’s get to coding a more complicated app. We’re going to build a Blackjack app using the Console the quickest way that we can and then after we will talk a little bit about some better ways to build this app. Designing a Blackjack App will be a good way for us to talk a little bit about some of the different variable types, do some basic math functions, create and use methods, and even start to talk about classes. This will all be used to further our goal of learning about C# and software engineering.
Normally in a professional environment you will start with requirements for your project. These requirements will typically be provided by a stakeholder. Since we don’t have an external stakeholder lets take a second and write down some basic requirements for designing a BlackJack App and get a little closer to our goal of learning about C#.
- The player will receive 2 cards
- The dealer will receive 2 cards
- Ask the player to hit or stay (and maybe split of double in a different version)
- The players cards will be totaled and displayed
- The cards will emulate a the typical cards you see in a card deck
- Do not display Suits.
- New cards will be from 2 to ACE
- If the card is an ace the player or dealer will select 1 or 11 (out of scope for first build)
- If any hand is over 21 the owner will bust and lose
- Display all output to console
- At the end of the game display an option to play another game or quit
Designing a Blackjack App- The Technical Design
Now that we are on to talking about design. It’s good a good idea to mention there are many ways to complete a technical design. Some software development methods require very detailed technical designs others just focus on creating a rough idea of the application. In these cases you usually focus more on the acceptance criteria than writing out details about how we will get to the end result.
Because we want to get to the coding as soon as possible we will just do a basic design. There are a lot of ways to tackle this so one persons design for a game like this might vary greatly from another persons.
Things that we will need to manage the game
- Create a class or some way to represent a card
- a card has a value
- a type (king , queen , numeric etc)
- Create a collection for the players cards and the dealers
- Create a method to calculate the numeric value of “hand”
- Determine if the hand is over 21
- Create a method to display the cards in a hand (possibly for the advanced version only)
- Create a method to get a new card from the dealer by rolling a random number from 2 to 14 with 11 representing a jack 12 a queen 13 – king and 14 Ace
Designing a Blackjack App – The game loop
- When the game starts we will get 2 new cards for the player and 2 for the dealer
- Display the dealers second card to the player
- Total the players cards and display them
- Ask the Player to type H for hit or S for stay
- Accept a uppercase or lower case response
- If the player chooses to “hit” another card will be drawn and the loop will continue until the player stays or busts
- After the player decides to “stay” the dealer’s turn will commence
- The dealer will follow Standard black jack rules
- When the dealer has served every player
- The dealer’s face-down card is turned up
- If the total is 17 or more, the dealer must stand.
- If the total is 16 or under, the dealer must hit.
- The dealer must continue to take cards until the total is 17 or more, at which point the dealer must stand.
- When the dealer stands, or busts the game is over.
- If the dealer busts, the player wins
- If the dealer does not bust, the total of the two hands is compared and the person with the highest total wins.
- Ask the Player to quit or play again