كود :
Public Class Form1
'هنا نضع المعرفات الاساسية التي سوف نستخدمها في المشروع
Dim clickedsquare(9), clickedx(9), clickedo(9) As Boolean
Private playerturn, turn_num As Integer
Dim check As String
'هذا حدث انهاء اللعبة و بدء لعبة جديد
Sub end_game()
Call btnnew_Click(Nothing, Nothing)
End Sub
'هذا الامر الرئيسي للتأكد من ان الفائز الاعب X
Sub winx(ByVal pnt1 As Integer, ByVal pnt2 As Integer, ByVal pnt3 As Integer)
If clickedx(pnt1.ToString) And clickedx(pnt2.ToString) And clickedx(pnt3.ToString) Then
MsgBox("X لقد فاز اللاعب", MsgBoxStyle.Information)
check = "true"
end_game()
End If
End Sub
'هذا الامر الرئيسي للتأكد من ان الفائز الاعب O
Sub wino(ByVal pnt1 As Integer, ByVal pnt2 As Integer, ByVal pnt3 As Integer)
If clickedo(pnt1.ToString) And clickedo(pnt2.ToString) And clickedo(pnt3.ToString) Then
MsgBox("O لقد فاز اللاعب")
check = "true"
end_game()
End If
End Sub
'هذا امر البحث عن ما ضغطه اللاعب O
Sub checko()
'vertical
wino(1, 2, 3)
wino(4, 5, 6)
wino(7, 8, 9)
'hortizantal
wino(1, 4, 7)
wino(2, 5, 8)
wino(3, 6, 9)
'cross
wino(1, 5, 9)
wino(3, 5, 7)
End Sub
'هذا امر البحث عن ما ضغطه اللاعب X
Sub checkX()
'vertical
winx(1, 2, 3)
winx(4, 5, 6)
winx(7, 8, 9)
'hortizantal
winx(1, 4, 7)
winx(2, 5, 8)
winx(3, 6, 9)
'cross
winx(1, 5, 9)
winx(3, 5, 7)
End Sub
'هذا اهم امر في المشروع و هو الذي يحدد الدور للاعب
Sub turn(ByVal pnt As Integer, ByVal lbl As Label)
If lbl.Tag <> "مضغوط" Then
turn_num += 1
If playerturn Mod 2 = 0 Then
lbl.Text = "X" : lbl.ForeColor = Color.Blue
lblturn.Text = "O دور اللاعب" : lblturn.ForeColor = Color.Red
clickedx(pnt) = True
clickedsquare(pnt) = True
playerturn += 1
ElseIf playerturn Mod 2 = 1 Then
lbl.Text = "O" : lbl.ForeColor = Color.Red
lblturn.Text = "X دور اللاعب" : lblturn.ForeColor = Color.Blue
clickedo(pnt) = True
clickedsquare(pnt) = True
playerturn += 1
End If
lbl.Tag = "مضغوط"
checko()
checkX()
If turn_num = 9 Then
If check <> "true" Then
MsgBox("تعااااادل", MsgBoxStyle.Information)
end_game()
End If
End If
Else
Exit Sub
End If
End Sub
'زر لعبة جديدة
Private Sub btnnew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnew.Click
Dim lbls(9) As Label : lbls(1) = sq1 : lbls(2) = sq2 : lbls(3) = sq3 : lbls(4) = sq4 : lbls(5) = sq5
lbls(6) = sq6 : lbls(7) = sq7 : lbls(8) = sq8 : lbls(9) = sq9
If xplayer.Checked = True Then
playerturn = 0 : lblturn.Text = "X دور اللاعب" : lblturn.ForeColor = Color.Blue
Else
playerturn = 1 : lblturn.Text = "O دور اللاعب" : lblturn.ForeColor = Color.Red
End If
For i = 1 To 9
clickedo(i) = False
clickedx(i) = False
clickedsquare(i) = False
lbls(i).Text = String.Empty
lbls(i).Enabled = True
lbls(i).Tag = Nothing
Next
turn_num = Nothing
check = Nothing
End Sub
'هذا حدث التسع المربعات النصية
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sq1.Click, sq2.Click, sq3.Click, _
sq4.Click, sq5.Click, sq6.Click, sq7.Click, sq8.Click, sq9.Click
turn(sender.tabindex, sender)
End Sub
'هذا حدث ترك الماوس
Private Sub Label1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles sq1.MouseLeave, sq2.MouseLeave, sq3.MouseLeave, _
sq4.MouseLeave, sq5.MouseLeave, sq6.MouseLeave, sq7.MouseLeave, sq8.MouseLeave, sq9.MouseLeave
If sender.Tag <> "مضغوط" Then
sender.Text = String.Empty
Else
Exit Sub
End If
End Sub
'هذا حدث تحرك الماوس
Private Sub Label1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sq1.MouseMove, sq2.MouseMove, _
sq3.MouseMove, sq4.MouseMove, sq5.MouseMove, sq6.MouseMove, sq7.MouseMove, sq8.MouseMove, sq9.MouseMove
If sender.Tag <> "مضغوط" Then
If playerturn Mod 2 = 0 Then
sender.Text = "X" : sender.ForeColor = Color.Blue
ElseIf playerturn Mod 2 = 1 Then
sender.Text = "O" : sender.ForeColor = Color.Red
End If
Else
Exit Sub
End If
End Sub
End Class