# Computer VS Computer GAME > JAVA < help

Discussion in 'Mac Programming' started by neelvaka, Sep 22, 2007.

1. ### neelvaka macrumors newbie

Joined:
Sep 15, 2007
#1
Code:
```import java.io.*;

public class Playself

{

public static void main(String args[]) throws IOException, NumberFormatException

{
int x = (int)(1000 * Math.random()) + 1;
int g1, g2, g3, g4, g5, g6, g7, g8, g9, g10;

int timesplayed;

int max;

int min;

double average;

int upper, lo, hi, count, guess;

boolean again = false;

String input;

timesplayed = 0;

max = 0;

min = 0;

average = 0;

System.out.println(" Guessing Game ");

do

{

timesplayed++;

do

{

System.out.print("\nWould you like to view the instructions, (y)es or (n)o? ");

if (input.equals("y") || input.equals("Y"))

{

System.out.println("\n\tYou will set an upper bound and think of a number between");

System.out.println("\t1 and that number. I will try to guess your number in as");

System.out.println("\tfew guesses as possible.");

}

} while (!(input.equals("y") || input.equals("Y") || input.equals("n") || input.equals("N")));

System.out.print("\nEnter the upper bound: ");

System.out.println();

count = 0;

lo = 1;

hi = upper;

guess = (upper / 2);

do {

System.out.print("\tMy guess is " + (hi+lo)/2 + ". Is that (l)ow, (h)igh, or (c)orrect? ");

if ((hi+lo)/2 == hi || (hi+lo)/2 == lo) {

System.out.println("\tCHEATER!!!!!!!!!!!!!!!!!!!!!!!!!!!");

break; }

count++;

switch(input.charAt(0)){

case 'l':

case 'L':

lo = (hi + lo) / 2;

break;

case 'h':

case 'H':

hi = (hi + lo) / 2;

break;

case 'c':

case 'C':

System.out.println("\n\tI guessed your number in only " + count + " tries!");

System.out.println("\nThank you for playing Guessing Game.");

break;

}

} while (!(input.equals("c") || input.equals("C")));

System.out.print("\nPlay again? (Y)es or (N)o: ");

if (goAgain.equals("y") || goAgain.equals("Y"))

{

again = true;

}

else

{

again = false;

}

if (timesplayed == 1)

{

max = count;

min = count;

average = count;

}

else

{

if (count > max)

{

max = count;

}

else if (count < min)

{

min = count;

}

average = (timesplayed - 1 * average) + count / timesplayed;

}

} while (again == true);

System.out.println("\nThank you for playing Guessing Game!");

System.out.println("\nStatistics:");

{

System.out.println("\n\tTimes played: " + timesplayed);

System.out.println("\tLowest number of guesses: " + min + " guesses.");

System.out.println("\tHighest number of guesses: " + max + " guesses.");

System.out.println("\tAverage number of guesses: " + average + " guesses.");

}

}

} ```
Thats wat i have so far and i have to get it so that that the computer picks a number between 1 and 1000 and the computer has to play against itself to find that number.
After it has chosen the number the comp will ask the comp guess my number. the comp should guess a random number to start with and comp will say low high or correct. It should also say how amny times it took.

so yeah i need some advice like where to start. PLZ DONT GIMME THE WHOLE CODE i wnna try this myself since i have the weekend to do it (today and tomorrow). If you want snippets of code are helped but yeah i just really want to no where to start and what i should do im very confused.

2. ### neelvaka thread starter macrumors newbie

Joined:
Sep 15, 2007
#2
plz no scanner type

i use eclipse platform

and this is JAVA

3. ### angelwatt Moderator emeritus

Joined:
Aug 16, 2005
Location:
USA
#3
It's really no different than your last assignment except you aren't doing the guessing. Now, instead of having code to take your guess you setup some simple logic to make guesses based on whether the last guess was low or high. It really shouldn't be any harder than your last assignment.

Work on it and if you come to a specific issue discuss that here. It's just very broad at this point.

4. ### neelvaka thread starter macrumors newbie

Joined:
Sep 15, 2007
#4
any way u can help me about where to start?

5. ### ChrisBrightwell macrumors 68020

Joined:
Apr 5, 2004
Location:
Huntsville, AL
#5
Schedule a meeting with your instructor during his or her office hours.

6. ### Mac Player macrumors regular

