/** Name: Hangman Description: Console based game of hangman Arguments: Fully distinguished path to a dictionary file Programmer: Spencer R. Shimko Date: 03-7-2003 **/ import java.util.*; import java.io.*; /** Class: hangman Main logical driver for game **/ public class hangman { // Begin: main public static void main(String arg[]){ // if not at least 1 argument if (arg.length!=1) { System.out.println("Usage: java hangman /path/to/dictionary.txt"); System.exit(1); } // endif not 1 args // else: we have 1 arg so try to set as dictfile it... else { dictionary dic = new dictionary(); //create dictionary object // setup dictionary and word list if (! dic.setDict(arg[0])){ System.exit(1);} boolean cont = true; while (cont){ String word=dic.getWord(); // go ahead and get word cont = runGame(word); // run the game } // end while loop repeating game }// end-else we have 1 command line arg so the game can run }//end main /** runGame: The game engine Receives: Secret word string Returns: Boolean status if users wish to play again **/ private static boolean runGame(String word){ int numCorrect=0; // tracks correct letters int numWrong=0; // tracks wrong guesses boolean notFound; // tracks if letter is found in word // setup and initalize array that will contain correct guesses char[] wordHidden=new char[word.length()]; for (int z = 0; z < wordHidden.length; z++){ wordHidden[z]='_';} LinkedList alphaList=new LinkedList(); // unguessed characters // setup alphabet list of available chars for(int x=1; x<=26; x++){ alphaList.add(new Character((char) (96+x))); } // create new display object that will manage the screen display disp = new display(); // this while loop will run until the game is won or lost boolean done=false; while ( !done ){ disp.hangman(numWrong); // display the hangman disp.secWord(wordHidden);// show the secret word w/ guesses char let = getChoice(alphaList, disp); // retreive and verify letter choice alphaList.remove(new Character(let)); //remove letter from LL notFound=true; // search for entered char and adjust game for (int y=0; y < word.length(); y++){ // if the guessed letter is in the secret word // at this position change the hidden word if (word.charAt(y) == let){ wordHidden[y]=let; numCorrect++; //add one to the total correct notFound = false; // the guessed letter is found } } // end for searching for guessed letter // if the guess was not found increment the wrong guess counter if ( notFound ) { numWrong++; } // test for the winning/losing conditions before // starting the loop again to prompt for a letter if ( numWrong == 6){ disp.lose(word.toCharArray()); //display the final hangman done = true; // set the exit condition } else if ( numCorrect == word.length()){ // elseif the user won disp.win(wordHidden); // display winning info done = true; // set the exit condition } } // end while loop b/c the game has been won or lost return playAgain(disp); // check and see if user wants to play again } //end runGame method /** getChoice: retreives letter selection and verifies validity Receives: LL of available characters, display object Returns: **/ private static char getChoice(LinkedList al, display disp){ boolean valid = false; // keeps status of letter validity Character let = new Character('9'); // chosen letter while (! valid ){ //loop till we get a valid letter disp.alpha(al);// show available letters try{ let = new Character((char) System.in.read()); //retrieve entered letter } catch(IOException e){ System.out.println("Input error has occured...exiting"); System.exit(1); } if (al.contains(let)) {valid=true;} // test entered letter } return let.charValue(); } // end getChoice method /** playAgain: ask player if they want to play again Receives: display object Returns: True is player wants to play again and flase otherwise **/ private static boolean playAgain(display disp){ disp.playAgain();// display play again prompt char let='N'; try{ let = (char) System.in.read(); //retrieve entered letter let = (char) System.in.read(); //retrieve entered letter } catch(IOException e){ System.out.println("Input error has occured...exiting"); System.exit(1); } if ((let == 'y') || (let == 'Y')){ return true; // return true if answer was yes } else { return false;} // return flase otherwise } }// end class hangman