Question

C++

For this program youll write a program that uses a recursive function. This program plays a simple token-taking game. The ruWould you like to play again? Y/N] Enter the number of tokens you want to reach: 79 What is the number of turns 10 Searching

For this program you'll write a program that uses a recursive function. This program plays a simple token-taking game. The rules are simple 1. 2. You start the game with exactly 13 tokens. On each turn, you may do one of two things You may ask for exactly 25 more tokens; or IF the number of tokens you have is an even number, you may give back exactly half of the tokens you have a. b. 3. The object of the game is to reach exactly K tokens within N turns, where K and Nare specified at the start of each game Think about this problem recursively; if the goal is to reach, say, 34 tokens within 10 turns, andI begin with 13 tokens and choose to take 25 more, then I have 38 tokens and 9 turns left. Your program must be recursive to get full credit. Your program should ask the user for the goal number of tokens, and the number of turns allowed. It should then search and either report to the user that the total cannot be reached within the desired number of turns, or the exact sequence of moves for doing so, in order from the start state .There may be more than 1 path from the starting state to the winning state; you do not need to find them all, count them all, or find the shortest one, only find a valid path within the specified number of turns if one exists Output Sample: Enter the number of tokens you want to reach: 34 What is the number ofturns 10 Searching for solution within 10 turn (s) . . . Adding 25, you get 38 tokens Reducing by half, you get 19 tokens. Adding 25, you get 44 tokens. Reducing by hall You get 22 tokens Reducing by half, you get 11 tokens. Adding 25, you get 36 tokens Reducing by half, you get 18 tokens. Reducing by half, you get 9 tokens. Adding 25, you get 34 tokens Solution found with 1 turn (s) remaining.
Would you like to play again? Y/N] Enter the number of tokens you want to reach: 79 What is the number of turns 10 Searching for solution within 10 turn (s). . Adding 25, you get 38 tokens. Reducing by half, you get 19 tokens. Adding 25, you get 44 tokens. Reducing by half, you get 22 tokens. Reducing by half, you get 11 tokens. Adding 25, you get 36 tokens. Reducing by half, you get 18 tokens. Reducing by half, you get 9 tokens. Adding 25, you get 34 tokens. Reducing by half, you get 17 tokens. Sorry, solution wasn't found within allotted turns Would you like to play again? Y/N] Thanks for playing!
0 0
Add a comment Improve this question Transcribed image text
Answer #1
Explanation:
  • Code in C++ is given below
  • Please read comments for better understanding of the code
  • Output is given at the end of the code

Code in C++::

#include <iostream>

using namespace std;

void token(int k,int n,int tokenValue);

int main() {

  /**

  * Two integer variables named k and n are declared below.

  * k is used to store the token to reach

  * n stores the number of turns

  */

  int k,n;

  /**

  * Following we use while loop to take input from

  * user until user do not want to exit

  */

  while(true){

    /**

    * First we ask user to enter the value of k

    * Next we ask for the number of turns

    */

    cout<<"Enter the number of tokens you want to reach:"<<endl;

    cin>>k;

    cout<<"What is the number of turns:"<<endl;

    cin>>n;

    cout<<"Searching for solution within "<<n<<" turn(s)..."<<endl;

    /**

    * An variable named tokenValue is declared

    * initialized to 13

    */

    int tokenValue=13;

    /**

    * Now we call the recursive function named token()

    */

    token(k,n,tokenValue);

    /**

    * Prompting user if would like to play again

    */

    cout<<"Would you like to play again? [Y/N]"<<endl;

    char ch;

    cin>>ch;

    if(ch=='N' || ch=='n'){

      cout<<"Thanks for playing!"<<endl;

      /*

      * We break the while loop

      */

      break;

    }

  }

}

void token(int k,int n,int tokenValue){

  /**

  * First check if n is 0 which means all turns are taken

  */

  if(n==0){

    cout<<"Sorry, solution wasn't found within allotted turns."<<endl;

  }else{

    /**

    * Else first we check if tokenValue is equal to k

    * If yes then we print success message and turns taken

    */

    if(tokenValue==k){

      cout<<"\nSolution found with "<<n<<" turn(s) remaining."<<endl;

    }else if(tokenValue%2==0){

      /**

      * tokenValue is even then we divide it by 2 and call the function with n-1

      */

      tokenValue=tokenValue/2;

      cout<<"Reducing by half, you get "<<tokenValue<<" tokens."<<endl;

      token(k,n-1,tokenValue);

    }else{

      /**

      * If tokenValue is odd then we add 25 and

      * call the function recursivily as n-1

      */

      tokenValue+=25;

      cout<<"Adding 25, you get "<<tokenValue<<" tokens."<<endl;

      token(k,n-1,tokenValue);

    }

  }

}

OUTPUT::

Enter the number of tokens you want to reach:

34

What is the number of turns:

10

Searching for solution within 10 turn(s)...

Adding 25, you get 38 tokens.

Reducing by half, you get 19 tokens.

Adding 25, you get 44 tokens.

Reducing by half, you get 22 tokens.

Reducing by half, you get 11 tokens.

Adding 25, you get 36 tokens.

