0
我一直在爲學校的二十一點項目工作,我遇到了一個惱人的邏輯錯誤,我似乎無法追查。當參與一個王牌時,球員得分不會加起來。我編碼它,以便當玩家抽牌並且他們的分數超過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
你有沒有逐行調試它,看看有什麼問題?究竟發生了什麼,什麼輸入等等?還要學會使用函數等去重複代碼。當只有一個代碼塊時,不需要有幾塊代碼只用不同的變量來做同樣的事情。 –
僞代碼將是'if handTotal <= 11,hand.contains(ace)then actualHandTotal = handTotal + 10'。 – jsheeran
您使用什麼調試器來逐步完成代碼或設置觀察點? – lit