9ボタンのシンプルな三目並べをコーディングしようとしています。いずれかのボタンをクリックすると、WINダイアログのあるメッセージボックスが表示されます。このメッセージボックスは、WINCASEが発生した場合にのみ表示されることを目的としています。問題は、Button.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