VB.NET Hanoi Kuleleri Uygulaması

Forum Altay

Forum Yöneticisi
FA ÜYESİ
Premium Üye
Yazılım Ekibi
Puanları 3
Çözümler 0
Katılım
3 May 2025
Mesajlar
18
Beğeniler
2
Forum Altay
# VB.NET Hanoi Kuleleri Uygulaması

Aşağıda VB.NET ile Hanoi Kuleleri oyununu gerçekleştiren bir uygulama örneği bulabilirsiniz. Bu uygulama hem konsol tabanlı hem de Windows Forms tabanlı olarak iki farklı şekilde sunulmuştur.

## Konsol Uygulaması

C#:
Module Module1
    Sub Main()
        Console.WriteLine("Hanoi Kuleleri Oyunu")
        Console.Write("Disk sayısını girin: ")
        Dim diskSayisi As Integer = Integer.Parse(Console.ReadLine())
        
        Hanoi(diskSayisi, "A", "C", "B")
        Console.WriteLine("Tüm hamleler tamamlandı!")
        Console.ReadLine()
    End Sub

    Sub Hanoi(disk As Integer, kaynak As String, hedef As String, ara As String)
        If disk = 1 Then
            Console.WriteLine($"Disk 1'i {kaynak} çubuğundan {hedef} çubuğuna taşı")
        Else
            Hanoi(disk - 1, kaynak, ara, hedef)
            Console.WriteLine($"Disk {disk}'i {kaynak} çubuğundan {hedef} çubuğuna taşı")
            Hanoi(disk - 1, ara, hedef, kaynak)
        End If
    End Sub
End Module

## Windows Forms Uygulaması

Daha görsel bir uygulama için Windows Forms kullanabiliriz:

C#:
Imports System.Threading

Public Class HanoiForm
    Inherits Form

    Private disks As Integer
    Private moves As New List(Of String)
    Private currentMove As Integer = 0
    Private pegA, pegB, pegC As New List(Of Integer)
    Private pegPositions As New Dictionary(Of String, Integer) From {
        {"A", 150}, {"B", 300}, {"C", 450}
    }
    Private pegHeight As Integer = 200
    Private diskHeight As Integer = 20
    Private maxDiskWidth As Integer = 150
    Private minDiskWidth As Integer = 40

    Public Sub New()
        Me.Text = "Hanoi Kuleleri"
        Me.ClientSize = New Size(600, 400)
        Me.BackColor = Color.White

        Dim lbl As New Label With {
            .Text = "Disk Sayısı:",
            .Location = New Point(50, 20)
        }
        Me.Controls.Add(lbl)

        Dim numDisks As New NumericUpDown With {
            .Minimum = 1,
            .Maximum = 8,
            .Value = 3,
            .Location = New Point(120, 20)
        }
        Me.Controls.Add(numDisks)

        Dim btnStart As New Button With {
            .Text = "Başlat",
            .Location = New Point(200, 20)
        }
        AddHandler btnStart.Click, Sub(sender, e)
                                      disks = CInt(numDisks.Value)
                                      InitializeGame()
                                      SolveHanoi(disks, "A", "C", "B")
                                      currentMove = 0
                                      Timer1.Start()
                                  End Sub
        Me.Controls.Add(btnStart)

        Dim btnNext As New Button With {
            .Text = "Sonraki Hamle",
            .Location = New Point(280, 20)
        }
        AddHandler btnNext.Click, Sub(sender, e)
                                     If currentMove < moves.Count Then
                                         ExecuteMove(moves(currentMove))
                                         currentMove += 1
                                     End If
                                 End Sub
        Me.Controls.Add(btnNext)

        Timer1.Interval = 500
        AddHandler Timer1.Tick, Sub(sender, e)
                                   If currentMove < moves.Count Then
                                       ExecuteMove(moves(currentMove))
                                       currentMove += 1
                                   Else
                                       Timer1.Stop()
                                   End If
                               End Sub
    End Sub

    Private WithEvents Timer1 As New Timer

    Private Sub InitializeGame()
        pegA.Clear()
        pegB.Clear()
        pegC.Clear()
        moves.Clear()
        currentMove = 0

        For i As Integer = disks To 1 Step -1
            pegA.Add(i)
        Next

        Me.Invalidate()
    End Sub

    Private Sub SolveHanoi(disk As Integer, source As String, target As String, auxiliary As String)
        If disk = 1 Then
            moves.Add($"{source}-{target}")
        Else
            SolveHanoi(disk - 1, source, auxiliary, target)
            moves.Add($"{source}-{target}")
            SolveHanoi(disk - 1, auxiliary, target, source)
        End If
    End Sub

    Private Sub ExecuteMove(move As String)
        Dim sourcePeg As String = move.Substring(0, 1)
        Dim targetPeg As String = move.Substring(2, 1)

        Dim sourceList As List(Of Integer) = GetPegList(sourcePeg)
        Dim targetList As List(Of Integer) = GetPegList(targetPeg)

        If sourceList.Count > 0 Then
            Dim disk As Integer = sourceList(sourceList.Count - 1)
            sourceList.RemoveAt(sourceList.Count - 1)
            targetList.Add(disk)
        End If

        Me.Invalidate()
    End Sub

    Private Function GetPegList(peg As String) As List(Of Integer)
        Select Case peg
            Case "A" : Return pegA
            Case "B" : Return pegB
            Case "C" : Return pegC
            Case Else : Return Nothing
        End Select
    End Function

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        MyBase.OnPaint(e)

        Dim g As Graphics = e.Graphics

        ' Çubukları çiz
        For Each peg In pegPositions
            g.FillRectangle(Brushes.Brown, peg.Value - 10, 100, 20, pegHeight)
        Next

        ' Diskleri çiz
        DrawDisks(g, pegA, "A")
        DrawDisks(g, pegB, "B")
        DrawDisks(g, pegC, "C")

        ' Bilgi yazısı
        g.DrawString($"Hamle: {currentMove}/{moves.Count}", Me.Font, Brushes.Black, 400, 20)
    End Sub

    Private Sub DrawDisks(g As Graphics, disks As List(Of Integer), peg As String)
        Dim x As Integer = pegPositions(peg)
        Dim y As Integer = 100 + pegHeight

        For i As Integer = 0 To disks.Count - 1
            Dim diskSize As Integer = disks(i)
            Dim width As Integer = minDiskWidth + (maxDiskWidth - minDiskWidth) * diskSize / disks.Count
            Dim rect As New Rectangle(x - width \ 2, y - diskHeight, width, diskHeight)
            
            Dim color As Color = Color.FromArgb(255, 100 + diskSize * 20, 50, 50)
            g.FillRectangle(New SolidBrush(color), rect)
            g.DrawRectangle(Pens.Black, rect)
            
            y -= diskHeight
        Next
    End Sub
