二十一点游戏中的逻辑错误 - 视觉基础

时间:2017-05-09 10:22:37

标签: logic blackjack

我一直在为学校做一个二十一点项目,我遇到了一个令人烦恼的逻辑错误,我似乎无法追查。当涉及到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

1 个答案:

答案 0 :(得分:0)

playerScore > bust

时,您未进行counter = 2检查

为了适应您的编码风格,您需要在第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

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

这可能无法获得您想要的东西。如果得分是破裂的,并非所有的A都必须转换为得分1,你是否考虑过程序逻辑?

思考范围更广,这里写了很多重复的代码。

逐步做事,首先要考虑的是将卡存储在数组或列表中,然后使用ForFor Each在每个卡上执行任务,而不是键入再次使用相同的代码。

之后的下一步是使用函数或子[例程]来收集公共代码,这样你只需要编写一次,但是多次调用它来产生你需要的逻辑。

祝你学习代码好运: - )

相关问题