Hangman - Solution

On se retrouve aujourd'hui pour la solution du précédent #KataOfTheWeek proposé par Kévin en début de semaine !

Voici la solution proposée :

public class Hangman {

    public static int ATTEMPTS = 6;

    public static void main(String[] args) throws FileNotFoundException {

        /** file containing the words to guess in resources folder :
            morbier
            abondance
            comté
            bleu de laqueuille
        **/
        File hangmanFile = new File(Hangman.class.getClassLoader().getResource("words-to-guess.txt").getFile());
        Scanner scanner = new Scanner(hangmanFile);
        Scanner input = new Scanner(System.in);

        List<String> words = new ArrayList();
        while (scanner.hasNext()) {
            words.add(scanner.nextLine());
        }

        // select randomly a word from the file
        Random rand = new Random();
        String word = words.get(rand.nextInt(words.size()));
        char[] wordArray = word.toCharArray();

        char[] playerGuesses = new char[wordArray.length];
        boolean endGame = false;

        System.out.println("Guess the following word : ");
        printWordState(word, playerGuesses);

        while (!endGame) {

            System.out.println("******* It's time to guess : enter a letter *******");

            String letter = input.next();
            // input validation
            while (letter.length() != 1 || Character.isDigit(letter.charAt(0))) {
                System.out.println("Error Input. Try again.");
                letter = input.next();
            }

            // check if letter from word has been found
            boolean found = false;
            for (int i = 0; i < wordArray.length; i++) {
                if (letter.charAt(0) == wordArray[i]) {
                    playerGuesses[i] = wordArray[i];
                    found = true;
                }
            }

            if (!found) {
                ATTEMPTS--;
                System.out.println("Wrong Letter !");
            }

            System.out.println("\n" + "Attempts remaining : " + ATTEMPTS);
            // draw hangman
            drawHangman(ATTEMPTS);

            // winning condition
            if (printWordState(word, playerGuesses)) {
                System.out.println("Congratulations ! You win !");
                endGame = true;
            }

            // losing condition
            if (ATTEMPTS == 0) {
                System.out.println("Sorry, you lose ! Maybe next time ! The word was " + word);
                endGame = true;
            }
        }
    }

    private static boolean printWordState(String word, char[] playerGuesses) {
        int correctCount = 0;

        for (int i = 0; i < word.length(); i++) {
            if (playerGuesses[i] == word.charAt(i)) {
                System.out.print(word.charAt(i));
                correctCount++;
            } else {
                System.out.print("-");
            }
        }
        System.out.println();

        return word.length() == correctCount;
    }

    public static void drawHangman(int attempts) {
        System.out.println(" -------");
        System.out.println(" |     |");
        if (attempts <= 5) {
            System.out.println(" 0");
        }

        if (attempts <= 4) {
            System.out.print("\\ ");
            if (attempts <= 3) {
                System.out.println("/");
            } else {
                System.out.println("");
            }
        }

        if (attempts <= 2) {
            System.out.println(" |");
        }

        if (attempts <= 1) {
            System.out.print("/ ");
            if (attempts <= 0) {
                System.out.println("\\");
            } else {
                System.out.println("");
            }
        }
    }
}

A bientôt pour un nouveau #KataOfTheWeek !

TakiVeille

TakiVeille