This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Hangman BufferReader ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hangman BufferReader ??" Watch "Hangman BufferReader ??" New topic
Author

Hangman BufferReader ??

Kris Danielewski
Greenhorn

Joined: Apr 01, 2009
Posts: 25
I am trying to make my first java game. Hangman.
I ve done so far a Player class, will post constructor..

which holds name , level and max noof tries (which can be extended later on so its not final)
I am getting lost a bit when i try to do the hiden word and guess things...
i manage to write

so if i declare new hiddenword lets say
String = "Chris";
and then assuming that player is created , how can i go through the string and get a message on the screen with position of the letter found.

Would be thanksfull for help



K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2247
    
    7

Hangman hey. Sound fun. Please use "code" tags when posting code.

Anyway where are you defining your hidden words? Once you do, you can change that string to a char array. Then your guess method takes in the user input and iterate or loop through the char array to see if any char matches the user input. If so display that char position. Also the case should not matter I think.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Kris Danielewski
Greenhorn

Joined: Apr 01, 2009
Posts: 25
thanks , that explains a lot.
Can I ask for mod not to close the topic due to fruther problems.Thanks.
Kris Danielewski
Greenhorn

Joined: Apr 01, 2009
Posts: 25
Oki I can't figure it out...

let say I create an array of Strings in the class i call WordToGuess

and I have my method of Player class


now how can I read through this string char by char to get correct result for the followig

I guess I need a method which will read char by char of my hWord and compare it with player guess...

Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

try something like this:



Hunter.


"If the facts don't fit the theory, get new facts" --Albert Einstein
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2247
    
    7

Further to Hunter's suggestion you can use ArrayList<String> to store a bunch of hidden words and randomly choose an index then convert that string to char array like this:

Kris Danielewski
Greenhorn

