• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Head Start Java Guessing Game Trouble

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Tom Mekeel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic