aspose file tools*
The moose likes Beginning Java and the fly likes Head Start Java Guessing Game Trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head Start Java Guessing Game Trouble" Watch "Head Start Java Guessing Game Trouble" New topic
Author

Head Start Java Guessing Game Trouble

Tom Mekeel
Greenhorn

Joined: Feb 14, 2006
Posts: 4
Hello--

I am having trouble compiling GuessingGame.java and GameLauncher.java. Player.java compiled fine to create Player.class.
All of the files are in the same directory...I'm stumped

Here is the code:

----GuessGame.java-----

public class GuessGame
{
Player p1;
Player p2;
Player p3;

public void startGame()
{
//create three players
p1 = new Player();
p2 = new Player();
p3 = new Player();

//create spots for the three players guesses
int guessp1 = 0;
int guessp2 = 0;
int guessp3 = 0;

//start out with the three guesses being incorrect
boolean p1isRight = false;
boolean p2isRight = false;
boolean p3isRight = false;

//create a target number for the guesses
int targetNumber = (int) (Math.random() * 10);
System.out.println ("I'm thinking of a number between 0 and 9...");


while (true) //keep going until they get it
{
System.out.println("Number to guess is " + targetNumber);

//call each players guess() method
p1.guess();
p2.guess();
p3.guess();

//get each players guess and display it
guessp1 = p1.number;
System.out.println("Player 1 guesses " + guessp1);

guessp2 = p2.number;
System.out.println("Player 2 guesses " + guessp2);

guessp3 = p3.number;
System.out.println("Player 3 guesses " + guessp3);

//check to see if someone gets it right which will end the game
if (guessp1 == targetNumber)
{
p1isRight = true;
}

if (guessp2 == targetNumber)
{
p2isRight = true;
}

if (guessp3 == targetNumber)
{
p3isRight = true;
}

if (p1isRight || p2isRight || p3isRight)
{
System.out.println("We have a winner!");
System.out.println("Player one got it right? " + p1isRight);
System.out.println("Player two got it right? " + p2isRight);
System.out.println("Player three got it right? " + p3isRight);
System.out.println("Game is over.");
break; //end loop because someone got it right
}

else //nobody got it right
{
System.out.println("Try again, nobody got it!");
}//end of conditional
}//end while loop
}//end method startGame
}//end class GuessGame

----GameLauncher.java----

public class GameLauncher
{
public static void main (String [] args)
{
GuessGame game = new GuessGame();
game.startGame();
}
}

----Player.java----

public class Player
{
int number = 0; //where the guess is stored

public void guess()
{
number = (int) (Math.random() * 10);
System.out.println("I'm guessing " + number);
}
}


Any help is GREATLY appreciated


I'm in a glass case of emotion.
Dani Atrei
Ranch Hand

Joined: Feb 17, 2004
Posts: 73
How did you do to try to compile? What error do you get?

This should work: javac *.java

Dan
[ February 19, 2006: Message edited by: Dani Atrei ]

Si altas son las torres, el valor es alto - Alberti
Tom Mekeel
Greenhorn

Joined: Feb 14, 2006
Posts: 4
Originally posted by Dani Atrei:
How did you do to try to compile? What error do you get?

This should work: javac *.java

Dan

[ February 19, 2006: Message edited by: Dani Atrei ]


Thanks for the quick reply. Here's the info out of terminal (OS X 10.4.5)

toms-power-mac-g5:~ Tom$ javac /Users/Tom/Documents/Programming/JavaPractice/Guessing\ Game/GuessGame.java
/Users/Tom/Documents/Programming/JavaPractice/Guessing Game/GuessGame.java:3: cannot resolve symbol
symbol : class Player
location: class GuessGame
Player p1;
^
/Users/Tom/Documents/Programming/JavaPractice/Guessing Game/GuessGame.java:4: cannot resolve symbol
symbol : class Player
location: class GuessGame
Player p2;
^
/Users/Tom/Documents/Programming/JavaPractice/Guessing Game/GuessGame.java:5: cannot resolve symbol
symbol : class Player
location: class GuessGame
Player p3;
^
/Users/Tom/Documents/Programming/JavaPractice/Guessing Game/GuessGame.java:10: cannot resolve symbol
symbol : class Player
location: class GuessGame
p1 = new Player();
^
/Users/Tom/Documents/Programming/JavaPractice/Guessing Game/GuessGame.java:11: cannot resolve symbol
symbol : class Player
location: class GuessGame
p2 = new Player();
^
/Users/Tom/Documents/Programming/JavaPractice/Guessing Game/GuessGame.java:12: cannot resolve symbol
symbol : class Player
location: class GuessGame
p3 = new Player();
^
6 errors

All of the files are in the Guessing Game folder. Here's the terminal info for the directory:

toms-power-mac-g5:~ Tom$ ls -a /Users/Tom/Documents/Programming/JavaPractice/Guessing\ Game
. GameLauncher.java Player.class
.. GuessGame.java Player.java


Thanks again for looking!
Eric Valley
Greenhorn

Joined: Feb 19, 2006
Posts: 1
The only thing that I can see is that your class for the game is named "GuessGame" but your Java file is "GuessingGame". If I understand it right it wont compile with different names. Thats the best I can do.

Good luck
E
Tom Mekeel
Greenhorn

Joined: Feb 14, 2006
Posts: 4
Originally posted by Eric Valley:
The only thing that I can see is that your class for the game is named "GuessGame" but your Java file is "GuessingGame". If I understand it right it wont compile with different names. Thats the best I can do.

Good luck
E


toms-power-mac-g5:~ Tom$ javac /Users/Tom/Documents/Programming/JavaPractice/Guessing\ Game/GuessGame.java

sorry i screwed that up in my description, but in reality the class and the file names are 1 and the same.
Tom Mekeel
Greenhorn

Joined: Feb 14, 2006
Posts: 4
Originally posted by Dani Atrei:
How did you do to try to compile? What error do you get?

This should work: javac *.java

Dan

[ February 19, 2006: Message edited by: Dani Atrei ]


Worked

Thanks. Is this how I have to compile multiple classes all of the time? I wonder why it doesn't link to other classes in a directory, in other words.
Thanks again for your help!
Tom
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Head Start Java Guessing Game Trouble