C# Blackjack

This page will contain a summary of my C# Blackjack Project. Development began on 19/9/10. This project is complete (30/9/10). 

The project is to produce a blackjack game in C#, that runs in a windows console. The game will involve two players (one human and one computer opponent). They both receive two cards at the beginning of the game, and can choose to twist or hold on their turn. Each player takes it in turns to go first. When a player twists, they receive a card. The aim of the game is to get as close to a score of 21 in your hand as possible, without going over. Going over 21 results in a bust, and the player loses. 

The idea of this project is to help me practice some of the object oriented programming techniques I have learnt thus far in C#, and also recall some more basic syntax that I have learnt previously. I will be updating this page as the project progresses. 


19th September 2010: 

Work has begun, and a first draft UML diagram for the class hierarchy has been produced. 


However, this diagram is very susceptible to change throughout development, as it may not function correctly. Currently, there are no class inheritances or method parameters, which may not be practical. More updates will be posted as work continues. 



 20th September 2010: 

A lot has developed it seems! The UML diagram in my previous post is now somewhat obselete. The major change is that IEntity is now named Entity, as it is a class as opposed to an interface. The main game loop now seems to be functioning correctly, running mostly from the Deck.Draw() and Entity.Turn() methods. These carry out the main data manipulation needed throughout the game, with the main function handling most of the looping and logic. 

This may be unimpressive to an experienced programmer, but I am somewhat proud of the system I have created for the Draw() method. The deck contains an array of card names, such as “Ace”, and an array of suits, such as ” of Hearts”. This way, I can create fusions of these strings to build a card as it is drawn. A random number between 1 and 13 is generated to choose a type, and another between 1 and 4 to choose a suit. These are then added together to create a card name. However, there is only one of each of these cards in a deck. Because of this, another array called Used, with capacity for 52 values, stores all used cards. If the string created is equal to any of the values in the Used array, another card is generated. If not, the value is added to the player’s score, and the string added to both his/her hand, and the Used array. 

The Opponent has a minimal amount of AI in it’s turn method, and it simply functions depending on it’s score. However, I seem to have encountered a small problem. At the beginning of a game, both the opponent object and the player object run the Draw() method twice, so they begin with a hand of two cards. However, the program seems to freeze after one run of the Draw() object. If I create a breakpoint here, and step through the code, however, it runs smoothly! I have been unable to solve this problem thus far and it is halting development. I have appealed for some help from the friendly folk at the Chaos Rift (see links). Anyway, thats all for now, more updates soon! 


21st September 2010: 

After a friendly reply on the Chaos Rift forums, a solution to my problem was found. A very basic mistake it turns out, involving an infinite loop in the Draw() method. But then again, this is all supposed to be a learning experience for me, and it is doing just that. I mentioned before that I used an array of 52 values to store used cards, I have since discovered that I can use the List collection in the .NET framework in order to store string values, and use the .Contains() method to check whether or not a string is in the list. This cuts out the need for a for loop to check over every value in the array. 

The main loop appears to be functioning well thus far, all of the data is being passed around correctly. Now I just have to play around with the flow control a little (especially regarding the turn order and opponent’s turn) to get the program to function correctly, clean up the output, and I’m well on my way to completing the project. Stay tuned for updates! 


30th September 2010: 

So, after some setbacks and rather a lot of recent work and other events, I feel I am now ready to mark this small starter project as complete. It may not have full functionality, but the basics of the game loop appear to be working. In order for me to progress with my learning, however, I feel I need to push forward with my book and delve into something a bit more complex. After sorting out a few logical issues, and cleaning up the output a little, I’m done with this project. I will be adding the code in the source file below, for those interested. Also, I will upload the .exe file itself. Hoping to move onto bigger things soon, stay tuned!



Source: http://www.mediafire.com/?mv1ziwmehmujq4v

Application: http://www.mediafire.com/?9of0he7lo6zj9qe (Unzip and install)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: