remote87

Simple Word Guessing Game

Nov 22nd, 2020
575
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2.  
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.  
  8.     /**
  9.      * constants to keep the words for guessing ( one constant for each level )
  10.      */
  11.     final static String[] EASY_WORDS = {"bag", "car", "man", "toy", "fat", "own", "bee", "ham", "egg", "spy", "pig"};
  12.     final static String[] NORMAL_WORDS = {"town", "house", "circus", "bridge", "hook", "crab", "cave", "hang", "wife"};
  13.     final static String[] HARD_WORDS = {"elephant", "accomplishment", "representative", "establishment", "barman", "economic", "cardinal", "bottle"};
  14.  
  15.     /**
  16.      * Simple scanner
  17.      */
  18.     final static Scanner SCAN = new Scanner(System.in);
  19.  
  20.     /**
  21.      * MAIN METHOD - calls the wannaPlay method.
  22.      * @param args
  23.      */
  24.     public static void main(String[] args) {
  25.         wannaPlay();
  26.     }
  27.  
  28.     ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  29.     /**
  30.      * wannaPLay - keeps rolling and ask the player to choose a level: easy, normal or hard (case insensitive)
  31.      * if player enters something different than level names, prompts the player to quit or not (Y/N case insensitive)
  32.      * if player enters n/N - keeps asking for a level, if y/Y - exits the game with a smile
  33.      */
  34.     public static void wannaPlay(){
  35.         System.out.println("Please, choose a level: easy, normal, hard: ");
  36.         String level = SCAN.nextLine();
  37.         String exit;
  38.         if((level.equalsIgnoreCase("easy")) || (level.equalsIgnoreCase("normal")) || (level.equalsIgnoreCase("hard"))){
  39.             chooseLevel(level);
  40.         }else{
  41.             System.out.println("Do you want to quit? Y/N: ");
  42.             exit = SCAN.nextLine();
  43.             if(exit.equalsIgnoreCase("y")){
  44.                 System.out.println("Ok, we will play another time. Bye!");
  45.             }else {
  46.                 wannaPlay();
  47.             }
  48.         }
  49.     }
  50.  
  51.     ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  52.  
  53.     /**
  54.      * chooseLevel - basic game method, provoked by the the wannaPlay() - method
  55.      * @param answer- level, chosen by the player, passed by the wannaPlay method
  56.      *
  57.      */
  58.     public static void chooseLevel(String answer){
  59.         if(answer.toLowerCase().equals("easy")){
  60.             hideAndGuessWord(EASY_WORDS, EASY_WORDS.length);
  61.         }else if(answer.toLowerCase().equals("normal")){
  62.             hideAndGuessWord(NORMAL_WORDS, NORMAL_WORDS.length);
  63.         }else if(answer.toLowerCase().equals("hard")){
  64.             hideAndGuessWord(HARD_WORDS, HARD_WORDS.length);
  65.         }
  66.     }
  67.  
  68.     ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  69.  
  70.     /**
  71.      * The main game method - picks a word, depending on the chosen level, hides it using asterisks and keeps rolling until player guesses the word
  72.      * @param constants - the level chosen by the player. Randomly picks a word from constants arrays with words
  73.      * @param numberWords - the number of words inside the string array. It's needed in order to randomly pick a word from that array.
  74.      */
  75.     public static void hideAndGuessWord(String[] constants, int numberWords) {
  76.  
  77.         Random rn = new Random();
  78.         String hiddenWord = constants[rn.nextInt(numberWords)];
  79.         StringBuilder guessedWord = new StringBuilder();
  80.         guessedWord.append("*".repeat(hiddenWord.length()));
  81.         char guess;
  82.         byte counter = 0;
  83.         System.out.println(guessedWord);
  84.  
  85.         System.out.printf("Choose a letter to guess the word ( A - Z ): \n");
  86.         guess = Character.toLowerCase(SCAN.next().charAt(0));
  87.  
  88.         while(!hiddenWord.equals(guessedWord.toString())){
  89.             if(hiddenWord.indexOf(guess) != -1){
  90.  
  91.                 char[] charArr = hiddenWord.toCharArray();
  92.                 for(int i = 0; i < charArr.length; i++){
  93.                     if(charArr[i] == guess){
  94.                         guessedWord.setCharAt(i, guess);
  95.                     }
  96.                 }
  97.  
  98.                 if(hiddenWord.equals(guessedWord.toString())){
  99.                     System.out.printf("Congratulations! You guess it! The word was %s. You needed %d tries to do it!", hiddenWord, counter);
  100.                     break;
  101.                 }
  102.                 System.out.println("Good job! Next character: ");
  103.                 System.out.println(guessedWord);
  104.                 guess = Character.toLowerCase(SCAN.next().charAt(0));
  105.  
  106.             }else{
  107.                 System.out.println("Sorry! Guess again: ");
  108.                 guess = Character.toLowerCase(SCAN.next().charAt(0));
  109.             }
  110.             counter++;
  111.         }
  112.     }
  113. }
RAW Paste Data