Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Luke Burgess 08016497
- Assessment
- Version 2.0 05/12/2016 */
- int randomNo; //Variable used to generate random number.
- int guess; //Variable to convert string to int data type.
- int chances = 20; //This is the lives/chances the player has to begin with.
- int guess2; //Used to compare numbers between rounds
- ArrayList<Integer> guesses = new ArrayList<Integer>();
- String inputText = ""; //This is the variable for the data entered by the player.
- String message = ""; //Variable to display hints/result.
- String messageEnd = ""; //Variable to display how many attempts the game took.
- void setup(){
- size(1000, 500); //Sets the size of the window.
- smooth();
- randomNo = int(random(1,100)); //generates a random number.
- }
- void draw(){
- background(0); // Makes background colour black.
- textAlign(CENTER);
- textSize(40);
- fill(70, 175, 10); //Dark green colour for text.
- text("Guess a number between 1-100", 500, 50); //Sets up instructions for game.
- textSize(30);
- text("Type a number and then press the ENTER key", 500, 200); //Further instructions
- textSize(15);
- text("Chances remaining: " + chances, 800, 400); //Displays chances remaining in bottom right corner of window.
- fill(255); //White colour fill to make hints/comments stand out.
- textSize(50);
- text(inputText, 500, 400);
- text(message, 500, 125); //Makes hints/results draw into program.
- text(messageEnd, 300, 450); //Makes total attempts needed draw on screen.
- //text(randomNo, 200, 400); //Shows randomNo generated - used for testing purposes and commented out when not required.
- guess = int(inputText); //Converts string data type into int to compare to random number generated.
- if(chances <= 0) { //If statement asking declaring what to do once the number of chances runs out.
- endScreen(); //Runs function "endScreen" to end the game once chances have run out.
- }
- }
- void keyPressed(){
- if (keyCode == BACKSPACE) { //Deletes last character if there is one.
- if (inputText.length() > 0) {
- inputText = inputText.substring(0, inputText.length()-1); //reduces length by 1 if backspace pressed and length is greater than 0.
- }
- }
- else if (keyCode == DELETE) { //Deletes all characters typed.
- inputText = "";
- }
- else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT) { //Allows use of modifier keys.
- inputText = inputText + key;
- }
- if(keyCode == ENTER) { //Defines actions on pressing ENTER.
- keyCode = ' ';
- inputText = "";
- if(guesses.contains(guess)) {
- message = "New number please!";
- }
- else {
- if (guess == randomNo) { //Defines actions if the number entered is equal to the random number generated.
- message = "Well done, you guessed the number!";
- messageEnd = "Attempts needed: " + (20 - chances);
- guesses.add(guess);
- }
- else if(guess > randomNo) { //Defines actions if the number entered is greater than the random number generated.
- message = "Too high, try again!";
- guesses.add(guess);
- chances = chances - 1;
- }
- else if(guess < randomNo) { //Defines actions if the number entered is less than the random number generated.
- message = "Too low, try again!";
- guesses.add(guess);
- chances = chances - 1;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement