Writing poker games with Visual Basic.NET

Source: Internet
Author: User
Tags exit count functions integer interface net shuffle tostring
Visual

There are a variety of poker games, the same game around the play is also different. Programming enthusiasts like to write some local play poker game. So where do you start to write your own poker game?

Poker game Programming Key has two points: first, the poker face of the drawing, the second is the rules of the Game of Poker algorithm implementation. Beginner Poker programming enthusiasts can start with some simple games and borrow some of the available resources. This article intends to use the Cards.dll and simple 21-point game of Windows as an example to introduce the initial method of poker game programming.

One, Poker face drawing

Cards.dll supports windows with its own games, such as Solitaire (card games). If we know how to use the API functions in Cards.dll, then we can draw poker faces like Windows ' own games. We need to use three of these basic functions: Cdtinit, Cdtdrawext, and Cdtterm. and requires two variables: Width and height are used to initialize the function Cdtinit. The statement of these interface functions and the parameter description are given below.

Private width as Integer = 0
Private height as Integer = 0

Declare Function cdtinit Lib "Cards.dll" (ByRef width as Integer, _
ByRef height as Integer) as Boolean

Parameter description: Width,height return card default width and height, unit is pixels.

Declare Function cdtdrawext Lib "Cards.dll" (ByVal hdc as INTPTR, _
ByVal x As Integer, ByVal y As Integer, ByVal dx As Integer, _
ByVal dy As Integer, ByVal card As Integer, _
ByVal mode as Integer, ByVal color as Long) as Boolean

Parameter description: HDC (handle to a device context) handle;

X,y the coordinate position of the upper left corner of the specified card;
Dx,dy the specified card width and height;
Card need to draw cards, 0-51[a (grass flowers, squares, hearts, Spades), 2,...,k];53-65 card back;
mode specifies the way to draw, the card facing up to 0, the card facing the next 1;
color specifies the background color.
 
Declare Sub cdtterm Lib "Cards.dll" ()

No parameters.

We need to call Cdtinit at the beginning of the game to initialize the Cards.dll so that we can use functions such as Cdtdrawex in Cards.dll; each time we draw a card, we have to adjust the cdtdrawext function; When we end the game, we call it once cdtterm to end it. The use of S.dll.

Second, the game Rules of algorithm implementation

21-point game is the player to obtain a larger than the number of points combined, but points more than 21 points is a burst card, and lost the injection code. J, Q, K calculate 10 points, a can be counted 1 or 11 points, the rest according to the value of the card count points. "Blackjack" is made up of A and J, Q, K or 10. At the beginning, each issued two cards, a Ming, a dark, where points less than 21 points, you can choose Bo Card. If the first two cards are pairs, you can select a separate card.

For simplicity, there are only two players in the program, dealer and player, who invent cards, have no betting process, do not record winning or losing, do not support licensing and doubling. 21 Point game, a card as long as there are four attribute description: Face cards, suit face, count points, Faceup board is up. Therefore, here we do not use the card class and the card structure.

Structure Card

Public face as Integer

Public suit as Integer

Public count as Integer

Public Faceup as Boolean

End Structure

At the beginning of the game, we first have to take a deck of cards, and then wash the cards, specify the starting from the first few card. In order to obtain the real random number, the My.Computer.Clock.TickCount is used as a seed to produce random numbers.

Dim Deck () as Card

Deck = New Card (51) {}

Dim Topcard as Integer

Private Sub Getdeck ()
Dim I, J as Integer
For i = 0 to 3
For j = 0 to 12
Deck (j + * i). face = J
Deck (j + * i). Suit = I
If J < Ten Then
Deck (j + * i). Count = j + 1
Else
Deck (j + * i). Count = 10
End If
Deck (j + * i). Faceup = False
Next
Next
End Sub

Private Sub Shuffle ()
Dim I, J, K as Integer
Dim TC as Card
For k = 1 to 500
i = CType (My.Computer.Clock.TickCount * RND (), Integer) Mod 52
j = CType (Int (Rnd () *), Integer)
TC = Deck (i)
Deck (i) = Deck (j)
Deck (j) = TC
Next
Topcard = 0
End Sub

In the game interface, we set three command buttons, two tabs. Button1 for "Licensing", Button2 for "to license", Button3 for "suspension." Label1 record the number of dealer points, Label2 record player points. In the course of the game, if a deck is finished, immediately wash a deck of cards, and pop-up message dialog box to inform. The following is a list of three button click event Codes. One of the game makers in the process, for the sake of simplification, never use game skills.

Dim Playercount as Integer = 0
Dim playerace as Integer = 0
Dim Dealercount as Integer = 0
Dim dealerace as Integer = 0
Dim Ipcard, Idcard as Integer

Private Sub Delay (ByVal DT as Integer)
Dim T as Integer
t = My.Computer.Clock.TickCount
Todo
If My.Computer.Clock.TickCount >= t + dt Then Exit do
Loop
End Sub

Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click

