Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Captain Fictitious and Treasure Box (Hint: this problem can be solved using recu

ID: 3889337 • Letter: C

Question

Captain Fictitious and Treasure Box (Hint: this problem can be solved using recursion) java porgram
This question involves a game with gold coins from Captain Fictitious’ 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 Fictitious allows you to have only a maximum number of steps in this game. You win the game if you end up
with exactly the target number of gold coins in hand without exhausting your steps.
For example, suppose that Captain Fictitious 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.
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). Use
appropriate message to indicate the user to enter the values.
2. The program should indicate whether it is possible or, not possible to win the game.
[Extra Credit: 5 points]: In case of winning possibility, display the steps to follow to win the game.

Explanation / Answer

import java.util.Scanner;

public class CaptainFictitiousAndTreasureBox {

   int totalCoins=0;
   int coinsInHand=0;
   int finalCoins=0;
   public boolean treasure_game(int startcoins, int targetcoins, int extracoins, int maxsteps){
      
      
       if(maxsteps>4){
       totalCoins=startcoins;
       System.out.println("totalCoins: "+totalCoins);
       maxsteps=maxsteps-1;
       boolean res=treasure_game(startcoins+extracoins, targetcoins, extracoins, maxsteps);
       return res;
       }
       if(maxsteps>0){
           finalCoins=startcoins;
           System.out.println("finalCoins: "+finalCoins);
           maxsteps=maxsteps-1;
           boolean res=treasure_game(startcoins/2, targetcoins, extracoins, maxsteps);
           return res;  
       }
      
       if(finalCoins==targetcoins){
           System.out.println("it is possible to win the game.");
           return true;
       }
       else {
           System.out.println("it is not possible to win the game");
           return false;
       }
   }
  
   public static void main(String[] args) {
      
       Scanner scn=new Scanner(System.in);
   System.out.println("Enter Captain Fictitious given Initial/Starting Coins:");
   int initialCoins=scn.nextInt();
   System.out.println("Enter target coins:");
   int targetCoins=scn.nextInt();
   System.out.println("Enter extra coins : ");
   int extraCoins=scn.nextInt();
   System.out.println("Enter Max. steps:");
   int maxSteps=scn.nextInt();
  
   CaptainFictitiousAndTreasureBox treasureBox=new CaptainFictitiousAndTreasureBox();
   boolean result=treasureBox.treasure_game(initialCoins, targetCoins, extraCoins, maxSteps);
      

   }
  
}

/*

case1:

Enter Captain Fictitious given Initial/Starting Coins:
60
Enter target coins:
18
Enter extra coins :
42
Enter Max. steps:
5
totalCoins: 60
finalCoins: 102
finalCoins: 51
finalCoins: 25
finalCoins: 12
it is not possible to win the game


case2:

Enter Captain Fictitious given Initial/Starting Coins:
60
Enter target coins:
18
Enter extra coins :
42
Enter Max. steps:
6
totalCoins: 60
totalCoins: 102
finalCoins: 144
finalCoins: 72
finalCoins: 36
finalCoins: 18
it is possible to win the game.


*/