1 Replies - 337 Views - Last Post: 18 December 2013 - 08:13 PM Rate Topic: -----

#1 GoldenFyre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 07-September 13

Having trouble disallowing the same input twice :/

Posted 18 December 2013 - 07:25 PM

So here I have a functioning word guess game. I've got the word, I've got the guessing, etc. The only thing I haven't been able to figure out is how to disallow the same input twice. EX: The user inputs E and when they try to input E again they get an error message and they're prompted to input a new letter

//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!

Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble disallowing the same input twice :/

#2 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: Having trouble disallowing the same input twice :/

Posted 18 December 2013 - 08:13 PM

This part of the code seems to be your problem.
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;
			}
		}


It would be easier and done correctly if you just loop through the array containing the letters already used. If a match is found you can set a bool flag and exit the loop. To exit just use the break keyword.

bool alreadyUsed = false;
for (int i = 0; i < usedLetter.length; ++i)
{
    if (if (usedLetter[x] == letter)
    {
          alreadyUsed = true;
          break;

    }
}


This post has been edited by salazar: 18 December 2013 - 08:14 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1