Fires of Heaven Guild Message Board  

Go Back   Fires of Heaven Guild Message Board > General forums > Development
User Name
Password
ForumSpy Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 02-08-2007, 10:29 PM   #1 (permalink)
Baek
Registered User
 
Baek's Avatar
 
Join Date: May 2006
Posts: 79
-2 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
Old 02-08-2007, 10:49 PM   #2 (permalink)
Zuuljin
So there's this plane on a treadmill...
 
Zuuljin's Avatar
 
Join Date: Jan 2005
Location: Southern California
Posts: 2,863
+2 Internets
Send a message via AIM to Zuuljin
Not *quite* sure what your asking, but instead of having it display "your hot", have a variable temperature or whatever to store if their hot, warm or cold. Also, dont use strings for each direction, just make 1 direction string, and set it to whatever direction they need to go. Finally, do something like System.out.println( "Your " + temperature + "! Move " + direction);


?

Edit: Also, to save even more typing, have it check north and south first, set the string equal to whichever, then seperatly check east and west, and have it append that choice, so you get a string that for instance will look like "north west" to avoid checking for all 8 directions.

Last edited by Zuuljin : 02-08-2007 at 10:53 PM.
Zuuljin is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
uberguilds network



All times are GMT -7. The time now is 02:27 AM.


Powered by vBulletin® Version 3.6.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0 RC6