End Class

Module Program
    Sub Main()
        Application.EnableVisualStyles()
        Application.Run(New HanoiForm())
    End Sub
End Module
```


## Nasıl Çalıştırılır?

1. **Konsol Uygulaması**:
- Visual Studio'da yeni bir VB.NET Konsol Uygulaması oluşturun
- Kodu yapıştırın ve çalıştırın
- Disk sayısını girerek çözüm adımlarını görebilirsiniz

2. **Windows Forms Uygulaması**:
- Visual Studio'da yeni bir VB.NET Windows Forms Uygulaması oluşturun
- Form'un kod kısmını tamamen silip bu kodu yapıştırın
- Uygulamayı çalıştırdığınızda disk sayısını seçip "Başlat" butonuna basarak animasyonu görebilirsiniz
- "Sonraki Hamle" butonu ile adım adım ilerleyebilirsiniz

## Özellikler

- Disk sayısını kullanıcı belirleyebilir (1-8 arası)
- Çözüm adımlarını animasyonlu olarak gösterir
- Her hamleyi adım adım takip edebilme
- Renkli disklerle görsel temsil

Bu uygulama, Hanoi Kuleleri probleminin özyinelemeli (recursive) çözümünü göstermektedir. Problemin çözümü için 2^n - 1 hamle gereklidir (n = disk sayısı).
 
Sitemiz bir forum sitesi olduğu için kullanıcılar her türlü görüşlerini önceden onay olmadan anında siteye yazabilmektedir. 5651 sayılı yasaya göre bu yazılardan dolayı doğabilecek her türlü sorumluluk yazan kullanıcılara aittir. 5651 sayılı yasaya göre sitemiz mesajları kontrolle yükümlü olmayıp, yasaya aykırı yada telif hakkı içeren paylaşımlar BURADAN bize ulaşıldığı taktirde, ilgili konu en geç 48 saat içerisinde kaldırılacaktır. Sitemizde Bulunan Videolar YouTube, Facebook, Dailymotion, v.b. video paylaşım sitelerinden alınmaktadır. Telif hakları sorumluluğu bu sitelere aittir. Videoların hiç biri sunucularımızda bulunmamaktadır.
  • Geri
    Üst