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", "mortality",
- "precious", "america", "subordinates", "unique", "susceptible", "tactical", "overload", "combinations",
- "broadcasters", "bomb", "karate", "birthday", "song", "consoles", "sentinel", "adept", "hangman",
- "season", "widow", "tracer", "competitive", "wrecked", "strawberry", "mass", "effect", "nomad",
- "incendiary", "disrupter", "cyrogenic", "pelican", "master", "chief", "covenant" };
- System.out.println("Welcome to Hangman");
- int whatWord = (int) (Math.random() * 40);
- String guessThis = word[(whatWord )];
- String[] array = guessThis.split("");
- String[] array2 = guessThis.split("");
- String[] array3 = "thisisastringthatis26Clong".split("");
- for (int i = 0; i < 26; i++) {
- array3[i] = "-";
- }
- 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;
- int index = 0;
- while (wrong < 6 && correct < toWin + 1) {
- System.out.println();
- System.out.println("Guess a letter");
- String guess = ScanIn.nextLine().trim().toLowerCase();
- if (guess.equals(guessThis)) correct = toWin + 1;
- for (int b = 0; b <= array3.length - 1; b++) {
- if (array3[b].equals(guess)) {
- System.out.println("You have already guessed that letter");
- if (guessThis.contains(guess)){
- correct = correct - 1;
- }
- }
- }
- if (guessThis.contains(guess)) {
- for (int v = 0; v <= guessThis.length() - 1; v++) {
- if (array[v].equals(guess)) {
- array2[v] = guess;
- correct++;
- array3[index] = guess;
- }
- }
- } else{
- wrong++;
- array3[index] = guess;
- }
- drawHangman(wrong);
- printArray(array2);
- index++;
- }
- if (correct > wrong) {
- System.out.println();
- System.out.println("You Win");
- System.out.println("Your ");
- } else {
- System.out.println();
- System.out.println("You Lose");
- System.out.println("The word was " + guessThis);
- }
- }
- 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