Joined:
Jan 19, 2006
#6
1) Break your code in methods
2) Use equalsIgnoreCase()
3) Dont decrlare so many variables at the beggining.
4) Dont use multiple println() for the same line instead break the code and use \n
5) Use object oriented design.
6) Your computer will gess the number in a maximum of log n times.

7. ### angelwatt Moderator emeritus

Joined:
Aug 16, 2005
Location:
USA
#7
Forums aren't meant to hold your hand through your class. If you need this much help you really need to talk to your instructor and/or TA about your trouble. Maybe you're just not meant to be a programmer. There's nothing wrong with asking questions, but you really need to study.

8. ### neelvaka thread starter macrumors newbie

Joined:
Sep 15, 2007
#8
look im not here 4 sum1 to hold my hand ok? and i did this so far:
Code:
```public class Playself {
public static void main(String args[]) {

int upper, lo, hi, numGuessToFind, min, max, avg, iNumPlayed = 0, guess, number, a, b, c, d, e, f, g, h, j;

min = 10000;
max = 0;
avg = 0;

for (int i = 0; i < 10; i++)

System.out.println("________________");
System.out.println("\\               \\");
System.out.println(" \\ Guessing Game \\");
System.out.println("  \\_______________\\");

do{
upper = 1000;
guess = upper / 2;
number = (int) (Math.random () * 1000) + 1;
iNumPlayed++;
numGuessToFind = 0;
lo = 1;
hi = upper;
do {
System.out.println("\tMy guess is " + guess );

if (guess < number) {
numGuessToFind = numGuessToFind + 1
;
lo = guess;
guess = (hi + lo) / 2;
}else{
if (guess > number) {
numGuessToFind = numGuessToFind +1;
hi = guess;

guess = (hi + lo) / 2;
}else{
if (guess == number)  {
numGuessToFind = numGuessToFind + 1;
System.out.println("\n\tI guessed my number in only " + numGuessToFind + " tries!");
avg += numGuessToFind;
if (numGuessToFind<min){
{min = numGuessToFind;}
}
if (numGuessToFind > max){
{max = numGuessToFind;}
}
System.out.println("\nThank you for playing Guessing Game.");

}
} while ( ! (number == guess) );

} while (! (iNumPlayed == 10));

System.out.println("\nHow many times played: " + iNumPlayed);
System.out.println("The average number of guesses I made: " + (avg / iNumPlayed));
System.out.println("The highest guesses I made: " + max);
System.out.println("The lowest guesses I made: " + min);

}
}
```
but one prob
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error, insert "while ( Expression ) ;" to complete DoStatement
Syntax error, insert "while ( Expression ) ;" to complete DoStatement
Syntax error, insert "}" to complete MethodBody

at Playself.main(Playself.java:69)

it says that
and it says the last two curlys are the prob? this is the only problem i have so what should i do?

Joined:
Sep 15, 2007
10. ### plinden macrumors 68040

Joined:
Apr 8, 2004
#10
You're missing a couple of "}" in the middle of the program. I'll leave it as an exercise for to find where (hint, check that all your "{" have a closing bracket).

I would also like to point you to the Java Coding Conventions. I know lots of people think that adhering to a standard is trivial as long as the program works, but applying consistent naming and indentation in particular makes it a lot easier for everyone, including yourself, to understand what a program does, and to find out what's causing problems like you one you're having here.

I was able to work out where the problem was only by re-indenting your code. If I were you I would make it a priority to get into a habit of using these conventions.

11. ### savar macrumors 68000

Joined:
Jun 6, 2003
Location:
District of Columbia
#11
God, I wish some of my coworkers would read that.

Good advice for the new programmer to learn. When you format your code correctly, syntax errors become painfully obvious. In fact, you'll make fewer of them to begin with. Sadly most people take the approach of, "I'll just randomly add braces here and there until it compiles." Which usually results in, "it compiles, but it doesn't work, help me!"

12. ### angelwatt Moderator emeritus

Joined:
Aug 16, 2005
Location:
USA
#12
Like the others said, format the code. I use to use Eclipse and I remember it can format the code for you, but I don't remember where the command is and I don't have it installed on my machine. It also allows you to give rules on how to format the code in the preferences. This would be very beneficial for you so give it a look.

13. ### plinden macrumors 68040

Joined:
Apr 8, 2004
#13
Select all (Cmd-A) followed by Cmd-I (replace Cmd with Ctrl if you're not using OS X).