Example
Public Class Form1
Dim n As Integer
Dim sz As Integer = 40
Dim px As Integer = sz
Dim py As Integer = sz
Dim turn As String = "X"
Dim l() As Label
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
n = 3
l = New Label(n * n - 1) {}
For i As Integer = 0 To n * n - 1
l(i) = New Label
l(i).Size = New Size(sz, sz)
l(i).Location = New Point(px, py)
px += sz
If (i + 1) Mod n = 0 Then
px = sz
py += sz
End If
l(i).AutoSize = False
l(i).BorderStyle = BorderStyle.FixedSingle
l(i).Font = New Font("Arial", 20, FontStyle.Bold)
Me.Controls.Add(l(i))
Me.Height = (n + 2) * sz + 30
Me.Width = (n + 2) * sz
AddHandler l(i).Click, AddressOf lb1_click
Next
End Sub
Public Sub lb1_click(ByVal sender As Object, ByVal e As EventArgs)
Dim l As Label = CType(sender, Label)
If l.Text = "" Then
If turn = "X" Then
l.Text = "X"
If checkwin(turn) Then
MsgBox(turn + "win")
Me.Close()
End If
turn = "0"
Else
l.Text = "0"
If checkwin(turn) Then
MsgBox(turn + "win")
Me.Close()
End If
turn = "X"
End If
If Draw() Then
MsgBox("Draw")
Me.Close()
End If
End If
End Sub
Private Sub setPosition()
Dim sc_width As Integer
Dim sc_height As Integer
sc_width = Screen.PrimaryScreen.Bounds.Left
sc_height = Screen.PrimaryScreen.Bounds.Top
Me.Left = sc_width - (Me.Width / 2) + 350
Me.Top = sc_height - (Me.Height / 2) + 350
End Sub
Public Function checkwin(ByVal turn As String) As Boolean
Dim cnt As Integer = 0
For i As Integer = 0 To n - 1
cnt = 0
For j As Integer = i * n To (i * n) + n - 1
If l(j).Text = turn Then
cnt += 1
End If
Next
If cnt >= n Then
Return True
End If
Next
For i As Integer = 0 To n - 1
cnt = 0
For j As Integer = 0 To n - 1
If l(j * n + i).Text = turn Then
cnt += 1
End If
Next
If cnt >= n Then
Return True
End If
Next
cnt = 0
For i As Integer = 0 To n - 1
If l(i * (n + 1)).Text = turn Then
cnt += 1
End If
Next
If cnt >= n Then
Return True
End If
cnt = 0
For i As Integer = n - 1 To n * (n - 1) Step n - 1
If l(i).Text = turn Then
cnt += 1
End If
Next
If cnt >= n Then
Return True
End If
Return False
End Function
Public Function Draw() As Boolean
For i As Integer = 0 To n * n - 1
If l(i).Text = "" Then
Return False
End If
Next
Return True
End Function
End Class
Public Class Form1
Dim n As Integer
Dim sz As Integer = 40
Dim px As Integer = sz
Dim py As Integer = sz
Dim turn As String = "X"
Dim l() As Label
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
n = 3
l = New Label(n * n - 1) {}
For i As Integer = 0 To n * n - 1
l(i) = New Label
l(i).Size = New Size(sz, sz)
l(i).Location = New Point(px, py)
px += sz
If (i + 1) Mod n = 0 Then
px = sz
py += sz
End If
l(i).AutoSize = False
l(i).BorderStyle = BorderStyle.FixedSingle
l(i).Font = New Font("Arial", 20, FontStyle.Bold)
Me.Controls.Add(l(i))
Me.Height = (n + 2) * sz + 30
Me.Width = (n + 2) * sz
AddHandler l(i).Click, AddressOf lb1_click
Next
End Sub
Public Sub lb1_click(ByVal sender As Object, ByVal e As EventArgs)
Dim l As Label = CType(sender, Label)
If l.Text = "" Then
If turn = "X" Then
l.Text = "X"
If checkwin(turn) Then
MsgBox(turn + "win")
Me.Close()
End If
turn = "0"
Else
l.Text = "0"
If checkwin(turn) Then
MsgBox(turn + "win")
Me.Close()
End If
turn = "X"
End If
If Draw() Then
MsgBox("Draw")
Me.Close()
End If
End If
End Sub
Private Sub setPosition()
Dim sc_width As Integer
Dim sc_height As Integer
sc_width = Screen.PrimaryScreen.Bounds.Left
sc_height = Screen.PrimaryScreen.Bounds.Top
Me.Left = sc_width - (Me.Width / 2) + 350
Me.Top = sc_height - (Me.Height / 2) + 350
End Sub
Public Function checkwin(ByVal turn As String) As Boolean
Dim cnt As Integer = 0
For i As Integer = 0 To n - 1
cnt = 0
For j As Integer = i * n To (i * n) + n - 1
If l(j).Text = turn Then
cnt += 1
End If
Next
If cnt >= n Then
Return True
End If
Next
For i As Integer = 0 To n - 1
cnt = 0
For j As Integer = 0 To n - 1
If l(j * n + i).Text = turn Then
cnt += 1
End If
Next
If cnt >= n Then
Return True
End If
Next
cnt = 0
For i As Integer = 0 To n - 1
If l(i * (n + 1)).Text = turn Then
cnt += 1
End If
Next
If cnt >= n Then
Return True
End If
cnt = 0
For i As Integer = n - 1 To n * (n - 1) Step n - 1
If l(i).Text = turn Then
cnt += 1
End If
Next
If cnt >= n Then
Return True
End If
Return False
End Function
Public Function Draw() As Boolean
For i As Integer = 0 To n * n - 1
If l(i).Text = "" Then
Return False
End If
Next
Return True
End Function
End Class
No comments:
Post a Comment