我是vb.net编程的新手。我创建了一个彩色游戏,其中有3个按钮,颜色为绿色,黄色和红色,最多3次点击,然后再次重新启动循环。一旦用户按下特定的颜色组合,程序将显示他们是否赢得了大奖奖金,安慰奖或什么都没有。如果相同的颜色被按下3次则会显示累积奖金,如果相同的颜色被按下2次而第3种颜色不同则会显示安慰奖,然后如果所有颜色被按下一次则会显示“抱歉,请再试一次”。我对游戏的逻辑部分有问题,如果我按特定绿色,黄色和红色作为第一组合颜色然后它会显示“抱歉再试一次”,然后当我按绿色或黄色3次作为第二个组合颜色它将显示安慰奖而不是奖金奖金,但如果我尝试红色3次,它将对循环的其余部分无效,除非我再次重新开始游戏但如果我尝试其他组合首先它完全正常工作“绿色,黄色,红色”组合将会让游戏失败。
这是游戏的逻辑:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
i = i + 1
g = g + 1
MessageBox.Show("g:" & g)
MessageBox.Show("i:" & i)
If g = 2 And i = 3 Then
'MessageBox.Show("You won the consolation prize")
My.Computer.Audio.Play(My.Resources.You_win_sound_effect_5, AudioPlayMode.Background)
conwon()
Form2.Show()
i = 0
g = 0
r = 0
y = 0
ElseIf g = 1 And y = 1 And r = 1 And i = 3 Then
'MessageBox.Show("Better luck Next time")
My.Computer.Audio.Play(My.Resources.Fail___sound_effect_, AudioPlayMode.Background)
sorry()
Form2.Show()
i = 0
g = 0
r = 0
y = 0
ElseIf y = 1 And g = 1 And r = 1 And i = 3 Then
'MessageBox.Show("Better luck Next time")
My.Computer.Audio.Play(My.Resources.Fail___sound_effect_, AudioPlayMode.Background)
sorry()
Form2.Show()
i = 0
g = 0
r = 0
y = 0
ElseIf r = 1 And y = 1 And g = 1 And i = 3 Then
'MessageBox.Show("Better luck Next time")
My.Computer.Audio.Play(My.Resources.Fail___sound_effect_, AudioPlayMode.Background)
sorry()
Form2.Show()
i = 0
g = 0
r = 0
y = 0
ElseIf g = 2 And y = 1 Or r = 1 And i = 3 Then
'MessageBox.Show("You won a consolation prize")
My.Computer.Audio.Play(My.Resources.You_win_sound_effect_5, AudioPlayMode.Background)
conwon()
Form2.Show()
i = 0
g = 0
r = 0
y = 0
ElseIf y = 2 And g = 1 Or r = 1 And i = 3 Then
'MessageBox.Show("You won a consolation prize")
My.Computer.Audio.Play(My.Resources.You_win_sound_effect_5, AudioPlayMode.Background)
conwon()
Form2.Show()
i = 0
g = 0
r = 0
y = 0
ElseIf r = 2 And g = 1 Or y = 1 And i = 3 Then
'MessageBox.Show("You won a consolation prize")
My.Computer.Audio.Play(My.Resources.You_win_sound_effect_5, AudioPlayMode.Background)
conwon()
Form2.Show()
i = 0
g = 0
r = 0
y = 0
ElseIf g = 3 And i = 3 Then
'MessageBox.Show("You won the Jackpot prize")
Form3.PictureBox1.Image = My.Resources.jackpot
Form3.Show()
jacwon()
i = 0
g = 0
r = 0
y = 0
ElseIf y = 3 And i = 3 Then
'MessageBox.Show("You won the Jackpot prize")
Form3.PictureBox1.Image = My.Resources.jackpot
Form3.Show()
jacwon()
i = 0
g = 0
r = 0
y = 0
ElseIf r = 3 And i = 3 Then
'MessageBox.Show("You won the Jackpot prize")
Form3.PictureBox1.Image = My.Resources.jackpot
Form3.Show()
jacwon()
i = 0
g = 0
r = 0
y = 0
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
i = i + 1
y = y + 1
MessageBox.Show("y:" & y)
MessageBox.Show("i:" & i)
If y = 2 And i = 3 Then
'MessageBox.Show("You won the consolation prize")
My.Computer.Audio.Play(My.Resources.You_win_sound_effect_5, AudioPlayMode.Background)
conwon()
Form2.Show()
i = 0
g = 0
r = 0
y = 0
ElseIf y = 1 And g = 1 And r = 1 And i = 3 Then
'MessageBox.Show("Better luck Next time)
My.Computer.Audio.Play(My.Resources.Fail___sound_effect_, AudioPlayMode.Background)
sorry()
Form2.Show()
i = 0
g = 0
r = 0
y = 0
ElseIf g = 1 And y = 1 And r = 1 And i = 3 Then
'MessageBox.Show("Better luck Next time")
My.Computer.Audio.Play(My.Resources.Fail___sound_effect_, AudioPlayMode.Background)
sorry()
Form2.Show()
i = 0
g = 0
r = 0
y = 0
ElseIf r = 1 And y = 1 And g = 1 And i = 3 Then
'MessageBox.Show("Better luck Next time")
My.Computer.Audio.Play(My.Resources.Fail___sound_effect_, AudioPlayMode.Background)
sorry()
Form2.Show()
i = 0
g = 0
r = 0
y = 0
ElseIf g = 2 And y = 1 Or r = 1 And i = 3 Then
'MessageBox.Show("You won a consolation prize")
My.Computer.Audio.Play(My.Resources.You_win_sound_effect_5, AudioPlayMode.Background)
conwon()
Form2.Show()
i = 0
g = 0
r = 0
y = 0
ElseIf y = 2 And g = 1 Or r = 1 And i = 3 Then
'MessageBox.Show("You won a consolation prize")
My.Computer.Audio.Play(My.Resources.You_win_sound_effect_5, AudioPlayMode.Background)
conwon()
Form2.Show()
i = 0
g = 0
r = 0
y = 0
ElseIf r = 2 And g = 1 Or y = 1 And i = 3 Then
'MessageBox.Show("You won a consolation prize")
My.Computer.Audio.Play(My.Resources.You_win_sound_effect_5, AudioPlayMode.Background)
conwon()
Form2.Show()
i = 0
g = 0
r = 0
y = 0
ElseIf y = 3 And i = 3 Then
'MessageBox.Show("You won the Jackpot prize")
Form3.PictureBox1.Image = My.Resources.jackpot
Form3.Show()
jacwon()
i = 0
g = 0
r = 0
y = 0
ElseIf g = 3 And i = 3 Then
'MessageBox.Show("You won the Jackpot prize")
Form3.PictureBox1.Image = My.Resources.jackpot
Form3.Show()
jacwon()
i = 0
g = 0
r = 0
y = 0
ElseIf r = 3 And i = 3 Then
'MessageBox.Show("You won the Jackpot prize")
Form3.PictureBox1.Image = My.Resources.jackpot
Form3.Show()
jacwon()
i = 0
g = 0
r = 0
y = 0
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
i = i + 1
r = r + 1
MessageBox.Show("r:" & r)
MessageBox.Show("i:" & i)
If r = 2 And i = 3 Then
'MessageBox.Show("You won the consolation prize")
My.Computer.Audio.Play(My.Resources.You_win_sound_effect_5, AudioPlayMode.Background)
conwon()
Form2.Show()
i = 0
g = 0
r = 0
y = 0
ElseIf r = 1 And g = 1 And y = 1 And i = 3 Then
'MessageBox.Show("Better luck Next time")
My.Computer.Audio.Play(My.Resources.Fail___sound_effect_, AudioPlayMode.Background)
sorry()
Form2.Show()
i = 0
g = 0
y = 0
ElseIf y = 1 And g = 1 And r = 1 And i = 3 Then
MessageBox.Show("Better luck Next time")
My.Computer.Audio.Play(My.Resources.Fail___sound_effect_, AudioPlayMode.Background)
sorry()
Form2.Show()
i = 0
g = 0
r = 0
y = 0
ElseIf g = 1 And y = 1 And r = 1 And i = 3 Then
'MessageBox.Show("Better luck Next time")
My.Computer.Audio.Play(My.Resources.Fail___sound_effect_, AudioPlayMode.Background)
sorry()
Form2.Show()
i = 0
g = 0
r = 0
y = 0
ElseIf g = 2 And y = 1 Or r = 1 And i = 3 Then
'MessageBox.Show("You won a consolation prize")
My.Computer.Audio.Play(My.Resources.You_win_sound_effect_5, AudioPlayMode.Background)
conwon()
Form2.Show()
i = 0
g = 0
r = 0
y = 0
ElseIf y = 2 And g = 1 Or r = 1 And i = 3 Then
'MessageBox.Show("You won a consolation prize")
My.Computer.Audio.Play(My.Resources.You_win_sound_effect_5, AudioPlayMode.Background)
conwon()
Form2.Show()
i = 0
g = 0
r = 0
y = 0
ElseIf r = 2 And g = 1 Or y = 1 And i = 3 Then
'MessageBox.Show("You won a consolation prize")
My.Computer.Audio.Play(My.Resources.You_win_sound_effect_5, AudioPlayMode.Background)
conwon()
Form2.Show()
i = 0
g = 0
r = 0
y = 0
ElseIf r = 3 And i = 3 Then
'MessageBox.Show("You won the Jackpot prize")
Form3.PictureBox1.Image = My.Resources.jackpot
Form3.Show()
jacwon()
i = 0
g = 0
r = 0
y = 0
ElseIf g = 3 And i = 3 Then
'MessageBox.Show("You won the Jackpot prize")
Form3.PictureBox1.Image = My.Resources.jackpot
Form3.Show()
jacwon()
i = 0
g = 0
r = 0
y = 0
ElseIf y = 3 And i = 3 Then
'MessageBox.Show("You won the Jackpot prize")
Form3.PictureBox1.Image = My.Resources.jackpot
Form3.Show()
jacwon()
i = 0
g = 0
r = 0
y = 0
End If
End Sub
任何帮助都会得到赞赏。
答案 0 :(得分:0)
正如评论中所指出的,这是一个调试问题,但该程序对于它需要做的事情来说过于复杂。所以它也是一个设计问题。当你编写代码时,如果你发现自己写了两次相同的东西,或两次接近相同的东西,那么你应该将逻辑封装在一个方法中。你的程序中有一个很多。我简化了它,现在你会发现它能正常工作。它应该更容易理解和调试,使其不再是一个调试项目。
Private g As Integer = 0, y As Integer = 0, r As Integer = 0
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
g += 1 : processResult()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
y += 1 : processResult()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
r += 1 : processResult()
End Sub
Private Sub processResult()
Select Case {g, y, r}.Max() * {g, y, r}.Sum()
Case 3 ' 3 different colors
loser() : clearCounters()
Case 6 ' 2 and 1
consolation() : clearCounters()
Case 9 ' all same color
jackpot() : clearCounters()
End Select ' cases 0, 1, 2, 4 ignored as game is not complete
End Sub
Private Sub loser()
My.Computer.Audio.Play(My.Resources.Fail___sound_effect_, AudioPlayMode.Background)
sorry() : Form2.Show()
End Sub
Private Sub consolation()
My.Computer.Audio.Play(My.Resources.You_win_sound_effect_5, AudioPlayMode.Background)
conwon() : Form2.Show()
End Sub
Private Sub jackpot()
Form3.PictureBox1.Image = My.Resources.jackpot
Form3.Show()
jacwon()
End Sub
Private Sub clearCounters()
g = 0 : y = 0 : r = 0
End Sub
为了在一个屏幕上找到答案,多行有时会与:
您不需要计数器,因为州的总和为g
,y
和r
。当总和为3时,游戏结束。此外,当总和为3时,有三种不同的状态。最大值为3,2或1,分别代表所有一种颜色,两种颜色和三种颜色。这两种计算的组合以下列方式涵盖了所有情况:
| game over | game active
---------|-------------|---------------
colorA | 3 2 1 | 2 1 1 0
colorB | 0 1 1 | 0 1 0 0
colorC | 0 0 1 | 0 0 0 0
---------|-------------|---------------
max*sum | 9 6 3 | 4 2 1 0
outcome | j c l | n n n n
因此,在游戏状态中使用Switch
语句可以告诉您具体操作方法,游戏是否结束以及您是否需要选择获胜者,或者游戏仍处于活动状态且您需要执行此操作什么都没有。
以下是所有可能的状态
游戏结束时有10个州。失败的几率是1/10,安慰是6/10,累积奖金是3/10。我当然希望你不是这个房子。