Recent

Author Topic: Tower of Hanoi  (Read 1017 times)

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Tower of Hanoi
« on: March 08, 2024, 01:30:10 pm »
Hi there, I was a little bored and inspired by Booleman for opened from time to time some simple game threads.
Here I present you my unfinished work in progress adaption of the Tower of Hanoi game.

Rules:
You got 3 disks of different sizes that must be moved from left to the right side.
You can move a disk to any position as long the moved disk is smaller than the underlying one.

My game logic has bugs so dont await too much of it :D
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Zvoni

  • Hero Member
  • *****
  • Posts: 2818
Re: Tower of Hanoi
« Reply #1 on: March 08, 2024, 01:46:48 pm »
Hi there, I was a little bored and inspired by Booleman for opened from time to time some simple game threads.
Here I present you my unfinished work in progress adaption of the Tower of Hanoi game.

Rules:
You got 3 disks of different sizes that must be moved from left to the right side.
You can move a disk to any position as long the moved disk is smaller than the underlying one.

My game logic has bugs so dont await too much of it :D
One rule is missing: Only 1 disk may be moved
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Tower of Hanoi
« Reply #2 on: March 08, 2024, 01:59:11 pm »
Thank you Zvoni for your concerns about game rules!

Okay, to express the rule more strict:
You are limited to move the topmost disk to any "free" position, where "free" means that no smaller disk is already present in target.
(Since topmost can be just 1 i'd thought it's self explanary that just 1 could be moved at a time)

For that purpose I have added under each tower 2 buttons to try move a disk somewhere.
The left tower disk can be moved to the middle or to the right tower.
The middle can move to left or right.
The right can move to middle or left.

At game start the left tower occupy all disks, the other towers are free for use.

I hope all rule related questions are clarified by this text.

Wishing you all a fantabulous weekend!
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Zvoni

  • Hero Member
  • *****
  • Posts: 2818
Re: Tower of Hanoi
« Reply #3 on: March 08, 2024, 02:10:15 pm »
Thank you Zvoni for your concerns about game rules!

Okay, to express the rule more strict:
You are limited to move the topmost disk to any "free" position, where "free" means that no smaller disk is already present in target.
(Since topmost can be just 1 i'd thought it's self explanary that just 1 could be moved at a time)

For that purpose I have added under each tower 2 buttons to try move a disk somewhere.
The left tower disk can be moved to the middle or to the right tower.
The middle can move to left or right.
The right can move to middle or left.

At game start the left tower occupy all disks, the other towers are free for use.

I hope all rule related questions are clarified by this text.

Wishing you all a fantabulous weekend!
You misunderstood: I just quoted the "official" rules https://en.wikipedia.org/wiki/Tower_of_Hanoi

Since this is supposed to be a game (as opposed to a "solving"-algorithm/function), i'd probably go along the lines of (didn't really think about it):
1) Each "rod" (of those 3) is a stack, where i can push, pop and peek a disk
2) Each disk gets a numerical value from Biggest to smallest (small=3, medium=2, big=1), which is returned bei "peek" (see 1). The rod itself gets value 0 ("Empty Stack")
3) a source-rod.pop and dest-rod.push is only possible, if it satisfies source-rod.Peek>dest-rod.peek
« Last Edit: March 11, 2024, 12:23:08 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Boleeman

  • Hero Member
  • *****
  • Posts: 769
Re: Tower of Hanoi
« Reply #4 on: March 13, 2024, 08:27:55 am »
Hi KodeZwerg.

The delphiforfun web site has a working version of the game, which has 2 modes (game mode and solution mode) and uses animated moves. It converts OK to Lazarus. Was wanting just plain drag and drop, but no animated moves.

http://delphiforfun.org/programs/towers_of_hanoi3.htm




Anyhow, I thought I would share. Take care.
« Last Edit: March 16, 2024, 04:35:05 am by Boleeman »

Zvoni

  • Hero Member
  • *****
  • Posts: 2818
Re: Tower of Hanoi
« Reply #5 on: March 13, 2024, 09:49:18 am »
Also attached is the CSharp version which I really like. This version seems to work smoothly, where the drag and drops can be made quite fast when playing the game. That was the one I tried to unsuccessfully convert.

Anyhow, I thought I would share. Take care.
There are binaries in the CSharp.zip.
Remove them
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Boleeman

  • Hero Member
  • *****
  • Posts: 769
Re: Tower of Hanoi
« Reply #6 on: March 15, 2024, 07:42:19 am »
Sorry, seem to had accidentally selected the bin folder.

 

TinyPortal © 2005-2018