Sign in with
Sign up | Sign in
Your question
Solved

Guessing Game - JAVA

Last response: in Applications
Share
March 28, 2011 1:08:06 AM

Hello all, My program is a guessing game that allows the user to enter the low and high numbers and how many guesses he/she gets. Then the program generates a random number between the low and high. Got everything working fine, just some small things that I need help with and one big thing that I am confused on.

SMALL THINGS:
1) When they incorrectly guess the number it tells you then says how many guesses you have left on your last guess it prints "Incorrect guess. You have 0 guess left". I don't want it to print that. Just want you to enter you last guess and if its wrong it just say " You lost!!!".

2) I need to have the while to evaluate a CHAR of either Y/N but I couldn't get it to work. I just threw in a int just to see if it was even capable of working, it was. I had it as String line = nextLine(); but when I had this in it didn't even allow me to type anything.

3) If you say yes to continue and you reenter the same low and high numbers the random number is always the same. For example I ran it 4 times with the low = 1 and high = 3 and the random number was always 3. Is there a way to make it randomize a new number every time they say yes to playing again?

BIG THING:
"GameInfo class. Your program should contain a class called GameInfo that will be used to store the following data during program execution: low and high range values, number of guesses allowed, number of guesses taken, did the player win or lose. Make sure you write get/set methods and at least one constructor."

I know how to make the new class and stuff. Just confused on how I would get the values in the guess class over to the GameInfo class so I can print to a file.

The files format is like this:
Won Hgh Low Alw Tkn

I need to put in the info for every time they play game until the program ends.




  1. import java.util.Scanner;
  2. import java.io.PrintStream;
  3. import java.util.Random;
  4.  
  5. public class Guess {
  6.  
  7.  
  8.  
  9.  
  10. public static void main(String[] args) {
  11.  
  12. int high;
  13. int low;
  14. int guess;
  15. int guesses;
  16. int rNum;
  17. int g;
  18.  
  19. Scanner input = new Scanner(System.in);
  20.  
  21. System.out.println("Number Guessing Program");
  22. System.out.println("------------------------\n");
  23. do
  24. {
  25.  
  26. System.out.printf("\nEnter the low value: ");
  27. low = input.nextInt();
  28. System.out.printf("Enter the high value: ");
  29. high = input.nextInt();
  30. System.out.printf("Enter the number of guesses allowed: ");
  31. guesses = input.nextInt();
  32.  
  33.  
  34. Random rand = new Random();
  35. rNum = rand.nextInt(high-low) + (low+1);
  36.  
  37. for (int c = 0; guesses > c; guesses--)
  38. {
  39. System.out.printf("Enter a guess (%d-%d): ",low,high);
  40. guess = input.nextInt();
  41.  
  42. if (guess != rNum)
  43. {
  44. System.out.printf("Incorrect guess. You have %d guess left\n",(guesses-1));
  45. }
  46. else
  47. {
  48. System.out.printf("You won!!!\n\n");
  49. break;
  50. }
  51.  
  52. if (guesses == 1)
  53. System.out.printf("You lost!!!");
  54.  
  55.  
  56. }
  57.  
  58. System.out.printf("\nDo you want to keep playing (Y/N): ");
  59. g = input.nextInt();
  60. } while ((g == 1) || (g == 1));
  61. System.out.println("\nGame over. Thank you\n\n");
  62.  
  63.  
  64.  
  65.  
  66. }
  67. }


What it should look like:



Thanks to all that help =D :hello: 

More about : guessing game java

March 28, 2011 3:57:16 AM

UPDATED CODE:

Still need help on all the SMALL THING from above. Also not sure how to get the how many guesses were taken for my log.

