Question: Captain Codejack and the Treasure Box ( Hint: this problem can be solved using recursion ) JAVA This question involves a game with gold coins

Captain Codejack and the Treasure Box (Hint: this problem can be solved using recursion) JAVA
This question involves a game with gold coins from Captain Codejacks treasure box. The game starts when the captain gives you some gold coins. He also sets a target number of coins and allows you to get a fixed number of extra coins at any step. In each step of the game, you can either request additional coins (each time same amount of extra coins will be provided) or, if you have even number of gold coins in hand you can return half of your coins back to him. However, Captain Codejack allows you to have only a maximum number of steps in this game. You win the game when you end up with exactly the target number of gold coins in hand without exhausting your steps. If you lose the game, then Captain Codejack makes you walk the plank!
For example, suppose Captain Codejack gives you 60 gold coins and sets your target to be 18, and allows you maximum 6 steps. He also agrees to give you 42 coins each time you request for additional coins. Then, you can make the following moves (steps).
Begin the game with 60 gold coins.
Step 1: Ask for additional coins. You get 42 more coins making coin total to be 102.
Step 2: Ask for additional coins. You get 42 more coins making your coin total to be 144. Step 3: Return half of your coins (144/2=72) to the Captain, leaving 72 coins in your hand. Step 4: Return half of your coins (72/2=36) to the Captain, leaving 36 coins in your hand. Step 5: Return half of your coins (36/2=18) to the Captain, leaving 18 coins in your hand. You have won the game as you end up with exactly 18 coins, which was your target, in hand.
Write a Java program to solve the above problem. The solution indicates whether a win is possible.
boolean treasure_game(int startcoins, int targetcoins, int extracoins, int maxsteps)
// Postcondition: A true return value means that it is possible to win the game.
// A false return value means that it is not possible to win the coin game within the specified number of steps.
Programming Instructions:
1. Your program should allow the user to enter initial 4 values (arguments of the treasure_game()
method). These values are passed as command-line arguments in the order of the parameters. [5]
2. The program should indicate whether it is possible or, not possible to win the game. [5]
[Extra Credit: 5 points]: In case of winning possibility, display the moves/steps that lead to winning the game.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!