Button1.visible = False
Label1.text= ""
Label2.text= ""
Label1.refresh ()
Label2.refresh ()
MyBase.CreateGraphics.Clear (Color.darkgreen)
Dealerace = 0
Playerace = 0
Dealercount = 0
Playercount = 0

Cdtdrawext (MYBASE.CREATEGRAPHICS.GETHDC, Deck (topcard). Face * 4 + Deck (topcard). Suit), 0, 0)

Playercount + = Deck (topcard). Count

If Deck (topcard). face = 0 Then Playercount + = 10:playerace + 1
Topcard + 1
If topcard >= Then Shuffle (): MsgBox ("NEW deck!")
Label2.Text = playercount.tostring
Label2.refresh ()
Delay (1000)

Cdtdrawext (MYBASE.CREATEGRAPHICS.GETHDC, Deck (topcard). Face * 4 + Deck (topcard). Suit), 0, 0)

Dealercount + = Deck (topcard). Count

If Deck (topcard). face = 0 Then Dealercount + = 10:dealerace + 1
Topcard + 1
If topcard >= Then Shuffle (): MsgBox ("NEW deck!")
Label1.Text = dealercount.tostring
Label1.refresh ()
Delay (1000)

Cdtdrawext (MYBASE.CREATEGRAPHICS.GETHDC, Deck (topcard). Face * 4 + Deck (topcard). Suit), 0, 0)

Playercount + = Deck (topcard). Count

If Deck (topcard). face = 0 and playerace = 0 Then Playercount + = 10:playerace + 1
Topcard + 1
If topcard >= Then Shuffle (): MsgBox ("NEW deck!")
Label2.Text = playercount.tostring
Label2.refresh ()
Delay (1000)

Cdtdrawext (MYBASE.CREATEGRAPHICS.GETHDC, Deck (topcard). Face * 4 + Deck (topcard). Suit), 0, 0)

Dealercount + = Deck (topcard). Count

If Deck (topcard). face = 0 and dealerace = 0 Then Dealercount + = 10:dealerace + 1
Topcard + 1
If topcard >= Then Shuffle (): MsgBox ("NEW deck!")
Label1.Text = dealercount.tostring
Label1.refresh ()
Delay (1000)
Ipcard = 2
Idcard = 2
Button2.visible = True
Button3.visible = True
End Sub

Private Sub button2_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button2.click

Cdtdrawext (MYBASE.CREATEGRAPHICS.GETHDC, Ipcard, Deck (topcard). Face * 4 + Deck (topcard). Suit), 0 , 0)

Playercount + = Deck (topcard). Count

If Deck (topcard). face = 0 Then Playercount + = 10:playerace + 1
Topcard + 1
If topcard >= Then Shuffle (): MsgBox ("NEW deck!")
Ipcard + 1
Label2.Text = playercount.tostring
Label2.refresh ()

If playercount > Then
If playerace >= 1 Then
Playercount-= 10
Playerace-= 1
Label2.Text = playercount.tostring
Label2.refresh ()
Else
MsgBox ("Player loss!")
Button1.visible = True
Button2.visible = False
Button3.visible = False
End If
End If
End Sub

Private Sub button3_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button3.click
Button2.visible = False
Button3.visible = False
Dealerplay ()
End Sub

Private Sub Dealerplay ()
Todo
If Dealercount < Then
Cdtdrawext (MYBASE.CREATEGRAPHICS.GETHDC, Idcard, Deck (topcard). Face * 4 + Deck (topcard). Suit), 0, 0)
Dealercount + = Deck (topcard). Count
If dealercount > dealerace = 1 Then dealercount-= 10:dealerace-= 1
If Deck (topcard). face = 0 and Dealercount <= Then Dealercount + 10
Topcard + 1
If topcard >= Then Shuffle (): MsgBox ("NEW deck!")
Idcard + 1
Else
Exit do
End If
Loop

Label1.Text = dealercount.tostring
Label1.refresh ()
If Dealercount <= Then
If playercount > Dealercount Then
MsgBox ("Player win!")
Else
MsgBox ("Dealer win!")
End If
Else
MsgBox ("Player win!")
End If

Button1.visible = True
Button2.visible = False
Button3.visible = False

End Sub

The results of the operation are shown in the following illustration:

Iii. Practice and improvement

In the above programming, we use the structure description card, to the card's face value (a,2,...,k) and suit value (Club,diamond,heart,spade) with values 0-12 and 0-3 for the expression. The rules of the game have also been simplified, with only two players and no description of player attributes (such as wealth, betting, licensed, licensed points, etc.). The practice shows that the better method is to use the enumerated data with card, player class, face and suit. These, we can incrementally add the perfect in programming.

With the deepening of programming practice, our experience will be enriched. How to write a series of classes to support a variety of games (including upgrades, landlords, etc. need to use the size of the trump card game)? How do I record a player's score? How do I support the network? How to deal with players leaving in online games? Mailroom After one months, two months, a year, two years of practice, you will become an expert.



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.