• Post Reply Bookmark Topic Watch Topic
  • New Topic

towers of hanoi in java (no recursion)  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

so I've got this game to code in Java. I don't need to come up with the actual recursion algorithm to solve the game as usual. Rather, the program has few simple steps:

- set the number of 'poles'
- set the number of 'disks'
- check for the current state of disks on poles
- ask for input - which disk and to which pole does the user want to move it
- validate the input against the rules
- if validated move the disk based on input, else return an error
- check for the current state of disks on poles
-repeat until solved

I am kind of new to Java. I managed to write this code with the help of my friend. I spent a lot of time on it, doing some prior research. I planned on using stack as this is an obvious example of it. But there's a lot of different ways to handle data structures in Java.. I've seen there are several types of arrays and stacks in and there isn't probably a single 'easiest' way to do it. Now my friend advised me to use a hashmap and a deque. Supposedly a deque is more advanced than a stack.

I am stuck at at point where if you run the code, it fails to validate the input for the target pole (A,B or C). I think there's an error in the validatePoleName method, but I am unable to correct it..
Also, in what way is using a hashmap and a deque more convenient than using a stack? Could I use an ArrayList that would act as a stack for this purpose?

Here's the unfinished code:
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Tyler wrote:I am kind of new to Java. I managed to write this code with the help of my friend. I spent a lot of time on it, doing some prior research. I planned on using stack as this is an obvious example of it. But there's a lot of different ways to handle data structures in Java.. I've seen there are several types of arrays and stacks in and there isn't probably a single 'easiest' way to do it. Now my friend advised me to use a hashmap and a deque. Supposedly a deque is more advanced than a stack.

I am stuck at at point where if you run the code, it fails to validate the input for the target pole (A,B or C). I think there's an error in the validatePoleName method, but I am unable to correct it..
Also, in what way is using a hashmap and a deque more convenient than using a stack? Could I use an ArrayList that would act as a stack for this purpose?

First, welcome to the ranch! We're glad you came by.

Second, gratz on properly using code tags in your first post. Well done indeed.

next...I'm not sure I'd say a deque is more advanced than a stack. Is a claw hammer more advanced than a ball-peen hammer? no. They're meant for different purposes, even if they do some of the same stuff.

WHY do you think there is an error in validatePoleName? What are you trying, what is happening, and what do you expect to happen?

Based on your questions, it seems to me that you haven't really thought through your task. Programming is really 90% THINKING, and 10% typing. you should have a good idea what you want to do before you ever write line 1 of your code. COULD you sue an ArrayList? sure. SHOULD you? I don't know. It depends on what you need to do with it. If you know you need to use a stack, why not use a stack?

Don't try and force the use of a HashMap just because someone said "You should use a HashMap". Think about what you need to do, think about what a HashMap does, and see if it makes sense. I don't see how it would help you here, but I've not spent more than about 30 seconds thinking about what you are doing.

note: this is just one old, cranky developers opinion. do not take it as any kind of gospel
 
Bartender
Posts: 1840
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing I think you need to consider given the steps you have defined.

- set the number of 'poles'
- set the number of 'disks

This seems to indicate that either one of these can change. So you could theoretically have five disks and seven poles.
Design your data structures accordingly. Having disk names of small medium and large limits you to three...


>- ask for input - which disk and to which pole does the user want to move it
Is this your wording or from your assignment?
The process that makes more sense to me is:
-- which pole do you want to move a disk from?
-- which pole do you want to move a disk to?
i.e. rather than asking for which disk, ask for which pole to move from.

I ran the code as is, and it seems to work in the 'good' case.
There is a null pointer exception right now if you don't choose a disk correctly, but otherwise it appears ok so far.
 
Mike Tyler
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response Fred.

You are absolutely right.. maybe I have just formulated my question badly. I have actually thought about this quite a lot so I know exactly what the program needs to be doing. It's just that I don't know about the different means in Java to accomplish it, so I was doing a lot of research on getting the data structure I want - 3 poles (A, B, C) with 3 disks on the pole A (small, medium, large). The goal of the game is to move all three disks to pole C according to the rules of the Hanoi towers (which I am sure you're familiar with).

I didn't manage to successfully use an online compiler for the code, so here are just screenshots of some errors from Eclipse:
The validation for the 1st user input runs alright (only s, m or l letters are accepted) but then crashes:


Here, the move is valid (taking the topmost disk from pole A to B) but it shows an error message and then doesn't even let me enter another input. (there is also an output of some of the variables - target shouldn't be null obviously)
 
Mike Tyler
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:

exactly! that's my second issue.. I am not really sure how to make the game 'adaptable' (forgot the correct term for this, but I know there is one :P ) - where it's all built based on dynamic variables that the user can change at the beginning, rather than working with set numbers and strings.

I need a little break and then will give it a go one more time.. my knowledge of Java is still quite limited, so I always end up just staring at the code blankly, feeling miserable
 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use letters for poles and numbers for disks.
 
Stefan Evans
Bartender
Posts: 1840
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Porblem #1:

Ok, so the NullPointerException is coming from your findTargetPole method.
Specifically this line of code: if(selectedDisk.equalsIgnoreCase(onTop)
Obviously the selectedDisk is null (you just got an error message about it) but it appears to be trying to use it anyway...

How do you ensure it is not null?

There are actually a couple of solutions here
1 - make sure you have a valid selected disk before you call this method
2 - modify findTargetPole so that it handles a null value for selectedDisk. One way: instead of selectedDisk.equalsIgnoreCase(onTop) how about onTop.equalsIgnoreCase(selectedDisk) (assuming onTop is not null of course)

Problem #2 Displays error message for valid move, and target is null:
You might think its a valid move, but your program doesn't.
If you fix that, then target will no longer be null.

Problem #3: Doesn't let me enter another input.
If it isn't asking for more input, you must have satisfied the conditions of the while loop.
How long does it keep asking for input?
How long should it keep asking for input?


  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!