Advertisement
Guest User

Lab Driver

a guest
Oct 19th, 2015
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class SecretWordGame {
  3.  
  4. //Creates a constant for the number of tries in this game
  5. public static final int NUM_TRIES = 5;
  6. public static void main(String[] args) {
  7.  
  8. //Creates the object for user input
  9. Scanner keyboard = new Scanner(System.in);
  10.  
  11. //Starts the overall game
  12. boolean quit = false;
  13. while(quit == false)
  14. {
  15. //Starts the main game loop
  16. System.out.println("Welcome to the word guessing game! You have "+NUM_TRIES+" tries to guess the secret word!");
  17. SecretWord sWord = new SecretWord();
  18. System.out.println("The current hint is\n "+sWord.getHintWord());
  19. while(sWord.getNumberOfTurns()<NUM_TRIES)
  20. {
  21. System.out.println("Guess a lowercase letter");
  22.  
  23. //Gets the first letter of whatever is entered into the console
  24. char guess = keyboard.nextLine().charAt(0);
  25.  
  26. //Calls the guessLetter method from secret word to update the hint
  27. sWord.guessLetter(guess);
  28.  
  29. System.out.println(sWord.getHintWord());
  30.  
  31. System.out.println("Guess the secret word");
  32.  
  33. String sGuess = keyboard.nextLine();
  34. //Checks to see if the guess was correct
  35. if(sGuess.equals(sWord.getSecretWord()))
  36. {
  37. System.out.println("You win!");
  38. break;
  39. }
  40. else
  41. {
  42. System.out.println("Keep trying!");
  43. }
  44. sWord.setNumberOfTurns(sWord.getNumberOfTurns()+1);
  45. }
  46. //Prompts the user to play again
  47. System.out.println("Game over! Would you like to try again?");
  48. String input = keyboard.nextLine();
  49. if(input.equalsIgnoreCase("no"))
  50. {
  51. quit = true;
  52. }
  53. else
  54. {
  55. System.out.println("Let's go again!");
  56. }
  57. }
  58. System.out.println("Goodbye!");
  59. }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement