Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Johnnie Hernandez
- //This program will play the game hangman!
- import java.util.*;
- import java.io.*;
- public class Hangman {
- private static String path;
- public static Scanner keyboard;
- public static int MAX_LETTERS=20;
- public static int GUESSES=8;
- public static int MAX_WORDS=15000;
- public static void main(String[] args) {
- path = "E:\\Users\\Johnnie\\workspace\\Assignment_12\\dictionary.txt";
- keyboard= new Scanner(System.in);
- boolean isWordFound=true;
- String[] dictionaryData=new String[MAX_WORDS];
- char[] wordForHangman=new char[MAX_LETTERS];
- String randomWord;
- String[] guessedLetters=new String[GUESSES];
- try{dictionaryData=readFile();}
- catch(IOException e){
- System.out.println(e.getMessage());
- System.out.println("the file dictionary can not be opened");
- }
- displayDirections();
- clearRenderScreen();
- randomWord=chooseWord(dictionaryData);
- int lengthOfWord=randomWord.length();
- wordForHangman=new char[lengthOfWord];
- wordForHangman=randomWord.toCharArray();
- char[] lettersToBeTrue= new char[lengthOfWord];
- for(int i=0;i<lengthOfWord;i++){lettersToBeTrue[i]='*';} //This loop will create the array of "*"
- for(int i=0;i<guessedLetters.length;i++){guessedLetters[i]=" ";} //This clears the array so not to print any null values to screen
- displayCharArray(wordForHangman, lengthOfWord);
- isWordFound=guessALetter(wordForHangman, lengthOfWord,lettersToBeTrue,guessedLetters,isWordFound);
- if(isWordFound){System.out.println("***you win***");}
- else{System.out.println("*****you lose*****\n sorry better luck next time!");}
- }
- /**
- * This is a method that will read and store the file under variable path into an String[array]
- * @return
- * @throws IOException
- */
- public static String[] readFile() throws IOException{
- FileReader fr = new FileReader(path);
- BufferedReader textReader= new BufferedReader(fr);
- int numberOfLines=readLines();
- String []dictionaryData= new String[numberOfLines];
- for(int i=0;i<numberOfLines; i++){
- dictionaryData[i]=textReader.readLine();
- }
- textReader.close();
- String[] temp=new String[numberOfLines];
- temp=dictionaryData;
- return temp;
- }
- /**
- * This is a method that will read the number of lines of the variable path
- * @return
- * @throws IOException
- */
- public static int readLines() throws IOException{
- FileReader dictionary = new FileReader(path);
- BufferedReader bf= new BufferedReader(dictionary);
- String aLine;
- int numberOfLines=0;
- while((aLine=bf.readLine()) != null){
- numberOfLines++;
- }
- bf.close();
- return numberOfLines;
- }
- /**
- * This is a test method for displaying the full contents of the array
- * @param dictionaryData
- */
- public static void displayArray(String[] dictionaryData){
- for(int i=0;i<dictionaryData.length;i++){
- System.out.println(dictionaryData[i]);
- }
- }
- /**
- * this will choose one random word from our dictionary and make it avaiable for the game
- * @param dictionaryData
- * @param randomInt
- * @return
- */
- public static String chooseWord(String[] dictionaryData){
- Random randomIntGen= new Random();
- int randomInt=randomIntGen.nextInt(dictionaryData.length-1);
- String temp=dictionaryData[randomInt];
- return temp;
- }
- /**
- * this method simply displays the directions and is put in a method to erase clutter in main
- */
- public static void displayDirections(){
- System.out.println("\t\t\t H A N G M A N\n\n"
- +"This is a word guessing game. A word will be selected at random\n" +
- "and kept hidden. You will try to figure out the secret word by\n" +
- "guessing letters which you think are in the word. You will guess\n" +
- "one leter at a time. If the letter you guess is correct, the\n" +
- "position(s) of the letter in the secret word will be shown.\n" +
- "You will be allowed 8 wrong guesses. If you guess incorrectly 8\n" +
- "times, you lose the game. If you guess all of the letters in the\n" +
- "word, you win.\n\n" + "Press enter to continue");
- keyboard.nextLine();
- }
- /**
- * This method will take the users guess and then check to see if it is a hit or miss,
- * misses will be added to the guessedLetters array
- * hits will replace '*' in the lettersTrue
- * @param wordForHangman
- * @param lengthOfWord
- * @param lettersToBeTrue
- * @param guessedLetters
- */
- public static boolean guessALetter(char[] wordForHangman,int lengthOfWord,char[] lettersToBeTrue,String[] guessedLetters,boolean isWordFound){
- String temp;
- boolean winOrlose=isWordFound;
- int guessCounter=0;
- while(isWordFoundYet(lettersToBeTrue,lengthOfWord)){
- boolean isGuessTrue=false;
- char guess;
- System.out.println("The word so far:");
- displayCharArray(lettersToBeTrue, lengthOfWord);
- displayStringArray(guessedLetters);
- System.out.println("\nMake a guess");
- temp=keyboard.next();
- guess=temp.charAt(0);
- for(int i=0;i<lengthOfWord;i++){
- if(guess==wordForHangman[i]){
- lettersToBeTrue[i]=guess;
- isGuessTrue=true;
- }
- }
- if(!isGuessTrue){
- guessedLetters[guessCounter]=Character.toString(guess);
- guessCounter++;
- if(guessCounter>=8){
- winOrlose=false;
- return winOrlose;
- }
- }
- clearRenderScreen();
- }
- return winOrlose;
- }
- /**
- * this will test to see if the word has been completely found yet
- * @param guessedLetters
- * @param lengthOfWord
- * @return
- */
- public static boolean isWordFoundYet(char[] guessedLetters, int lengthOfWord){
- boolean temp=false;
- for(int i=0;i<lengthOfWord;i++){if(guessedLetters[i]=='*'){temp=true;}}
- return temp;
- }
- /**
- * this array simply displays the letters found and '*" for all non found values
- * @param lettersToBeTrue
- * @param lengthOfWord
- */
- public static void displayCharArray(char[]lettersToBeTrue, int lengthOfWord){
- for(int i=0;i<lengthOfWord;i++){
- System.out.print(lettersToBeTrue[i]+" ");
- }
- System.out.print("\n");
- }
- /**
- * this array simply displays the letters guessed
- * @param guessedLetters
- */
- public static void displayStringArray(String[] guessedLetters){
- System.out.println("These are the letters you have guessed:");
- for(int i=0;i<guessedLetters.length;i++){
- System.out.print(guessedLetters[i]+" ");
- }
- }
- /**
- * This will print 80 enter keys in order to give a new blank screen
- */
- public static void clearRenderScreen(){
- for(int i=0;i<80;i++)
- System.out.println("\n");
- }
- }
Add Comment
Please, Sign In to add comment