Joined: Apr 01, 2009
Posts: 25
The result I have recieved is OK, however if I want to let user know that letter he entered wasnt match i get the "no match" message as many times as index in the loop, any idea?
Let stick to the simple version so far for tests ( I will use Tsang's later)
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2247
    
    7

Put the "no match" line outside for loop. Add "break" inside if once matched and the else is redundant
Kris Danielewski
Greenhorn

Joined: Apr 01, 2009
Posts: 25
Put the "no match" line outside for loop. Add "break" inside if once matched and the else is redundant

how about if the first and the last letter or the word match? break wouldn;'t allow it to loop for it. correct?
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2247
    
    7

Hehe the break is really not needed. But it depends on your behavior such as when using enter 1 char it checks and return if that input is correct or something. If you check whenever user input 1 char then I think you need the break.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

or you could do something like this:



Hunter.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2247
    
    7

Hey Kris hows your program coming? I did my own version very simple at least for simple player.
Kris Danielewski
Greenhorn

Joined: Apr 01, 2009
Posts: 25
Well, what I am trying to do is search for the char user enter in the hidden word and the return how many letters he found and what are they and on which position...
I wonder if I would create x JButton of the lenght of hidden word and somehow make them empty , i could then make them apper if user enter correct char, can I do that?
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2247
    
    7

Kris Danielewski wrote:Well, what I am trying to do is search for the char user enter in the hidden word and the return how many letters he found and what are they and on which position...


This part should be somewhere in the previous posts. Hunter and I both did it. Finetuning is your job In fact in my own version, I use 2 char arrays - one for the actual hidden word and the other one for when user guessed the letter correct.

I wonder if I would create x JButton of the lenght of hidden word and somehow make them empty , i could then make them apper if user enter correct char, can I do that?


You planning to make GUI for this? I think you meant JLabel to display the hidden word length and the correct char.
Kris Danielewski
Greenhorn

Joined: Apr 01, 2009
Posts: 25
Yea, I know the answer is in previous post, I am working on it, so far I decided to decrease complex level by defining simple list of Player<String>.
Yes I will be doing GUI, should I firstly make it work without?

I have seen some examples on the web but didnt find any easy one.

Could you explain me why did you use 2 char arrays?
I know the way above is correct , problem is with representation to the user, should I create a

correct?
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2247
    
    7

The reason I used 2 char arrays is one to store the hidden word and the other for user input. When the input matches one of the char in the hidden word array I increment a count variable. By the time this count = the word's length I end the program. Also I have another variable to keep count of lives - if incorrect match i decrement the lives.

The problem I am having now (having played with it a bit) is how to handle duplicate inputs. For example I have the hidden word "hello". I enter 'h' as my input, count=1. If I enter 'h' again count=2; eventually count will =5 and assume you have guessed the word correctly.

For your representation, in my version I looped through the 2nd char array and display h _ _ _ _
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

Kris Danielewski wrote:



This is not how you compare Strings.

Next, I would urge you to refer to indexOf , lastIndexOf , substring functions in the String class, you probably wont require to run so many loops in the first place.


Even without using the above Functions,
you can:

1. Modify String containing Dashes
2. Check whether the guess was a miss

In a SINGLE loop.


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Kris Danielewski
Greenhorn

Joined: Apr 01, 2009
Posts: 25
I have come up with this..



got problem there , just tell me im on the good way

[edit]
Created second char array . but have also problem with representation


small bit is missing because it doesnt compile , .... how to make it work...
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

its nice to know you are trying in a positive direction.

Here are some enhancements:

1. You do NOT need character arrays, a String class object is sufficient.
2. letter is an array, does guess() return an array ? (if not then obviously its incompatible)

logic

Kris Danielewski
Greenhorn

Joined: Apr 01, 2009
Posts: 25
salvin francis wrote:


Well, don't really know how can I do it using String..I know equals method compering two String , but nothing to find a letter in the String ...
and how to replace everydash with correct guess...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Don't even try with a String. At least two possibilities:
  • Put the letters into a char[] array; there is a String method which does that very easily.
  • Use a java.lang.StringBuilder which allows replacement of parts of a String.
  • Actually for a StringBuilder, delete and insert might be easier. The array might be easier still.
    Kris Danielewski
    Greenhorn

    Joined: Apr 01, 2009
    Posts: 25
    Campbell Ritchie wrote:Don't even try with a String. At least two possibilities:
  • Put the letters into a char[] array; there is a String method which does that very easily.

  • Isn't it what I have done?
    Problem I have now is how can I replace dashes by correct letter entered...



    Problem with replace of dashes with letter user entered.
    By the way , thanks everyone for advices. They open my mind.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38075
        
      22
    Yes, you have used a char[]. Sorry.
    Kris Danielewski
    Greenhorn

    Joined: Apr 01, 2009
    Posts: 25
    It's Ok.
    Any idea how to replace an index of inp[] if letter was found in ary[i], that would be happy ending.


    K. Tsang
    Bartender

    Joined: Sep 13, 2007
    Posts: 2247
        
        7

    Ah Kris, you got to where I'm at now. I thought about this - use a collection like Set and "probably" use set.contains() to check if the input char already exist. But I'm not getting it to work properly yet
    Kris Danielewski
    Greenhorn

    Joined: Apr 01, 2009
    Posts: 25
    im not there yet , because last bit doesn't replace my dashes haha, yea good idea with Set
    K. Tsang
    Bartender

    Joined: Sep 13, 2007
    Posts: 2247
        
        7

    Kris Danielewski wrote:im not there yet , because last bit doesn't replace my dashes


    You know this is why I used 2 char arrays!! Instead of doing:


    Do this:


    Then to display you loop through the guessAry char array (in your case the inp char array) instead of your original ary char array.
    Paul Yule
    Ranch Hand

    Joined: May 12, 2008
    Posts: 229
    Kris Danielewski wrote:It's Ok.
    Any idea how to replace an index of inp[] if letter was found in ary[i], that would be happy ending.



    I'm not sure I follow you here, sorry. Is inp[i]=ary[i]; not doing that? Do you mean print the partially completed word with dashes in it? Just loop through the char array. Or is the letter variable not matching the char in the ary with that ==? is letter a string or a Char? I would just do

    char letter= Sc.next().toCharArray()[0];
    Hunter McMillen
    Ranch Hand

    Joined: Mar 13, 2009
    Posts: 492

    Ok for my solution to this all of the words were strings, the only characters i used was the guess, I also convert to a charArray once.
    I initialized my hiddenWord to word.length() and filled it with x's, so if the word was cow, my hiddenword would be xxx

    you can search your actual word to see if the letter guessed matches using a for loop, then put the correct guess in the hiddenWord at the same index.

    So if i guessed 'o', then it would print xox

    Hunter.
    Kris Danielewski
    Greenhorn

    Joined: Apr 01, 2009
    Posts: 25
    Hunter McMillen wrote: then put the correct guess in the hiddenWord at the same index.

    I stuck at this bit ;p , Your example is the same way we are trying to achive. I mean , I am trying ...
    Paul Yule
    Ranch Hand

    Joined: May 12, 2008
    Posts: 229
    Kris Danielewski wrote:
    Hunter McMillen wrote: then put the correct guess in the hiddenWord at the same index.

    I stuck at this bit ;p , Your example is the same way we are trying to achive. I mean , I am trying ...


    inp[i]=ary[i]; is doing that. Is it not replacing correctly? or is the (letter== ary[i]) not evaluating to true?
    Are you printing the whole input array at some point to show them --ll- if they guessed correctly the letter 'l'? If they guess correctly it should be replaced already in the code you showed us. What happens when you print the whole inp array?



    Kris Danielewski
    Greenhorn

    Joined: Apr 01, 2009
    Posts: 25

    And the result I am getting is:


    ...
    Paul Yule
    Ranch Hand

    Joined: May 12, 2008
    Posts: 229
    Try

    System.out.println(" match " + new String(input));

    I think that's what you're looking for. You are printing out the hashcode of the array because the default toString() method prints the hashcode. So the conditional is evaluating correctly you just need to make sure you print the contents of the array.
    Kris Danielewski
    Greenhorn

    Joined: Apr 01, 2009
    Posts: 25
    Yea... I modified toString method on the same class , dont know why it didn;t work.
    Well done!! ;] thanks
    That was really great expirience I start to like this forum. People don't let you know the answers but show you ways and points of view.
    Hopefully I will not stuck anywhere from now , until GUI
    K. Tsang
    Bartender

    Joined: Sep 13, 2007
    Posts: 2247
        
        7

    Your input is a char array and if you do System.out.println(input); it is the same as saying input.toString(); And you know what that gives you - a meaningless string.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38075
        
      22
    The unoverridden toString() method does not return a meaningless String. It is hard to understand, but by no means meaningless.
    Kris Danielewski
    Greenhorn

    Joined: Apr 01, 2009
    Posts: 25
    mostly done and everything seem to be ok.
    Problem with initialising amount of dashes/x-es to the lenght of hiddenword.


    i get arrayIndexoutOfboundsException...
    Hunter McMillen
    Ranch Hand

    Joined: Mar 13, 2009
    Posts: 492

    well you have a word right? because you are supposed to start every game with one word.

    so try this:




    Hunter.
    Paul Yule
    Ranch Hand

    Joined: May 12, 2008
    Posts: 229
    I assume the message says index 0 is out of bounds? Just initialize the array with the proper amount of space to hold the entire word.
    char[] input = new char[ary.length];
    Kris Danielewski
    Greenhorn

    Joined: Apr 01, 2009
    Posts: 25
    Paul Yule wrote:
    char[] input = new char[ary.length];

    That was good.

    Ehhh, another problem..
    After entering the same letter few times you can win because it increase the counter == word.length
    Should I create another char array and store there letter user entered and make it unable to enter again or easier way
    I have tried to change while formula;

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Hangman BufferReader ??
     
    Similar Threads
    Hangman type game
    Hangman problem!
    Hangman game
    Small problem with arrays in Hangman java game
    Updating the _ _ _ _ _ in a hangman game?