Questions tagged [towers-of-hanoi]

A simple puzzle involving moving discs between rods. Commonly used as a programming kata/exercise and frequently set as homework when introducing the concept of recursion.

The Tower of Hanoi or Towers of Hanoi , also called the Tower of Brahma or Towers of Brahma, is a mathematical game or puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.

The objective of the puzzle is to move the entire stack to another rod, obeying the following rules:

  • Only one disk may be moved at a time.
  • Each move consists of taking the upper disk from one of the rods and sliding it onto another
  • rod, on top of the other disks that may already be present on that rod.
  • No disk may be placed on top of a smaller disk.
310 questions
-2
votes
1 answer

Towers of hanoi (moving discs)

Im trying to make the game Towers of hanoi with pygame. My biggest struggle is now that i have to move the disks, this is my code so far, but it is not moving the disks and i dont know why. I think it is also registering only the…
Tycho Atsma
  • 61
  • 1
  • 2
  • 10
-2
votes
2 answers

non-static method towersOfHanoi(int, int, int, int) cannot be referenced from a static context?

When i compile the tester, it says that in line 9: non-static method towersOfHanoi(int, int, int, int) cannot be referenced from a static context Why cant it reach the towersOfHanoi method? I provided the two classes below. import java.io.*; import…
-2
votes
2 answers

Approach while learning Algorithms

I am learning algorithms and came to this Hanoi Tower. I know how to do that practically. But I am unable to code it. I haven't read the given code yet. I am trying it first myself. What should be my approach-keep trying or read the given code or…
Temp Id
  • 229
  • 1
  • 2
  • 6
-3
votes
1 answer

Recursive Python code for Tower of Hanoi works for up to 3 disks, but not more

I am trying to implement Tower of Hanoi using recursion with my own approach. I don't know if it is a valid solution at all. But it definitely works 1, 2, and 3 disks. In fact, given a number of disks more than 3, it gives the right moves for…
KramerDK
  • 15
  • 7
-3
votes
1 answer

Obtaining number of moves in Towers of Hanoi

Given this method, public static String solve(int n, String start, String middle, String end) I want to return the number of moves. I can somewhat obtain this with this code: public static String solve(int n, String start, String middle, String…
kprog
  • 59
  • 2
  • 8
-3
votes
4 answers

An error in my codes. I cant find that

import java.io.*; import java.util.Scanner; public class Helloworld{ public static BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); public static void main(String[]args) throws IOException{ …
-3
votes
1 answer

Towers of hanoi, with every move shown

I need to write a recursive towers of hanoi program, which I have done, but it needs to output the positions of the discs represented by stars and that's what I'm having trouble with. It needs to output a picture representation of the discs'…
-4
votes
1 answer

how this recursion is reaching the cout statement ?

I have this function to solve the Tower of Hanoi problem and fortunately It's working good but can anybody explain to me if the function is calling it self before the cout statement in case m!=0 then how does it ever reach the cout statement or even…
user10430883
-6
votes
1 answer

Hanoi Towers - another output

for Towers of Hanoi, the output should look like this: ?-move(3,[1,2,3],[],[],A1,B1,C). A1=[]. B1=[1,2,3] C=[]. I know how to do this: move(1,[H|T],B,C,A1,B1,C) :- A1 = T, B1 = [H|B], …
user1400451
  • 87
  • 1
  • 8
1 2 3
20
21