# Stanford CS106A Hangman Console – Based Game Solution

I am really proud to say that I was able to figure out part 1 of Hangman game as documented in Assignment 4 all on my own without any outside help!!! AND the more I program, the more I love it. I couldn’t think of a more perfect way to spend this Friday 11/11/11 š

So here is what I have as my solution:

I must say, I’m really liking these strings. You can also find my solution on gist:

```&lt;pre&gt;
/*
* File: Hangman.java
* ------------------
* This program will eventually play the Hangman game from
* Assignment #4.
*/

import acm.graphics.*;
import acm.program.*;
import acm.util.*;

import java.awt.*;

public class Hangman extends ConsoleProgram {

private HangmanLexicon hangmanWords = new HangmanLexicon();

private RandomGenerator rgen = RandomGenerator.getInstance();

/** Tracks the number of guesses the player has */
private int guessCounter = 8;

public void run() {
setUpGame();
playGame();
}

/*Set up the game by displaying the welcome message,
*how many letters there are in the word,
*and how many guesses the user has
*/
private void setUpGame() {
println(&quot;Welcome to Hangman!&quot;);
println(&quot;The word now looks like this: &quot; + hiddenWord);
println(&quot;You have &quot; + guessCounter + &quot; guesses left.&quot;);
}

//Generates a random word selected from the HangmanLexicon
private String pickWord() {
int randomWord = rgen.nextInt(0, (hangmanWords.getWordCount() - 1));
String pickedWord = hangmanWords.getWord(randomWord);
return pickedWord;
}

//Shows how many letters there are in the word as part of game setup
private String showNumberOfLetters() {
String result = &quot;&quot;;
for(int i = 0; i&amp;lt; word.length(); i++) {
result = result + &quot;-&quot;;
}
return result;
}

private void playGame() {
while(guessCounter &amp;gt; 0) {
while (true) {
if(getChar.length() &amp;gt; 1) {
getChar = readLine(&quot;You can only guess one letter at a time. Try again: &quot;);
}
if(getChar.length() == 1) break;
}
ch = getChar.charAt(0);
if(Character.isLowerCase(ch)) {
ch = Character.toUpperCase(ch);
}
letterCheck();
if (hiddenWord.equals(word)) {
println(&quot;You guessed the word: &quot; + word);
println(&quot;You win&quot;);
break;
}
println(&quot;The word now looks like this: &quot; + hiddenWord);
println(&quot;You have &quot; + guessCounter + &quot; guesses left.&quot;);

}
if (guessCounter == 0) {
println(&quot;You're completely hung.&quot;);
println(&quot;The word was:&quot; + word);
println(&quot;You lose.&quot;);
}
}

//updates the hiddenWord if the character entered is correct
private void letterCheck() {
//checks to see if the guessed letter is in the word
if(word.indexOf(ch) == -1) {
println(&quot;There are no &quot; + ch + &quot;'s in the word&quot;);
guessCounter--;
}
if(word.indexOf(ch) != -1) {
println(&quot;The guess is correct.&quot;);
}
//goes through each of the letters in the word and checks if it matches the guessed letter,
//if it's a match, updates the hidden word to reveal the position of the guessed letter
for(int i = 0; i &amp;lt; word.length(); i++) {
if (ch == word.charAt(i)) {
if (i &amp;gt; 0) {
hiddenWord = hiddenWord.substring(0, i) + ch + hiddenWord.substring(i + 1);
}
if(i == 0) {
hiddenWord = ch + hiddenWord.substring(1);
}
}
}
}

//This is the secret word
private String word = pickWord();

//This is the word being guessed
private String hiddenWord = showNumberOfLetters();

//This is the latest character entered by the user
private char ch;
}
&lt;/pre&gt;
```

Enjoy the article? Join over 20,000+ Swift developers and enthusiasts who get my weekly updates.