console hangman problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dav3
    New Member
    • Nov 2006
    • 94

    #1

    console hangman problem

    I am trying to code a little console program (no swing, no awt, just console). But I am having some trouble and I am not sure if its my logic, or my code, or possibly both?!

    Anyways heres what I currently have, I have not taken into consideration a user winning by guessing the word yet. Just working on displaying the hidden word and updating it with respect to the users guesses.

    Code:
    public class whatever {
    
    	public static void main(String[] args) {
    		
    		List<String> alpha = new ArrayList<String>(); //list of letters user can choose from
    		int maxTries = 7;
    //		final int maxWordLen = 25;
    //		int wrongGuesses = 0;
    //		int correctGuess;
    //		String guess;
    		String hiddenWord ="";
    		String correctChars= ""; // previously correctly guessed chars
    		String guessX = ""; // the new guess
    		
    
    		String wordList [] = {"car", "desk"};
    		//select a word from array
    		int word = 0 + (int) (Math.random() * 2);
    
    		String secretWord = wordList[word];
    		
    		for(int x = 0; x<secretWord.length(); x++)
    		{
    			hiddenWord += "-";
    		}
    		System.out.println("This is the hidden word: "+hiddenWord);
    		
    		
    		//System.out.println("The secret word is: "+secretWord);
    
    		
    	//Add alphabet to list	
    		alpha.add("a");
    		alpha.add("b");
    		alpha.add("c");
    		alpha.add("d");
    		alpha.add("e");
    		alpha.add("f");
    		alpha.add("g");
    		alpha.add("h");
    		alpha.add("i");
    		alpha.add("j");
    		alpha.add("k");
    		alpha.add("l");
    		alpha.add("m");
    		alpha.add("n");
    		alpha.add("o");
    		alpha.add("p");
    		alpha.add("q");
    		alpha.add("r");
    		alpha.add("s");
    		alpha.add("t");
    		alpha.add("u");
    		alpha.add("v");
    		alpha.add("w");
    		alpha.add("x");
    		alpha.add("y");
    		alpha.add("z");
    		
    		
    //below here is good,kinda		
    		while(maxTries > 0 )
    		{
    		
    			System.out.println(hiddenWord);
    			
    			System.out.println("Here are your letters to chose from\n"+alpha);
    			
    			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    			System.out.println("Please make a guess: ");
    			try
    			{
    				guessX = br.readLine();
    			} 
    			catch (IOException ioe) 
    			{
    				System.out.println("IO error!");
    	        	System.exit(1);
    			}
    			
    //			if in word use String newSecret and display correct letters
    			if(guessX.indexOf(secretWord)!= 0 )
    			{
    				alpha.remove(guessX);
    				String newSecret= secretWord.replaceAll("[^"+correctChars+guessX+"]", "-");
    				hiddenWord = newSecret;
    				System.out.println(hiddenWord);
    			}
    			else
    			{
    				System.out.println("Sorry your guess is not in the word");
    				maxTries --;
    			}
    			System.out.println("You have "+maxTries+" chances remaining");
    		}//while
    	}
    }
    I have viewed all of the threads revolving around this problem at this site and haven't been able to figure this out yet.

    The output now will show: -e-- if the user entered e. But when it goes to the next line it is ----. Any thoughts?
  • dav3
    New Member
    • Nov 2006
    • 94

    #2
    whoops

    Code:
    if(guessX.indexOf(secretWord)!= 0 )
    fixed that its now

    Code:
    if(secretWord.indexOf(guessX != -1)

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by dav3
      whoops

      Code:
      if(guessX.indexOf(secretWord)!= 0 )
      fixed that its now

      Code:
      if(secretWord.indexOf(guessX != -1)
      Any reason why your alphabet is made up strings and not chars? Then you can add the alphabet to the list using a loop.

      Comment

      • dav3
        New Member
        • Nov 2006
        • 94

        #4
        i knew how to add them easily (not eloquently) as strings. Is it incorrect to do it this way? Incorrect in such away that my program will fail, at this point design principles are not a concern. I can always refine once i have a working program.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by dav3
          i knew how to add them easily (not eloquently) as strings. Is it incorrect to do it this way? Incorrect in such away that my program will fail, at this point design principles are not a concern. I can always refine once i have a working program.
          In that case I'll reserve the rest of my comments.

          Comment

          • dav3
            New Member
            • Nov 2006
            • 94

            #6
            Originally posted by r035198x
            In that case I'll reserve the rest of my comments.

            Sorry if I offended you. I have made a very strong effort on this program, but am in need of help. I don't know why my newSecret word is only showing one correct letter at a time.


            Ah wasnt adding to my correctChars variable. Problem solved.


            Please do post your comments I am interested in becoming a better programmer, but when i spend hours coding something and have someone ask me about something that isnt the problem... i get defensive, sorry.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by dav3
              Sorry if I offended you. I have made a very strong effort on this program, but am in need of help. I don't know why my newSecret word is only showing one correct letter at a time.


              Ah wasnt adding to my correctChars variable. Problem solved.


              Please do post your comments I am interested in becoming a better programmer, but when i spend hours coding something and have someone ask me about something that isnt the problem... i get defensive, sorry.
              Shouldn't your while condition also test whether the word has now been guessed correctly or not?

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Again: regular expressions are your friend here; suppose we have this:

                [code=java]
                String secretWord= "apple";
                String correctChars= "pe";
                String displayWord= secretWord.repl aceAll("[^"+correctChars +"]", "-");
                [/code]

                a newChar is correct iff:

                [code=java]
                bool correct= secrectWord.ind exOf(newChar) >= 0;
                [/code]

                The word has been guessed correctly iff:

                [code=java]
                bool guessed= displayWord.ind exOf('-') < 0;
                [/code]

                kind regards,

                Jos

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by JosAH
                  Again: regular expressions are your friend here; suppose we have this:

                  [code=java]
                  String secretWord= "apple";
                  String correctChars= "pe";
                  String displayWord= secretWord.repl aceAll("[^"+correctChars +"]", "-");
                  [/code]

                  a newChar is correct iff:

                  [code=java]
                  bool correct= secrectWord.ind exOf(newChar) >= 0;
                  [/code]

                  The word has been guessed correctly iff:

                  [code=java]
                  bool guessed= displayWord.ind exOf('-') < 0;
                  [/code]

                  kind regards,

                  Jos
                  Which basically completes it.
                  I must say it was a very good effort from dav3.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by r035198x
                    Which basically completes it.
                    I must say it was a very good effort from dav3.
                    Yup, but if you use regular expressions the 'business logic' for this little game
                    doesn't need more than, say, 20 lines or so of code.

                    kind regards,

                    Jos

                    Comment

                    • dav3
                      New Member
                      • Nov 2006
                      • 94

                      #11
                      Thank you. I have not completed the program yet, I wanted to be able to produce the word as expected before worrying about whether the person had one,lost, whatever. I also plan on making this a networked game where multiple clients can connect and play.

                      Project will also contain numerous word lists read from a file, each file containing words about a certain subject. ie Computer Science, Animals, Cities etc....


                      Thanks for your help guys.

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by dav3
                        Thank you. I have not completed the program yet, I wanted to be able to produce the word as expected before worrying about whether the person had one,lost, whatever. I also plan on making this a networked game where multiple clients can connect and play.

                        Project will also contain numerous word lists read from a file, each file containing words about a certain subject. ie Computer Science, Animals, Cities etc....


                        Thanks for your help guys.
                        You're welcome of course. Don't get over enthousiastic for now. The best thing to
                        do is to 'decouple' your game's components. A WordListLoader (which is an
                        interface) would be one part:

                        [code=java]
                        public interface WordsListLoader {
                        WordsList load(String identifier);
                        }
                        [/code]

                        A WordsListLoader gives you a WordsList (also an interface). It can do simple
                        things like this:

                        [code=java]
                        public interface WordsList extends List<String> {
                        public String randomWord();
                        public String randomWord(Stri ng subject);
                        }
                        [/code]

                        etc. etc. Note that I didn't implement anything; if you manage to implement your
                        'core' of the game using just those interfaces you can implement those interfaces
                        independent of each other, e.g. a WordListLoader doesn't have to deal with the
                        production of actual words (either in a subject or not).

                        The saying is: low coupling, high coherence.

                        kind regards,

                        Jos

                        Comment

                        Working...