如何让Visual Basic骰子游戏的胜负计数器显示正确数值
Craps骰子游戏胜负计数问题修复
问题描述
我开发了一款Craps骰子游戏,规则如下:
- 点击Play按钮掷骰子,若首次掷出胜负数字则直接提示结果;
- 若未分胜负,可点击Roll按钮继续掷骰子,直到与首次掷出的点数匹配(获胜)或掷出7(失败)。
目前使用if else语句统计胜负次数并显示到Label控件,但仅在首次点击Play按钮获胜时计数更新,多次点击Roll按钮获胜/失败时计数无变化,相关Visual Basic代码如下:
Imports System.IO Public Class CrapsGame ' die roll constants Enum DiceNames SNAKE_EYES = 2 TREY = 3 CRAPS = 7 LUCKY_SEVEN = 7 YO_LEVEL = 11 BOX_CARS = 12 End Enum ' file name and directory constants Const FILE_PREFIX As String = "/images/die" Const FILE_SUFFEX As String = ".png" ' instance variables Dim myPoint As Integer = 0 Dim randomobject As New Random() Dim winCount As Integer Dim lossCount As Integer ' begin new game and determine point Private Sub PlayButton_Click(sender As Object, e As EventArgs) Handles PlayButton.Click 'intialize variables for new game myPoint = 0 PointBox.Text = "Point" OutputLabel.Text = " " ' remove point die images PointDie1PictureBox.Image = Nothing PointDie2PictureBox.Image = Nothing Dim Sum As Integer = RollDice() ' roll dice 'check die roll Select Case Sum ' win on first roll Case DiceNames.LUCKY_SEVEN, DiceNames.YO_LEVEL OutputLabel.Text = "you win!!" ' lose on first roll Case DiceNames.SNAKE_EYES, DiceNames.TREY, DiceNames.BOX_CARS OutputLabel.Text = "sorry you lose." Case Else ' player must match point myPoint = Sum PointBox.Text = "Point is " & Sum OutputLabel.Text = "Roll again!" PointDie1PictureBox.Image = Die1PictureBox.Image PointDie2PictureBox.Image = Die2PictureBox.Image PlayButton.Enabled = False ' disable Play Button RollButton.Enabled = True ' enable Roll Button End Select ' sum If (Sum = myPoint Or Sum = DiceNames.CRAPS) Then If OutputLabel.Text = "you win!!" Then winCount = winCount + 1 Else lossCount = lossCount + 1 End If Label1.Text = "Win Count - " & winCount.ToString() + Environment.NewLine + "Loss Count - " & lossCount.ToString() + Environment.NewLine End If End Sub ' playbutton click ' determine outcome of next roll Private Sub rollButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RollButton.Click Dim sum As Integer = RollDice() ' roll dice ' determine outcome of roll If sum = myPoint Then ' player matches point OutputLabel.Text = "You win!!!" RollButton.Enabled = False ' disable Roll Button PlayButton.Enabled = True ' enable Play Button ElseIf sum = DiceNames.CRAPS Then ' player loses OutputLabel.Text = "Sorry, you lose." RollButton.Enabled = False ' disable Roll Button PlayButton.Enabled = True ' enable Play Button End If End Sub ' rollButton_Click ' generate random die rolls Function RollDice() As Integer ' roll the dice Dim die1 As Integer = randomobject.Next(1, 7) Dim die2 As Integer = randomobject.Next(1, 7) ' display image corresponding to each die DisplayDie(Die1PictureBox, die1) DisplayDie(Die2PictureBox, die2) Return (die1 + die2) ' return sum of dice values End Function ' RollDice ' display die image Sub DisplayDie(ByVal die As PictureBox, ByVal face As Integer) ' assign die images to PictureBox die.Image = Image.FromFile(Directory.GetCurrentDirectory & FILE_PREFIX & face & FILE_SUFFEX) End Sub ' DisplayDie End Class
问题分析
- RollButton_Click事件无计数逻辑:当前仅在PlayButton_Click里更新胜负计数,但Roll按钮触发的胜负完全没有对应的计数更新代码,所以Roll后的胜负不会统计。
- PlayButton_Click里的判断条件错误:首次掷骰子时
myPoint初始值为0,Sum = myPoint永远不会成立,只有Sum = DiceNames.CRAPS(即7)时才会进入判断,但首次失败的情况(如掷出2、3、12)不会触发计数更新,这也是首次失败计数不生效的隐藏问题。
修复方案
- 抽离计数更新逻辑为独立Sub,避免重复代码;
- 在RollButton_Click事件中,当玩家获胜或失败时调用计数更新;
- 修正PlayButton_Click里的判断逻辑,直接根据掷骰结果判断胜负并更新计数。
修改后的完整代码
Imports System.IO Public Class CrapsGame ' die roll constants Enum DiceNames SNAKE_EYES = 2 TREY = 3 CRAPS = 7 LUCKY_SEVEN = 7 YO_LEVEL = 11 BOX_CARS = 12 End Enum ' file name and directory constants Const FILE_PREFIX As String = "/images/die" Const FILE_SUFFEX As String = ".png" ' instance variables Dim myPoint As Integer = 0 Dim randomobject As New Random() Dim winCount As Integer Dim lossCount As Integer ' 抽离的更新计数与Label显示的方法 Private Sub UpdateScore(isWin As Boolean) If isWin Then winCount += 1 Else lossCount += 1 End If Label1.Text = $"Win Count - {winCount}{Environment.NewLine}Loss Count - {lossCount}{Environment.NewLine}" End Sub ' begin new game and determine point Private Sub PlayButton_Click(sender As Object, e As EventArgs) Handles PlayButton.Click 'intialize variables for new game myPoint = 0 PointBox.Text = "Point" OutputLabel.Text = " " ' remove point die images PointDie1PictureBox.Image = Nothing PointDie2PictureBox.Image = Nothing Dim Sum As Integer = RollDice() ' roll dice 'check die roll Select Case Sum ' win on first roll Case DiceNames.LUCKY_SEVEN, DiceNames.YO_LEVEL OutputLabel.Text = "you win!!" UpdateScore(True) ' 更新获胜计数 ' lose on first roll Case DiceNames.SNAKE_EYES, DiceNames.TREY, DiceNames.BOX_CARS OutputLabel.Text = "sorry you lose." UpdateScore(False) ' 更新失败计数 Case Else ' player must match point myPoint = Sum PointBox.Text = "Point is " & Sum OutputLabel.Text = "Roll again!" PointDie1PictureBox.Image = Die1PictureBox.Image PointDie2PictureBox.Image = Die2PictureBox.Image PlayButton.Enabled = False ' disable Play Button RollButton.Enabled = True ' enable Roll Button End Select ' sum End Sub ' playbutton click ' determine outcome of next roll Private Sub rollButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RollButton.Click Dim sum As Integer = RollDice() ' roll dice ' determine outcome of roll If sum = myPoint Then ' player matches point OutputLabel.Text = "You win!!!" UpdateScore(True) ' 更新获胜计数 RollButton.Enabled = False ' disable Roll Button PlayButton.Enabled = True ' enable Play Button ElseIf sum = DiceNames.CRAPS Then ' player loses OutputLabel.Text = "Sorry, you lose." UpdateScore(False) ' 更新失败计数 RollButton.Enabled = False ' disable Roll Button PlayButton.Enabled = True ' enable Play Button End If End Sub ' rollButton_Click ' generate random die rolls Function RollDice() As Integer ' roll the dice Dim die1 As Integer = randomobject.Next(1, 7) Dim die2 As Integer = randomobject.Next(1, 7) ' display image corresponding to each die DisplayDie(Die1PictureBox, die1) DisplayDie(Die2PictureBox, die2) Return (die1 + die2) ' return sum of dice values End Function ' RollDice ' display die image Sub DisplayDie(ByVal die As PictureBox, ByVal face As Integer) ' assign die images to PictureBox die.Image = Image.FromFile(Directory.GetCurrentDirectory & FILE_PREFIX & face & FILE_SUFFEX) End Sub ' DisplayDie End Class
关键修改点说明
- 新增
UpdateScore方法,统一处理胜负计数更新和Label文本设置,减少代码冗余; - 在PlayButton_Click的首次胜负分支直接调用
UpdateScore,替换原来错误的条件判断; - 在RollButton_Click的胜负分支分别调用
UpdateScore,实现Roll后的计数更新; - 使用字符串插值
$"..."优化Label文本的拼接,提升可读性。
内容的提问来源于stack exchange,提问作者Franklin Araujo
相关产品推荐
相关产品推荐

