Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. /*Luke Burgess 08016497
  2. Assessment
  3. Version 2.0 05/12/2016 */
  4.  
  5. int randomNo; //Variable used to generate random number.
  6. int guess; //Variable to convert string to int data type.
  7. int chances = 20; //This is the lives/chances the player has to begin with.
  8. int guess2; //Used to compare numbers between rounds
  9. ArrayList<Integer> guesses = new ArrayList<Integer>();
  10. String inputText = ""; //This is the variable for the data entered by the player.
  11. String message = ""; //Variable to display hints/result.
  12. String messageEnd = ""; //Variable to display how many attempts the game took.
  13.  
  14.  
  15. void setup(){
  16. size(1000, 500); //Sets the size of the window.
  17. smooth();
  18.  
  19. randomNo = int(random(1,100)); //generates a random number.
  20.  
  21. }
  22.  
  23.  
  24.  
  25. void draw(){
  26. background(0); // Makes background colour black.
  27.  
  28. textAlign(CENTER);
  29. textSize(40);
  30. fill(70, 175, 10); //Dark green colour for text.
  31. text("Guess a number between 1-100", 500, 50); //Sets up instructions for game.
  32. textSize(30);
  33. text("Type a number and then press the ENTER key", 500, 200); //Further instructions
  34.  
  35. textSize(15);
  36. text("Chances remaining: " + chances, 800, 400); //Displays chances remaining in bottom right corner of window.
  37.  
  38. fill(255); //White colour fill to make hints/comments stand out.
  39. textSize(50);
  40. text(inputText, 500, 400);
  41. text(message, 500, 125); //Makes hints/results draw into program.
  42. text(messageEnd, 300, 450); //Makes total attempts needed draw on screen.
  43.  
  44. //text(randomNo, 200, 400); //Shows randomNo generated - used for testing purposes and commented out when not required.
  45.  
  46. guess = int(inputText); //Converts string data type into int to compare to random number generated.
  47.  
  48. if(chances <= 0) { //If statement asking declaring what to do once the number of chances runs out.
  49. endScreen(); //Runs function "endScreen" to end the game once chances have run out.
  50. }
  51.  
  52.  
  53.  
  54.  
  55.  
  56. }
  57.  
  58.  
  59. void keyPressed(){
  60.  
  61. if (keyCode == BACKSPACE) { //Deletes last character if there is one.
  62. if (inputText.length() > 0) {
  63. inputText = inputText.substring(0, inputText.length()-1); //reduces length by 1 if backspace pressed and length is greater than 0.
  64. }
  65. }
  66. else if (keyCode == DELETE) { //Deletes all characters typed.
  67. inputText = "";
  68. }
  69. else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT) { //Allows use of modifier keys.
  70. inputText = inputText + key;
  71. }
  72.  
  73. if(keyCode == ENTER) { //Defines actions on pressing ENTER.
  74. keyCode = ' ';
  75. inputText = "";
  76. if(guesses.contains(guess)) {
  77.  
  78. message = "New number please!";
  79.  
  80. }
  81.  
  82. else {
  83. if (guess == randomNo) { //Defines actions if the number entered is equal to the random number generated.
  84. message = "Well done, you guessed the number!";
  85. messageEnd = "Attempts needed: " + (20 - chances);
  86. guesses.add(guess);
  87. }
  88. else if(guess > randomNo) { //Defines actions if the number entered is greater than the random number generated.
  89. message = "Too high, try again!";
  90. guesses.add(guess);
  91. chances = chances - 1;
  92. }
  93. else if(guess < randomNo) { //Defines actions if the number entered is less than the random number generated.
  94. message = "Too low, try again!";
  95. guesses.add(guess);
  96. chances = chances - 1;
  97. }
  98.  
  99. }
  100.  
  101. }
  102.  
  103.  
  104.  
  105.  
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement