我正在尝试用 9 个按钮编写一个简单的井字游戏。当我单击任何按钮时,我会看到一个带有 WIN 对话框的消息框。尽管此消息框仅在出现 WIN CASE 时才显示。问题在于它没有正确比较 Button.Text 值。
最初 All 按钮的 TEXT 属性为空。单击时,我将他们的 TEXT 设置为“x”或“0”,视情况而定。这假设可以正常工作,但无法弄清楚为什么它没有将新分配的clicked
按钮文本与其他按钮的文本进行比较。
这是我的代码
Public Class Form1
Dim count As Int32 = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Sub checkwin()
If Button1.Text = Button2.Text And Button2.Text = Button3.Text Then
MessageBox.Show(Button1.Text & "wins")
Call reset()
ElseIf Button4.Text = Button5.Text And Button5.Text = Button6.Text Then
MessageBox.Show(Button4.Text & "wins")
Call reset()
ElseIf Button7.Text = Button8.Text And Button8.Text = Button9.Text Then
MessageBox.Show(Button7.Text & "wins")
Call reset()
ElseIf Button1.Text = Button4.Text And Button4.Text = Button7.Text Then
MessageBox.Show(Button1.Text & "wins")
Call reset()
ElseIf Button2.Text = Button5.Text And Button5.Text = Button8.Text Then
MessageBox.Show(Button2.Text & "wins")
Call reset()
ElseIf Button3.Text = Button6.Text And Button6.Text = Button9.Text Then
MessageBox.Show(Button3.Text & "wins")
Call reset()
ElseIf Button1.Text = Button5.Text And Button5.Text = Button9.Text Then
MessageBox.Show(Button1.Text & "wins")
Call reset()
ElseIf Button3.Text = Button5.Text And Button5.Text = Button7.Text Then
MessageBox.Show(Button3.Text & "wins")
Call reset()
End If
End Sub
Sub reset()
For Each b as Button in Controls
b.Text = " "
b.Enabled = True
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click
If sender Is Button1 Then
count = count + 1
If count Mod 2 = 0 Then
Button1.Text = "0"
Else
Button1.Text = "x"
End If
Button1.Enabled = False
Call checkwin()
End If
If sender Is Button2 Then
count = count + 1
If count Mod 2 = 0 Then
Button2.Text = "0"
Else
Button2.Text = "x"
End If
Button2.Enabled = False
Call checkwin()
End If
If sender Is Button3 Then
count = count + 1
If count Mod 2 = 0 Then
Button3.Text = "0"
Else
Button3.Text = "x"
End If
Button3.Enabled = False
Call checkwin()
End If
If sender Is Button4 Then
count = count + 1
If count Mod 2 = 0 Then
Button4.Text = "0"
Else
Button4.Text = "x"
End If
Button4.Enabled = False
Call checkwin()
End If
If sender Is Button5 Then
count = count + 1
If count Mod 2 = 0 Then
Button5.Text = "0"
Else
Button5.Text = "x"
End If
Button5.Enabled = False
Call checkwin()
End If
If sender Is Button6 Then
count = count + 1
If count Mod 2 = 0 Then
Button6.Text = "0"
Else
Button6.Text = "x"
End If
Button6.Enabled = False
Call checkwin()
End If
If sender Is Button7 Then
count = count + 1
If count Mod 2 = 0 Then
Button7.Text = "0"
Else
Button7.Text = "x"
End If
Button7.Enabled = False
Call checkwin()
End If
If sender Is Button8 Then
count = count + 1
If count Mod 2 = 0 Then
Button8.Text = "0"
Else
Button8.Text = "x"
End If
Button8.Enabled = False
Call checkwin()
End If
If sender Is Button9 Then
count = count + 1
If count Mod 2 = 0 Then
Button9.Text = "0"
Else
Button9.Text = "x"
End If
Button9.Enabled = False
Call checkwin()
End If
End Sub
End Class