Question

c++ launguage please help The game Pico Fermi Bagel is a number guessing game. The computer...

c++ launguage please help

The game Pico Fermi Bagel is a number guessing game. The computer picks a secret number: the secret number must be 3 digits, none of the digits can be the same, and it cannot start with a zero. 104 is OK, but 091 and 212 are not. The user tries to guess the secret number - if none of the digits in the user's number are in the secret number, then the program replies with BAGEL (or B). For each digit in the user's number that is in the secret number but not in the right place value, the program replies PICO (or P). For each digit in the user's number that is in the secret number and is in the right place, the program replies FERMI (or F). When the user has Fermi Fermi Fermi (FFF) the secret number has been found.

In a file, write the secret number, then write the user guess and its result (e.g. 123 B, 456 FF, 789 P, 846 FFF). When the user wins, write the number of guesses it took. Allow the user to quit the program and allow the user to play again. If the user quits, display the secret number.

After your project is complete, you will be assessed on the following:

  • Generating random numbers
  • Use of the modulus
  • Text file operations : writing a file
  • Conditionals : if, if-else, switch
  • Loops : while, do-while, for
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<fstream>
using namespace std;
//method to generate the random number
int generate()
{
    int n,a,b,c,opt;
    while(1)
    {
    n = rand() % ( 999 - 100 + 1 ) + 100;
    a=n%10;
    b=(n/10)%10;
    c=n/100;
    if(a!=b && b!=c && a!=c && c!=0)
    break;
   }
    return n;
}
//driver program
int main()
{
   int n,a,b,c,m,i,j,opt,count=0,x,y;
   //declare file object for write purpose
    ofstream outfile;
    //open a file for write purpose
    outfile.open("output.txt");
    //condition for unable to open
    if(!outfile)
    {
       cout<<endl<<"Unable to open the file";
       exit(0);
   }
   //loop to control the menu operations
   while(1)
   {
      cout<<endl<<"1. PLAY 0. EXIT";
      cout<<endl<<"Enter your choice";
      cin>>opt; //read the choice
      count=0;
      if(opt==1) //condition for play
      {
      n=generate(); //generate the random number
      cout<<endl<<"Generated number "<<n;
        
      for(;;count++) //loop till user enter the correct number
      {
      //read the number from user
      do
      {
      cout<<endl<<"Enter a three digit number";
      cin>>m;  
       }while(m<100 || m>999);//validate the users entry number
   if(n==m)   //if the number same with generated number
   {
   cout<<endl<<n<<" "<<m<<" "<<"FFF"; //write FFF
   outfile<<endl<<n<<" "<<m<<" "<<"FFF"; //write FFF
   cout<<endl<<"Number of guesses : "<<count+1;
   outfile<<"Number of Guesses : "<<count+1;
   break;  
   }
   else
       {
           c=0;//set count to 0
           i=n;//assign the generated numbet to i
           j=m;//assign user entry number to j
           while(j!=0)
           {
              a=(j%10);
              i=n;
              while(i!=0)
              {
                 if(a==i%10)
                 c++;
                 i=i/10;
               }
               j=j/10;
           }
             
       b=0;//count the similarity of digits in relative position
           //check for relative positions
           for(i=n,j=m;i!=0;i/=10,j/=10)
           {
               x=i%10;
               y=j%10;
               if(x==y)
               b++;  
           }
             
               if(c==3)//digits of guessed number is in generated number but not in relative position
               {
                  //cout<<endl<<n<<" "<<m<<" "<<"P";//write into console
                  outfile<<endl<<n<<" "<<m<<" "<<"P";//write into file
               }
                        
           else
           if(b==2)//two digits are in relative positions
           {
           // cout<<endl<<n<<" "<<m<<" "<<"FF";
               outfile<<endl<<n<<" "<<m<<" "<<"FF";  
               }
             
           else
           if(b==1)//single digit is relative positions
           {
           // cout<<endl<<n<<" "<<m<<" "<<"F";
                   outfile<<endl<<n<<" "<<m<<" "<<"F";  
                   }
             
           else
           {
               //cout<<endl<<n<<" "<<m<<" "<<"B";
               outfile<<endl<<n<<" "<<m<<" "<<"B";
           }
             
          
       }
  
           }
   }
   else
   if(opt==0)
   break;
}
outfile.close();//close the file
}

