科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件用Visual Basic.NET编写扑克牌游戏

用Visual Basic.NET编写扑克牌游戏

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

本文拟借用Windows自带的Cards.dll和简单的21点游戏为例,介绍扑克游戏编程的初步方法

作者:朱罗海 来源:天极网 2007年11月9日

关键字:

  • 评论
  • 分享微博
  • 分享邮件
二、 游戏规则的算法实现

  二十一点游戏是玩家要取得比庄家更大的点数总和,但点数超过二十一点即为爆牌,并输掉注码。J、Q、K算10点,A可算1点或11点,其余按牌面值计点数。“BlackJack”是由一张A和J、Q、K或10所组成。开始时每人发两张牌,一张明,一张暗,凡点数不足二十一点,可选择博牌。如果首两张牌是对子可选择分牌。

  为简化起见,程序中只有两个玩家Dealer和Player,都发明牌,无下注过程,不记录输赢,不支持分牌和加倍等。二十一点游戏中,一张牌只要有四个属性说明:Face牌面大小、Suit牌面花色,Count点数,FaceUp牌面是否向上。因此,这里我们不用Card 类而用Card结构。

Structure card

Public face As Integer

Public suit As Integer

Public count As Integer

Public faceup As Boolean

End Structure

  游戏开始时,我们首先要取一副牌,然后将牌洗好,指定从第几张牌开始发起。洗牌时为取得真正的随机数,用My.Computer.Clock.TickCount作产生随机数的种子。

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
 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() * 52), Integer)
  tc = Deck(i)
  Deck(i) = Deck(j)
  Deck(j) = tc
 Next
 topcard = 0
End Sub


  游戏界面中,我们设置三个命令按钮,两个标签。Button1为“发牌”、Button2为“要牌”、Button3为“停牌”。Label1记录庄家点数,Label2记录玩家点数。游戏过程中,如果一副牌发完,立即重洗一副牌,并弹出消息对话框告知。以下列出三个按钮单击事件代码。其中庄家游戏过程中,为简化起见,未曾使用游戏技巧。

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
 Do
  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, 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 : dealerAce += 1
 TopCard += 1
 If TopCard >= 52 Then Shuffle() : MsgBox("NEW DECK!")
 Label1.Text = dealerCount.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 = 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, 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 -= 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()
 Do
  If dealerCount < 17 Then
   cdtDrawExt(MyBase.CreateGraphics.GetHdc, 200 + 20 * idcard, 10, 75, 100, (Deck(TopCard).face * 4 + Deck(TopCard).suit), 0, 0)
   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() : MsgBox("NEW DECK!")
  idcard += 1
 Else
  Exit Do
 End If
Loop

Label1.Text = dealerCount.ToString
Label1.Refresh()
If dealerCount <= 21 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

  运行结果如下图所示:


  三、 实践与提高

  上述编程中,我们用了结构描述Card,对Card的Face取值(A,2,…,K)和Suit取值(Club,Diamond,Heart,Spade)用了数值0-12和0-3表示。游戏规则也作了简化,只有两个玩家,也未对玩家属性(如:财富、下注、所持牌、持牌点数等)进行描述。实践表明,较好的方法是用Card、player类,Face和Suit用枚举型数据。这些,我们可以在编程中逐步地添加完善。

  随着编程实践的深入,我们的经验也会随之丰富起来。如何写一系列的类去支持各式游戏(包括升级、斗地主等需要用大小王牌的游戏)?如何记录玩家得分?如何支持网络?如何处理网络游戏中玩家离开?如此等等。经一个月、两个月,一年、两年的实践后,你将成为行家里手。

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章