Reducing by half, you get 18 tokens.

Reducing by half, you get 9 tokens.

Adding 25, you get 34 tokens.

Solution found with 1 turn(s) remaining.

Would you like to play again? [Y/N]

y

Enter the number of tokens you want to reach:

79

What is the number of turns:

10

Searching for solution within 10 turn(s)...

Adding 25, you get 38 tokens.

Reducing by half, you get 19 tokens.

Adding 25, you get 44 tokens.

Reducing by half, you get 22 tokens.

Reducing by half, you get 11 tokens.

Adding 25, you get 36 tokens.

Reducing by half, you get 18 tokens.

Reducing by half, you get 9 tokens.

Adding 25, you get 34 tokens.

Reducing by half, you get 17 tokens.

Sorry, solution wasn't found within allotted turns.

Would you like to play again? [Y/N]

n

Thanks for playing!

Please provide the feedback!!

Thank You!!

Add a comment
Know the answer?
Add Answer to:
For this program you'll write a program that uses a recursive function. This program plays a simp...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • URGENT!!!!! I need to develop a C++ program that uses a recursive function to solve this...

    URGENT!!!!! I need to develop a C++ program that uses a recursive function to solve this maze problem: (No classes please!!!!) A robot is asked to navigate a maze. It is placed at a certain position (the starting position) in the maze and is asked to try to reach another position (the goal position). Positions in the maze will either be open or blocked with an obstacle. Positions are identified by (x,y) coordinates. At any given moment, the robot can...

  • Write a JAVA program that plays a number guessing game with the user. A sample run...

    Write a JAVA program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in boldface in the sample run. Welcome to the game of Guess It! I will choose a number between 1 and 100. You will try to guess that number. If your guess wrong, I will tell you if you guessed too high or too low. You have 6 tries to get the number. OK, I am...

  • **IN JAVA** Make a Game -Game Strategy The program and the user alternate turns, picking up...

    **IN JAVA** Make a Game -Game Strategy The program and the user alternate turns, picking up one, two, or three straws on each turn. The program goes first. The player to pick up the last straw loses. -Game process Your program should start by generating and displaying a random integer (which represents the number of straws) between 10 and 20 inclusive. If this number is 1 more than a multiple of 4, add 1. For example, if the random integer...

  • Hi please help with this program C++ language thanks Write a recursive solution to solve the...

    Hi please help with this program C++ language thanks Write a recursive solution to solve the Partition problem. Partition: Given a set A of n integers a, a, .....a., can you find a subset A_1 of integers such that the sum of the Integers in A_1 is half of the Sum of all the n Integers (that Is, sigma_alpha 1 A_1 a_i)? Input: 1.Number of integers of set A: n 2.n Integers: a_1 a_2, a_n. Output: Print out yes if...

  • python code( using functions please)! Rules of the game: - This game requires a dice. The...

    python code( using functions please)! Rules of the game: - This game requires a dice. The goal of the game is to collect the correct number of coins to create a dollar. Players take coins based on a roll of the dice. The numbers on the dice correlate to the coin values as follows:  1—penny  2—nickel  3—dime  4—quarter  5— ( pick a random card from 1 to 4): o 1 = penny o 2 =...

  • Write a JAVA program that plays a simple Pac-man game. The game plays on a 10...

    Write a JAVA program that plays a simple Pac-man game. The game plays on a 10 by 10 grid (absolutely, you can make it bigger). Pac-man is depicted as “#” and others as “*”. Please see the below.   You should ask how many pac-man plays before starting the game. Then, you should create the number of pac-man objects. The pac-mans are defined as classes, and the objects are stored in an array. The pac-man class has a “move()” method, which...

  • Option 2: Conwav's Game of Life Your task is to write a program that plays Conway's...

    Option 2: Conwav's Game of Life Your task is to write a program that plays Conway's Game of Life (for details and ex amples, see https://en.vikipedia.org/wiki/Convay's.Game of Life). The basic gam ts played on each generation, each cell changes according to the following instructions: nal grid of square cells, each of which is either alive or dead. With 1. Any dead cell with exactly three live neighbors (out of its eight nearest neighbors) comes to life [reproduction]. 2. Any live...

  • python Exercise: ex9.py Write a program that plays a number and you guess the number. The...

    python Exercise: ex9.py Write a program that plays a number and you guess the number. The program helps you with a response 1t the number you guessed is higher or lower than the mystery number. To generate a random number, we need to import some routines. guessing game. It generates a random mystery import random mysteryNumber random.randint (1, 100) The random.randint (1, 100) creates a random integer between 1 and 100. Here is a sample output: Let's play the guessing...

  • This program will implement a simple guessing game... Write a program that will generate a random...

    This program will implement a simple guessing game... Write a program that will generate a random number between 1 and 50 and then have the user guess the number. The program should tell the user whether they have guessed too high or too low and allow them to continue to guess until they get the number or enter a 0 to quit. When they guess the number it should tell them how many guesses it took. At the end, the...

  • How to solve this Problem in C++ . The Problem Write program that uses a class...

    How to solve this Problem in C++ . The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of items 2. Get the number of items in the set...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT