Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Name: Erik Marquez
- // Description: Program that runs a game of hangman wih 3 possible difficulty levels and replayability
- // Date Created: January 21, 2019
- // Date Revised: January 22, 2019
- import java.util.Scanner;
- import java.util.Random;
- public class hangmanMAR
- {
- public static void main (String [] args)
- {
- String[] ezWords = {"hapon", "wisp", "kirby", "atari", "metroid", "brexit", "luigi", "saints", "hornet", "jello"}; // Declares required variables along with arrays with words for each difficulty
- String[] normWords = {"king lear", "emperor palpatine", "sicko mode", "puffer fish", "ubiquitous", "hallucinations", "indefatigable", "computer science"};
- String[] hardWords = {"james t. kirk", "barney the dinosaur", "incomprehensible", "multidisciplinary", "supercalifragilisticexpialidocious", "palaeoanthropology", "fitnessgram pacer test", "rambunctiousness"};
- int difficulty;
- Scanner scanner = new Scanner(System.in); // Code required for scnner class and randomization
- Random random = new Random();
- boolean gameRunning = true; // Replay function
- while (gameRunning)
- {
- System.out.print("Hola. PLease choose your desired difficulty\n 1. Easy\n 2. Normal\n 3. Hard\n ");
- difficulty = scanner.nextInt();
- if (difficulty == 1)
- {
- char[] randomEz = ezWords[random.nextInt(ezWords.length)].toCharArray(); // Breaks word(s) into individual characters
- int guessNum = randomEz.length; // Number of guesses alloted until game over
- char[] playerGuess = new char [guessNum]; // Visualization --> _ _ _ _
- for (int i = 0; i < playerGuess.length; i++)
- {
- playerGuess[i] = '_';
- }
- boolean wordSuccess = false;
- int attempts = 0;
- while (!wordSuccess && attempts != guessNum) // While word is not guessed and attempts have not all been used, run the following
- {
- System.out.print("Current guesses--> ");
- printArray(playerGuess); // Calls printArray
- System.out.printf("You have %d attempts left.\n ", guessNum - attempts); // Takes amount of characters minus amount of attempts to display remaining amount of guesses
- System.out.println("Enter a single character (lowercase) ");
- char input = scanner.nextLine().charAt(0); // Makes it so if user unputs more than a single character it'll still only use the first character inputted
- attempts++;
- if (input == '-') // If user inputs minus sign, game instantly ends
- {
- gameRunning = false;
- wordSuccess = true;
- }
- else
- {
- for (int i = 0; i < randomEz.length; i++)
- {
- if (randomEz[i] == input)
- {
- playerGuess[i] = input;
- }
- }
- if (wordSuccess(playerGuess))
- {
- wordSuccess = true;
- System.out.println("Congrats, you won! ");
- }
- }
- }
- if (!wordSuccess) // if player loses, asks if they'd like to replay
- {
- System.out.println("You ran out of guesses! ");
- System.out.println("Do you want to play again? (yes/no) ");
- String playAgain = scanner.nextLine();
- if (playAgain.equals("no"))
- {
- gameRunning = false;
- }
- }
- }
- if (difficulty == 2)
- {
- char[] randomNorm = normWords[random.nextInt(normWords.length)].toCharArray(); // Breaks word(s) into individual characters
- int guessNum = randomNorm.length; // Number of guesses alloted until game over
- char[] playerGuess = new char [guessNum]; // Visualization --> _ _ _ _
- for (int i = 0; i < playerGuess.length; i++)
- {
- playerGuess[i] = '_';
- }
- boolean wordSuccess = false;
- int attempts = 0;
- while (!wordSuccess && attempts != guessNum) // While word is not guessed and attempts have not all been used, run the following
- {
- System.out.print("Current guesses--> ");
- printArray(playerGuess); // Calls printArray
- System.out.printf("You have %d attempts left.\n ", guessNum - attempts); // Takes amount of characters minus amount of attempts to display remaining amount of guesses
- System.out.println("Enter a single character (lowercase) ");
- char input = scanner.nextLine().charAt(0); // Makes it so if user unputs more than a single character it'll still only use the first character inputted
- attempts++;
- if (input == '-') // If user inputs minus sign, game instantly ends
- {
- gameRunning = false;
- wordSuccess = true;
- }
- else
- {
- for (int i = 0; i < randomNorm.length; i++)
- {
- if (randomNorm[i] == input)
- {
- playerGuess[i] = input;
- }
- }
- if (wordSuccess(playerGuess))
- {
- wordSuccess = true;
- System.out.println("Congrats, you won! ");
- }
- }
- }
- if (!wordSuccess) // if player loses, asks if they'd like to replay
- {
- System.out.println("You ran out of guesses! ");
- System.out.println("Do you want to play again? (yes/no) ");
- String playAgain = scanner.nextLine();
- if (playAgain.equals("no"))
- {
- gameRunning = false;
- }
- }
- }
- if (difficulty == 3)
- {
- char[] randomHard = hardWords[random.nextInt(hardWords.length)].toCharArray(); // Breaks word(s) into individual characters
- int guessNum = randomHard.length; // Number of guesses alloted until game over
- char[] playerGuess = new char [guessNum]; // Visualization --> _ _ _ _
- for (int i = 0; i < playerGuess.length; i++)
- {
- playerGuess[i] = '_';
- }
- boolean wordSuccess = false;
- int attempts = 0;
- while (!wordSuccess && attempts != guessNum) // While word is not guessed and attempts have not all been used, run the following
- {
- System.out.print("Current guesses--> ");
- printArray(playerGuess); // Calls printArray
- System.out.printf("You have %d attempts left.\n ", guessNum - attempts); // Takes amount of characters minus amount of attempts to display remaining amount of guesses
- System.out.println("Enter a single character (lowercase) ");
- char input = scanner.nextLine().charAt(0); // Makes it so if user unputs more than a single character it'll still only use the first character inputted
- attempts++;
- if (input == '-') // If user inputs minus sign, game instantly ends
- {
- gameRunning = false;
- wordSuccess = true;
- }
- else
- {
- for (int i = 0; i < randomHard.length; i++)
- {
- if (randomHard[i] == input)
- {
- playerGuess[i] = input;
- }
- }
- if (wordSuccess(playerGuess))
- {
- wordSuccess = true;
- System.out.println("Congrats, you won! ");
- }
- }
- }
- if (!wordSuccess) // if player loses, asks if they'd like to replay
- {
- System.out.println("You ran out of guesses! ");
- System.out.println("Do you want to play again? (yes/no) ");
- String playAgain = scanner.nextLine();
- if (playAgain.equals("no"))
- {
- gameRunning = false;
- }
- }
- }
- }
- System.out.print("Game over");
- }
- public static void printArray (char[] array)
- {
- for (int i = 0; i < array.length; i++)
- {
- System.out.print(array[i] + " ");
- }
- System.out.println();
- }
- public static boolean wordSuccess (char[] array)
- {
- for (int i = 0; i < array.length; i++)
- {
- if (array[i] == '_') return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement