manish

Hangman by manish

May 18th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Hangman {
  4.  
  5.     static String words[] = {"expression", "illegal", "query", "elaborate", "sunshine", "darkness", "stationary", "original", "confused", "superior"};
  6.     static int x = ((int) (Math.random() * 10)), life = 7;
  7.     static String word = words[x], word2, word3, name, chosen = "";
  8.     static char letter;
  9.  
  10.     static void getword() {
  11.         word2 = "";
  12.         for (int i = 0; i < word.length(); i++) {
  13.             word2 = word2 + '*';
  14.         }
  15.         System.out.println(" Welcome to HangMan. You start with 7 lives. You lose a live for every wrong letter. ");
  16.         System.out.println(" The letters are:        " + word2);
  17.     }
  18.  
  19.     static void input() {
  20.         System.out.println(" Enter any letter");
  21.         letter = new Scanner(System.in).next().charAt(0);
  22.         if (chosen.indexOf(letter) >= 0) {
  23.             System.out.println("You have already chosen this letter. Please try again.");
  24.             input();
  25.         } else if (word.indexOf(letter) >= 0) {
  26.             chosen = chosen + letter;
  27.             correctLetter();
  28.         } else {
  29.             chosen = chosen + letter;
  30.             wrongLetter();
  31.         }
  32.     }
  33.  
  34.     static void correctLetter() {
  35.         word3 = "";
  36.         for (int i = 0; i < word.length(); i++) {
  37.             if (word.charAt(i) == letter) {
  38.                 word3 = word3 + letter;
  39.             } else if (word2.charAt(i) == '*') {
  40.                 word3 = word3 + '*';
  41.             } else {
  42.                 word3 = word3 + word2.charAt(i);
  43.             }
  44.         }
  45.         word2 = word3;
  46.         System.out.println(" You have guessed a correct letter. ");
  47.         System.out.println("   The letters are:        " + word2);
  48.         finalcheck();
  49.     }
  50.  
  51.     static void wrongLetter() {
  52.         System.out.println(" You have chosen a wrong letter and so, you lose a life.");
  53.         life--;
  54.         System.out.println(life + " lives remaining.");
  55.         System.out.println("   The letters are:        " + word2);
  56.         finalcheck();
  57.     }
  58.  
  59.     static void finalcheck() {
  60.         if (life == 0) {
  61.             System.out.println(" You have lost all your lives and so, you lose the game. The correct word was '" + word + "' .");
  62.         } else if (word2.indexOf('*') < 0) {
  63.             System.out.println(" Congratulations, You guessed the correct word '" + word + "' and have won the game with " + life + " lives remaining.");
  64.         } else {
  65.             input();
  66.         }
  67.     }
  68.  
  69.     public static void main(String s[]) {
  70.         getword();
  71.         input();
  72.     }
  73. }
Add Comment
Please, Sign In to add comment