Advertisement
Guest User

Hangman Judge

a guest
Mar 28th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner sc = new Scanner(System.in);
  9.        
  10.         while(sc.hasNext()) {
  11.             int rounds = Integer.parseInt(sc.nextLine());
  12.             if(rounds != -1) {
  13.                 HashMap<Character, Boolean> solution = new HashMap<>();
  14.                 String thisSolution = sc.nextLine();
  15.                 for(int i = 0; i < thisSolution.length(); i++) {
  16.                     solution.put(thisSolution.charAt(i), false);
  17.                 }
  18.                 String thisGuess = sc.nextLine();
  19.                 ArrayList<Character> guesses = new ArrayList<>();
  20.                 for(int i = 0; i < thisGuess.length(); i++) {
  21.                     guesses.add(thisGuess.charAt(i));
  22.                    
  23.                 }
  24.                 System.out.println("Round " + rounds);
  25.                 System.out.println(gameResult(solution, guesses));
  26.             }
  27.         }
  28.     }
  29.    
  30.     static String gameResult(HashMap<Character, Boolean> gameQ, ArrayList<Character> guess) {
  31.         ArrayList<Character> guessedCorrectChar = new ArrayList<>();
  32.         ArrayList<Character> guessedWrongChar = new ArrayList<>();
  33.         for(int i = 0; i < guess.size(); i++) {
  34.             if(!guessedCorrectChar.contains(guess.get(i)) || !guessedWrongChar.contains(guess.get(i))) {
  35.                 for(Character k : gameQ.keySet()) {
  36.                     if(guess.get(i) == k) {
  37.                         gameQ.replace(k, true);
  38.                         if(!guessedCorrectChar.contains(k)) {
  39.                             guessedCorrectChar.add(k);
  40.                         }
  41.                     }
  42.                 }
  43.                 if(!guessedCorrectChar.contains(guess.get(i)) && !guessedWrongChar.contains(guess.get(i))) {
  44.                     guessedWrongChar.add(guess.get(i));
  45.                 }
  46.             }
  47.             if(!gameQ.containsValue(false) && guessedWrongChar.size() < 7) {
  48.                 return "You win.";
  49.             }
  50.         }
  51.        
  52.         if(guessedWrongChar.size() >= 7) {
  53.             return "You lose.";
  54.         }
  55.        
  56.             return "You chickened out.";
  57.     }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement