【问题标题】:logic error in blackjack game - visual basic二十一点游戏中的逻辑错误 - Visual Basic
【发布时间】:2017-10-07 14:41:51
【问题描述】:

我一直在为学校做一个二十一点项目,我遇到了一个我似乎无法追踪的恼人的逻辑错误。当涉及 ace 时,玩家的得分不会正确加起来。我对它进行了编码,以便当玩家抽牌并且他们的分数超过 21 时,ace 的值应该从 11 变为 1。有什么建议吗?总的来说,任何改进我的编码的方法都会很棒。谢谢。

Private Sub ButtonDraw_Click(sender As Object, e As EventArgs) Handles 
ButtonDraw.Click

    counter = counter + 1
    Dim bust As Integer = 21


    ' holds the numeric value for the players cards
    Dim card1 As Integer
    Dim card2 As Integer
    Dim card3 As Integer
    Dim card4 As Integer
    Dim card5 As Integer


    If counter = 1 Then

        'generates a value between 1 and 14 for the card
        card1 = random.Next(2, 15)

        'after getting card value this if statement determines if it is a 
face card and outputs the appropriate face value
        If card1 = 11 Then
            card1 = 10
            LabelCard1.Text = "J"
        ElseIf card1 = 12 Then
            card1 = 10
            LabelCard1.Text = "Q"
        ElseIf card1 = 13 Then
            card1 = 10
            LabelCard1.Text = "K"
        ElseIf card1 = 1 Then
            LabelCard1.Text = "A"
        ElseIf card1 = 14 Then
            card1 = 11
            LabelCard1.Text = "A"
        Else
            LabelCard1.Text = card1
        End If

        'displays the players card
        LabelCard1.Visible = True

        playerScore = playerScore + card1

        'automatically moves to card 2 since you draw 2 cards each game
        counter = counter + 1

    End If

    If counter = 2 Then

        card2 = random.Next(2, 15)

        If card2 = 11 Then
            card2 = 10
            LabelCard2.Text = "J"
        ElseIf card2 = 12 Then
            card2 = 10
            LabelCard2.Text = "Q"
        ElseIf card2 = 13 Then
            card2 = 10
            LabelCard2.Text = "K"
        ElseIf card2 = 1 Then
            LabelCard2.Text = "A"
        ElseIf card2 = 14 Then
            card2 = 11
            LabelCard2.Text = "A"
        Else
            LabelCard2.Text = card2
        End If


        'totals the player score
        playerScore = playerScore + card2

        'enables stay button
        ButtonStay.Enabled = True

        'displays player score in green text for 21, red for bust, and black 
for under
        If playerScore = 21 Then
            LabelPlayerScore.ForeColor = Color.Green
        ElseIf playerScore > 21 Then
            LabelPlayerScore.ForeColor = Color.Red
        Else
            LabelPlayerScore.ForeColor = Color.Black
        End If

        'updates the player score
        LabelPlayerScore.Text = playerScore

        'displays the players card
        LabelCard2.Visible = True



    End If

    If counter = 3 Then

        card3 = random.Next(2, 15)

        If card3 = 11 Then
            card3 = 10
            LabelCard3.Text = "J"
        ElseIf card3 = 12 Then
            card3 = 10
            LabelCard3.Text = "Q"
        ElseIf card3 = 13 Then
            card3 = 10
            LabelCard3.Text = "K"
        ElseIf card3 = 1 Then
            LabelCard3.Text = "A"
        ElseIf card3 = 14 Then
            card3 = 11
            LabelCard3.Text = "A"
        Else
            LabelCard3.Text = card3
        End If

        playerScore = playerScore + card3

        'changes the ace from an 11 to a 1 value if score exceeds 21
        If playerScore > bust Then
            If card1 = 11 Then
                card1 = 1
                playerScore = card1 + card2 + card3
            End If
            If card2 = 11 Then
                card2 = 1
                playerScore = card1 + card2 + card3
            End If
            If card3 = 11 Then
                card3 = 1
                playerScore = card1 + card2 + card3
            End If
        End If



        'changes the color of the players score if 21 or bust
        If playerScore = 21 Then
            LabelPlayerScore.ForeColor = Color.Green
        ElseIf playerScore > 21 Then
            LabelPlayerScore.ForeColor = Color.Red
        ElseIf playerScore < 21 Then
            LabelPlayerScore.ForeColor = Color.Black
        End If

        LabelPlayerScore.Text = playerScore

        LabelCard3.Visible = True

        'if player goes over 21 automatically updates loss counter and 
