Captain Fictitious and Treasure Box (Hint: this problem can be solved using recu
ID: 3889295 • Letter: C
Question
Captain Fictitious and Treasure Box (Hint: this problem can be solved using recursion)
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
override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = UIColor.blackColor() } override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) setupMainComponents() } private func setupMainComponents() { setupCaptureCameraDevice() setupPreviewLayer() setupMotionManager() setupGestureRecognizer() setupDismissButton() } let captureSession = AVCaptureSession() let motionManager = CMMotionManager() var previewLayer: AVCaptureVideoPreviewLayer! // The following four are already created for you! var treasure: Treasure! var foundImageView: UIImageView! var dismissButton: UIButton! var foundTreasure = false // The following two are not--make sure to copy/paste them into Xcode var quaternionX: Double = 0.0 { didSet { if !foundTreasure { treasure.item.center.y = (CGFloat(quaternionX) * view.bounds.size.width - 180) * 4.0 } } } var quaternionY: Double = 0.0 { didSet { if !foundTreasure { treasure.item.center.x = (CGFloat(quaternionY) * view.bounds.size.height + 100) * 4.0 } } } extension ViewController { private func setupCaptureCameraDevice() { let cameraDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) let cameraDeviceInput = try? AVCaptureDeviceInput(device: cameraDevice) guard let camera = cameraDeviceInput where captureSession.canAddInput(camera) else { return } captureSession.addInput(camera) captureSession.startRunning() } }