Introduction to a blackjack game

Source: Internet
Author: User

Demo download

Program Framework download

Game Code download

Introduction:
This project provides a framework for creating a blackjack game. I don't know if it is useful to some people? Well, we can provide testing opportunities for people who love to play this game. First of all, you must briefly introduce some of them. I will put them together later, so there is enough space for function expansion. For details about the rules and related information of the game, see Wikipedia.

Code:
The Code contains five modules:
Blackjack-Gui Program
Common-a common class, called by the framework for enumeration and functions
Game-used to process game logic
Player-players controlled by computers
Humanplayer-player control


Interface: Chart, general information, including:

Deck class-Draw poker
Hand class-manage players
IPlayer interface-player interface
Playeroptions Enum-options in the player round
Playeringamestatus Enum-Current player status (playing or standing)
Playerendgamestatus Enum-player status when the game ends (Win, lose ...)

Game composition:
Game components include:
Playerstatus-package of players; current player data and status.
Game-game logic control
Playerdirectory-dynamically call player Member objects
Single Game algorithm:
Start a new game
Shuffling
Start a new game
Send two cards to all players
Ask if all players want to resume their cards in the "playing" status: If yes, send another card to the player. If not, set the status to "standing ".
Repeat Step 1 for all players in the "playing" status.
The game rules are used to compare the player's card value to determine whether to win or not, and the player status is refreshed to end the game.
Shuffling, playing cards and putting them to the end
If it is not the last round, return to step 1.
The algorithm of the first game is that it is the first game, and it is the turn of the player to issue the game to the player. The final game is completed by the beginround, playerturn, givecardtoplayer, and endround methods of the game class. Games can be monitored by events (game members ):

Onroundend
Ongameend
Onplayeradded
Onplayerremoved
Onplayerlistcleared
Ondealerchange

The game runs on the GUI to synchronize the animation thread, which runs after each Board ends. After each game ends, the game thread calls waitforanimation and waits until the animation ends in the GUI thread. The function animationfinished is called. Startgame, stopgame, pausegame, and continuegame are used to control the game.
Call and player operations:
Playerdirectory class has functions that call player operations and functions. In this class structure, if a player class is included, all files in the./players folder will be detected and called. The name of the player class must be myplayer and must be in the player namespace.
Other files are added to the loadplayer method and specified paths for compilation. To create a player example, you only need to call createinstanceofplayer and specify the Set Name and construction parameters.
Playerdirectory. createinstanceofplayer ("playerwithnameandage ",
New object [] {"someone", 30 });
Simple players
A player uses a simple logic:

 

"Stand if I have Blackjack"
"If there are 21 points, stand"
"If it is at or above, it will be stand"
"If it is less than, you need another one"
These are executed by the following code:
Collapseusing 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 ();}
}
}

Project framework:
The project framework provides a simple method to detect the player's logical environment and project environment. Clear all unwanted resource code.
Additional information:
There are also many improvements (for example, cards can be drawn using classes instead of numbers, and Animation programs need to be Zeng Qiang ). Of course, some game rules can also be added.
Good, my efforts are challenging to make players decide. Hope you like it

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.