Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Marcos T Lopez
- *
- */
- import java.util.Scanner;
- public class Hangman {
- public static Scanner ScanIn = new Scanner(System.in);
- public static void main(String [] args) {
- String[] word=
- {"geoengineering", "glaciers" , "hunter", "distrubution", "originating" ,"climate"};
- System.out.println("Welcome to Hangman");
- System.out.println("Please pick a number between 1 - 6");
- int whatWord = ScanIn.nextInt();
- String guessThis = word[(whatWord - 1)];
- String[] array = guessThis.split("");
- String[] array2 = guessThis.split("");
- for (int i = guessThis.length() - 1; i >= 0; i = i - 1){
- array2[i] = "_";
- }
- //printArray(array2);
- drawHangman(0);
- int count = 0;
- int toWin = guessThis.length();
- int correct = 0;
- System.out.println("The first letter is given to you");
- while (count < 6 && correct < toWin + 1){
- System.out.println("Guess a letter");
- String guess = ScanIn.nextLine().trim().toLowerCase();
- if (guessThis.contains(guess)){
- int a = guessThis.indexOf(guess);
- System.out.println(array[a]);
- correct++;
- array2 [a] = guess;
- printArray(array2);
- }
- else
- count++;
- drawHangman(count);
- }
- if (correct > count){
- System.out.println("You Win");
- }
- else {
- System.out.println("You Lose");
- }
- }
- public static void drawHangman(int a){
- String A = " ____";
- String B = " |/ |";
- String C = " |";
- String D = " |";
- String E = " |";
- String F = " |";
- String G = "----";
- if (a >= 1) C = " | O";
- if (a == 2) D = " | |";
- if (a == 3) D = " | /|";
- if (a >= 4) D = " | /|\\";
- if (a == 5) E = " | / ";
- if (a >= 6) E = " | / \\ ";
- System.out.println();
- System.out.println(A);
- System.out.println(B);
- System.out.println(C);
- System.out.println(D);
- System.out.println(E);
- System.out.println(F);
- System.out.println(G);
- }
- private static void printArray(String[] anArray) {
- for (int i = 0; i < anArray.length; i++) {
- if (i > 0) {
- System.out.print("");
- }
- System.out.print(anArray[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement