Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Exercise4 {
- String[] possibleWords = {"cat", "dog", "cow", "milk", "work", "hello", "world", "monkey", "banana"};
- String secretWord = pickRandomWord();
- String underScores;
- int lives = 0;
- boolean isWordGuessed = false;
- public static void main(String[] args) {
- new Exercise4().run();
- }
- public void run() {
- int menuSelection = -1;
- while (menuSelection != 0 && !isWordGuessed && lives != 10) {
- printMenu();
- System.out.println();
- System.out.println("Please select menu option");
- menuSelection = userInputMenu(0, 2);
- if (menuSelection == 1) {
- startGame();
- } else if (menuSelection == 2) {
- System.out.println(secretWord);
- } else {
- System.out.println("Invalid");
- }
- }
- }
- public void printMenu() {
- System.out.println
- ("******************************************\n" +
- "* Hangman\n" +
- "******************************************\n" +
- "* 1) play\n" +
- "* 2) Show the word\n" +
- "* 0) Exit\n" +
- "******************************************");
- }
- public int userInputMenu(int lower, int higher) {
- Scanner sc = new Scanner(System.in);
- int userInput = sc.nextInt();
- while (userInput < lower && userInput >= higher) {
- System.out.print("That's an invalid input try again: ");
- userInput = sc.nextInt();
- }
- return userInput;
- }
- public String pickRandomWord() {
- int randomNumberOne = (int) (Math.random() * possibleWords.length);
- int randomNumberTwo = (int) (Math.random() * possibleWords.length);
- String randomWord = possibleWords[randomNumberOne];
- possibleWords[randomNumberOne] = possibleWords[randomNumberTwo];
- possibleWords[randomNumberTwo] = randomWord;
- return randomWord;
- }
- public void startGame() {
- Scanner sc = new Scanner(System.in);
- while (lives > 10 && underScores.contains("_"))
- System.out.println("Enter any letter");
- System.out.println(underScores);
- String userGuess = sc.next();
- gameConditions(userGuess);
- }
- public void gameConditions(String userGuess) {
- String newUnderScore = "";
- for (int i = 0; i < secretWord.length(); i++) {
- if (secretWord.charAt(i) == userGuess.charAt(0)) {
- newUnderScore += userGuess.charAt(0);
- } else if (underScores.charAt(i) != '_') {
- newUnderScore += secretWord.charAt(i);
- } else {
- newUnderScore += "_";
- }
- }
- if (underScores.equals(newUnderScore)) {
- lives++;
- printBoard();
- } else {
- underScores = newUnderScore;
- }
- if (underScores.equals(secretWord)) {
- System.out.println("You are the winner! the word was: " + secretWord);
- isWordGuessed = true;
- }
- }
- public void printBoard() {
- if (lives <= 0) {
- System.out.println("");
- System.out.println("");
- System.out.println("");
- System.out.println("");
- System.out.println("");
- System.out.println("_ _ _");
- } else if (lives == 1) {
- System.out.println("");
- System.out.println(" |");
- System.out.println(" |");
- System.out.println(" |");
- System.out.println(" |");
- System.out.println("_|_ _");
- } else if (lives == 2) {
- System.out.println(" ____");
- System.out.println(" |");
- System.out.println(" |");
- System.out.println(" |");
- System.out.println(" |");
- System.out.println("_|_ _");
- } else if (lives == 3) {
- System.out.println(" ____");
- System.out.println(" |/");
- System.out.println(" |");
- System.out.println(" |");
- System.out.println(" |");
- System.out.println("_|_ _");
- } else if (lives == 4) {
- System.out.println(" ____");
- System.out.println(" |/ |");
- System.out.println(" |");
- System.out.println(" |");
- System.out.println(" |");
- System.out.println("_|_ _");
- } else if (lives == 5) {
- System.out.println(" ____");
- System.out.println(" |/ |");
- System.out.println(" | o");
- System.out.println(" |");
- System.out.println(" |");
- System.out.println("_|_ _");
- } else if (lives == 6) {
- System.out.println(" ____");
- System.out.println(" |/ |");
- System.out.println(" | o");
- System.out.println(" | 0");
- System.out.println(" |");
- System.out.println("_|_ _");
- } else if (lives == 7) {
- System.out.println(" ____");
- System.out.println(" |/ |");
- System.out.println(" | o");
- System.out.println(" | /0");
- System.out.println(" |");
- System.out.println("_|_ _");
- } else if (lives == 8) {
- System.out.println(" ____");
- System.out.println(" |/ |");
- System.out.println(" | o");
- System.out.println(" | /0\\");
- System.out.println(" |");
- System.out.println("_|_ _");
- } else if (lives == 9) {
- System.out.println(" ____");
- System.out.println(" |/ |");
- System.out.println(" | o");
- System.out.println(" | /0\\");
- System.out.println(" | /");
- System.out.println("_|_ _");
- } else {
- System.out.println(" ____");
- System.out.println(" |/ |");
- System.out.println(" | o");
- System.out.println(" | /0\\");
- System.out.println(" | / \\");
- System.out.println("_|_ _");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement