Skip to content

Instantly share code, notes, and snippets.

@Cheesebaron
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cheesebaron/25230082df7a9ccd869e to your computer and use it in GitHub Desktop.
Save Cheesebaron/25230082df7a9ccd869e to your computer and use it in GitHub Desktop.
Shitty hangman
import java.util.*;
public class HangMan {
private static String[] words = {
"motherboard", "case", "powersupply", "harddrive", "mouse", "keyboard", "peripheral",
"graphicscard", "processor", "latency", "ping", "gaming", "resolution", "watercooling",
"cache", "memory", "chipset", "intel", "notebook", "desktop", "aliasing" };
public static void main(String[] args) {
Random rand = new Random();
String userChoice =
readLine("Welcome to a game of hangman!\nDo you want to play? (Y/N): ");
int gameCount = 0;
int lostGames = 0;
while (!userChoice.equalsIgnoreCase("n")) {
switch(userChoice.toLowerCase()) {
case "y":
String word = words[rand.nextInt(words.length)];
if (!playGame(word, 6))
lostGames++;
gameCount++;
break;
default:
System.out.println("Sorry, didn't understand that.");
break;
}
userChoice =
readLine("You have played " + gameCount + " games. Do you want to play again?");
}
System.out.println("Thanks for playing");
}
private static Boolean playGame(String word, int guessesLeft) {
String hiddenWord = initHiddenWord(word);
while(guessesLeft > 0)
{
String inChar = readLine("Your guess: ");
while(true) {
if (inChar.length() > 1)
inChar = readLine("You can only guess one letter at a time");
if (inChar.length() == 1) break;
}
char ch = Character.toLowerCase(inChar.charAt(0));
if (!letterOk(word, ch))
guessesLeft--;
else
hiddenWord = replaceLetters(word, hiddenWord, ch);
if (hiddenWord.equals(word)) {
System.out.println("You guessed the word: " + word);
System.out.println("You win");
return true;
}
System.out.println("The word now looks like this: " + hiddenWord);
System.out.println("You have " + guessesLeft + " guesses left.");
}
return false;
}
private static String initHiddenWord(String word) {
String result = "";
for(int i = 0; i < word.length(); i++)
result = result + "-";
return result;
}
private static Boolean letterOk(String word, char guess) {
if (word.indexOf(guess) == -1) {
System.out.println("There are no " + guess + "'s in the word.");
return false;
} else {
System.out.println("Found " + guess + "'s in the word.");
return true;
}
}
private static String replaceLetters(String word, String hiddenWord, char guess) {
for(int i = 0; i < word.length(); i++) {
if (guess == word.charAt(i)) {
if (i > 0) hiddenWord = hiddenWord.substring(0, i) + guess + hiddenWord.substring(i + 1);
if (i == 0) hiddenWord = guess + hiddenWord.substring(1);
}
}
return hiddenWord;
}
static Scanner scan = new Scanner(System.in);
private static String readLine(String consoleOutput) {
System.out.println(consoleOutput);
return scan.next();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment