-
April 7th, 2007, 01:29 PM
#1
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.
-
April 7th, 2007, 01:30 PM
#2
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() ;
}
-
April 7th, 2007, 01:51 PM
#3
Re: Bagels(fermi,pico,bagels) code help
Please use code-tags.
Where are you stuck?
- petter
-
April 7th, 2007, 02:03 PM
#4
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.
-
April 7th, 2007, 02:57 PM
#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
-
April 7th, 2007, 02:59 PM
#6
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).
Old Unix programmers never die, they just mv to /dev/null
-
April 7th, 2007, 03:02 PM
#7
Re: Bagels(fermi,pico,bagels) code help
Further comments:
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.
}
}
-
April 7th, 2007, 03:12 PM
#8
Re: Bagels(fermi,pico,bagels) code help
thanks alot for every ones assistance i'm going to try your suggestions and i'll let you guys know how it goes. Again i really appreciate your input.
-
April 7th, 2007, 03:41 PM
#9
Re: Bagels(fermi,pico,bagels) code help
i took your suggestions i put the playguess input into getGuess(). Then in the main i put this:
public class BagelsTest
{
public static void main(String [] args)
{
Bagels myBagels = new Bagels() ;
myBagels.playGame() ;
}
}
Now it actually is evaluating my numbers now i know i need to use a do while loop in this program and i need it to print out bagels if no fermi or pico but how do i implement that. I'm sorry i'm new at this
Last edited by Strobe; April 7th, 2007 at 03:44 PM.
-
April 7th, 2007, 04:04 PM
#10
Re: Bagels(fermi,pico,bagels) code help
this is my code now does everything except ask players if they want to quit or play again and keep track of how many tries it takes to win.
import java.util.Random ;
import java.util.Scanner ;
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()
{
Scanner input = new Scanner(System.in);
System.out.println ("Input your guess between 100 and 999");
playGuess = input.nextInt();
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") ;
if(user1!=rand1||user1!=rand2||user1!=user3&&user2!=rand1||user2!=rand2||user2!=rand3||user3!=rand1||user3!=rand2||user3!=rand3)
System.out.println("Bagels") ;
}
public void playGame()
{
getGuess();
evalGuess();
}
}
MY TEST CLASS IS:
public class BagelsTest
{
public static void main(String [] args)
{
Bagels myBagels = new Bagels() ;
myBagels.playGame() ;
}
-
April 7th, 2007, 04:18 PM
#11
Re: Bagels(fermi,pico,bagels) code help
Three pieces of advice:
- put a loop in your game that continues til the game is over.
- put all code in code tags. The better we are able to read your code, the better we can help.
- Read on the section on doing homework. Remember that the only way to learn is to struggle with it yourself for a while. Effort really pays off.
Good luck!
Last edited by petes1234; April 7th, 2007 at 04:44 PM.
-
April 7th, 2007, 07:07 PM
#12
Re: Bagels(fermi,pico,bagels) code help
-
April 7th, 2007, 11:56 PM
#13
Re: Bagels(fermi,pico,bagels) code help
Other suggestions:
- Consider testing for and rejecting entries w/ duplicate digits. If not a guess of 511 with a secret number of 301 will result in Fermi Pico when it should probably be just Fermi.
- Consider keeping a running tally of fermiCnt and picoCnt. You could then print the required number of "Fermi" and "Pico" at the end of testing if desired. The main benefit would be to easily test for no matches:
Code:
if (fermiCnt == 0 && picoCnt == 0)
{
System.out.print("bagels ");
}
- Finally, consider breaking your secret number and guess number down into two arrays of 3 ints. That way you can do an elegant check for matches using nested for loops. While testing for (array1[i] == array2[j]), lets you decide if a match is found, as an added benefit testing for (i == j) lets you decide whether a match is a Fermi or a Pico.
Last edited by petes1234; April 8th, 2007 at 12:06 AM.
-
April 8th, 2007, 09:43 AM
#14
Re: Bagels(fermi,pico,bagels) code help
First, i would like to tell you thank you very much for your help. I took your advice and added some code that rejected entries with duplicate digits,now it works good for everything except when i plug in a winning number it's giving me that message try again no digit may be the same.Now as far as keeping count of fermis and picos, how would i go about that ? As far as arrays i still am not too familiar with that my teacher just covered that but not expected on this assignment. What i would of liked is to implement a do while statement
where i would create an instance variable called goodGuess which i would initialize as false. So then i'd do something like:
do
{
loop body
}
while(goodGuess) ;
Do this in my playGame() method. How does that sound?Oh i also wanted to keep count of the guesses thinking about adding count++ to my getGuess() method does that sound about right?
Last edited by Strobe; April 8th, 2007 at 09:53 AM.
-
April 8th, 2007, 10:02 AM
#15
Re: Bagels(fermi,pico,bagels) code help
I like your loop plan. I've been taught to always have the condition check at the top of the loop (though to be honest, I'm not sure why,... probably easier to read). Myself, I usually do it like so:
Code:
public static void main(String[] args)
{
Scanner myScan = new Scanner(System.in);
boolean keepPlaying = true;
while (keepPlaying)
{
Bagels myBagels = new Bagels();
myBagels.playGame();
// request input and check it,
// then set keepPlaying to true or false
}
}
Also, count++ will do nicely as well. If you do loop your program, make sure that you re-initialize the secret number, the count, etc...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|