展开全部
在from中放8个command控件组成一组,再放一个command控件,名为32313133353236313431303231363533e78988e69d8331333262363134"重来",见图,复制如下代码
玩法:轮流用鼠标点击相应数字的格子,当O或X横竖斜排成一线就算赢了。
Dim ox(3, 3) As Integer
Dim condition As Integer
Dim result As Integer
Private Sub Command2_Click()
Dim i, j As Integer
result = 0: condition = 0
For i = 0 To 8
Command1(i).Caption = ""
Command1(i).Enabled = True
Next i
For i = 0 To 2
For j = 0 To 2
ox(i, j) = 0
Next j
Next i
End Sub
Private Sub Form_Load()
Dim i, j As Integer
condition = 0: result = 0
For i = 0 To 2
For j = 0 To 2
ox(i, j) = 0
Next j
Next i
End Sub
Private Sub Command1_Click(Index As Integer)
Dim i, j As Integer
Dim over As Boolean
i = Index \ 3
j = Index Mod 3
If ox(i, j) = 0 Then
If condition = 0 Then
Command1(Index).Caption = "O"
condition = 1
ox(i, j) = 1
Else
Command1(Index).Caption = "X"
condition = 0
ox(i, j) = 2
End If
Rem 判断是否连成一线
For i = 0 To 2
If ox(0, i) = 1 And ox(1, i) = 1 And ox(2, i) = 1 Then
result = 1
End If
If ox(i, 0) = 1 And ox(i, 1) = 1 And ox(i, 2) = 1 Then
result = 1
End If
If ox(0, i) = 2 And ox(1, i) = 2 And ox(2, i) = 2 Then
result = 2
End If
If ox(i, 0) = 2 And ox(i, 1) = 2 And ox(i, 2) = 2 Then
result = 2
End If
Next i
If ox(0, 0) = 1 And ox(1, 1) = 1 And ox(2, 2) = 1 Then
result = 1
End If
If ox(0, 0) = 2 And ox(1, 1) = 2 And ox(2, 2) = 2 Then
result = 2
End If
If ox(0, 2) = 1 And ox(1, 1) = 1 And ox(2, 0) = 1 Then
result = 1
End If
If ox(0, 2) = 2 And ox(1, 1) = 2 And ox(2, 0) = 2 Then
result = 2
End If
If result = 1 Then
MsgBox "O赢了"
End If
If result = 2 Then
MsgBox "X赢了"
End If
If result <> 0 Then
For i = 0 To 8
Command1(i).Enabled = False
Next i
Else
over = True
For i = 0 To 2
For j = 0 To 2
If ox(i, j) = 0 Then
over = False
End If
Next j
Next i
If over Then
MsgBox "结束"
End If
End If
End If
End Sub