output

contents of file

Add a comment
Know the answer?
Add Answer to:
c++ launguage please help The game Pico Fermi Bagel is a number guessing game. The computer...
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
  • Please do it by C++ programming! By completing this project, you will demonstrate your understanding of:...

    Please do it by C++ programming! By completing this project, you will demonstrate your understanding of: 1) Arithmetic operations (addition, subtraction, multiplication, division, modulus) 2) Conditional statements (If, If-Else, If-ElseIf-Else, Conditional Operator, Switch) 3) Precondition, postcondition, and indexing loops (Do-While, While, For) 4) Standard text file operations (open, close, read, write) 5) Modularizing code by breaking into functions (including functions with input parameters, and ones that return a specific data type) There is a number guessing game similar to MasterMind...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • I need help with this assignment. Please include comments throughout the program. Thanks Develop an algorithm...

    I need help with this assignment. Please include comments throughout the program. Thanks Develop an algorithm and write the program in java for a simple game of guessing at a secret five-digit code. When the user enters a guess at the code, the program returns two values: the number of digits in the guess that are in the correct position and the sum of those digits. For example, if the secret code is 13720, and the user guesses 83521, the...

  • Write a program that allows a user to play a guessing game. Pick a random number...

    Write a program that allows a user to play a guessing game. Pick a random number in between 1 and 100, and then prompt the user for a guess. For their first guess, if it’s not correct, respond to the user that their guess was “hot.” For all subsequent guesses, respond that the user was “hot”if their new guess is strictly closer to the secret number than their old guess and respond with“cold”, otherwise. Continue getting guesses from the user...

  • Using C programming REQUIREMENTS: This program is a letter guessing game where a simple Al opponent...

    Using C programming REQUIREMENTS: This program is a letter guessing game where a simple Al opponent generates a secret letter for the user to guess. The user must be able to take turns guessing the secret letter, with the Al responding to the user's guesses. After successfully guessing, the user must be allowed to play again. The Al must count how many turns the user has taken. Here is an example of a game in progress where the human user...

  • Write a guessing game where the user has to guess a secret number. After every guess...

    Write a guessing game where the user has to guess a secret number. After every guess the program tells the user whether their number was too large or too small. At the end the number of tries needed should be printed. It counts only as one try if they input the same number multiple times consecutively. Using PYTHON.

  • Write a C++ program to play the number guessing game with user as follows.

    Write a C++ program to play the numberguessing game with user as follows.The user determines a number between 1~100.The program has 4 tries to guess the number byproviding a range (low, high) on each try.The user tells the program whether the number isbelow, within, or above the range.The program announces a game loss after 4 incorrecttries.

  • JAVA program. I have created a game called GuessFive that generates a 5-digit random number, with...

    JAVA program. I have created a game called GuessFive that generates a 5-digit random number, with individual digits from 0 to 9 inclusive. (i.e. 12345, 09382, 33044, etc.) The player then tries to guess the number. With each guess the program displays two numbers, the first is the number of correct digits that are in the proper position and the second number is the sum of the correct digits. When the user enters the correct five-digit number the program returns...

  • Code in JAVA.................... Guess the Number In this activity, you will write a REST server to...

    Code in JAVA.................... Guess the Number In this activity, you will write a REST server to facilitate playing a number guessing game known as "Bulls and Cows". In each game, a 4-digit number is generated where every digit is different. For each round, the user guesses a number and is told the exact and partial digit matches. An exact match occurs when the user guesses the correct digit in the correct position. A partial match occurs when the user guesses...

  • A simple game of chance Using a while loop write a simple coin flip game. Specifics:...

    A simple game of chance Using a while loop write a simple coin flip game. Specifics: The user starts with a bank of $10.00 It costs a dollar to play A correct guess pays $2.00 The while loop is used for the game loop. It should ask if the user wants to play. The while loop will check for a value of 'Y' or 'N'. You should also allow for lower case letters to be input. You MUST prime the...

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