Engineeringserver.com

A community for computer science students & developers about software development, game development, game design, games, anime preview & reviews and more!


How to create a Java guessing game

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5 out of 5)
Loading ... Loading ...
3,741 views

A basic Java guessing game I made to show you how it could be done. You can specify the amount of numbers to guess and shows the total guesses you made to guess the correct number.

I've commented out the "continue" part because I'm too lazy to fix the small error that but if you uncomment the code you will see whats wrong. I suppose i'll fix that later.

Anyway here's the code:

JAVA:
  1. import java.util.InputMismatchException;
  2. import java.util.Scanner;
  3. public class GuessGame {
  4. String guessThisNumber = "";
  5. int[] secretNumber ;
  6. boolean guessedCorrectNumber = false, continueOrExitGame = false, debug = true;
  7. int tries = 0, difficulty, rounds = 1;
  8. //int difficulty;
  9. public static void main(String[] args) {
  10. GuessGame GG = new GuessGame();
  11. GG.usage();
  12. GG.playGame();
  13. }
  14. public void playGame(){
  15. Scanner sc = new Scanner(System.in);
  16. System.out.print("How many numbers you want to guess? ");
  17. try{
  18. difficulty = sc.nextInt();
  19. this.generateSecretNumber(difficulty);
  20. Scanner sd = new Scanner(System.in);
  21. while(!this.guessedCorrectNumber){
  22. System.out.print("Guess the secret number: ");
  23. String guess = sd.nextLine();
  24. if(guess.length()> difficulty || guess.length()
  25. System.out.println("Oops, please type " + difficulty  +  " numbers!");
  26. }
  27. if(guess.length() == difficulty){
  28. this.checkSecretNumber(guess);
  29. }
  30. this.tries++;
  31. if(guess.equals(this.guessThisNumber)){
  32. this.guessedCorrectNumber = true;
  33. System.out.println();
  34. System.out.println("Concratulations! the secret number is indeed " + this.guessThisNumber);
  35. System.out.println("Total tries: " + this.tries);
  36. System.out.println();
  37. /*
  38. Scanner quitGameChoice = new Scanner(System.in);
  39. System.out.print("Again? Y/N: ");
  40. String continueOrExit = quitGameChoice.nextLine();
  41. while(!continueOrExitGame){
  42. if(continueOrExit.equals("n") || continueOrExit.equals("N")){
  43. continueOrExitGame = true;
  44. System.out.println("Thanks for playing!");
  45. System.exit(0);
  46. }
  47. if(continueOrExit.equals("y") || continueOrExit.equals("Y")){
  48. continueOrExitGame = true;
  49. guessedCorrectNumber = false;
  50. tries = 0;
  51. rounds++;
  52. System.out.println("Alright! Ready for round " + rounds +  "?");
  53. System.out.println();
  54. this.playGame();
  55. }
  56. else{
  57. System.out.println("Please make a choice, again?: Y/N");
  58. continueOrExit = quitGameChoice.nextLine();
  59. }
  60. }
  61. */
  62. }
  63. }
  64. }
  65. catch(InputMismatchException IME){
  66. System.out.println("That is not a valid number");
  67. }
  68. }
  69. public void generateSecretNumber(int totalNumbers){
  70. guessThisNumber = "";
  71. secretNumber = new int[totalNumbers];
  72. for (int i = 0; i <totalNumbers; i++){
  73. int randomNumber = (int) (0+ Math.random()*10);
  74. secretNumber[i] = randomNumber;
  75. }
  76. for(int i = 0; i <secretNumber.length; i++){
  77. guessThisNumber +=secretNumber[i];
  78. //System.out.print(secretNumber[i]);
  79. }
  80. if(debug){
  81. System.out.println();
  82. System.out.println("Secret number is: " + guessThisNumber);
  83. }
  84. System.out.println("Alright, the secret number(s) are generated!");
  85. System.out.println();
  86. }
  87. public void checkSecretNumber(String userInput){
  88. int[] compareNumber = new int[userInput.length()];
  89. for (int i = 0; i <userInput.length(); i++){
  90. compareNumber[i] = userInput.charAt(i);
  91. }
  92. /*
  93. System.out.print(compareNumber[0]-48); // -48?
  94. */
  95. for (int i = 0; i <userInput.length(); i++){
  96. if(compareNumber[i]-48 <secretNumber[i]){
  97. System.out.print(compareNumber[i]-48 + "+ ");
  98. }
  99. if(compareNumber[i]-48> secretNumber[i]){
  100. System.out.print(compareNumber[i]-48 + "- ");
  101. }
  102. if(compareNumber[i]-48 == secretNumber[i]){
  103. System.out.print(compareNumber[i]-48 + "! ");
  104. }
  105. }
  106. System.out.println();
  107. }
  108. public void usage(){
  109. System.out.println("Welcome to the guessing game, guess the correct number!");
  110. System.out.println("By: HappyFace - www.engineeringserver.com :: v1.0 initial release");
  111. System.out.println("");
  112. System.out.println("Legenda: ");
  113. System.out.println("+ means higher");
  114. System.out.println("- means lower");
  115. System.out.println("! means correct");
  116. System.out.println("");
  117. }
  118. }

Output:

Welcome to the guessing game, guess the correct number!
By: HappyFace - www.engineeringserver.com :: v1.0 initial release

Legenda:
+ means higher
- means lower
! means correct

How many numbers you want to guess? 4

Secret number is: 6303
Alright, the secret number(s) are generated!

Guess the secret number: 6303
6! 3! 0! 3!

Concratulations! the secret number is indeed 6303
Total tries: 1

//edit
Oh and before I forget to mention it.. to disable the secretcode that shows on the screen just change "debug = true" into "debug = false" and the secretcode won't be visible.


Your Ad Here