PC Guesses Remembered Number


/ Published in: Java
Save to your folder(s)

The player selects a range from 0 to n, remembers a number in this range and then the script asks the player a question trying to guess the number. The PC will always guess the correct number.\r\n\r\nI could have added more varied questions, but the task did not state it had to be interesting, and being a lazy creature I took the path of least résistance.


Copy this code and paste it in your HTML
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class GuessNumber
  5. {
  6. public static short range;
  7. public static ArrayList<Integer> collection = new ArrayList<Integer>();
  8.  
  9. public static void main(String args[])
  10. {
  11. // We need a range
  12. System.out.print("\nI need a range from 0 to 32,767.\n\nPlease choose a number: ");
  13.  
  14. // Get and populate the range
  15. Scanner input = new Scanner(System.in);
  16. range = input.nextShort();
  17. range = (range == 0) ? 1 : range;
  18. for (int i=0; i<=range; i++) collection.add(i);
  19.  
  20. // A few ground rules
  21. System.out.printf("\nNow pick a number in the range 0 to %s and remember\n", range);
  22. System.out.println("it. I will then try to guess the number you chose.\n");
  23. System.out.println("Answer \"yes\" or \"no\" to the questions.\n\n---");
  24.  
  25. // Keep asking as long as found is false
  26. boolean found = false;
  27. while ( ! found)
  28. {
  29. // Get the size of the collection
  30. int size = collection.size();
  31.  
  32. // As long as there are two or more numbers left
  33. if (size > 2)
  34. {
  35. // The centre index
  36. int centre = size / 2;
  37. int theNumber = collection.get(centre);
  38.  
  39. // Default question
  40. System.out.printf("\nIs your number less than %s? ", theNumber);
  41.  
  42. // Evaluate input
  43. String answer = input.next();
  44. if (answer.equals("yes") || answer.equals("1")) delete(theNumber, true);
  45. else if (answer.equals("no") || answer.equals("0")) delete(theNumber, false);
  46. else { error(answer); found = true; }
  47.  
  48. // If only one number is left then we are done.
  49. if (collection.size() == 1) {
  50. int num = collection.get(0);
  51. answer(num);
  52. found = true;
  53. }
  54. }
  55. // Otherwise there are now 2 values in the array.
  56. else {
  57. // Get the two values
  58. int num1 = collection.get(0);
  59. int num2 = collection.get(1);
  60.  
  61. // Ask question and get answer
  62. System.out.print("\nIs your number greater than " + num1 + "? ");
  63. String answer = input.next().trim().toLowerCase();
  64.  
  65. // Resolve answer
  66. if (answer.equals("yes") || answer.equals("1")) {
  67. answer(num2);
  68. found = true;
  69. } else if (answer.equals("no") || answer.equals("0")) {
  70. answer(num1);
  71. found = true;
  72. } else {
  73. error(answer);
  74. found = true;
  75. }
  76. }
  77. }
  78. }
  79.  
  80. // Deletes numbers less or greater than n
  81. public static void delete(int n, boolean lt)
  82. {
  83. // Create a temporary collection
  84. ArrayList<Integer> newCollection = new ArrayList<Integer>();
  85.  
  86. // Add numbers lesser than n
  87. if (lt) { for (int i:collection) if(i<n) newCollection.add(i); }
  88. // Add numbers greater than n
  89. else { n=n-1; for (int i:collection) if(i>n) newCollection.add(i); }
  90. // Replace the collection with the new one
  91. collection = newCollection;
  92. }
  93.  
  94. // The answer
  95. public static void answer(int num)
  96. {
  97. System.out.println("\n---\n\nThen your number is: " + num);
  98. System.out.println("\nThank you for playing! To play again press the Up arrow");
  99. System.out.println("on your keyboard and then Enter.\n\nHave a nice day :)");
  100. }
  101.  
  102. // An error
  103. public static void error(String answer)
  104. {
  105. System.out.printf("\nI am a machine. I do not understand \"%s\"...\n", answer);
  106. System.out.println("\nTo play again press the Up arrow on your keyboard and then Enter.");
  107. }
  108. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.