hangman java game

Solutions on MaxInterview for hangman java game by the best coders in the world

showing results for - "hangman java game"
Hind
28 Mar 2016
1public class Hangman
2{
3   public static void main(String[] args)
4   {
5      Scanner keyboard = new Scanner(System.in);
6 
7      int  noOfGuesses =0,lettersCorrect=0;
8      String choiceToPlay, userGuess;
9      char menuChoice, letter;
10      char [] guessWord = {'h', 'e', 'l', 'l', 'o'};
11      char [] correctLetter = {'-', '-', '-', '-', '-'};
12 
13      System.out.println("Hello and welcome to Hangman!");
14 
15      //main game menu
16      do
17      {  System.out.println("Do you want to play? Press Y/N\n");
18         choiceToPlay = keyboard.next();
19         menuChoice = choiceToPlay.charAt(0);
20 
21         if(!(menuChoice == 'y'||menuChoice == 'Y'||menuChoice == 'n'||menuChoice =='N'))
22         {
23           System.out.println("You have entered an invalid option. Try again!\n");
24         }//if invalid
25 
26      }//do
27      while (!(menuChoice =='Y' ||menuChoice=='y'||menuChoice == 'n'|| menuChoice =='N'));
28 
29      if(menuChoice == 'N'||menuChoice =='n')
30      {
31         System.out.println("You have chosen to leave the game.");
32         System.out.println("Goodbye!");
33         System.exit(0);
34      }//if
35 
36      else
37      {
38 
39         while(lettersCorrect <5)
40         {
41            //Getting users guess.
42            System.out.println("\nThe guess word has 5 letters.");
43            System.out.println("Enter a letter to guess: ");
44 
45            userGuess = keyboard.next();
46            letter = userGuess.charAt(0);
47 
48            //Incrementing letters each time.
49            noOfGuesses++;
50 
51            if(letter == guessWord[0])
52            {
53               System.out.println("There is 1 H in the word");
54               System.out.println("You have guessed the first letter correctly.");
55               correctLetter[0] = letter;
56               System.out.println(correctLetter);
57               System.out.println("You have had " + noOfGuesses + " guesses, so far");
58 
59               lettersCorrect++;
60 
61               System.out.println("Letters correct so far: "+lettersCorrect);
62            }//if first letter
63 
64            else if(letter==guessWord[1])
65            {
66               System.out.println("There is 1 E in the word");
67               System.out.println("You have guessed the second letter correctly.");
68 
69               correctLetter[1] = letter;
70               System.out.println(correctLetter);
71               System.out.println("You have had " + noOfGuesses + " guesses, so far");
72 
73               lettersCorrect++;
74 
75               System.out.println("Letters correct so far: "+lettersCorrect);
76            }//if second letter
77 
78            else if(letter ==guessWord[2]||letter==guessWord[3])
79            {
80               System.out.println("There are 2 L's in the word");
81               System.out.println("You have guessed the third and fourth letters correctly.");
82 
83               correctLetter[2] = letter;
84               correctLetter[3]=letter;
85               System.out.println(correctLetter);
86               System.out.println("You have had " + noOfGuesses + " guesses, so far");
87 
88               lettersCorrect+=2;
89 
90               System.out.println("Letters correct so far: "+lettersCorrect);
91 
92            }//if third and fourth letters
93 
94            else if(letter ==guessWord[4])
95            {
96               lettersCorrect++;
97               System.out.println("There is 1 O in the word");
98               System.out.println("You have guessed the fifth letter correctly.");
99               correctLetter[4] = letter;
100               System.out.println(correctLetter);
101               System.out.println("You have had " + noOfGuesses + " guesses, so far");
102               System.out.println("Letters correct so far: "+lettersCorrect);
103            }//if fifth letter
104 
105            else
106            {
107               System.out.println("The letter you guessed is not in the word.\n");
108               System.out.println("Guesses taken so far: "+noOfGuesses);
109               System.out.println("Letters correct so far: "+lettersCorrect);
110            }//else incorrect letter
111 
112         }//while loop
113 
114         System.out.println("\nYou found the word!");
115         System.out.println("It was hello.");
116         System.out.println("Total guesses: "+noOfGuesses);
117         System.exit(0);
118 
119      }//else
120   }//main
121}//class
122