Let’s build a basic Blackjack App using C# and the Console  Part 1 - The Design
Tags: csharp, beginners, dotnet
Published: 2020-02-01 15:46:32

<div class="boldgrid-section">
<div class="container">
<div class="row">
<div class="col-md-12 col-xs-12 col-sm-12">
<h1 style="text-align: center;">Designing a Blackjack App</h1>
<h2 class=""><img class="wp-image-234 hoverZoomLink aligncenter" title="" src="https://www.thedevelopingdeveloper.net/wp-content/uploads/2019/12/spread-cards-1245868-300x200.jpg" alt="cards" width="365" height="243" /></h2>
<h2 class=""></h2>
<h3 class="" style="text-align: center;"><strong>Software design practices and good habits! </strong></h3>
<p class="">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.</p>

<h3 class=""></h3>
<p class=""><img class="wp-image-239 hoverZoomLink aligncenter" title="" src="https://www.thedevelopingdeveloper.net/wp-content/uploads/2019/12/startup-3267505_1920-1-300x200.jpg" alt="Design" width="419" height="279" /></p>
&nbsp;
<h3 class="" style="text-align: center;"><strong>Requirements</strong></h3>
<p class="">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#.</p>

<ul class="">
 	<li>The player will receive  2 cards</li>
 	<li>The dealer will receive 2 cards</li>
 	<li>Ask the player to hit or stay (and maybe split of double in a different version)</li>
 	<li>The players cards  will be totaled and displayed</li>
 	<li>The cards will emulate a the typical cards you see in a  card deck</li>
 	<li>Do not display Suits.</li>
 	<li>New cards will be from 2 to ACE</li>
 	<li>If the card is an ace the player or dealer will select 1 or 11 <strong>(out of scope for first build)</strong></li>
 	<li>If any hand is over 21 the owner will bust and lose</li>
 	<li>Display all output to console</li>
 	<li>At the end of the game display an option to play another game or quit</li>
</ul>
<h3 class="" style="text-align: center;"><strong>Designing a Blackjack App- The Technical Design
</strong></h3>
<p class="">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.</p>
<p class="">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.</p>

<h4><strong>Things that we will need to manage the game </strong></h4>
<ul class="">
 	<li>Create a class or some way to represent a card
<ul>
 	<li>a card has a value</li>
 	<li>a type (king , queen , numeric etc)</li>
</ul>
</li>
 	<li>Create a collection for the players cards and the dealers</li>
 	<li>Create a method to calculate the numeric value of "hand"</li>
 	<li>Determine if the hand is over 21</li>
 	<li>Create a method to display the cards in a hand (possibly for the advanced version only)</li>
 	<li>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</li>
</ul>
<h3 class=""></h3>
[caption id="attachment_241" align="aligncenter" width="300"]<img class="size-medium wp-image-241 hoverZoomLink" title="" src="https://www.thedevelopingdeveloper.net/wp-content/uploads/2019/12/traffic-signal-496790_960_720-2-300x298.jpg" alt="The Loop" width="300" height="298" /> The Loop[/caption]
<h3 class="" style="text-align: center;"><strong>Designing a Blackjack App - The game loop </strong></h3>
&nbsp;
<ul class="">
 	<li>When the game starts we will get 2 new cards for the player and 2 for the dealer</li>
 	<li>Display the dealers <strong>second card</strong> to the player</li>
 	<li>Total the players cards and display them</li>
 	<li>Ask the Player to type H for hit or S for stay
<ul class="">
 	<li>Accept a uppercase or lower case response</li>
</ul>
</li>
 	<li>If the player chooses to "<strong>hit"</strong> another card will be drawn and the loop will continue until the player stays or busts</li>
 	<li>After the player decides to "<strong>stay"</strong> the dealer's turn will commence</li>
 	<li>The dealer will follow<a href="https://bicyclecards.com/how-to-play/blackjack/"> Standard black jack rules</a>
<ul class="">
 	<li>When the <b>dealer</b> has served every player</li>
 	<li>The <b>dealer's</b> face-down card is turned up</li>
 	<li>If the total is 17 or more, the dealer must stand.</li>
 	<li>If the total is 16 or under, the dealer must hit.</li>
 	<li>The <b>dealer</b> must continue to take cards until the total is 17 or more, at which point the <b>dealer</b> must stand.</li>
</ul>
</li>
 	<li>When the dealer stands, or busts the game is over.</li>
 	<li>If the dealer busts, the player wins</li>
 	<li>If the dealer does not bust, the total of the two hands is compared and the person with the highest total wins.</li>
 	<li>Ask the Player to quit or play again</li>
</ul>
<p class=""><a href="https://www.thedevelopingdeveloper.net/2020/01/15/the-basic-blackjack-app/">Now lets get to the learning about C# part...Next  up... lets code a basic version of Blackjack utilizing as few C# features as possible.</a></p>
&nbsp;
<p class=""></p>

</div>
</div>
</div>
</div>
<div id="hzImg" style="border: 1px solid #ffffff; line-height: 0; overflow: hidden; padding: 2px; margin: 0px; position: absolute; z-index: 2147483647; border-radius: 3px; box-shadow: rgba(0, 0, 0, 0.33) 3px 3px 9px 5px; opacity: 1; top: 298px; left: 1022px; background-color: #ffffff; display: none;"></div>
<div class="boldgrid-section">
<div class="container">
<div class="row">
<div class="col-md-12 col-xs-12 col-sm-12">
<p class="mod-reset"><img id="hzDownscaled" style="position: absolute; top: -10000px;" /></p>

</div>
</div>
</div>
</div>
<div class="boldgrid-section">
<div class="container">
<div class="row">
<div class="col-md-12 col-xs-12 col-sm-12">

<img id="hzDownscaled" style="position: absolute; top: -10000px;" />

</div>
</div>
</div>
</div>
<div id="hzImg" style="border: 1px solid #ffffff; line-height: 0; overflow: hidden; padding: 2px; margin: 0px; position: absolute; z-index: 2147483647; border-radius: 3px; box-shadow: rgba(0, 0, 0, 0.33) 3px 3px 9px 5px; opacity: 1; top: 1661px; left: 563px; background-color: #ffffff; cursor: pointer; pointer-events: none; display: none;"></div>
<div id="hzImg" style="border: 1px solid #ffffff; line-height: 0; overflow: hidden; padding: 2px; margin: 0px; position: absolute; z-index: 2147483647; border-radius: 3px; box-shadow: rgba(0, 0, 0, 0.33) 3px 3px 9px 5px; opacity: 1; top: 311px; left: 606px; background-color: #ffffff; display: none;"></div>
<div id="hzImg" style="border: 1px solid #ffffff; line-height: 0; overflow: hidden; padding: 2px; margin: 0px; position: absolute; z-index: 2147483647; border-radius: 3px; box-shadow: rgba(0, 0, 0, 0.33) 3px 3px 9px 5px; opacity: 1; top: 0px; left: -53px; background-color: #ffffff; cursor: pointer; pointer-events: none; display: none;"></div>