📄 frmcardgamemain.vb
字号:
Public Class frmCardGameMain
Dim rm As Resources.ResourceManager
Private Structure card
Public face As Integer
Public suit As Integer
Public count As Integer
Public faceup As Boolean
End Structure
Private Enum CardKind
Club = 0 ' 梅花
Diamond = 1 ' 方块
Heart = 2 ' 红心
Spade = 3 ' 黑桃
End Enum
Dim Deck() As card = New card(51) {}
Dim TopCard As Integer
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 GetDeck()
Dim i, j As Integer
For i = 0 To 3
For j = 0 To 12
Deck(j + 13 * i).face = j + 1
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
Next
End Sub
Private Sub Shuffle()
Dim i, j, k As Integer
Dim tc As card
For k = 1 To 500
i = CType(Environment.TickCount * Rnd(), Integer) Mod 52
j = CType(Int(Rnd() * 52), Integer)
tc = Deck(i)
Deck(i) = Deck(j)
Deck(j) = tc
Next
TopCard = 0
End Sub
Private Sub Delay(ByVal dt As Integer)
Dim t As Integer
t = Environment.TickCount
Do
If Environment.TickCount >= t + dt Then Exit Do
Loop
End Sub
Private Sub dealerPlay()
Dim bmpCardFace As Bitmap
Do
If dealerCount < 17 Then
bmpCardFace = CType(rm.GetObject("bmp" & CStr((Deck(TopCard).face + Deck(TopCard).suit * 13))), System.Drawing.Bitmap)
MyBase.CreateGraphics.DrawImage(bmpCardFace, 35 + 20 * idcard, 12, New Rectangle(0, 0, 71, 96), GraphicsUnit.Pixel)
dealerCount += Deck(TopCard).count
If dealerCount > 21 And dealerAce = 1 Then dealerCount -= 10 : dealerAce -= 1
If Deck(TopCard).face = 0 And dealerCount <= 11 Then dealerCount += 10
TopCard += 1
If TopCard >= 52 Then Shuffle() : lblMsg.Text = "牌已发完,新启一副牌"
idcard += 1
Else
Exit Do
End If
Loop
If dealerCount <= 21 Then
If playerCount > dealerCount Then
lblMsg.Text = "玩家赢!"
Else
lblMsg.Text = "电脑赢!"
End If
Else
lblMsg.Text = "玩家赢!"
End If
miSendCard.Enabled = True
miGetCard.Enabled = False
miStop.Enabled = False
End Sub
Private Sub miSendCard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles miSendCard.Click
MyBase.CreateGraphics.Clear(Color.DarkGreen)
lblMsg.Text = String.Empty
lblMsg.Refresh()
Dim bmpCardFace As Bitmap
miSendCard.Enabled = False
Label1.Text = "玩家:0点"
Label2.Text = "电脑:0点"
dealerAce = 0
playerAce = 0
dealerCount = 0
playerCount = 0
' 发玩家第一张牌
bmpCardFace = CType(rm.GetObject("bmp" & CStr((Deck(TopCard).face + Deck(TopCard).suit * 13))), System.Drawing.Bitmap)
MyBase.CreateGraphics.DrawImage(bmpCardFace, 35, 12, New Rectangle(0, 0, 71, 96), GraphicsUnit.Pixel)
playerCount += Deck(TopCard).count
If Deck(TopCard).face = 0 Then playerCount += 10 : playerAce += 1
TopCard += 1
If TopCard >= 52 Then Shuffle() : lblMsg.Text = "牌已发完,新启一副牌"
Label1.Text = "玩家:" & playerCount.ToString & "点"
Label1.Refresh()
Label2.Text = "电脑:" & dealerCount.ToString & "点"
Label2.Refresh()
Delay(500)
' 发电脑第一张牌
bmpCardFace = CType(rm.GetObject("bmp" & CStr((Deck(TopCard).face + Deck(TopCard).suit * 13))), System.Drawing.Bitmap)
MyBase.CreateGraphics.DrawImage(bmpCardFace, 35, 134, New Rectangle(0, 0, 71, 96), GraphicsUnit.Pixel)
dealerCount += Deck(TopCard).count
If Deck(TopCard).face = 0 Then dealerCount += 10 : dealerAce += 1
TopCard += 1
If TopCard >= 52 Then Shuffle() : lblMsg.Text = "牌已发完,新启一副牌"
Label1.Text = "玩家:" & playerCount.ToString & "点"
Label1.Refresh()
Label2.Text = "电脑:" & dealerCount.ToString & "点"
Label2.Refresh()
Delay(500)
' 发玩家第二张牌
bmpCardFace = CType(rm.GetObject("bmp" & CStr((Deck(TopCard).face + Deck(TopCard).suit * 13))), System.Drawing.Bitmap)
MyBase.CreateGraphics.DrawImage(bmpCardFace, 55, 12, New Rectangle(0, 0, 71, 96), GraphicsUnit.Pixel)
playerCount += Deck(TopCard).count
If Deck(TopCard).face = 0 And playerAce = 0 Then playerCount += 10 : playerAce += 1
TopCard += 1
If TopCard >= 52 Then Shuffle() : lblMsg.Text = "牌已发完,新启一副牌"
Label1.Text = "玩家:" & playerCount.ToString & "点"
Label1.Refresh()
Label2.Text = "电脑:" & dealerCount.ToString & "点"
Label2.Refresh()
Delay(500)
' 发电脑第二张牌
bmpCardFace = CType(rm.GetObject("bmp" & CStr((Deck(TopCard).face + Deck(TopCard).suit * 13))), System.Drawing.Bitmap)
MyBase.CreateGraphics.DrawImage(bmpCardFace, 55, 134, New Rectangle(0, 0, 71, 96), GraphicsUnit.Pixel)
dealerCount += Deck(TopCard).count
If Deck(TopCard).face = 0 And dealerAce = 0 Then dealerCount += 10 : dealerAce += 1
TopCard += 1
If TopCard >= 52 Then Shuffle() : lblMsg.Text = "牌已发完,新启一副牌"
Label1.Text = "玩家:" & playerCount.ToString & "点"
Label1.Refresh()
Label2.Text = "电脑:" & dealerCount.ToString & "点"
Label2.Refresh()
Delay(500)
ipcard = 2
idcard = 2
miGetCard.Enabled = True
miStop.Enabled = True
End Sub
Private Sub miGetCard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles miGetCard.Click
Dim bmpCardFace As Bitmap
bmpCardFace = CType(rm.GetObject("bmp" & CStr((Deck(TopCard).face + Deck(TopCard).suit * 13))), System.Drawing.Bitmap)
MyBase.CreateGraphics.DrawImage(bmpCardFace, 35 + 20 * ipcard, 12, New Rectangle(0, 0, 71, 96), GraphicsUnit.Pixel)
playerCount += Deck(TopCard).count
If Deck(TopCard).face = 0 Then playerCount += 10 : playerAce += 1
TopCard += 1
If TopCard >= 52 Then Shuffle() : lblMsg.Text = "牌已发完,新启一副牌"
ipcard += 1
Label1.Text = "玩家:" & playerCount.ToString & "点"
Label1.Refresh()
Label2.Text = "电脑:" & dealerCount.ToString & "点"
Label2.Refresh()
If playerCount > 21 Then
If playerAce >= 1 Then
playerCount -= 10
playerAce -= 1
Label2.Text = playerCount.ToString
Else
lblMsg.Text = "电脑赢!"
miSendCard.Enabled = True
miGetCard.Enabled = False
miStop.Enabled = False
End If
ElseIf playerCount = 21 Then
lblMsg.Text = "玩家赢!"
miSendCard.Enabled = True
miGetCard.Enabled = False
miStop.Enabled = False
End If
End Sub
Private Sub miStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles miStop.Click
miGetCard.Enabled = False
miStop.Enabled = False
dealerPlay()
End Sub
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
miSendCard.Enabled = True
miGetCard.Enabled = False
miStop.Enabled = False
rm = New Resources.ResourceManager("CardGameDemo.Resources", GetType(My.Resources.Resources).Assembly)
GetDeck() ' 取一副牌
Shuffle() ' 洗牌
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -