Jackson_Pike

HangMan.java

Oct 24th, 2017
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 KB | None | 0 0
  1. package Testing;
  2. import java.util.Scanner;
  3. /*
  4.  * @authors Jackson Pike, Kally Smith, Garrett Marion, Andrew Arthur, and Timothy Whalen
  5.  */
  6. public class HangMan {
  7.     //Initialize Scanner and Variables
  8.     Scanner input = new Scanner(System.in);
  9.     String printUsed;
  10.     int count;
  11.     int word;
  12.     int left;
  13.     String num;
  14.     //Helper method to add a line pace
  15.     public void space() {
  16.         System.out.println();
  17.     }
  18.     //Main constructor
  19.     public HangMan() {
  20.         //Print The welcome message
  21.         images.print("Welcome to HangMan the computer game.");
  22.         images.o0();
  23.         //Start first();
  24.         first();
  25.     }
  26.     //Method to get the current state of the noose
  27.     public void getHangStat() {
  28.         //Change count to an String and assign that value to num. This method references the class 'images'
  29.         num = Integer.toString(count);
  30.         if (num.equals("1")) {
  31.             images.o1();
  32.             space();
  33.         } else if (num.equals("0")) {
  34.             images.o0();
  35.             space();
  36.         } else if (num.equals("2")) {
  37.             images.o2();
  38.             space();
  39.         } else if (num.equals("3")) {
  40.             images.o3();
  41.             space();
  42.         } else if (num.equals("4")) {
  43.             images.o4();
  44.             space();
  45.         } else if (num.equals("5")) {
  46.             images.o5();
  47.             space();
  48.         } else if (num.equals("6")) {
  49.             images.o6();
  50.             space();
  51.         } else if (num.equals("7")) {
  52.             images.o7();
  53.             space();
  54.         }
  55.     }
  56.     //Method to print the current guessed letters
  57.     public void printUsed(String used[]) {
  58.         //Uses 'used' array to print out _ _ _ _ _ _ _ replacing the '_' with letters if guessed
  59.         System.out.println(used[0] + " " + used[1] + " " + used[2] + " " + used[3] + " " + used[4] + " " + used[5] + " "
  60.                 + used[6]);
  61.     }
  62.     //Method defining the 'used' array
  63.     public void first() {
  64.         String used[] = new String[7];
  65.         //Assign all the values in the array to '_' to begin with
  66.         used[0] = "_";
  67.         used[1] = "_";
  68.         used[2] = "_";
  69.         used[3] = "_";
  70.         used[4] = "_";
  71.         used[5] = "_";
  72.         used[6] = "_";
  73.         //Start the method 'Second'
  74.         second(used);
  75.     }
  76.     //Method where all the action happens.
  77.     public void second(String used[]) {
  78.         //Happens while the word isn't guessed and they haven't guessed 7 times
  79.         do {
  80.             //Space method referenced above
  81.             space();
  82.             //Print out the guess
  83.             System.out.println("Enter your guess: ");
  84.             space();
  85.             //Assign choice1 the value of user input
  86.             String choice1 = input.next();
  87.             space();
  88.             //If statements that do the actual guessing
  89.             if (choice1.equalsIgnoreCase("a")) {
  90.                 used[1] = "A";
  91.                 word++;
  92.                 getHangStat();
  93.                 printUsed(used);
  94.             } else if (choice1.equalsIgnoreCase("G")) {
  95.                 used[0] = "G";
  96.                 word++;
  97.                 getHangStat();
  98.                 printUsed(used);
  99.             } else if (choice1.equalsIgnoreCase("R")) {
  100.                 used[2] = "R";
  101.                 used[3] = "R";
  102.                 word++;
  103.                 word++;
  104.                 getHangStat();
  105.                 printUsed(used);
  106.             } else if (choice1.equalsIgnoreCase("E")) {
  107.                 used[4] = "E";
  108.                 word++;
  109.                 getHangStat();
  110.                 printUsed(used);
  111.             } else if (choice1.equalsIgnoreCase("T")) {
  112.                 used[6] = "T";
  113.                 used[5] = "T";
  114.                 word++;
  115.                 word++;
  116.                 getHangStat();
  117.                 printUsed(used);
  118.             } else {
  119.                 count++;
  120.                 System.out.println("Try again");
  121.                 getHangStat();
  122.                 printUsed(used);
  123.                 if (count > 7) {
  124.                     break;
  125.                 }
  126.             }
  127.             left = 7 - count;
  128.             space();
  129.             System.out.println("You have " + left + " guesses left.");
  130.         } while (count < 7 && word < 7);
  131.         if (count == 7) {
  132.             System.out.print(
  133.                     "Ahhh crap. You didn't quite guess it soon enough. The man was hanged. The word was: G A R R E T");
  134.  
  135.         }
  136.         if (word == 7) {
  137.             System.out.println("G A R R E T T");
  138.             System.out.println("Congrats, you saved him!!!");
  139.         }
  140.     }
  141.  
  142.     public static void main(String[] args) {
  143.         new HangMan();
  144.  
  145.     }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment