Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Random;
- import java.util.Scanner;
- public class Main {
- /**
- * constants to keep the words for guessing ( one constant for each level )
- */
- final static String[] EASY_WORDS = {"bag", "car", "man", "toy", "fat", "own", "bee", "ham", "egg", "spy", "pig"};
- final static String[] NORMAL_WORDS = {"town", "house", "circus", "bridge", "hook", "crab", "cave", "hang", "wife"};
- final static String[] HARD_WORDS = {"elephant", "accomplishment", "representative", "establishment", "barman", "economic", "cardinal", "bottle"};
- /**
- * Simple scanner
- */
- final static Scanner SCAN = new Scanner(System.in);
- /**
- * MAIN METHOD - calls the wannaPlay method.
- * @param args
- */
- public static void main(String[] args) {
- wannaPlay();
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * wannaPLay - keeps rolling and ask the player to choose a level: easy, normal or hard (case insensitive)
- * if player enters something different than level names, prompts the player to quit or not (Y/N case insensitive)
- * if player enters n/N - keeps asking for a level, if y/Y - exits the game with a smile
- */
- public static void wannaPlay(){
- System.out.println("Please, choose a level: easy, normal, hard: ");
- String level = SCAN.nextLine();
- String exit;
- if((level.equalsIgnoreCase("easy")) || (level.equalsIgnoreCase("normal")) || (level.equalsIgnoreCase("hard"))){
- chooseLevel(level);
- }else{
- System.out.println("Do you want to quit? Y/N: ");
- exit = SCAN.nextLine();
- if(exit.equalsIgnoreCase("y")){
- System.out.println("Ok, we will play another time. Bye!");
- }else {
- wannaPlay();
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * chooseLevel - basic game method, provoked by the the wannaPlay() - method
- * @param answer- level, chosen by the player, passed by the wannaPlay method
- *
- */
- public static void chooseLevel(String answer){
- if(answer.toLowerCase().equals("easy")){
- hideAndGuessWord(EASY_WORDS, EASY_WORDS.length);
- }else if(answer.toLowerCase().equals("normal")){
- hideAndGuessWord(NORMAL_WORDS, NORMAL_WORDS.length);
- }else if(answer.toLowerCase().equals("hard")){
- hideAndGuessWord(HARD_WORDS, HARD_WORDS.length);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * The main game method - picks a word, depending on the chosen level, hides it using asterisks and keeps rolling until player guesses the word
- * @param constants - the level chosen by the player. Randomly picks a word from constants arrays with words
- * @param numberWords - the number of words inside the string array. It's needed in order to randomly pick a word from that array.
- */
- public static void hideAndGuessWord(String[] constants, int numberWords) {
- Random rn = new Random();
- String hiddenWord = constants[rn.nextInt(numberWords)];
- StringBuilder guessedWord = new StringBuilder();
- guessedWord.append("*".repeat(hiddenWord.length()));
- char guess;
- byte counter = 0;
- System.out.println(guessedWord);
- System.out.printf("Choose a letter to guess the word ( A - Z ): \n");
- guess = Character.toLowerCase(SCAN.next().charAt(0));
- while(!hiddenWord.equals(guessedWord.toString())){
- if(hiddenWord.indexOf(guess) != -1){
- char[] charArr = hiddenWord.toCharArray();
- for(int i = 0; i < charArr.length; i++){
- if(charArr[i] == guess){
- guessedWord.setCharAt(i, guess);
- }
- }
- if(hiddenWord.equals(guessedWord.toString())){
- System.out.printf("Congratulations! You guess it! The word was %s. You needed %d tries to do it!", hiddenWord, counter);
- break;
- }
- System.out.println("Good job! Next character: ");
- System.out.println(guessedWord);
- guess = Character.toLowerCase(SCAN.next().charAt(0));
- }else{
- System.out.println("Sorry! Guess again: ");
- guess = Character.toLowerCase(SCAN.next().charAt(0));
- }
- counter++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement