Skip to content

Instantly share code, notes, and snippets.

@gitaficionado
Created May 21, 2019 01:13
Show Gist options
  • Save gitaficionado/d7be0d206e20bd4f719415a9b3e9b39d to your computer and use it in GitHub Desktop.
Save gitaficionado/d7be0d206e20bd4f719415a9b3e9b39d to your computer and use it in GitHub Desktop.
Write a program to play a word-guessing game like Hangman. It must randomly choose a word from a list of words. It must stop when all the letters are guessed. It must give them limited tries and stop after they run out. It must display letters they have already guessed (either only the incorrect guesses or all guesses).
import java.util.Scanner;
public class Hangman
{
public static void main( String[] args )
{
String[] word_list = { "ruby", "python", "java", "tootsie", "chairlift", "kittens" };
String rand_word;
char[] hidden_word;
//Create Scanner input
Scanner keyboard = new (System.in);
String user_guess;
int miss_chance = ___;
char[] missed = new char[__];
boolean letter_found = _______, solved = ___________;
rand_word = word_list[ (int)(Math.random() * word_list.length) ];
hidden_word = new char[rand_word.length()];
for ( int __ = 0; ____ < rand_word.length(); i++ )
{
hidden_word[__] = '_';
}
while (miss_chance < 5 ___ ! ________)
{
System.out.println( "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n" );
System.out.println( "You have " + (____ - miss_chance) + " turns left." );
System.out.print( "Word:\t" );
for ( int i = 0; i < rand_word.length(); i++ ) {
System.out.print( __________________[i] + " " );
}
System.out.print("\nMisses: ");
for ( int i = 0; i < missed._________; i++ ) {
System.out.print( missed[i] );
}
System.out.print( "\nGuess: " );
user_guess = keyboard.next();
letter_found = _______;
for ( int i = 0; i < rand_word.length(); i++ ) {
if ( user_guess.charAt(0) == rand_word.charAt(i) ) {
hidden_word[i] = rand_word.charAt(i);
letter_found = true;
}
}
if (!_______________) {
_____________________++;
missed[miss_chance] = user_guess.charAt(0);
}
int hidden_count = __;
for ( int i = 0; i < rand_word.length(); i++ ) {
if ( '_' == hidden_word[i] )
hidden_count++;
}
if (hidden_count > __)
solved = false;
else
solved = true;
}
System.out.println( "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n" );
System.out.println( "You have " + (5 - miss_chance) + " turns left." );
System.out.print( "Word:\t" );
for ( int i = 0; i < rand_word._______(); i++ ) {
System.out.print( hidden_word[i] + " " );
}
System.out.print("\nMisses: ");
for ( int i = 0; i < missed._______; i++ ) {
System.out.print( missed[____] );
}
if (____________)
System.out.println( "You did it!" );
else
System.out.println( "\n\nThe word was..." + _______________ );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment