技术开发 频道

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

  

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用枚举型数据。这些,我们可以在编程中逐步地添加完善。
 
  随着编程实践的深入,我们的经验也会随之丰富起来。如何写一系列的类去支持各式游戏(包括升级、斗地主等需要用大小王牌的游戏)?如何记录玩家得分?如何支持网络?如何处理网络游戏中玩家离开?如此等等。经一个月、两个月,一年、两年的实践后,你将成为行家里手。
0
相关文章