disables stay button. displays bust msg
        If playerScore > 21 Then
            losses += 1
            LabelLoseCounter.Text = losses
            ButtonStay.Enabled = False
            ButtonDraw.Enabled = False
            ButtonPlayAgain.Enabled = True
            Dim response = MessageBox.Show("You bust!", "Bust", 
MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation)
            If response = 4 Then
                ButtonPlayAgain.PerformClick()
            End If
        End If

    End If

    If counter = 4 Then

        card4 = random.Next(2, 15)

        If card4 = 11 Then
            card4 = 10
            LabelCard4.Text = "J"
        ElseIf card4 = 12 Then
            card4 = 10
            LabelCard4.Text = "Q"
        ElseIf card4 = 13 Then
            card4 = 10
            LabelCard4.Text = "K"
        ElseIf card4 = 1 Then
            LabelCard4.Text = "A"
        ElseIf card4 = 14 Then
            card4 = 11
            LabelCard4.Text = "A"
        Else
            LabelCard4.Text = card4
        End If

        playerScore = playerScore + card4

        'changes the ace from an 11 to a 1 value if score exceeds 21
        If playerScore > bust Then
            If card1 = 11 Then
                card1 = 1
                playerScore = card1 + card2 + card3 + card4
            End If
            If card2 = 11 Then
                card2 = 1
                playerScore = card1 + card2 + card3 + card4
            End If
            If card3 = 11 Then
                card3 = 1
                playerScore = card1 + card2 + card3 + card4
            End If
            If card4 = 11 Then
                card4 = 1
                playerScore = card1 + card2 + card3 + card4
            End If
        End If



        'changes the color of the players score if 21 or bust
        If playerScore = 21 Then
            LabelPlayerScore.ForeColor = Color.Green
        ElseIf playerScore > 21 Then
            LabelPlayerScore.ForeColor = Color.Red
        ElseIf playerScore < 21 Then
            LabelPlayerScore.ForeColor = Color.Black
        End If


        LabelPlayerScore.Text = playerScore

        LabelCard4.Visible = True


        'if player goes over 21 automatically updates loss counter and 
disables stay button
        If playerScore > bust Then
            losses += 1
            LabelLoseCounter.Text = losses
            ButtonStay.Enabled = False
            ButtonDraw.Enabled = False
            ButtonPlayAgain.Enabled = True
            Dim response = MessageBox.Show("You bust!", "Bust", 
MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation)
            If response = 4 Then
                ButtonPlayAgain.PerformClick()
            End If
        End If

    End If

    If counter = 5 Then

        card5 = random.Next(2, 15)

        If card5 = 11 Then
            card5 = 10
            LabelCard5.Text = "J"
        ElseIf card5 = 12 Then
            card5 = 10
            LabelCard5.Text = "Q"
        ElseIf card5 = 13 Then
            card5 = 10
            LabelCard5.Text = "K"
        ElseIf card5 = 1 Then
            LabelCard5.Text = "A"
        ElseIf card5 = 14 Then
            card5 = 11
            LabelCard5.Text = "A"
        Else
            LabelCard5.Text = card5
        End If

        playerScore = playerScore + card5


        'changes the ace from an 11 to a 1 value if score exceeds 21
        If playerScore > bust Then
            If card1 = 11 Then
                card1 = 1
                playerScore = card1 + card2 + card3 + card4 + card5
            End If
            If card2 = 11 Then
                card2 = 1
                playerScore = card1 + card2 + card3 + card4 + card5
            End If
            If card3 = 11 Then
                card3 = 1
                playerScore = card1 + card2 + card3 + card4 + card5
            End If
            If card4 = 11 Then
                card4 = 1
                playerScore = card1 + card2 + card3 + card4 + card5
            End If
            If card5 = 11 Then
                card5 = 1
                playerScore = card1 + card2 + card3 + card4 + card5
            End If
        End If


        'changes the color of the players score if 21 or bust
        If playerScore = 21 Then
            LabelPlayerScore.ForeColor = Color.Green
        ElseIf playerScore > 21 Then
            LabelPlayerScore.ForeColor = Color.Red
        Else
            LabelPlayerScore.ForeColor = Color.Black
        End If

        LabelPlayerScore.Text = playerScore

        LabelCard5.Visible = True

        'if player goes over 21 automatically updates loss counter and 
disables stay button
        If playerScore > 21 Then
            losses += 1
            LabelLoseCounter.Text = losses
            ButtonStay.Enabled = False
            ButtonDraw.Enabled = False
            ButtonPlayAgain.Enabled = True
            Dim response = MessageBox.Show("You bust!", "Bust", 
MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation)
            If response = 4 Then
                ButtonPlayAgain.PerformClick()
            End If
        Else
            wins += 1
            LabelLoseCounter.Text = wins
            ButtonStay.Enabled = False
            ButtonDraw.Enabled = False
            ButtonPlayAgain.Enabled = True
            Dim response = MessageBox.Show("Winner, Winner, Chicken 
Dinner!!", "Winner!", MessageBoxButtons.RetryCancel, 
MessageBoxIcon.Exclamation)
            If response = 4 Then
                ButtonPlayAgain.PerformClick()
            End If
        End If

    End If
End Sub

【问题讨论】:

  • 你有没有逐行调试,看看有什么问题?究竟会发生什么,输入什么等?还可以学习使用函数等对代码进行重复数据删除。当可能只有一个变量时,没有必要让多个代码块使用不同的变量来做同样的事情。
  • 伪代码为if handTotal &lt;= 11 and hand.contains(ace) then actualHandTotal = handTotal + 10
  • 您使用什么调试器来单步调试代码或设置观察点?
  • 我不确定观察点是什么,但我使用的是 Visual Studio 2010。如果我从前 2 张牌中的 ace 开始,则 11 的值永远不会变为 1。如果我从2 ace 我有 22。我不知道我错过了什么。如果玩家手牌超过 21,A 应该会自动改变。

标签: logic blackjack


【解决方案1】:

counter = 2 时你没有playerScore &gt; bust 检查

为了适应你的编码风格,你需要在第 80 行左右添加这样的代码:

   If playerScore > bust Then
        If card1 = 11 Then
            card1 = 1
            playerScore = card1 + card2
        End If
        If card2 = 11 Then
            card2 = 1
            playerScore = card1 + card2
        End If
    End If

与你的意图相比,这可能是你错过的。

这可能不会得到你想要的东西。如果比分被淘汰,并不是所有的ace都必须转换为1分,你在程序逻辑中考虑过吗?

放宽思路,这里写了很多重复的代码。

一步一步地做事,首先要研究的是将卡片存储在数组或列表中,然后使用ForFor Each 对每个卡片执行任务,而不是再次输入相同的代码。

之后的下一步是使用函数或子[例程]来收集通用代码,这样您只需编写一次,但多次调用它即可生成所需的逻辑。

祝你学习编码好运 :-)

【讨论】:

    猜你喜欢
    • 2015-06-02
    • 2023-03-21
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 2020-03-06
    • 2014-10-22
    • 2022-11-12
    相关资源
    最近更新 更多