neovilla.blogg.se

Hanoi towers java
Hanoi towers java










hanoi towers java
  1. Hanoi towers java how to#
  2. Hanoi towers java code#

The abbreviation just serves to confusion. I think your "Cont" class means "Controller", right? There's no need to abbreviate that, just write the name out in full. Read the wikipedia entry on what a stack is and what it's operations are then I have found this (Java) which seems to be pretty good implementation.

Hanoi towers java code#

Also to answer the question on your code yes I guess it should be A.pop(), in your case your method should return an int. Then x will be int but you pass the String "x" to the method. Not sure what Cont is and they you pass a String to the movePiece method but that takes in a Stack. On the other hand from your code I am unable to really now what is going on. The Main, will take care of the inputs from the user and contain the main method. The Disk class Mainly just contain the size and then a compareTo method. For implementation I would use a private inner class call it node or something like that. My idea that came first into my head: You can make the game in three classes There are millions of ways to approach it. It depends on how you want to approach it.

Hanoi towers java how to#

So far this is what i have, i think i got the idea, but cant really implment it to java.Īlso im not native English speaker that's why i have a bad grammar.Įdit 1: now i understand how to move a piece from towers, if i use C.push(A.pop()) i move a disk from one stack to another, but i dont know how to choose between 2 stack's, for example If i can move the pieces, then i could use the peek method too see if a user can move the piece there, peek<"piece", if is true this statement then the user cant move the piece in that tower. im trying to make a method that when i call it, the player can choose between 2 towers to move a piece. first im using stacks, im adding 3 disc to a Stack. My idea to complete this task is using stacks. we can make it via console without GUI because we havent got there yet. using "model view controller"(any explanations for this will be deeply appreciated) and exceptions make a java game of hanoi towers(not solving the problem, actually moving the pieces). im new to java and all the semester my teacher had been absent and give us really short classes, so im really having trouble with anything related to java. Im kinda sick of seeing awful tutorials in youtube, the only good one i have saw was from the new Bostonįor my java class my teacher gave me the task to make a game of hanoi towers using java. In base case, we just print which ring is moving from which peg to which peg using some peg.Any advices, tutorials for stacks, books i could use will be apreciated.

hanoi towers java

Inside this method, we have two cases one is base and other is the recursive case. void tower(int n, char source, char des, char via) In the parameters of this method I have provided the number of disks and source and destination and via a peg.

hanoi towers java

I have made a method called tower which is a recursive method. I have used the concept of Recursion in this project which is very important to solve the problem of the tower of Hanoi. So, I have made this project on the tower of Hanoi in which first it takes the input of the number of disks and then it gives the output in which it displays the order in which the disks are being moved. Some take more number of moves to solve and some take less. Many people had tried this problem with a different number of disks and some succeed and some not. But it's not that simple because there is catch here, you can move one disk at a time and no larger disk can be above the smaller disk while performing shifting. Now you might be thinking this is the easiest problem you have heard off, you can just pick all the disks and put it in the destination peg without using any third peg. You have to shift all the disks from source to destination using the via a peg. There will be three pegs and every peg has its own name as the first peg is named "source" and the second peg is named "destination" and the third peg named as "via". The set up of the problem is like in the above picture. In this problem, you have to shift disk from one peg to another peg using the third peg. People had been solving it for many years.












Hanoi towers java