3/30/2022

Blackjack C Sharp

Blackjack C Sharp Rating: 4,0/5 8787 votes

Building a C# adventure game can be a fun way to learn how to code. While constructing a text-based game, you can gain an understanding of fundamental programming concepts, and an introduction to object-oriented theory.

Learning to program is similar to learning a musical instrument, or a foreign language. Practice is important. At the end of each chapter you are encouraged to create additional smaller programs. These are opportunities to experiment and practice what you've learned. By the end of this series you'll have several console applications that show your understanding of programming and C#.

Let's hope your teacher doesn't know how to play blackjack! You're initially dealt 2 cards, before you hit or stay, not one. If you go over 21, and the dealer goes over 21, you do not win. In this video we go over the common loops provided by C# and use them to create a console black jack game. Console blackjack in C#. GitHub Gist: instantly share code, notes, and snippets. Im tasked with developing a simple blackjack program for a class. The program I have currently compiles and runs but it isnt paying out properly. A blackjack (21) should pay out 1.5.the wager, but it is doing it more than it should. Here is the code I have. Console BlackJack Practice v1 (21 Rule Only) Fun. Posted by 2 hours ago. Console BlackJack Practice v1 (21 Rule Only) Fun.

C# Adventure Game Overview

We will focus on programming basics that are, for the most part, similar across multiple languages with console applications. At first C# fundamentals will be our priority. Later, we will add some design elements to our interface.

This book is written for those new to C#, or to programming in general. Often the most straightforward way to code is not necessarily the most efficient or elegant. As we move through the material, we'll talk about ways to program more efficiently, and how to optimize earlier code.

While making the C# Adventure Game, you'll supply the content. Guided assignments along the way will make this adventure experience uniquely yours. Try to add elements you've never seen before; the more original the content, the better.

Styling Conventions

Special styles indicate tasks and expandable boxes.

  • Tasks are marked to indicate when you need to take action (Example task).

Expandable Toggle Boxes

More information is inside of expandable boxes. Clicking the title reveals explanations, code examples, and other items that might be useful (but not essential for you to build the Adventure Game).

Expand these boxes to learn more. If you are feeling overwhelmed with new information, skip the box. You can always go back and read it later.

Try it out.

Audience

If you've never worked with C# or are just learning to program, this book was written for you.

Read through the chapters - even if you don't understand all of what you are reading. Do the assignments. You might not fully 'get' a concept until you've built a couple of applications. That's okay. Some of the ideas are complex and you just need time and practice to understand them.

Tips

  • Read through all the chapters
  • Do the practice assignments listed in the 'To Do' sections
  • Practice a few times a week

To Do Sections: Experiment & Practice

Blackjack C Sharp

Learning programming requires practice. The more you program, the easier it will get. If you feel like a chapter is a challenge, don't give up. Keep practicing and moving through the assignments.

At the end of each chapter are activities for experimentation and practice. The more applications you make, the more your skills will improve. Practice what you've learned, then improve your adventure game project with the new knowledge.

Practice Multiple Days Each Week

If you practice programming a few days a week you will have better results.

Programming is a creative activity that also involves logic and problem-solving. Research suggests that taking a break between consciously and unconsciously thinking about a problem will yield better results1.

Focus and think critically about what you are learning and creating, then take a break and 'sleep on it'2. When you return to the problem you will likely have a better understanding of it.

C# Language

Blackjack

C# (pronounced C-sharp) is a language Microsoft developed.

It is classified as an Object-Oriented Programming (OOP) language because object-oriented theory is inherent; code is organized as objects that have properties and behaviors. We’ll talk more about what that means and how we use objects as we work through this book.

Next Step

Your next step is to set up your development environment. C# Adventure Game screenshots show the Integrated Development Environment (IDE) Visual Studio Community 2015 and Visual Studio Enterprise 2017. Visual Studio Community is a free application that will help you write and debug your code.

Blackjack C Sharp Vs


If you already have Visual Studio (or an alternative development environment) set up, you can skip the first chapter, and move to C# Adventure 02: Output where we will walk through how to create a new project in Visual Studio, and write text to the screen (Console Window).


  1. Dijksterhuis, A. (2004). Think different: The merits of unconscious thought in preference development and decision making. Journal of Personality and Social Psychology, 87, 586-598.
  2. Weinschenk, S. 2016, May 19. Why 'Stepping Away' Increases Your Creativity https://medium.com/@weinschenk/why-stepping-away-increases-your-creativity-bc958057b8dc

The following tutorial is only some kind of “thought provoking” one: shows you some logic and class structure using enums and dynamically created images with Winform application.

Maybe I skipped some of the official rules. If so, I’m sorry I’m not a big gambler. This tutorial is not for teaching you Blackjack, but showing you some C# practices.

Here are some of the rules I have implemented:

  • we have a shuffled deck of 52 cards
  • the player starting hand has 2 cards
  • the computer hand has also 2 cards: one of them is visible the other one is not
  • the Jack, Queen and King has the value of 10
  • the Ace has the value of 11 or 1 if 11 would be too much in hand
  • the player starts to draw (official phrase is HIT)
  • the main goal is to stop at 21 (or close to 21)
  • the player can skip the drawing phase (STAY) if the total score is 16 at least
  • if player stays, the computer turn comes
  • first we can see the computer’s second (hidden) card
  • the computer starts to draw using the same rules as ours mentioned before

To show you how to use System.Drawing.Graphics class, I create the images of the cards by cutting 1 big image into pieces. Click on the following image to download it:

I have 3 main classes: Card, Deck and Hand. Of course the form has its own Form1 class.

I also have 2 enums: CardValue and CardSuit. Both enum indexing starts from 1. The CardValue contains the type of the cards (Ace, King, 9, 7, 2, etc.). The CardSuit contains the colors (hearts, spades, etc.).

Create a new class: Card.cs

Blackjack C Sharp Scale

Under the class place the 2 enums:

The Card class will have 3 properties, 1 constructor, 1 new method and 1 overridden method. What properties does a card have? Of course its value, its color and its picture. So create the private properties with their public encapsulated fields. The Image property won’t have setter, the value and color setter will have the image loader method:

With the constructor let’s create a dummy card with no color and value:

The attached image has the following properties: it’s 392 pixel high and 950 wide. So 1 card is about 97 high and 73 wide. The method has to slice the image depending on the following color from the deck (one color in each row on the image) and depending on the value (the image has the values in ascending order).

C Sharp Programming Download

And the overridden ToString method, just for fun (not used later in the code):

C Sharp Online Compiler

In the next chapter I’ll show you how to create the class for the Hand and the Deck.