Home > other >  [translation] blackjack game simulation
[translation] blackjack game simulation

Time:09-25



Introduction to
This project is to provide a blackjack game simulator framework, I think this strategy can help gamblers test they heard, I TouQiao around some places in the process, so there are many place to improve, about 21 points in the rules of the game, you can query, this article
Use the code
Solution contains five components
L BlackJack - GUI part
L Common - framework using a generic class, enums and interface
L Game - responsible for performing the blackjack Game logic part
L Player - very simple computer players
L HumanPlayer - human players simple interface

Component diagram
Common component
Common component contains the following contents:
Deck class - management deck
Hand management of class - the player's hand
IPlayer interface - blackjack player interface
Possible options for the enumeration - players round set
State enumeration - players on the field players state
Player game over state - when the player at the end of the game, win or lose
Game components will use the method of event notification IPlayer
Game component
Game components include:
PlayerStatus class - class to the packing of the Player, the data include the current state of the Player's hand and
Game class - blackjack Game logic and control
PlayerDirectory class - dynamic loading player class

The game is a simple algorithm:
Start a new game
Shuffle
Start a new round of
Two CARDS to each of the players
Asked each state is "play" whether the player a card, if the answer is, so to give them a card, if the answer is not, then changed his status to "standing"
Repeat step 5 know that there is no player is "playing" state
Compare the player's hand according to the rules of the game to determine the winner, updating data and game end state,
Shuffling and stacked card into the card inside the
If it is not the final round, skip to step 3
Algorithm is described in the Round, BeginRound PlayerTurn, GiveCardToPlayer and EndRound method, can support the following events:
OnRoundEnd
OnGameEnd
OnPlayerAdded
OnPlayerRemoved
OnPlayerListCleared
OnDealerChange
Game is running in its own thread and at the end of the round and GUI animation synchronization, at the end of the round, the game thread calls WaitForAnimation and wait for the GUI thread end of the animation called AnimationFinished signal, the StartGame, StopGame PauseGame and ContinueGame are used to control the game
Load and initialize the players
PlayerDirectory class is responsible for loading and initial players,
Other resources through LoadPlayer method loads, in order to create a Player instance, call CreateInstanceOfPlayer method, the statement resource name and structure parameters,
PlayerDirectory. CreateInstanceOfPlayer (" PlayerWithNameAndAge, "
The new object [] {30} "Someone,");
Simple players
A simple player has the following logic:
If I have A black, that I don't move
"I if I have a 21 point, that I don't move
If I hand over 17 points, I still
If I can't hand 17 points, I continue to card.
You can use the following code
Using Common;
The 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 intdealersFirstCard, int numberOfDecks)
{_myHand. Restart (); }

Public PlayerOptions Turn (int left)
{
If (_myHand IsBlackJack | | _myHand. Values. The Contains (21))
Return PlayerOptions. Stand;

Return _myHand. Values [0] <=
17? PlayerOptions. Hit: PlayerOptions. Stand;
}

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

Public voidEndRound (PlayerEndGameStatus status,
The List dealedCards) {}

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

Public MyPlayer () {_name="strange name +"
GetHashCode (), ToString (); }
}
}
Framework
Framework provides a simple way to test the logic of the player, all don't need to code has been removed
Additional information
As I said, there are a lot of improve space (such as the card should be represented by a class rather than an integer, animation effects can be improved), and some rules of 21 points has not been implemented, such as the player's segmentation options
Interested in point
My roommate to do a clever player with some interest, and did some test, I hope you will like it,
  • Related