摘要:[新手上路] OX小遊戲
判斷勝利的方式很笨,適用字串在判斷的,沒辦法,功力趨近於零,想不到其他方法了!
圈叉按鈕.VB
1 Public Class 圈叉按鈕
2 Inherits Windows.Forms.Button
3 Public Shared 圈叉判斷 As Boolean = True
4 Public Sub New()
5 MyBase.New()
6 End Sub
7 End Class
2 Inherits Windows.Forms.Button
3 Public Shared 圈叉判斷 As Boolean = True
4 Public Sub New()
5 MyBase.New()
6 End Sub
7 End Class
FORM1.VB
001 Public Class Form1
002 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
003 Dim bb As 圈叉按鈕
004 圈叉按鈕.圈叉判斷 = True
005 For i As Integer = 0 To 2
006 For j As Integer = 0 To 2
007 bb = New 圈叉按鈕
008 With bb
009 .Size = New Size(100, 100)
010 .Left = i * 100
011 .Top = j * 100
012 .Text = ""
013 .Font = New Font("新細明體", 48)
014 End With
015 AddHandler bb.MouseDown, AddressOf 按下按鈕
016 Me.Controls.Add(bb)
017 Next
018 Next
019 End Sub
020 Private Sub 按下按鈕(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
021 If CType(sender, Button).Text <> "" Then
022 Exit Sub
023 End If
024 If e.Button = Windows.Forms.MouseButtons.Left Then '左鍵
025 If 圈叉按鈕.圈叉判斷 = True Then
026 CType(sender, Button).Text = "O"
027 圈叉按鈕.圈叉判斷 = False
028 Else
029 CType(sender, Button).Text = "X"
030 圈叉按鈕.圈叉判斷 = True
031 End If
032
033 End If
034 將字串填入陣列然後判斷勝利()
035 End Sub
036 Private Sub 將字串填入陣列然後判斷勝利()
037 Dim o As Object
038 Dim x(9) As String
039 Dim bb As Button
040 Dim i, j As Integer
041 For Each o In Me.Controls
042 If TypeOf o Is Button Then
043 bb = CType(o, Button)
044 If bb.Left < 400 Then
045 x(i) = bb.Text
046 i += 1
047 End If
048 End If
049 Next
050 If x(0) <> "" And ((x(0) = x(3) And x(3) = x(6)) Or (x(0) = x(1) And x(1) = x(2)) Or (x(0) = x(4) And x(4) = x(8))) Then
051 宣告勝利(x(0))
052
053 ElseIf x(3) <> "" And x(3) = x(4) And x(4) = x(5) Then
054 宣告勝利(x(3))
055
056 ElseIf x(6) <> "" And x(6) = x(7) And x(7) = x(8) Then
057 宣告勝利(x(6))
058
059 ElseIf x(1) <> "" And x(1) = x(4) And x(4) = x(7) Then
060 宣告勝利(x(1))
061
062 ElseIf x(2) <> "" And (x(2) = x(5) And x(5) = x(8) Or x(2) = x(4) And x(4) = x(6)) Then
063 宣告勝利(x(2))
064 Else
065 For k As Integer = 0 To 8
066 If x(k) <> "" Then
067 j += 1
068 End If
069 Next
070 End If
071 If j = 9 Then
072 MsgBox("平手", MsgBoxStyle.OkOnly)
073 If CBool(MsgBoxResult.Ok) Then
074 清除()
075 End If
076 End If
077 End Sub
078 Private Sub 宣告勝利(ByVal a As String)
079 If a = "O" Then
080 MsgBox("O贏了", MsgBoxStyle.OkOnly)
081 ElseIf a = "X" Then
082 MsgBox("X贏了", MsgBoxStyle.OkOnly)
083 End If
084 If CBool(MsgBoxResult.Ok) Then
085 清除()
086 End If
087 End Sub
088 Private Sub 清除()
089 Dim o As Object
090 Dim bb As Button
091 For Each o In Me.Controls
092 If TypeOf o Is Button Then
093 bb = CType(o, Button)
094 If bb.Left < 400 Then
095 bb.Text = ""
096 End If
097 End If
098 Next
099 圈叉按鈕.圈叉判斷 = True
100 End Sub
101 End Class
102
002 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
003 Dim bb As 圈叉按鈕
004 圈叉按鈕.圈叉判斷 = True
005 For i As Integer = 0 To 2
006 For j As Integer = 0 To 2
007 bb = New 圈叉按鈕
008 With bb
009 .Size = New Size(100, 100)
010 .Left = i * 100
011 .Top = j * 100
012 .Text = ""
013 .Font = New Font("新細明體", 48)
014 End With
015 AddHandler bb.MouseDown, AddressOf 按下按鈕
016 Me.Controls.Add(bb)
017 Next
018 Next
019 End Sub
020 Private Sub 按下按鈕(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
021 If CType(sender, Button).Text <> "" Then
022 Exit Sub
023 End If
024 If e.Button = Windows.Forms.MouseButtons.Left Then '左鍵
025 If 圈叉按鈕.圈叉判斷 = True Then
026 CType(sender, Button).Text = "O"
027 圈叉按鈕.圈叉判斷 = False
028 Else
029 CType(sender, Button).Text = "X"
030 圈叉按鈕.圈叉判斷 = True
031 End If
032
033 End If
034 將字串填入陣列然後判斷勝利()
035 End Sub
036 Private Sub 將字串填入陣列然後判斷勝利()
037 Dim o As Object
038 Dim x(9) As String
039 Dim bb As Button
040 Dim i, j As Integer
041 For Each o In Me.Controls
042 If TypeOf o Is Button Then
043 bb = CType(o, Button)
044 If bb.Left < 400 Then
045 x(i) = bb.Text
046 i += 1
047 End If
048 End If
049 Next
050 If x(0) <> "" And ((x(0) = x(3) And x(3) = x(6)) Or (x(0) = x(1) And x(1) = x(2)) Or (x(0) = x(4) And x(4) = x(8))) Then
051 宣告勝利(x(0))
052
053 ElseIf x(3) <> "" And x(3) = x(4) And x(4) = x(5) Then
054 宣告勝利(x(3))
055
056 ElseIf x(6) <> "" And x(6) = x(7) And x(7) = x(8) Then
057 宣告勝利(x(6))
058
059 ElseIf x(1) <> "" And x(1) = x(4) And x(4) = x(7) Then
060 宣告勝利(x(1))
061
062 ElseIf x(2) <> "" And (x(2) = x(5) And x(5) = x(8) Or x(2) = x(4) And x(4) = x(6)) Then
063 宣告勝利(x(2))
064 Else
065 For k As Integer = 0 To 8
066 If x(k) <> "" Then
067 j += 1
068 End If
069 Next
070 End If
071 If j = 9 Then
072 MsgBox("平手", MsgBoxStyle.OkOnly)
073 If CBool(MsgBoxResult.Ok) Then
074 清除()
075 End If
076 End If
077 End Sub
078 Private Sub 宣告勝利(ByVal a As String)
079 If a = "O" Then
080 MsgBox("O贏了", MsgBoxStyle.OkOnly)
081 ElseIf a = "X" Then
082 MsgBox("X贏了", MsgBoxStyle.OkOnly)
083 End If
084 If CBool(MsgBoxResult.Ok) Then
085 清除()
086 End If
087 End Sub
088 Private Sub 清除()
089 Dim o As Object
090 Dim bb As Button
091 For Each o In Me.Controls
092 If TypeOf o Is Button Then
093 bb = CType(o, Button)
094 If bb.Left < 400 Then
095 bb.Text = ""
096 End If
097 End If
098 Next
099 圈叉按鈕.圈叉判斷 = True
100 End Sub
101 End Class
102