Guess Class
  1. import java.util.Scanner;
  2. import java.io.PrintStream;
  3. import java.util.Random;
  4.  
  5. public class Guess {
  6.  
  7.  
  8.  
  9.  
  10. public static void main(String[] args) {
  11.  
  12. int high;
  13. int low;
  14. int guess;
  15. int guesses;
  16. int rNum;
  17. int won;
  18. int hgh;
  19. int lw;
  20. int allow;
  21. int taken;
  22. int g;
  23. int p = 0;
  24. PrintStream out;
  25. GameInfo game = new GameInfo();
  26.  
  27. Scanner input = new Scanner(System.in);
  28.  
  29. System.out.println("Number Guessing Program");
  30. System.out.println("------------------------\n");
  31.  
  32. try
  33. {
  34. out = new PrintStream ("gamelog.txt");
  35. out.println("Won Hgh Low Alw Tkn");
  36.  
  37.  
  38. do
  39. {
  40.  
  41. System.out.printf("\nEnter the low value: ");
  42. low = input.nextInt();
  43. System.out.printf("Enter the high value: ");
  44. high = input.nextInt();
  45. System.out.printf("Enter the number of guesses allowed: ");
  46. guesses = input.nextInt();
  47. game.setAllowed(guesses);
  48. allow = game.getAllowed();
  49.  
  50.  
  51. Random rand = new Random();
  52. rNum = rand.nextInt(high-low) + (low+1);
  53.  
  54. for (int c = 0; guesses > c; guesses--)
  55. {
  56. System.out.printf("Enter a guess (%d-%d): ",low,high);
  57. guess = input.nextInt();
  58.  
  59. if (guess != rNum)
  60. {
  61. System.out.printf("Incorrect guess. You have %d guess left\n",(guesses-1));
  62. }
  63. else
  64. {
  65. System.out.printf("You won!!!\n\n");
  66. p = 1;
  67. break;
  68. }
  69.  
  70. if (guesses == 1)
  71. System.out.printf("You lost!!!");
  72.  
  73.  
  74. }
  75.  
  76. game.setHigh(high);
  77. game.setLow(low);
  78. game.setTaken(guesses);
  79. game.setWon(p);
  80.  
  81. hgh = game.getHigh();
  82. low = game.getLow();
  83. taken = game.getTaken();
  84.  
  85. if (p == 0)
  86. {
  87. out.println("No" + " " + hgh + " " + low + " " + allow + " " + taken);
  88. }
  89. else
  90. {
  91. out.println("Yes" + " " + hgh + " " + low + " " + allow + " " + taken);
  92. }
  93.  
  94. System.out.printf("\nDo you want to keep playing (Y/N): ");
  95. g = input.nextInt();
  96. } while ((g == 1) || (g == 1));
  97. System.out.println("\nGame over. Thank you\n\n");
  98.  
  99. out.close();
  100. }
  101.  
  102. catch (Exception e)
  103. {
  104. System.out.println("ERROR: Could not open file!");
  105. }
  106.  
  107.  
  108. }
  109. }



GameInfo Class
  1. class GameInfo {
  2. private int won;
  3. private int high;
  4. private int low;
  5. private int allowed;
  6. private int taken;
  7.  
  8. GameInfo(){
  9. won = 0;
  10. high = 0;
  11. low = 0;
  12. allowed = 0;
  13. taken = 0;
  14. }
  15.  
  16. public void setWon(int newWon)
  17. {
  18. won = newWon;
  19. }
  20.  
  21. public void setHigh(int newHigh)
  22. {
  23. high = newHigh;
  24. }
  25.  
  26. public void setLow(int newLow)
  27. {
  28. low = newLow;
  29. }
  30.  
  31. public void setAllowed(int newAllowed)
  32. {
  33. allowed = newAllowed;
  34. }
  35.  
  36. public void setTaken(int newTaken)
  37. {
  38. taken = newTaken;
  39. }
  40.  
  41. public int getWon()
  42. {
  43. return won;
  44. }
  45.  
  46. public int getHigh()
  47. {
  48. return high;
  49. }
  50.  
  51. public int getLow()
  52. {
  53. return low;
  54. }
  55.  
  56. public int getAllowed()
  57. {
  58. return allowed;
  59. }
  60.  
  61. public int getTaken()
  62. {
  63. return taken;
  64. }
  65.  
  66.  
  67.  
  68. }
March 29, 2011 12:13:22 AM

