Cards.dll supports Windows comes with games such as Solitaire. If we know how to use the API functions in Cards.dll, we can draw a playing card like a game comes with Windows. We need to use three basic functions: CDTINIT, CDTDRAWEXT, and CDTTERM. And need two variables: Width and Height are used to initialize function CDTITINIT for initialization. The declarations and parameter descriptions of these interface functions are given below.
Private width as integer = 0private height as integer = 0
Declare function cdtinit lib "cards.dll" (Byref Width AS Integer) AS Boolean
Parameter Description: Width, Height return card default width and high, 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 (HDC (HDL to a Device Context) handle;
X, Y specifies the upper left corner of the card; DX, DY specifies the bandwidth and high; Card needs to draw the card, 0-51 [A (grass flower, square, red peach, black peach), 2, ..., k]; 53 -65 card back; Mode specifies how to draw, the card is 0, and the card is 1; color specifies the background color. Declare Sub CdTTERM LIB "Cards.dll" ()
No parameters.
We need to call CDTIT in the game to initialize Cards.dll, so we can use the CDTDRAWEX in Cards.dll; each draw a card, we must call a cdddrawext function; when we end the game, call once CDTTERM to end the use of Cards.dll. Second, the algorithm of the game rules
Twenty-one game is the player to get the sum of the sum of points than the Director, but the number of points is more than twenty-one points, which is a burst, and outputs the bet. J, q, k calculate 10 points, A can be 1 or 11 points, and the rest is based on the number of faces. "BlackJack" is composed of one A and J, Q, K or 10. At the beginning, each person sends two cards, a bright, a dark, all the number of points is less than 21 points, you can choose the blogger. If the first two cards are pairs to choose a split.
For simplicity, only two players in DEALER and Player, inventive cards, no betting processes, do not record losers, and double-oriented. In the twenty-one game, a card only has four attributes description: Face brand size, Suit brand color, count points, whether the FaceUp is up. Therefore, here we don't have to use a Card structure with a CARD.
Structure Card
Public Face AS INTEGER PUBLIC FACE
Public suit as integer
Public Count as INTEGERPUBLIC FACEUP As Boolean
End structure
When the game starts, we must first take a pair, then wash the card, specify starting from the first few cards. When the shuffle is a real random number, use my.computer.clock.tickcount to produce a random seed.
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 13 * i) .FACE = J DECK (J 13 * i). Suit = i IF j < 10 THEN DECK (J 13 * i) .count = j 1 else deck (j 13 * i) .count = 10 end if deck (j 13 * i) .Faceup = false Next Nextend Sub
Private subshuffle () 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) () * 52), INTEGER TC = Deck (i) deck (i) = deck (j) deck (j) = tc next topcard = 0nd Sub
In the game interface, we set three command buttons, two labels. Button1 is "licensing", butTon2 is "To Brand", butTon3 is "suspended". Label1 records the number of points of the detail, Label2 records the number of players. During the game, if a panel is sent, you will immediately redraw a pair, and the message dialog is popped up. The three buttons are listed below Click Event Code. Among them, in the process of the game, the game skills have not been used for simplification.
DIM PlayerCount AS Integer = 0dim Playerace AS Integer = 0dim DealerCount AS Integer = 0dim Dealerace As INTEGER = 0DIM IPCARD, IDCARD AS INTEGER
Private sub delay (byval dt as integer) DIM T as integer t = my.computer.clock.tickcount do if my.computer.clock.tickcount> = T DT THEN EXIT DO LOOPEND SUB
Private sub button1_click (byvale 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, 200, 200, 75, 100, (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> = 52 Then shuffle (): msgbox ("New deck!") Label2.text = PlayerCount.toString label2. Refresh () delay (1000)
CDTDRAWEXT (MyBase.creategraphics.GetHDC, 200, 10, 75, 100, (DECK (Topcard) .FACE * 4 Deck (TopCard). Suit), 0, 0)
DealerCount = Deck (Topcard) .count
IF deck (TopCard) .face = 0 Then DealerCount = 10: dealerage = 1 TopCard = 1 if TopCard> = 52 Then shuffle (): msgbox ("new deck!") Label1.text = dearerCount.toString label1. Refresh () delay (1000)
CDTDRAWEXT (MyBase.creategraphics.getHDC, 220, 200, 75, 100, (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> = 52 Then shuffle (): msgbox ("new deck!") Label2.text = Playercount .Tostring label2.refresh () Delay (1000)
CDTDRAWEXT (MyBase.creategraphics.GetHDC, 220, 10, 75, 100, (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> = 52 Then shuffle (): msgbox ("new deck!") Label1.text = dearercount .ToString Label1.Refresh () delay (1000) ipcard = 2 idcard = 2 Button2.Visible = True Button3.Visible = TrueEnd SubPrivate Sub Button2_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Cdtdrawext (MyBase.creategraphics.GetHDC, 200 20 * IPCard, 200, 75, 100, (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> = 52 Then shuffle (): msgbox ("New deck!") Ipcard = 1 label2.text = Playercount .Tostring label2.refresh ()
If playerCount> 21 Then If playerAce> = 1 Then playerCount - = 10 playerAce - ( "! Player loss") = 1 Label2.Text = playerCount.ToString Label2.Refresh () Else MsgBox Button1.Visible = True Button2.Visible = False Button3.visible = false end if end ifend sub
Private sub Button3_Click (Byval E AS System.EventArgs) Handles Button3.Click Button2.Visible = false Button3.Visible = false dealerplay () end SUB
Private sub dealerplay () do if dearercount <17 TEN CDTDRAWEXT (MyBase.creategraphics.getHDC, 200 20 * IDCard, 10, 75, 100, (Deck (Topcard) .Face * 4 Deck (TopCard). Suit), 0 , 0) DealerCount = Deck (TOPCARD) .count if dearercount> 21 and dealerace = 1 Then dearerCount - = 10: dealerace - = 1 if decount
Button1.visible = trueButton2.visible = falsebutton3.visible = false
End Sub