HANOİ KULELERİ OYUNU
Kodlar :
Imports DevExpress.XtraEditors
Public Class frmOyun
Dim BaslangicZamani As Date
Dim GeceZaman As Integer
Dim tst As Integer = 0
Dim Tasi As Boolean = False
Dim x, y As Integer
Dim Taslar(10) As PictureBox
Dim Uzaklik As Integer
Dim SeciliNesne As PictureBox
Dim K1(10) As PictureBox
Dim K2(10) As PictureBox
Dim K3(10) As PictureBox
Dim Ka1, Ka2, Ka3 As Integer
Dim EskiNokta As Point
Dim AdimSayisi As Integer = 0
Sub YeniOyun()
For i As Integer = 0 To 3
Dim btn As New PictureBox
btn.Width = 130 - (i * 20)
btn.Height = 25
btn.Top = 500 - (30 * i)
btn.Left = 50 + (i * 10)
Taslar(i) = btn
K1(i) = btn
Ka1 += 1
btn.BringToFront()
'btn.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.Office2003
btn.Image = My.Resources.HanoiTas1
btn.SizeMode = PictureBoxSizeMode.StretchImage
'btn.Text = "Taş " & i + 1
AddHandler btn.MouseMove, AddressOf MouseHareketi
Me.Controls.Add(btn)
PicBaslik.SendToBack()
PictureBox3.SendToBack() : PictureBox4.SendToBack() : PictureBox5.SendToBack()
Kule1.SendToBack()
Kule2.SendToBack()
Kule3.SendToBack()
PictureBox2.SendToBack()
Next
BaslangicZamani = Now
Zaman.Start()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Ka1 = 0
Ka2 = 0
Ka3 = 0
End Sub
Sub YerAyarlar(ByVal btn As PictureBox)
If Tasi = False Then
EskiNokta = btn.Location
End If
x = MousePosition.X - Me.Location.X - btn.Width / 2
y = 100 'MousePosition.Y - Me.Location.Y - btn.Height * 2
btn.Location = New Point(x, y)
End Sub
Private Sub MouseHareketi(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.None And Tasi = True Then
Odakla(SeciliNesne)
Tasi = False
ElseIf e.Button = Windows.Forms.MouseButtons.Left Then
YerAyarlar(sender)
SeciliNesne = sender
Tasi = True
End If
tst += 1
End Sub
Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If e.Button = Windows.Forms.MouseButtons.None And Tasi = True Then
Odakla(SeciliNesne)
Tasi = False
End If
End Sub
Sub Odakla(ByVal btn As PictureBox)
If SeciliNesne.Left + SeciliNesne.Width / 2 < Kule2.Left Then
Uzaklik = Kule2.Left - Kule1.Left
Uzaklik = Kule1.Left + Uzaklik / 2
If SeciliNesne.Left >= Uzaklik Then
'2. KULEYE AKTAR
SeciliNesne.Left = Kule2.Left - SeciliNesne.Width / 2 + Kule2.Width / 2
Ka2 += 1
K2(Ka2 - 1) = SeciliNesne
SeciliNesne.Top = 500 - (Ka2 - 1) * 30
'MsgBox("KA2")
Try
If SeciliNesne.Width > K2(Ka2 - 2).Width Then
SeciliNesne.Location = EskiNokta
Ka2 -= 1
EskiNokta_HangiKule(True)
End If
Catch ex As Exception
'MsgBox("H1")
End Try
Else
'1. KULEYE AKTAR
SeciliNesne.Left = Kule1.Left - SeciliNesne.Width / 2 + Kule1.Width / 2
Ka1 += 1
K1(Ka1 - 1) = SeciliNesne
SeciliNesne.Top = 500 - (Ka1 - 1) * 30
'MsgBox("KA1")
Try
If SeciliNesne.Width > K1(Ka2 - 2).Width Then
SeciliNesne.Location = EskiNokta
Ka1 -= 1
EskiNokta_HangiKule(True)
End If
Catch ex As Exception
'MsgBox("H2")
End Try
End If
Else
Uzaklik = Kule3.Left - Kule2.Left
Uzaklik = Kule2.Left + Uzaklik / 2
If SeciliNesne.Left >= Uzaklik Then
'3. KULEYE AKTAR
SeciliNesne.Left = Kule3.Left - SeciliNesne.Width / 2 + Kule3.Width / 2
Ka3 += 1
K3(Ka3 - 1) = SeciliNesne
SeciliNesne.Top = 500 - (Ka3 - 1) * 30
' 'MsgBox("KA3")
Try
If SeciliNesne.Width > K3(Ka3 - 2).Width Then
SeciliNesne.Location = EskiNokta
Ka3 -= 1
EskiNokta_HangiKule(True)
End If
Catch ex As Exception
' 'MsgBox("H3")
End Try
Else
'2. KULEYE AKTAR
SeciliNesne.Left = Kule2.Left - SeciliNesne.Width / 2 + Kule2.Width / 2
Ka2 += 1
K2(Ka2 - 1) = SeciliNesne
SeciliNesne.Top = 500 - (Ka2 - 1) * 30
' 'MsgBox("KA3-1")
Try
If SeciliNesne.Width > K2(Ka2 - 2).Width Then
SeciliNesne.Location = EskiNokta
Ka2 -= 1
EskiNokta_HangiKule(True)
End If
Catch ex As Exception
' 'MsgBox("H4")
End Try
End If
End If
EskiNokta_HangiKule()
Kontrol()
End Sub
Sub EskiNokta_HangiKule(Optional ByVal Arttir As Boolean = False)
' 'MsgBox(EskiNokta.X)
Dim k1, k2, k3, x, k1_uz, k2_uz, k3_uz As Integer
x = EskiNokta.X
k1 = Kule1.Location.X
k2 = Kule2.Location.X
k3 = Kule3.Location.X
k1_uz = k1 - x : If k1_uz < 0 Then k1_uz *= -1
k2_uz = k2 - x : If k2_uz < 0 Then k2_uz *= -1
k3_uz = k3 - x : If k3_uz < 0 Then k3_uz *= -1
If k1_uz < k2_uz And k1_uz < k3_uz Then
'k1
If Arttir = True Then
Ka1 += 1
Else
Ka1 -= 1
End If
Else
If k2_uz < k3_uz Then
'k2
If Arttir = True Then
Ka2 += 1
Else
Ka2 -= 1
End If
Else
'k3
If Arttir = True Then
Ka3 += 1
Else
Ka3 -= 1
End If
End If
End If
End Sub
Sub Kontrol()
ListBoxControl1.Items.Clear()
ListBoxControl2.Items.Clear()
ListBoxControl3.Items.Clear()
For i As Integer = 9 To 0 Step -1
Try
ListBoxControl1.Items.Add(K1(i).Left)
Catch ex As Exception
ListBoxControl1.Items.Add("<free>")
End Try
Try
ListBoxControl2.Items.Add(K2(i).Left)
Catch ex As Exception
ListBoxControl2.Items.Add("<free>")
End Try
Try
ListBoxControl3.Items.Add(K3(i).Left)
Catch ex As Exception
ListBoxControl3.Items.Add("<free>")
End Try
Next
txtK1.Text = Ka1
txtK2.Text = Ka2
txtK3.Text = Ka3
AdimSayisi += 1
lblAdimSayisi.Text = AdimSayisi & ".Adım"
If Ka3 = 4 Then
Tebrikler.Start()
End If
End Sub
Private Sub btnYeni_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnYeni.Click
YeniOyun()
End Sub
Private Sub Zaman_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Zaman.Tick
GeceZaman += 1
lblZaman.Text = GeceZaman & " Saniye"
End Sub
Dim TebrikMesaji As Integer = 0
Private Sub Tebrikler_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tebrikler.Tick
TebrikMesaji += 1
If picTebrikler.Visible = True Then
picTebrikler.Visible = False
Else
picTebrikler.Visible = True
End If
If TebrikMesaji = 5 Then
Tebrikler.Stop()
End If
End Sub
End Class
Hiç yorum yok:
Yorum Gönder