Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: java quessing game

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Location
    Czech Republic
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb java quessing game

    Hello,
    i have some troubles with my homework... i need to make quessing game...
    i think random number 1-100 and program will queesing this number and asking me for question and i will reply only yes/no
    my idea is make lots of this:
    System.out.println("is your number greater than 50? yes/no");
    String a = sc.next();
    if(a.equals("yes")){
    System.out.print("is your number geater than 85? yes/no")
    .
    .
    .
    .
    }
    but it is soooo long.. can you help with some better solution?
    thank you very much and sorry for my low english


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java quessing game

    Use variables vs hardcoding numbers:
    "is your number greater than 50? yes/no");
    Replace the 50 with a variable that starts at 50 and then is computed as the game goes on.
    Something like this:
    first = 1
    last = 50
    top = 100
    when > 50, last = (top - last)/2

    That's a very rough idea. It needs some work.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    brox (November 4th, 2013)

  4. #3
    Junior Member
    Join Date
    Nov 2013
    Location
    Czech Republic
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java quessing game

    wow.
    It is still hard for me..
    i dont understand the principle of this.. please can you write me something.. more?
    thank you

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java quessing game

    Try doing the program in a series of small steps:
    First step:
    Change your current code to use variables as I suggested. Ask the question using the variable.
    Change the variables to new values depending on the user's response. Print out the new values of the variables and the first step is done.


    A way to visualize the problem would be two write a line with numbers from 1 to 20 (not 100 for now). Set a ptr to the number the user has chosen and pointers to the beginning and end of the range of possible value.
    Now do a binary search by asking the user relative to the middle of the range.
    If above the middle, move the lower range marker up to the middle and compute a new middle. If user says below move the top marker to the middle. As you ask questions one or the other marker will move. Eventually there will be one number between/at the markers.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Nov 2013
    Location
    Czech Republic
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java quessing game

    so.. i made 3 variables first,last and top
    i ask user System.out.print("is your number lesser than " +last+ "?");
    now userīs answer is no.
    so.. now i will replace last(50) with 25 ?
    edit:
    now i have this
    int first = 1;
    int last = 50;
    int top = 100;
    System.out.print("is you number lesser than " +last+ "?");
    String a = sc.next();
    if(a.equals("yes")){
    last = 25;
    }

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java quessing game

    Please post the code you are talking about.

    If the response is not less that means greater. Change the bottom/left to the middle.
    Have you tried the visual exercise I suggested?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Nov 2013
    Location
    Czech Republic
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java quessing game

    i tried to imagine it like a axis
    |----|----|
    the middle "|" is 50.
    if userīs answer is "is not greater than 50" the axis will be
    |--|-----|
    after this, program will ask "its your number greater than 25?" userīs answer will be ... no. so axis will be
    |-|------|
    sorry if i understand wrong..

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java quessing game

    That's probably right. Now try writing the simple one step program I suggested:
    set initial values
    ask question
    move markers as per user's response
    print out new value of markers
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Nov 2013
    Location
    Czech Republic
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java quessing game

    yes i have it.
    i will try to do it for right side of axis.

    --- Update ---

    oh god... its really hard for me... its so many lines... i have only this and i am lost in it..
    System.out.print("is your number lesser than " + last + "?");
    String a = sc.next();
    if (a.equals("yes")) {
    last = 25;
    }
    System.out.print("is your number lesser than " + last + "?");
    String b = sc.next();
    if (b.equals("yes")) {
    last = 12;
    } else if (b.equals("no")) {
    last = 38;
    }
    System.out.print("is your number lesser than " + last + "?");
    String c = sc.next();
    if (c.equals("yes")) {
    last = 32;
    } else if (b.equals("no")) {
    last = 45;
    }

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java quessing game

    That is NOT the code I was talking about. Try again. Write code that only does the simple first step as discussed in posts #8 and #4. Make a complete class, that will compile and execute. Do it: One small step at a time.

    Be sure to wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Nov 2013
    Location
    Czech Republic
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java quessing game

    i still thinking about it... so .. if userīs answer is "lesser" the only thing i change is "last(50)" wil be "last(25)" right ?
    i working in Eclipse so i think i dont need that tags
    my code:
    package project;
     
    import java.util.Scanner;
     
    public class Game {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int first = 1;
    		int last = 50;
    		int top = 100;
    		System.out.print("is your number lesser than " + last + "?");
    		String a = sc.next();
    		if (a.equals("yes")) {
    			last = 25;
    		}
    		System.out.print("is your number lesser than " + last + "?");
    		String b = sc.next();
    		if (b.equals("yes")) {
    			last = 12;
    		}
    		System.out.print("je vaše číslo měnší než " + last + "?");
    		String c = sc.next();
    		if (c.equals("ano")) {
    			last = 6;
    		}
    		System.out.print("je vaše číslo měnší než " + last + "?");
    		String d = sc.next();
    		if (d.equals("ano")) {
    			last = 3;
    		}
    		System.out.print("is your number lesser than " + last + "?");
    		String e = sc.next();
    		if (e.equals("yes")) {
    			last = 2;
    		}
    	}
    }

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java quessing game

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.


    The code should set the values of the variables with hardcoded values ONLY at the start.
    After that the variables values are copied from other variables or computed.

    The only two initial values would be 1 and 100. The middle would be computed. That would allow the range to be changed to 1 to 20 for easier testing.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Junior Member
    Join Date
    Nov 2013
    Location
    Czech Republic
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java quessing game

    oh sorry I didnt know that you mean post

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java quessing game

    Copy the statement(s) that you don't understand and ask a question about it.

    In this statement:
    last = 25;
    25 is a hardcoded value. last's value should be computed based on the previous values of first, top and last. Look at the row of number I asked you to use to see how the markers are to be moved.

    What happens to the logic of that statement if the range of numbers is changed to 1 - 20 for testing?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Junior Member
    Join Date
    Nov 2013
    Location
    Czech Republic
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java quessing game

    please can you write me code for range 1 - 20 ? because i can do it same as i wrote for range 1-100 but it will be just
    package project;
     
    import java.util.Scanner;
     
    public class Game {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int first = 1;
    		int last = 10;
    		int top = 20;
    		System.out.print("is your number lesser than " + last + "?");
    		String a = sc.next();
    		if (a.equals("yes")) {
    			last=last/2;
    		}
    		System.out.print("is your number lesser than " + last + "?");
    		String b = sc.next();
    		if (b.equals("yes")) {
    			last = last/2;
    		}
     
    	}
    }

    i really dont know how to solve it i am beginner..

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java quessing game

    I suggested that you work on the problem, one small step at a time. You seem to have ignored my suggestion and are trying to write the whole program at once.

    Try writing the code for the small first step as I suggested.

    If you'd rather write all the code at once, I'll leave and let some one else try to help you.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #17
    Junior Member
    Join Date
    Nov 2013
    Location
    Czech Republic
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java quessing game

    I know how to imagine it. But I dont have idea how to write it. I know..there is values top and first but I dont know how to use them in code ? How to compute middle value?

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java quessing game

    The code you have posted shows some of the statements you will need. Trying to write the whole program at once will make for confusion. Doing it one step at a time will be a better approach.
    Are you going to write the program I suggested in posts #4 and #8?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #19
    Junior Member
    Join Date
    Nov 2013
    Location
    Czech Republic
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java quessing game

    I dont know how to write it..

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java quessing game

    package project;
     
    import java.util.Scanner;
     
    public class Game {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int first = 1;
    		int last = 10;
    		int top = 20;
    		System.out.print("is your number lesser than " + last + "?");
    		String a = sc.next();
    		if (a.equals("yes")) {
    			last=last/2;
    		}
    		System.out.print("is your number lesser than " + last + "?");
    		String b = sc.next();
    		if (b.equals("yes")) {
    			last = last/2;
    		}
     
    	}
    }

    I dont know how to write it..
    Who wrote the code you posted in post#15? That has most of what the small first step should have.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Game in Java
    By Freedom-Claw in forum Object Oriented Programming
    Replies: 2
    Last Post: March 27th, 2013, 03:50 PM
  2. Help with Snake Game Java Code: It's Impossible to Lose the Game
    By haruspex_icis in forum What's Wrong With My Code?
    Replies: 20
    Last Post: December 17th, 2012, 12:21 PM
  3. [SOLVED] How to write Java applets to create 2D games?
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: November 2nd, 2012, 01:23 PM
  4. [SOLVED] Java Game
    By aman_chauhan in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 3rd, 2012, 04:53 AM
  5. Need help for my java game
    By blunderblitz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2010, 05:32 AM