Let’s build a basic Blackjack App using C#

Back in part one of this series we talked about HOW we are going to do this from a high level but now we’re going to start working on the code.

So for this part of the project we are going to have to learn quite a few new development concepts.You can skip this if you feel like you would rather just do the code first or in case you already know how this stuff works.

Lets start with a review of variables and collections and a little farther down we will look into classes and how to generate random numbers.

What is a Variable?

Variables are used to store information to be referenced and manipulated in a computer program.

They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program.

What is an Array? (Or sometimes called a collection)

An array is an ordered list of elements that can be of any type. You can define an array by placing a list of elements between brackets. As you can see an array is basically a collection of values that are generally used in the same way as variables are. We assign the collection (or array) a name and then we can access it later when needed. When accessing the values stored in an array (or collection) we just use the name , some brackets, and then an index value to say which item we want from the collection. It might help to visualize this a bit:

 

Array
An integer array

As you can see above, we have initialized an array. We’ve named it “intArray” and told the compiler that this array has a max of 5 integers (numbers) that we can store in it. (This last bit where we told it how large the collection will be is somewhat important because it is the limit of how many values we can add to it. The memory gets allocated for this when it gets instantiated and we must abide by that decision we made else we can have issues.)

Assigning values to an Array

Now lets assign some values to our array!

adding value
Adding values to our array

So in the above screenshot we created an array and added values to the [0] , [1] , and [4] position. You can think of it as looking like this:

Accessing the values in an array

Accessing the values is pretty easy too, we just have to tell the compiler the name of the collection that we want to access and then tell it the position or “index” of the value that we want. So if we wanted to get the value of the 4th position (index) then it looks like this:

int sum = intArray[4];
**One important thing to note is that collections always start at the index of 0 (zero) not one. So the value that you think of as the 4th value is actually the 3rd index. **

A quick note about other types of collections (Lists)

Before moving on to other topics I want to take a second to mention that while arrays are what many people think of first when speaking of collections in .net we have other types of lists which are (in my opinion more flexible and easy to work with) these lists have a number of handy features which make working with them really nice!

Here is what an example of a List<T> :

The main differences between a list and an array is seen above. First off we do not have to initialize the size of the collection when we use a list.

In addition to that, when adding values to a list we do not need to specify where we want to put the value.

We just use the .Add(int value) method.

 

The importance classes and the benefits of Object Oriented Programming

In my experience learning how classes work and when to use them is something that takes a little while to understand so I’m neither going to spend a ton of time explaining them here nor trying to explain the ins and outs of their role in programming in detail. I am however going to talk a little bit about why and how I intend to use them in this Blackjack App.

The first thing I want to mention is that you can build this or any other application without ever using OOP many old school programmers have not embraced and don’t fully understand these concepts. They build applications just fine without them (I guess! ).

Using classes in programming is useful as it allows us to create units of code for reuse throughout the application. An example that I often see when people try to explain classes is the idea of a Car and how you might have different types of cars.

 

Think of the class as the general idea of what a car is.
A standard car has :

 

  • 4 wheels
  • an engine
  •  a max speed
  • a steering wheel
  • other stuff

In OOP (Object Oriented Programming) use classes.  Classes let us consolidate code that represents the similarities between objects. (Think of it as common properties that like objects share. For instance every TV has a display size) We can also we use Inheritance  with classes to let other , more specific classes inherit those commonalities.

The other thing that can be very handy about using classes is we can take multiple variables and contain them withing a class  definition. Then when we want to make that thing that needs all of those variables (often referred to as “Properties” ) . Instead of manually recreating that set of variables over and over we can just create a new instance of the class and then fill in the variables.

If you aren’t fully with me here that’s ok ! Just remember what I said when looking over the next section. ( and if you have any questions leave me a comment below)

 

 

 

 

 

 

 

Leave a Reply

Your email address will not be published.