CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Member
Join Date
Apr 2007
Posts
33

Bagels(fermi,pico,bagels) code plz help

I need help with this assignment:

Programming Assignment #7

"Repetition and Decision-Making"

I. The Game of Bagels

This assignment is to write an interactive Java program to play the game of Bagels. Here are the rules of the game:

The computer will generate a "secret" three digit number at random. The first number will not be 0, and all the digits will be different. The user tries to guess the number. If the user guesses correctly, then the game is over.

If not, the computer gives a hint and the player tries again.

The hints:

• for each digit that matches the secret number in the proper place, the computer prints "Fermi"

• for each digit that matches, but not in the proper place, the computer prints "Pico"

• if none of the digits match, the computer prints "Bagels"

Examples (supposing that the secret number is 482):

guess = 637, Bagels

guess = 381, Fermi

guess = 382, Fermi Fermi

guess = 832, Fermi Pico

guess = 328, Pico Pico

guess = 428, Fermi Pico Pico

guess = 482, Winner! (the game is over)

When the game is over, the computer tells the user how many tries it took, and invites the user to play again.

 To avoid making it too easy for the player, you should print all Fermis first, and then the Picos, for each guess.

II. High-Level Algorithm for the Game

Generate the Secret Number.

repeat

Get User's Guess.
Evaluate User's Guess.
If not winningGuess then
Give User a Chance to Quit.

until (winningGuess or userQuits)

Print results – did user win or quit, number of guesses taken, and a comment (preferably sarcastic)

III. The Bagels Class

Your Bagels class may or may not have a constructor, but either way it will have only one other public method, playGame(). This method will call other private, “utility” methods to:

1. generate the secret number
2. evaluate the current guess and print hints
3. print game results (won or lost, number of guesses, etc)

Private methods can be called only by other methods of the class (e.g. playGame()), and not by the client code. Alternatively, you might want to call the “generate secret number” method from the class constructor, but the other methods will be called from playGame().

IV. The Client Code (i.e., the “Test Class”)

Your test class will create a Bagels object and call the method that plays the game. That is all. Repeat as long as the user wants to play another game, no matter whether she won the previous game or quit.
Last edited by Strobe; April 7th, 2007 at 01:43 PM.

2. Member
Join Date
Apr 2007
Posts
33

Re: Bagels(fermi,pico,bagels) code help

this is what i got so far:

import java.util.Random ;

public class Bagels
{
// instance variables

private int secNum ; //
private int playGuess ; //
private int rand1 ;
private int rand2 ;
private int rand3 ;
private int user1 ; //
private int user2 ; //
private int user3 ; //
private Random generator = new Random () ;

public Bagels()
{
this.secNum = secNum ;
this.playGuess = playGuess ;
do
{
secNum = generator.nextInt(900)+100 ;
rand1 = secNum/100 ;
rand2 = (secNum%100)/10 ;
rand3 = (secNum%100)%10 ;
}
while(rand1==rand2 || rand1== rand3 || rand2 == rand3) ;
System.out.println(secNum) ;

}
private int getGuess()
{
return playGuess ;
}

private void evalGuess ()
{

if(secNum == playGuess)
System.out.println("Winner! (The Game is Over)");
else
{
user1 = playGuess/100 ;
user2 = (playGuess%100)/10 ;
user3 = (playGuess%100)%10 ;
}

if(rand1 == user1)
System.out.println("Fermi") ;
if(rand2 == user2)
System.out.println("Fermi") ;
if(rand3 == user3)
System.out.println("Fermi") ;

if(rand1 == user2)
System.out.println("Pico") ;
if(rand1 == user3)
System.out.println("Pico") ;
if(rand2 == user1)
System.out.println("Pico") ;
if(rand2 == user3)
System.out.println("Pico") ;
if(rand3 == user1)
System.out.println("Pico") ;
if(rand3 == user2)
System.out.println("Pico") ;
}
public void playGame()
{
getGuess() ;
evalGuess() ;
}

}
import java.util.Scanner ;

public class BagelsTest
{
public static void main(String [] args)
{
Scanner input = new Scanner (System.in) ;
Bagels myBagels = new Bagels() ;
myBagels.playGame() ;
System.out.println("Input your guess between 100 and 999");
int playGuess = input.nextInt() ;
}

3. Re: Bagels(fermi,pico,bagels) code help

Where are you stuck?

- petter

4. Member
Join Date
Apr 2007
Posts
33

Re: Bagels(fermi,pico,bagels) code help

when i compile the program and plug in any number all it says is process complete. I print out the secretnumber just to see what happens if i input the same exact number and it just says process complete. I don't know what i'm doing wrong.

5. Re: Bagels(fermi,pico,bagels) code help

Your prog is doing just what you are telling it to do. In bagelstest it is taking in an input integer, setting it equal to an integer variable "playGuess", and then ending. Nowhere do you tell the program to process the input or loop til the game is won. You have to think this through more thoroughly. Keep at it. It will come to you w/ effort. Good luck.

Pete

6. Re: Bagels(fermi,pico,bagels) code help

Code:
```import java.util.Scanner;

public class BagelsTest
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

Bagels myBagels = new Bagels();
// this initializes your class.  no prob here.  I see that your
// random number is generated in the constructor.  That part
// looks good.

myBagels.playGame();
// here you call playgame before you get any input and without
// any parameters to compare the input w/ your random number.

System.out.println("Input your guess between 100 and 999");

int playGuess = input.nextInt();
// Finally you get some input, but then you need to do something with it.
// Again, no parameters to pass this input into the class, no ability to
// compare it w/ the original random number.
// instead the prog justs ends.
}

}```

7. Senior Member
Join Date
Apr 2004
Location
Posts
1,342

Re: Bagels(fermi,pico,bagels) code help

You put the code that allows the user to enter his guess in main(), after calling playGame(). The code is supposed to be called from playGame(), specifically from the getGuess() method. Put the input code in the getGuess() method and it'll work fine.

Code:
```private int getGuess ()
{
Scanner input = new Scanner(System.in);
System.out.println ("Input your guess between 100 and 999");
playGuess = input.nextInt();
return playGuess;
}```
(Apart from that you're missing a lot of other parts of your program, like the loop and the code that allows the user to quit, but I'm assuming you haven't finished writing your program).

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•