二十一点游戏逻辑错误 - 视觉基本
问题描述:
我一直在为学校的二十一点项目工作,我遇到了一个恼人的逻辑错误,我似乎无法追查。当参与一个王牌时,球员得分不会加起来。我编码它,以便当玩家抽牌并且他们的分数超过21时,王牌应该从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
答
你没有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
这可能与你的意图相比你错过了。
虽然这可能不会如你所愿。如果分数太低,不是所有的王牌都必须转换成1分,你是否在程序逻辑中考虑过?
更广泛的思考,这里写了很多重复的代码。
做事按部就班,首先要考虑将存储在数组或列表的卡,然后使用For
或For Each
执行每一个任务,而不是再次输入相同的代码。
接下来的步骤是使用函数或子[例程]来收集通用代码,因此您只需编写一次,但将其调用多次即可生成所需的逻辑。
祝你好运与你学习代码:-)
你有没有逐行调试它,看看有什么问题?究竟发生了什么,什么输入等等?还要学会使用函数等去重复代码。当只有一个代码块时,不需要有几块代码只用不同的变量来做同样的事情。 –
伪代码将是'if handTotal jsheeran
您使用什么调试器来逐步完成代码或设置观察点? – lit