jwood

[Freestyle] Hangman

Nov 20th, 2013
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.78 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. import javax.swing.JOptionPane;
  5.  
  6. public class Hangman {
  7.     ///////////////////////////////////
  8.     // Global Variables
  9.     ///////////////////////////////////
  10.  
  11.     private static String[] correctPhrase = new String[5];
  12.     private static String[] currentGuessPhrase = new String[5];
  13.     private static char[] currentGuesses = new char[26];
  14.     private static int totalWrong = 0;
  15.     private static int totalGuesses = 0;
  16.     private static int count;
  17.     private static char guess;
  18.  
  19.     ///////////////////////////////////
  20.     // Methods
  21.     ///////////////////////////////////
  22.  
  23.     public static void makePhrase() {
  24.         //Clear guesses
  25.         for (int x = 0; x < currentGuesses.length; x++){
  26.             currentGuesses[x] = ' ';
  27.         }
  28.         totalWrong = 0;
  29.         totalGuesses = 0;
  30.         //Preset Phrases (Must be 5 words)
  31.         String[] phraseOne = { "this", "is", "a", "sample", "phrase" };
  32.         String[] phraseTwo = { "another", "phrase", "is", "right", "here" };
  33.         String[] phraseThree = { "finally", "this", "is", "the", "last" };
  34.         //Random words for selection
  35.         String[] wordBank = { "a", "ate", "apple", "banana", "bored", "bear",
  36.                 "cat", "cow", "carried", "died", "during", "deer", "elephant",
  37.                 "flame", "fire", "fruit", "forgave", "forged", "fears", "goat",
  38.                 "good", "game", "gave", "greeted", "glory", "ham", "hairy",
  39.                 "heaven", "horrible", "I", "illegal", "important", "jammed",
  40.                 "juice", "kangaroo", "liar", "loved", "money", "miracles",
  41.                 "monday", "named", "never", "noun", "now", "nor", "orange",
  42.                 "obligated", "person", "people", "peeled", "quit", "retired",
  43.                 "rain", "saved", "sunny", "soaring", "salmon", "sealed",
  44.                 "today", "tomorrow", "trained", "the", "umbrella", "up",
  45.                 "under", "violent", "violin", "when", "while", "year", "zoo" };
  46.         //Get phrase type
  47.         Scanner in = new Scanner(System.in);
  48.         System.out.println("\n(1) Random Words (2) Presets (3) Custom");
  49.         int phraseType = in.nextInt();
  50.         if (phraseType == 1){
  51.             for (int x = 0; x < 5; x++) {
  52.                 correctPhrase[x] = wordBank[(int) Math.round(Math.random() * 61)];
  53.             }
  54.         } else if (phraseType == 2) {
  55.             int phrase = (int) Math.round(Math.random() * 2);
  56.             switch (phrase){
  57.                 case 0: correctPhrase = phraseOne.clone();
  58.                 case 1: correctPhrase = phraseTwo.clone();
  59.                 case 2: correctPhrase = phraseThree.clone();
  60.             }
  61.         } else if (phraseType == 3){
  62.             Scanner in2 = new Scanner(System.in);
  63.             System.out.println("5 Word Phrase: ");
  64.             correctPhrase = in2.nextLine().split("\\s");
  65.         }
  66.  
  67.         //Create duplicate with underscores
  68.         for (int x = 0; x < correctPhrase.length; x++) {
  69.             currentGuessPhrase[x] = correctPhrase[x].replaceAll(".", "_");
  70.         }
  71.     }
  72.    
  73.     public static char getGuess() {
  74.         Scanner in = new Scanner(System.in);
  75.         // Retrieve next guess
  76.         System.out.println("\nGuess:");
  77.         char guessInput = in.next().charAt(0);
  78.         return Character.toLowerCase(guessInput);
  79.     }
  80.  
  81.     public static boolean checkGuess(char guess) {
  82.         // Add to guessed chars
  83.         currentGuesses[totalGuesses] = guess;
  84.         totalGuesses++;
  85.         // Count number of occurrences
  86.         count = 0;
  87.         for (int x = 0; x < correctPhrase.length; x++) {
  88.             for (int a = 0; a < correctPhrase[x].length(); a++) {
  89.                 if (correctPhrase[x].charAt(a) == guess) {
  90.                     count++;
  91.                 }
  92.             }
  93.         }
  94.         if (count == 0) {
  95.             return false;
  96.         } else {
  97.             return true;
  98.         }
  99.     }
  100.  
  101.     public static void updateGuess(char guess) {
  102.         // Define char array from currentGuess for alerting
  103.         char[][] currentGuessArray = new char[currentGuessPhrase.length][];
  104.         for (int x = 0; x < currentGuessPhrase.length; x++) {
  105.             currentGuessArray[x] = currentGuessPhrase[x].toCharArray();
  106.         }
  107.         //Assign valid values of guess to currentGuessArray
  108.         for (int x = 0; x < correctPhrase.length; x++) {
  109.             for (int a = 0; a < correctPhrase[x].length(); a++) {
  110.                 if (correctPhrase[x].charAt(a) == guess) {
  111.                     currentGuessArray[x][a] = guess;
  112.                 }
  113.             }
  114.         }
  115.         // Convert chars back to string array
  116.         for (int x = 0; x < currentGuessArray.length; x++) {
  117.             currentGuessPhrase[x] = new String(currentGuessArray[x]);
  118.         }
  119.     }
  120.     public static void drawBoard(){
  121.        
  122.         // Print previous guesses
  123.         System.out.println("\nGuesses:\n");
  124.         for (int x = 0; x < currentGuesses.length; x++) {
  125.             System.out.print(currentGuesses[x] + " ");
  126.         }
  127.         // Draw hangman
  128.         System.out.print(" \n ");
  129.         if (totalWrong == 0){
  130.             System.out.print("\n______" +
  131.                              "\n|    |" +
  132.                              "\n|     " +
  133.                              "\n|     " +
  134.                              "\n|     " +
  135.                              "\n|     " +
  136.                              "\n|     ");
  137.         } else if (totalWrong == 1){
  138.             System.out.print("\n______" +
  139.                              "\n|    |" +
  140.                              "\n|    O" +
  141.                              "\n|     " +
  142.                              "\n|     " +
  143.                              "\n|     " +
  144.                              "\n|     ");
  145.         } else if (totalWrong == 2){
  146.             System.out.print("\n______" +
  147.                              "\n|    |" +
  148.                              "\n|    O" +
  149.                              "\n|    |" +
  150.                              "\n|     " +
  151.                              "\n|     " +
  152.                              "\n|     ");
  153.         } else if (totalWrong == 3){
  154.             System.out.print("\n______" +
  155.                              "\n|    |" +
  156.                              "\n|    O" +
  157.                              "\n|    |" +
  158.                              "\n|   / " +
  159.                              "\n|     " +
  160.                              "\n|     ");
  161.         } else if (totalWrong == 4){
  162.             System.out.print("\n______" +
  163.                              "\n|    |" +
  164.                              "\n|    O" +
  165.                              "\n|    |" +
  166.                              "\n|   / \\" +
  167.                              "\n|     " +
  168.                              "\n|     ");      
  169.         } else if (totalWrong == 5){
  170.             System.out.print("\n______" +
  171.                              "\n|    |" +
  172.                              "\n|    O" +
  173.                              "\n|    |-" +
  174.                              "\n|   / \\" +
  175.                              "\n|     " +
  176.                              "\n|     ");  
  177.         } else if (totalWrong == 6){
  178.             System.out.print("\n______" +
  179.                              "\n|    |" +
  180.                              "\n|    O" +
  181.                              "\n|   -|-" +
  182.                              "\n|   / \\" +
  183.                              "\n|     " +
  184.                              "\n|     " +
  185.                              "\n\n YOU DIED!");
  186.             //Print correct phrase
  187.             System.out.println("\n");
  188.             for (int x = 0; x < correctPhrase.length; x++){
  189.                 System.out.print(correctPhrase[x] + " ");
  190.             }
  191.         }
  192.         //Print guessPhrase
  193.         System.out.println("\n");
  194.         for (int x = 0; x < currentGuessPhrase.length; x++){
  195.             System.out.print(currentGuessPhrase[x] + " ");
  196.         }
  197.     }
  198.     public static boolean goAgain(){
  199.         //Retreive yes/no
  200.         int dialogButton = JOptionPane.YES_NO_OPTION;
  201.         int dialogResult = JOptionPane.showConfirmDialog(null, "Play again?", "Hangman", dialogButton);
  202.         if (dialogResult == 0 ){
  203.             return true;
  204.         } else {
  205.             return false;
  206.         }
  207.     }
  208.  
  209.     ///////////////////////////////////
  210.     // Main Method
  211.     ///////////////////////////////////
  212.  
  213.     public static void main(String[] args) {
  214.         boolean goAgain = true;
  215.         boolean isCorrect;
  216.         makePhrase();
  217.         while (goAgain){
  218.             //Print correct for debugging
  219.             /*for (int x = 0; x < correctPhrase.length; x++){
  220.                 System.out.print(correctPhrase[x] + " ");
  221.             }
  222.             */
  223.             drawBoard();
  224.             guess = getGuess();
  225.             isCorrect = checkGuess(guess);
  226.             //Update board
  227.             if (isCorrect) {
  228.                 updateGuess(guess);
  229.             } else {
  230.                 totalWrong++;
  231.             }
  232.             //Determine loss
  233.             if (totalWrong == 6){
  234.                 drawBoard();
  235.                 goAgain = goAgain();
  236.                 if (goAgain){
  237.                     makePhrase();
  238.                 } else {
  239.                     break;
  240.                 }
  241.             }
  242.             //Determine win
  243.             if (Arrays.equals(correctPhrase, currentGuessPhrase)){
  244.                 System.out.println("\nYOU WIN!" +
  245.                                    "\n     O" +
  246.                                    "\n    -|-" +
  247.                                    "\n    / \\");
  248.                 for (int x = 0; x < correctPhrase.length; x++){
  249.                     System.out.print(correctPhrase[x] + " ");
  250.                 }
  251.                 goAgain = goAgain();
  252.                 if (goAgain){
  253.                     makePhrase();
  254.                 }
  255.             }
  256.         }
  257.     }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment