import java.lang.*; import java.io.*; import java.util.*; import javax.swing.*; class HangManType { // Variables public String word; // The word public int numRight; // Number of correct guesses public int numWrong; // Number of incorrect guesses public String guessed; // The letters already guessed // Methods public HangManType() { this.word = ""; this.numRight = 0; this.numWrong = 0; this.guessed = ""; } void reset() { // Reset all the values in the object this.word = ""; this.guessed = ""; this.numRight = 0; this.numWrong = 0; } } public class hangman { public static void getMove( HangManType game ) { String s = ""; // User's choice InputStreamReader inStream = null; BufferedReader in = null; // Primpt for a letter try { inStream = new InputStreamReader( System.in ); in = new BufferedReader( inStream ); System.out.print( "Your letter: " ); s = in.readLine( ); s = s.substring( 0, 1 ); // Read only one character } // try catch ( IOException e ) { System.out.println( "Severe problem with System.in!" + e ); System.exit( 1 ); } // catch while ( game.guessed.contains( s ) ) { // Keep asking until a valid choice is entered System.out.println( "You have already guessed " + s + ", please try again!" ); try { inStream = new InputStreamReader( System.in ); in = new BufferedReader( inStream ); System.out.print( "Your letter: " ); s = in.readLine( ); s = s.substring( 0, 1 ); // Read only one character } // try catch ( IOException e ) { System.out.println( "Severe problem with System.in!" + e ); System.exit( 1 ); } // catch } game.guessed += s; // Add the choice to the list of guessed letters // Increase the proper guess counter if ( game.word.contains( s ) ) game.numRight++; else game.numWrong++; } public static void drawGallows( int numWrong ) { System.out.println( " ___" ); System.out.print( "| | \n|" ); if (numWrong >= 1) System.out.print( " o " ); System.out.print( "\n|" ); if (numWrong >= 2) System.out.print( " /" ); if (numWrong >= 3) System.out.print( "|" ); if (numWrong >= 4) System.out.print( "\\" ); System.out.print( "\n|" ); if (numWrong >= 5) System.out.print( " /" ); if (numWrong >= 6) System.out.print( " \\" ); System.out.println( "\n|" ); System.out.println( "|_________\n" ); } public static String pickWord( String[] words ) { String[] buff = new String[ words.length -1 ]; String result = ""; Random r1 = new Random( ); int rValue = r1.nextInt( words.length ); int index = 0; for ( index = 0; index < rValue; index++ ); result = words[index]; for ( int i = 0; i < words.length; i++ ) { if ( i < index ) buff[ i ] = words[ i ]; else if ( i > index ) buff[ i -1 ] = words[ i ]; } words = buff; buff = null; return result; } public static void cls() { String ESC = "\033["; System.out.print(ESC + "2J"); System.out.flush(); } public static void main( String args[] ) { HangManType game = new HangManType(); // The game object String[] words = null; // Words list RandomAccessFile in = null; // File reader String s; // Input buffer String playAgain = "y"; // Loop trigger int numWords = 0; // Number of words (lines) in the file int lines = 0; InputStreamReader inStream = null; BufferedReader inReader = null; // Get the number of lines in the file try { in = new RandomAccessFile( "hangman.java", "r" ); while( in.readLine() != null ) lines++; in.close(); in = new RandomAccessFile( "words.txt", "r" ); // Get the length of the file s = in.readLine(); while ( s != null ) { numWords++; s = in.readLine(); } words = new String[ numWords ]; // Create the word buffer in.seek( 0 ); // Go back to the start of the file // Load the array s = in.readLine(); for (int i = 0; i < numWords; i++) { words[ i ] = s; s = in.readLine(); } // Close the file in.close(); } catch( IOException e ) { System.out.println( "Error reading file!" ); }; while ( playAgain.equalsIgnoreCase( "y" ) ) { game.reset(); game.word = pickWord( words ); // Select a word from the array while ( (game.numWrong < 6) && ( game.numRight < game.word.length()) ) { // Play until the user guesses the word or makes 6 mistakes //System( "clear" ); cls(); System.out.println( "Java Hangman (Array Based)\nLines of code: " + lines + "\nRemaining words: " + words.length + "\n\n" ); drawGallows( game.numWrong ); for ( int i = 0; i < game.word.length(); i++ ) { if ( game.guessed.contains( "" + game.word.charAt( i ) ) ) System.out.print( game.word.charAt( i ) + " " ); else System.out.print( "_ " ); } System.out.println( "\nYou have guessed: " + game.guessed ); getMove( game ); } //system( "clear" ); cls(); System.out.println( "Java Hangman (Array Based)\nLines of code: " + lines + "\nRemaining words: " + words.length + "\n\n" ); drawGallows( game.numWrong ); if ( game.numWrong >= 6 ) { System.out.println( "The word was: " + game.word + "\nY o u L o s e !\nYou made " + (game.numWrong + game.numRight) + " guesses." ); } else { System.out.println( "The word was: " + game.word + "\nY o u W i n !\nYou made " + (game.numRight + game.numWrong) + " guesses." ); } try { inStream = new InputStreamReader( System.in ); inReader = new BufferedReader( inStream ); System.out.print( "Play Again (Y/N)? " ); playAgain = inReader.readLine( ); playAgain = playAgain.substring( 0, 1 ); // Read only one character } // try catch ( IOException e ) { System.out.println( "Severe problem with System.in!" + e ); System.exit( 1 ); } // catch } } }