Figured out everything, just wondering if you pick Y to play again, is there a way to clear all the values? Here's the code to look at.

  1. import java.util.Scanner;
  2. import java.io.PrintStream;
  3. import java.util.Random;
  4.  
  5. public class Guess {
  6.  
  7.  
  8.  
  9.  
  10. public static void main(String[] args) {
  11.  
  12. int high;
  13. int low;
  14. int guess;
  15. int guesses;
  16. int rNum;
  17. int won;
  18. int hgh;
  19. int lw;
  20. int allow;
  21. int taken;
  22. String s1;
  23. int gues = 0;
  24. int p = 0;
  25. PrintStream out;
  26. GameInfo game = new GameInfo();
  27.  
  28. Scanner input = new Scanner(System.in);
  29.  
  30. System.out.println("Number Guessing Program");
  31. System.out.println("------------------------\n");
  32.  
  33. try
  34. {
  35. out = new PrintStream ("gamelog.txt");
  36. out.println("Won Hgh Low Alw Tkn");
  37.  
  38.  
  39. do
  40. {
  41.  
  42. System.out.printf("\nEnter the low value: ");
  43. low = input.nextInt();
  44. System.out.printf("Enter the high value: ");
  45. high = input.nextInt();
  46. System.out.printf("Enter the number of guesses allowed: ");
  47. guesses = input.nextInt();
  48. game.setAllowed(guesses);
  49. allow = game.getAllowed();
  50.  
  51.  
  52. Random rand = new Random();
  53. rNum = rand.nextInt(high-low) + (low+1);
  54.  
  55. for (int c = 0; guesses > c; guesses--)
  56. {
  57. System.out.printf("Enter a guess (%d-%d): ",low,high);
  58. guess = input.nextInt();
  59.  
  60. if (guess != rNum)
  61. {
  62. System.out.printf("Incorrect guess. You have %d guess left\n",(guesses-1));
  63. }
  64. else
  65. {
  66. System.out.printf("You won!!!\n\n");
  67. p = 1;
  68. gues++;
  69. break;
  70. }
  71.  
  72. if (guesses == 1)
  73. System.out.printf("You lost!!!");
  74.  
  75. gues++;
  76. }
  77.  
  78. game.setHigh(high);
  79. game.setLow(low);
  80. game.setTaken(gues);
  81. game.setWon(p);
  82.  
  83. hgh = game.getHigh();
  84. low = game.getLow();
  85. taken = game.getTaken();
  86.  
  87. if (p == 0)
  88. {
  89. out.println("No" + " " + hgh + " " + low + " " + allow + " " + taken);
  90. }
  91. else
  92. {
  93. out.println("Yes" + " " + hgh + " " + low + " " + allow + " " + taken);
  94. }
  95.  
  96. System.out.printf("\nDo you want to keep playing (Y/N): ");
  97. s1 = input.next();
  98.  
  99. } while (s1.equalsIgnoreCase("y"));
  100. System.out.println("\nGame over. Thank you\n\n");
  101.  
  102. out.close();
  103. }
  104.  
  105. catch (Exception e)
  106. {
  107. System.out.println("ERROR: Could not open file!");
  108. }
  109.  
  110.  
  111. }
  112. }


GAME INFO
  1. class GameInfo {
  2. private int won;
  3. private int high;
  4. private int low;
  5. private int allowed;
  6. private int taken;
  7.  
  8. GameInfo(){
  9. won = 0;
  10. high = 0;
  11. low = 0;
  12. allowed = 0;
  13. taken = 0;
  14. }
  15.  
  16. public void setWon(int newWon)
  17. {
  18. won = newWon;
  19. }
  20.  
  21. public void setHigh(int newHigh)
  22. {
  23. high = newHigh;
  24. }
  25.  
  26. public void setLow(int newLow)
  27. {
  28. low = newLow;
  29. }
  30.  
  31. public void setAllowed(int newAllowed)
  32. {
  33. allowed = newAllowed;
  34. }
  35.  
  36. public void setTaken(int newTaken)
  37. {
  38. taken = newTaken;
  39. }
  40.  
  41. public int getWon()
  42. {
  43. return won;
  44. }
  45.  
  46. public int getHigh()
  47. {
  48. return high;
  49. }
  50.  
  51. public int getLow()
  52. {
  53. return low;
  54. }
  55.  
  56. public int getAllowed()
  57. {
  58. return allowed;
  59. }
  60.  
  61. public int getTaken()
  62. {
  63. return taken;
  64. }
  65.  
  66.  
  67.  
  68. }
Related resources

Best solution

a b L Programming
April 3, 2011 5:40:49 PM
Share

So your code is such that the majority of the game logic is in a single method - your main method. Whilst this can be ok for small programs it's not best practice to do so.

What you really want to do is push this logic into a class somewhere perhaps a new class called Game. In your main method create a new instance of Game and call start on it. Obviously in your Game class you'll need to implement a start method! The Game class should have a private member that's the GameInfo.

Within your start method you can look for "y" to start again. In this scenario you could call a method called say reset which would:
1) Set the private gameInfo variable to an new GameInfo instance
2) Call the start method again

Does that make sense?
April 13, 2011 1:14:25 AM

Not really, we just are starting with multiple classes and inheritance. I am actually going to be posting a new program that has 3 classes. Probably going to need some help with it.
April 13, 2011 1:14:34 AM

Best answer selected by laserpp.
!