Sei sulla pagina 1di 4

Blackjack Game Simulator - CodeProject http://www.codeproject.com/KB/game/Blackjack_Game_Simulator.

aspx

Not quite what you are looking for? You may want to try: ×
SoundPlayer bug - Calling unmanaged APIs
Blackjack - a real world OOD example
highlights off

7,584,974 members and growing! (30,368 online)


Email Password Sign in Join Remember me? Lost password?

Home Articles Questions & Answers Learning Zones Features Help! The Lounge Design a blackjack game.
» Platforms, Frameworks & Libraries » Game Development » Games

Licence CPOL

Blackjack Game Simulator First Posted


Views
24 Aug 2007
25,848
See Also
More like this
By Mladen Jankovic | 24 Aug 2007 Bookmarked 23 times
More by this author

.NET2.0 C#2.0 Windows Visual-Studio Dev WinForms Design Intermediate

Framework for blackjack game simulation

Article Browse Code Stats Revisions 4.00 (8 votes) Sponsored Links

Download demo application - 102.2 KB


Download framework project - 121.0 KB
Download full project - 142.3 KB

See Also...

Announcements

The Daily Insider


Introduction
This project provides a framework for a blackjack game simulator. I don't know if this will be useful to
someone... Well, it could be useful to gamblers for testing tactics they have heard of. I had to do a
presentation, so I hacked something together and in the end, I got this, so it leaves many places for
improvement. For more information about the blackjack game and rules, Wikipedia's article is a nice
place to start.

1 of 4 3/1/2011 8:42 AM
Blackjack Game Simulator - CodeProject http://www.codeproject.com/KB/game/Blackjack_Game_Simulator.aspx

Using the Code


The solution contains five components:

BlackJack - GUI application


Common - Common classes, enums and interfaces used by the framework
Game - Component that takes care of blackjack game logic
Player - Very simple computer player
HumanPlayer - Simple interface for human players

Figure: Component diagram

Common Component

The Common component contains:

Deck class - managing card deck


Hand class - managing player's hand
IPlayer interface - interface for blackjack players
PlayerOptions enum - possible options on player's turn
PlayerInGameStatus enum - current player status (playing or standing)
PlayerEndGameStatus enum - player status at the end of the game (winner, loser...)

IPlayer's methods are called by the Game component to notify the player about events.

Game Component

The Game component contains:

PlayerStatus class - wrapper class for player; data such as current player's hand and current
status are stored in this class to prevent cheating :)
Game class - blackjack game logic and control
PlayerDirectory class - dynamically loads the player's components

Simplified algorithm of the game:

1. Start a new game


2. Shuffle the deck
3. Begin a new round
4. Deal two cards to all players
5. Ask all players with the status "playing" if they want another card: if someone does, give them
another card; if someone doesn't, change his status to "standing"
6. Repeat step 5 while there are players with "playing" status
7. Compare players' hands to declare winner and losers according to the rules of the game, updating
player statistics and end game status
8. Shuffle, deal cards and return them to the end of the deck
9. If this is not the last round, jump to step 3.

The described algorithm is implemented in the Round, BeginRound, PlayerTurn, GiveCardToPlayer


and EndRound methods of the Game class. Game can be observed with events (members of the Game
class):

OnRoundEnd
OnGameEnd
OnPlayerAdded
OnPlayerRemoved
OnPlayerListCleared
OnDealerChange

Game runs in its own thread and has methods for synchronization with GUI animations, which are
played at the end of a round. At round end, the game thread calls WaitForAnimation and waits until
the GUI thread signals that the animation is finished by calling AnimationFinished. Methods

2 of 4 3/1/2011 8:42 AM
Blackjack Game Simulator - CodeProject http://www.codeproject.com/KB/game/Blackjack_Game_Simulator.aspx

StartGame, StopGame, PauseGame and ContinueGame are used to control the game.

Loading and Instantiation of Players

The PlayerDirectory class has the tasks of loading players' components and instantiation of players.
In the class constructor, all assemblies from the .\Players folder are checked and loaded if containing a
player's class. The name of a player class must be MyPlayer and must be in the Player namespace.

Other assemblies can be added by calling the LoadPlayer method and specifying the path to assembly.
To create an instance of a player, just call CreateInstanceOfPlayer, specifying the assembly name
and constructor parameters as needed.

Collapse
playerDirectory.CreateInstanceOfPlayer("PlayerWithNameAndAge",
new object[]{ "Someone", 30 } );

Simple Player

A simple player with simple logic:

"If I have blackjack, I'll stand"


"If I have 21, I'll stand"
"If have more than or equal to 17, I'll stand"
"If I have less than 17, I'll take another"

...can be implemented in a few lines of code.

Collapse
using Common;
namespace Player
{
public class MyPlayer : IPlayer
{
private Hand _myHand = new Hand();

private string _name;


public string Name { get { return _name; } }

public void NewRound(bool newGame,


bool isDealer, int dealersFirstCard, int numberOfDecks)
{ _myHand.Restart(); }

public PlayerOptions Turn(int left)


{
if (_myHand.IsBlackJack || _myHand.Values.Contains(21))
return PlayerOptions.Stand;

return _myHand.Values[0] <=


17 ? PlayerOptions.Hit : PlayerOptions.Stand;
}

public void GiveCard(int card) { _myHand.Add(card); }

public void EndRound(PlayerEndGameStatus status,


List<int> dealedCards) { }

public MyPlayer(string name) { _name = name; }

public MyPlayer() { _name = "strange name " +


GetHashCode().ToString(); }
}
}

Framework Project
The framework project provides an easy way of testing a player's logic with complete setup of
environment and project. All unneeded source code has been stripped.

Additional Information
Like I said, there's room for many improvements (i.e. cards should be presented by class not integer
number, animation code can be improved). Also, some rules from blackjack are not implemented, like
the split option for players.

Points of Interest
Well, my college has had some fun making smarter players and had a little competition. I hope you will
enjoy it, too.

History
24 August, 2007 -- Original version posted

License

3 of 4 3/1/2011 8:42 AM
Blackjack Game Simulator - CodeProject http://www.codeproject.com/KB/game/Blackjack_Game_Simulator.aspx

This article, along with any associated source code and files, is licensed under The Code Project Open
License (CPOL)

About the Author

Mladen Jankovic

Software Developer
Coolsoft Software
Development
Serbia

Member

Sign Up to vote for this article


Article Top

Comments and Discussions


You must Sign In to use this message board. (secure sign-in)
FAQ Search

Noise Tolerance Medium Layout Normal Per page 25 Update

(Refresh)
-- There are no messages in this forum --

link | Privacy | Terms of Use | Mobile Copyright 2007 by Mladen Jankovic


Last Updated: 24 Aug 2007 Everything else Copyright © CodeProject, 1999-2011
Web22 | Advertise on the Code Project

4 of 4 3/1/2011 8:42 AM

Potrebbero piacerti anche