Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Hangman2 {
- 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");
- int whatWord = (int)(Math.random() * 6) ;
- 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 wrong = 0;
- int toWin = guessThis.length();
- int correct = 1;
- //System.out.println("The first letter is given to you");
- while (wrong < 6 && correct < toWin + 1){
- System.out.println("Guess a letter");
- String guess = ScanIn.nextLine().trim().toLowerCase();
- if (guessThis.contains(guess)){
- for (int v = 0; v <= guessThis.length() - 1; v++){
- if (array[v].equals(guess)){
- array2[v] = guess;
- correct++;
- }
- }
- printArray(array2);
- }
- else
- wrong++;
- drawHangman(wrong);
- }
- if (correct > wrong){
- 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