//GoldenFyre //Due Date //Programming Assignment 5 public class WordGuessGame { //Declare and initialize class constants static final String SECRET = "encyclopedia"; //Holds the SECRET Word "ENCYCOLPEDIA" static final char FLAG = '!'; //Holds the flag '!' //Declare and initialize class variables static String wordSoFar = " "; //Holds the wordSoFar static char letter = ' '; //Holds the current letter guessed static String wordGuess = " "; //Holds the word guess if the players enter the flag static int guess = 1; //Holds the guess number static int score = 100; //Holds the score static String guesses = " "; //Holds the players guess if ! is inputted static boolean winLoss = false; static char [ ] usedLetter = new char[0]; static int letterNumber = 0; public static void main(String [] args) //Holds the wordSoFar { //Declare and initialize variable int count = 0; //Stores a letter guess counter that has no matches boolean match = true; //Holds the true or false of match String tempMatch = " "; //Holds wordSoFar before it gets changed String tempString = " "; //Holds the letters when they're converted to string //Print the opening to the game startGame(); //Print the word in dashes printDashes(); //Declare and Intialize char array char [ ] array = wordSoFar.toCharArray(); //The player makes letter guesses do { //Get a valid letter guess letter = getLetterGuess(); //Hold the old wordSoFar tempMatch = wordSoFar; //Loop through the letters of the secret word for (int x = 0; x < SECRET.length(); x++) { //Check if the user's letter mathes any letters in the secret word if (letter == SECRET.charAt(x)) array[x] = letter; } //Emptying wordSoFar wordSoFar = ""; //Update wordSoFar with any letter matches stored in the array for (int x = 0; x < array.length; x++) wordSoFar += String.valueOf(array[x]); System.out.println(wordSoFar); //If letter has no matches if (tempMatch.equals(wordSoFar)) { score -= 5; } guess++; } while (!wordSoFar.equals(SECRET) && score >= 10); //If user enters the "flag" to end guessing letters and chooses to guess the word //Check if the user's guess matches the secret word and print a win/lose message if (score == 0) { System.out.println("YOU LOSE! YOUR FINAL SCORE WAS 0!!!"); System.out.println("The secret word is ENCYCLOPEDIA"); System.out.println("You made " + guess + "guesses"); } if (wordSoFar.equals(SECRET)) { System.out.println("YOU WIN! YOUR FINAL SCORE WAS " + score + " out of 100!!!!"); System.out.println("The secret word is ENCYCLOPEDIA"); System.out.println("You made " + guess + " guesses"); } }//End of main()method //Method to print title, description, and instructions public static void startGame() { //Outputting the games instructions System.out.println("---------------------------------------------------------------------"); System.out.println(""); System.out.println(" WELCOME TO THE WORD GUESS GAME!!!!"); System.out.println("THE GOAL OF THE GAME IS TO GUESS THE SECRET WORD HANGMAN STYLE!"); System.out.println(" YOU LOSE IF YOUR SCORE HITS ZERO - HAVE FUN!!!"); System.out.println(""); System.out.println("---------------------------------------------------------------------"); }//End of startGame() method //Method to print the secret word in correct number of dashes public static void printDashes() { //Printing the dashes based on the amount of letters in the SECRET word for (int x = 1; x < SECRET.length(); x++) wordSoFar += "-"; System.out.println(wordSoFar); }//End of printDashes()method //Method to return a valid letter guess from the user public static char getLetterGuess() { //Declare the variable that will hold the users guess char letter = ' '; char temp = ' '; //Outputting the score System.out.println("Score: " + score); //Prompt the user for the letter System.out.print("Guess " + guess + " (a-z):\t\t\t"); temp = In.getChar(); //After getting temp, convert it to lower case and getting it put into letter letter = Character.toLowerCase(temp); //EndofMainMethod if (letter == '!') { System.out.println(""); System.out.println("What is your guess?:\t\t"); wordGuess = In.getString(); wordGuess = wordGuess.toLowerCase(); if (wordGuess.equals(SECRET)) { System.out.println(""); System.out.println("YOU WIN! YOUR FINAL SCORE WAS " + score + " out of 100!!!!"); System.out.println("The secret word is ENCYCLOPEDIA"); System.out.println("You made " + guess + " guesses"); System.exit(0); } else { System.out.println(""); System.out.println("YOU LOSE! YOUR FINAL SCORE WAS " + score + " out of 100!!!!"); System.out.println("The secret word is ENCYCLOPEDIA"); System.out.println("You made " + guess + " guesses"); System.exit(0); } } //Checking if the char is actually a letter while (letter < 97 || letter > 122) { System.out.println("INVALID\n"); System.out.print("Guess " + guess + " (a-z):\t\t\t"); letter = In.getChar(); letter = Character.toLowerCase(letter); //usedLetter[letterNumber] = letter; }//End of While for (int x = 0; x < usedLetter.length; x++) { if (usedLetter[x] == letter); { System.out.println("INVALID - LETTER HAS BEEN USED\n"); System.out.print("Guess " + guess + " (a-z):\t\t\t"); letter = In.getChar(); letter = Character.toLowerCase(letter); usedLetter[x] = letter; } } //Outputing a space to make the output look good System.out.println(""); //Making justInCase equal the letter //Returning the letter return letter; }//End of getLetterGuess()method }//End of Class
-------------------------------------OUTPUT------------------------------
--------------------Configuration: <Default>--------------------
---------------------------------------------------------------------
WELCOME TO THE WORD GUESS GAME!!!!
THE GOAL OF THE GAME IS TO GUESS THE SECRET WORD HANGMAN STYLE!
YOU LOSE IF YOUR SCORE HITS ZERO - HAVE FUN!!!
---------------------------------------------------------------------
-----------
Score: 100
Guess 1 (a-z): e
INVALID - LETTER HAS BEEN USED
Guess 1 (a-z): E
e-------e---
Score: 100
Guess 2 (a-z): b
INVALID - LETTER HAS BEEN USED
Guess 2 (a-z): b
e-------e---
Score: 95
Guess 3 (a-z): c
INVALID - LETTER HAS BEEN USED
Guess 3 (a-z): c
e-c-c---e---
Score: 95
Guess 4 (a-z): y
INVALID - LETTER HAS BEEN USED
Guess 4 (a-z): y
e-cyc---e---
Score: 95
Guess 5 (a-z):
----------------------------------------------------------------------------------
So I know it's something to do with the getLetterGuess method and the usedLetter[] array in the for loop. I have no idea how to get it working. Is it where I'm placing the
usedLetter[x] = letter;?
The project is due tomorrow and I'd really like some help on this final part...
(P.S.) I'm going to polish up the code before handing it in.. fixing the whitespace, taking out the unnecessary variables, etc...
Thank you!