1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Java - Logical guessing game - urgent help needed with this.

Discussion in 'Programming' started by wvccboy, Nov 27, 2007.

  1. #1
    I'm trying to create a program where the computer guesses a number that you have chosen and guesses until it has found the right number.

    Now, it should not guess a random number, but should make the most logical guesses to reach the destination.

    The first guess would be 50, and then it would decide if it were larger or less than, then guess either 25 or 75, etc until it reached the number the user chose it to be, between 1 and 100.

    How should I set this up to start with?

    Here's what I have:

    I can't seem to be able to figure out the loops about it, because that doesn't work. How can I change the loops above to make it a little easier to logically guess it instead of running through every number through a hundred?

    Doing this for an extensive program and need some advice quickly.

    Thanks!
     
    wvccboy, Nov 27, 2007 IP
  2. WL_Marketing

    WL_Marketing Banned

    Messages:
    216
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You need to keep track of min and max possibilities:
     
    WL_Marketing, Nov 27, 2007 IP
  3. webmaster_TSU

    webmaster_TSU Peon

    Messages:
    449
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can't subtract a static number from the computer guesses like that, it won't work well I don't think. For example, it will never guess 13. Your code would assign the computer guess to 25, then 12, then 75 in one loop (and, with how it's coded, that's just one guess, too!). It would repeat this loop forever, just run through the code yourself to see.

    First I would change this:

    System.out.print("Enter a number you're thinking of that the computer must guess: ");
    number = reader.readInt();
    Code (markup):
    To

    boolean user_input_ok = false;
    while(!user_input_ok){
    System.out.print("Enter a number between 1 and 100 you're thinking of that the computer must guess: ");
    number = reader.readInt();
    if((number >= 1) && (number <=100)){
    user_input_ok = true;
    }
    }
    Code (markup):
    Then I would add something like this (note start int = count = 1 instead of 0):

    
    compguess_change = 25;
    while(compguess != number){
    if(number < compguess){
    System.out.println(compguess + " is incorrect. Try a smaller number");
    compguess = compguess - compguess_change;
    compguess_change = round(compguess_change / 2);
    }else if(number > compguess){
    System.out.println(compguess + " is incorrect. Try a larger number");
    compguess = compguess + compguess_change;
    compguess_change = round(compguess_change / 2);
    }else if(number == compguess){
    System.out.print(compguess + " is correct! It took the computer " + count + " guesses to guess the number you chose.");
    }
    count++;
    }
    
    Code (markup):
    That's just off the top of my head, might not work perfectly I can't compile it here. Running through the code with 13 we would get comp guesses

    50 (compguess_change = 25)
    25 (compguess_change = 13)
    12 (compguess_change = 7)
    19 (compguess_change = 4)
    15 (compguess_change = 2)
    13
     
    webmaster_TSU, Nov 28, 2007 IP
    wvccboy likes this.
  4. wvccboy

    wvccboy Notable Member

    Messages:
    2,632
    Likes Received:
    81
    Best Answers:
    1
    Trophy Points:
    250
    #4
    Awesome thanks.

    One problem here.

    It runs the guess about 3-4 times, guessing 50, then 25, then 12, then stopping.

    How would I edit it a little bit to guess the numbers in between? I want the program to guess through all the numbers through a hundred, just more logically.
     
    wvccboy, Nov 28, 2007 IP
  5. webmaster_TSU

    webmaster_TSU Peon

    Messages:
    449
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I fixed it. This does it the most logical way...it starts in the middle of the 100 numbers, at 50. If it's lower, then you know 51 through 100 or out, so you throw out the most possible numbers each wrong guess, statistically. Then, since it's lower, you split those numbers in half and guess 25, then 12, etc, etc.

    I don't think you want it to guess 50, then 49, then 48. That would take forever and not be efficient. Here is the code I got:

    /*
     * Test.java
     *
     * Created on November 28, 2007, 10:14 PM
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
    import java.io.*;
    import java.math.*;
    /**
     *
     * @author Mike
     */
    public class Test {
        
        public static void main(String[] args) {
            int range = 100;
            int number = 0;
            int input = 0;
            int compguess = 0;
            int count = 0;
            int compguess_change = Math.round(range / 4);
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            boolean user_input_ok = false;
            boolean got_it = false;
            
            while(!user_input_ok){
                System.out.println("Enter a number between 1 and 100 you're thinking of that the computer must guess: ");
                try{
                    number = Integer.parseInt(in.readLine());
                    if((number >= 1) && (number <= range)){
                        user_input_ok = true;
                    }
                }catch (IOException ioe) { ioe.printStackTrace(); }
            }
            compguess = Math.round(range / 2); //computer picks in the middle of the number range
            
            while(!got_it){
                if(number < compguess){
                    System.out.println("Guessing: " + compguess + "...incorrect. Trying a smaller number.");
                    compguess = compguess - compguess_change;
                    if(compguess_change != 1){
                        compguess_change = Math.round(compguess_change / 2);
                    }
                }else if(number > compguess){
                    System.out.println("Guessing: " + compguess + "...incorrect. Trying a larger number.");
                    compguess = compguess + compguess_change;
                    if(compguess_change != 1){
                        compguess_change = Math.round(compguess_change / 2);
                    }
                }else if(number == compguess){
                    System.out.println("Guessing: " + compguess + "...correct!");
                    System.out.println("It took the computer " + count + " guesses to guess the number you chose.");
                    got_it = true;
                }
                count++;
            }
        }
    }
    
    Code (markup):
    Note I had to add this line:
    if(compguess_change != 1){
    Code (markup):
    Because if it's compguess_change = 1 the next time it would round down to 0...infinite loop = bad.

    I also added the int:

            int range = 100;
    Code (markup):
    So you can change the range of accept values. If you want to play the game with numbers between 1 and 1000, just change that to 1000! I get too into these things..

    Here is the output I got for 10:

    Enter a number between 1 and 100 you're thinking of that the computer must guess:
    10
    Guessing: 50...incorrect. Trying a smaller number.
    Guessing: 25...incorrect. Trying a smaller number.
    Guessing: 13...incorrect. Trying a smaller number.
    Guessing: 7...incorrect. Trying a larger number.
    Guessing: 10...correct!
    It took the computer 4 guesses to guess the number you chose.



    Here is the output for 76:

    Enter a number between 1 and 100 you're thinking of that the computer must guess:
    76
    Guessing: 50...incorrect. Trying a larger number.
    Guessing: 75...incorrect. Trying a larger number.
    Guessing: 87...incorrect. Trying a smaller number.6
    Guessing: 81...incorrect. Trying a smaller number.3
    Guessing: 78...incorrect. Trying a smaller number.1
    Guessing: 77...incorrect. Trying a smaller number.1
    Guessing: 76...correct!
    It took the computer 6 guesses to guess the number you chose.


    This is the fastest way to do it, I don't think the comp will ever take more then 7 or so guess to get a number. The biggest thing I don't like about this program is that Math.round() rounds .5 down instead of up.
     
    webmaster_TSU, Nov 28, 2007 IP