本帖最後由 arthurliuliu 於 2010-6-23 08:59 AM 編輯
- Const row = 10
- Const col = 10
- Dim state(row, col) As Integer
- Dim xgrid As Integer
- Dim ygrid As Integer
- ' -----------------------------------------------
- Private Sub Form_Activate()
- xgrid = ScaleWidth / col ' 求小方格寬度
- ygrid = ScaleHeight / row ' 求小方格高度
- For i = 0 To (row - 1)
- For j = 0 To (col - 1)
- X1 = i * xgrid ' 矩形左上角
- Y1 = j * ygrid
- X2 = (i + 1) * xgrid ' 矩形右下角
- Y2 = (j + 1) * ygrid
- Line (X1, Y1)-(X2, Y2), , B ' 繪方格
- Next j
- Next i
- End Sub
- Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
- Dim r As Integer
- Dim PicState As Integer
- r = 140
- X = X \ xgrid ' 求欲處理的方格位置
- Y = Y \ ygrid
- cx = (X * xgrid + (X + 1) * xgrid) / 2 ' 中心點
- cy = (Y * ygrid + (Y + 1) * ygrid) / 2
- black = RGB(0, 0, 0)
- white = RGB(255, 255, 255)
- FormColor = Form1.BackColor =============>增加此敍述
- If (Button = 1) Then ' 按滑鼠左邊鍵
- If state(X, Y) = 0 Then
- Circle (cx, cy), r, black ' 繪圓
- state(X, Y) = 1
- GameOver = IsGameOver(X, Y, 1) ========>增加此敍述
- If GameOver = "Game Over" Then MsgBox "game over" & vbCrLf & vbCrLf & "O 獲勝!!" ========>增加此敍述
- ElseIf state(X, Y) = 1 Then
- Circle (cx, cy), r, FormColor ' 清除圓
- state(X, Y) = 0
- End If
- End If
- If (Button = 2) Then ' 按滑鼠右邊鍵
- If state(X, Y) = 0 Then ' 繪交叉線
- Line (cx - r, cy - r)-(cx + r, cy + r), black
- Line (cx - r, cy + r)-(cx + r, cy - r), black
- state(X, Y) = 2
- GameOver = IsGameOver(X, Y, 2) ========>增加此敍述
- If GameOver = "Game Over" Then MsgBox "game over" & vbCrLf & vbCrLf & "X 獲勝!!" ========>增加此敍述
- ElseIf state(X, Y) = 2 Then ' 清除交叉線
- Line (cx - r, cy - r)-(cx + r, cy + r), FormColor '這裡建議改成表單的背景顏色
- Line (cx - r, cy + r)-(cx + r, cy - r), FormColor
- state(X, Y) = 0
- End If
- End If
- End Sub
- Function IsGameOver(X_val As Single, Y_val As Single, status As Integer) As String
- Dim num1, num2, num3, num4 As Integer
- Dim X1, X2, Y1, Y2 As Single
- Dim i, j, k, L, m, n, p, q As Integer
- num1 = 1
- num2 = 1
- num3 = 1
- num4 = 1
- X2 = X_val
- Y2 = Y_val
- '------判斷橫的-------------------------------
- X1 = X2: Y1 = Y2
- For i = X1 - 1 To 0 Step -1
- If state(i, Y1) = status Then
- num1 = num1 + 1
- Else
- Exit For
- End If
- Next
- X1 = X2
- For j = X1 + 1 To 9
- If state(j, Y1) = status Then
- num1 = num1 + 1
- Else
- Exit For
- End If
- Next
- '||||||判斷直的||||||||||||||||||||||||||||||||||||
- X1 = X2: Y1 = Y2
- For k = Y1 - 1 To 0 Step -1
- If state(X1, k) = status Then
- num2 = num2 + 1
- Else
- Exit For
- End If
- Next
- Y1 = Y2
- For L = Y1 + 1 To 9
- If state(X1, L) = status Then
- num2 = num2 + 1
- Else
- Exit For
- End If
- Next
- '///////判斷右上~左下////////////////////////////////////
- X1 = X2: Y1 = Y2
- For m = X1 - 1 To 0 Step -1
- Y1 = Y1 + 1
- If Y1 <= 9 Then
- If state(m, Y1) = status Then
- num3 = num3 + 1
- Else
- Exit For
- End If
- End If
- Next
- X1 = X2: Y1 = Y2
- For n = X1 + 1 To 9
- Y1 = Y1 - 1
- If Y1 >= 0 Then
- If state(n, Y1) = status Then
- num3 = num3 + 1
- Else
- Exit For
- End If
- End If
- Next
- '\\\\\\\判斷左上~右下\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
- X1 = X2: Y1 = Y2
- For p = X1 - 1 To 0 Step -1
- Y1 = Y1 - 1
- If Y1 >= 0 Then
- If state(p, Y1) = status Then
- num4 = num4 + 1
- Else
- Exit For
- End If
- End If
- Next
- X1 = X2: Y1 = Y2
- For q = X1 + 1 To 9
- Y1 = Y1 + 1
- If Y1 <= 9 Then
- If state(q, Y1) = status Then
- num4 = num4 + 1
- Else
- Exit For
- End If
- End If
- Next
- If num1 >= 3 Or num2 >= 3 Or num3 >= 3 Or num4 >= 3 Then
- IsGameOver = "Game Over"
- Else
- IsGameOver = ""
- End If
- End Function
複製代碼 ... |