programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Writing a compuer algorithm to solve a problem within 3 steps

Greenhorn
Posts: 25
Hey there folks, for those who remember me, I posted a topic earlier this weekend on how to write a program that allows the user to take one set of toothpicks to another in order to solve it in 3 steps. The second part was having the computer solve the problem itself, which I also wrote.

However, this 3rd step is much more complicated or maybe not? I am supposed to prompt the user to enter in 3 numbers for 3 stacks. for example, A B C, user enters 21 for stack A, 25 for stack B and 88 for stack C.

I am then supposed to have the computer solve the problem for me, meaning in 3 steps, the computer moves the "toothpicks" into the correct stack to have an average of the 3 at the last step. The trick is, a stack can only be moved to another stack if the stack you are moving it from has MORE than the stack you are moving to. So for example, I can't move 21 (in A) to 25 (in B) because A doesn't have enough to double the size of B. But I can move B to A, leaving 4 in B, and 42 in A.

I am supposed to show these 3 moves and somehow write the program. I have never done anything like this before. Can anyone help me please?? here is my code so far.

Author
Rancher
Posts: 12617
If you already have the logic for solving the puzzle, how is it different if the number of toothpicks are different?

Cal Mil
Greenhorn
Posts: 25
well, when I wrote the code for the computer to solve it. It was easy because I already had the number of toothpicks in each puzzle so I just did simple math to add,subtract and println. In this one, the user enters the number and the computer solves it for the user. So I am unsure of how to write code for the computer to make the correct moves in order to solve it (or not solve it if the number isnt divisible by 3). My friend said something about "brute force" using every possible solution, so with 3 choices would make 6*6*6 making 216 possible moves, but I don't know how to implement this.

David Newton
Author
Rancher
Posts: 12617
The number of choices varies depending on how many toothpicks are in each pile.

Were you given any hints, suggestions of data structures to use, etc?

Cal Mil
Greenhorn
Posts: 25
nope! nothing at all. it's actually far beyond the scope we even reached in class (we have only had 3 class sessions). The professor just said:

"Have your program go through all possible sequences of moves. There's not that many, since we have max 3 moves.

For each move there are 3 initial source stacks, and for each of those there are 2 destination stacks, for a total of 6 possibilities for each move.

For 3 moves, that makes 6 x 6 x 6 moves, for a total of 216 possible different move sequences.

Think of writing the code to handle a single move. Inside each of those moves again "repeat" this code, which in effect handles each of the second moves. Within each of these again "repeat" this code, which has the effect of handling the 3rd move."

No real example. My first two parts of the code are 85% of the grade, so I have a B at the moment (if I don't get docked points for silly things). But I want that A!.

Cal Mil
Greenhorn
Posts: 25
I also posted a comment in my old thread (yesterday) but I will re-post here. I wanted to say sorry about being rude in my previous post to you (Newton) and Wouter. I was getting mad that I was having so much trouble, but after going through all this process I learned a lot and I wanted to say sorry and thank you for your suggestions and help. It has helped me as a guide.

David Newton
Author
Rancher
Posts: 12617
No need to apologize; I'm not always the most patient answerer :)

Have you covered recursion at all? Have you determined how to figure out each legal move for a given configuration of toothpicks?

Marshal
Posts: 56608
172
Thank you for the apology

Cal Mil
Greenhorn
Posts: 25
np. and nope, we have not covered recursion whatsoever, we just started the semester and have only 3 classes and all we did was review println statements and some loops, that's why im so surprised we have this complex program due tomorrow already. and I got work in an hour :X

Bartender
Posts: 2700
There is no need to apologize. We all get frustrated by code from time to time. Especially in the begin of learning how to code.

 It is sorta covered in the JavaRanch Style Guide.