Thread: java help
View Single Post
Old 02-08-2007, 10:29 PM   #1 (permalink)
Baek
Registered User
 
Baek's Avatar
 
Join Date: May 2006
Posts: 86
-1 Internets
java help

Yes this is a homework question.

This program makes a random point then the user inputs a guess to try and guess the point. The computer gives you hints as to which direction you should go (north south east west, north west, south west, etc.) I cant find a way to print out to the user which direction they should go. Ive made 4 strings based on the users guess and the real (computer generated) point that tell the user to go n, s, e, w. The only way I can see is to have about 16 lines under each hot, warm or cold hint telling the user every possible direction they should go. If anyone has an idea on how to do this I will be very thankful!



This current code compiles. Using jdk 1.60 and jre 1.60.
Code:
import java.util.*; import java.awt.*; public class GuessingGame { public static final int MAX_DISTANCE = 20; public static void main(String[] args) { Scanner console = new Scanner(System.in); intro(); String answer = "y"; while (answer.toLowerCase().startsWith("y")) { int gamesPlayed; playGame(console); answer = playAgain(console); } } public static String playAgain(Scanner console) { System.out.print("Play again? "); String answer = console.next(); System.out.println(); return answer; } public static void playGame(Scanner console) { Random r = new Random(); int computersNumberX; // A random number picked by the computer. int computersNumberY; int usersGuessX; // A number entered by user as a guess. int usersGuessY; int guessCount; // Number of guesses the user has made. int gamesPlayed; computersNumberX = r.nextInt(MAX_DISTANCE) + 1; computersNumberY = r.nextInt(MAX_DISTANCE) + 1; Point computersPoint = new Point (computersNumberX, computersNumberY); System.out.println(computersPoint); guessCount = 0; System.out.println("Guess x and y "); while (true) { usersGuessX = console.nextInt(); // get the user's guess usersGuessY = console.nextInt(); guessCount++; double distance = Math.sqrt(((usersGuessX - computersNumberX) * (usersGuessX - computersNumberX)) + ((usersGuessY - computersNumberY) * (usersGuessY - computersNumberY))); if (distance == 0) { System.out.println("You got it in " + guessCount + " guesses!"); break; // the game is over; the user has won } // If we get to this point, the game continues. add another if thingy if u find another place to break // Tell the user if the guess was too high or too low. // tells user to go north, south, east, or west, based on their guess. //THIS IS THE PROBLEM AREA if (distance >= 0 || distance <= 0) { //this line has no meaning if (usersGuessX - computersNumberX < 0) { String east = "east"; }else if (usersGuessX - computersNumberX > 0) { String west = "west"; }else if (usersGuessY - computersNumberY < 0) { String north = "north"; }else if (usersGuessY - computersNumberY > 0) { String south = "south"; }else if (distance <= 1.5) { System.out.println("You're hot " ); } else if (distance <= 5.0) { System.out.println("ur warm "); } else { System.out.println("cold"); } } } } public static void intro() { System.out.println("This program is a 2-D guessing game."); System.out.println("I will think of a point somewhere"); System.out.println("between (1, 1) and (20, 20)"); System.out.println("and give hints until you guess it."); } } // end of class GuessingGame
Baek is offline   Reply With Quote

 
Uberguilds Network