2007-06-10 16:50:23 Kenny

井字遊戲

 

 

'主題:井字遊戲
'前置作業:預先製作2張圖片
'規則:橫向/縱向/斜向 先連成一線者勝
'動作:選擇誰先下
'應用一:使用按鈕控制項陣列,設style=1
'應用二:使用2維陣列,儲存雙方按下時的值,黑子=1,紅子=2
'連線判斷:當3相關格子內的值乘積=1,表示黑子已連線成功,當乘積為8 (2*2*2)表示紅子連線。

 



Dim times As Integer
Dim black As Boolean
Dim GameOver As Boolean
Dim cel(2, 2) As Integer
Dim wait As Integer

Private Sub cmdPlay_Click(Index As Integer)
Timer1.Enabled = False
cmdPlay(4).Caption = ""
If GameOver Then Exit Sub       '比賽已結束

Option1(0).Enabled = False
Option1(1).Enabled = False

Row = Int(Index / 3)
Col = Int(Index Mod 3)

If cel(Row, Col) = 0 Then
    Select Case black
    Case True
        cel(Row, Col) = 1
        cmdPlay(Index).Picture = Image1(0).Picture
        black = False
        Label1.Caption = "紅子下!"
    Case False
        cel(Row, Col) = 2
        cmdPlay(Index).Picture = Image1(1).Picture
        black = True
        Label1.Caption = "黑子下!"
    End Select
else
    Exit Sub
End If


For i = 0 To 2

If cel(i, 0) * cel(i, 1) * cel(i, 2) = 1 Or _
   cel(0, i) * cel(1, i) * cel(2, i) = 1 Or _
   cel(0, 0) * cel(1, 1) * cel(2, 2) = 1 Or _
   cel(0, 2) * cel(1, 1) * cel(2, 0) = 1 Then
   Label1 = ""
   MsgBox "黑子 勝!", , "遊戲結束"
   GameOver = True
   Exit Sub
End If
If cel(i, 0) * cel(i, 1) * cel(i, 2) = 8 Or _
   cel(0, i) * cel(1, i) * cel(2, i) = 8 Or _
   cel(0, 0) * cel(1, 1) * cel(2, 2) = 8 Or _
   cel(0, 2) * cel(1, 1) * cel(2, 0) = 8 Then
   Label1 = ""
   MsgBox " 紅子 勝!", , "遊戲結束"
   GameOver = True
   Exit Sub
End If

Next i
cmdReset.SetFocus
times = times + 1
If times >= 9 Then
    Label1 = ""
   MsgBox "平手!", , "遊戲結束"
   GameOver = True
   Call Reset
End If

End Sub

Private Sub cmdReset_Click()
Call Reset
End Sub
Private Sub Reset()
For i = 0 To 8
    cmdPlay(i).Picture = Nothing
Next
For i = 0 To 2
    For j = 0 To 2
        cel(i, j) = 0
    Next
Next
black = True
GameOver = False                '重新比賽
Option1(0).Value = True         '預設黑子先下
Option1(0).Enabled = True
Option1(1).Enabled = True
Label1 = ""
times = 0
Timer1.Enabled = True
wait = 0
End Sub
Private Sub Command3_Click()
End
End Sub

Private Sub Form_Load()
GameOver = False
black = True
Label1 = ""
times = 0
wait = 0
Timer1.Enabled = True
End Sub

Private Sub Option1_Click(Index As Integer)
If Option1(0) Then      '當選項更動
    black = True
Else
    black = False
End If

End Sub

Private Sub Timer1_Timer()
If wait Mod 4 = 0 Then
    cmdPlay(4).Caption = "|"
    ElseIf wait Mod 4 = 1 Then
        cmdPlay(4).Caption = "/"
    ElseIf wait Mod 4 = 2 Then
        cmdPlay(4).Caption = "-"
Else
        cmdPlay(4).Caption = "\"
       
End If
wait = wait + 1
End Sub