File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hangman BufferReader ??

 
Kris Danielewski
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3228
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kris Danielewski
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks , that explains a lot.
Can I ask for mod not to close the topic due to fruther problems.Thanks.
 
Kris Danielewski
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try something like this:



Hunter.
 
K. Tsang
Bartender
Pie
Posts: 3228
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3228
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put the "no match" line outside for loop. Add "break" inside if once matched and the else is redundant
 
Kris Danielewski
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3228
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or you could do something like this:



Hunter.
 
K. Tsang
Bartender
Pie
Posts: 3228
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Kris hows your program coming? I did my own version very simple at least for simple player.
 
Kris Danielewski
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3228
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3228
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 1230
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kris Danielewski
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 1230
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 25
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 47300
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, you have used a char[]. Sorry.
     
    Kris Danielewski
    Greenhorn
    Posts: 25
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 3228
    12
    Android Java
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 25
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    im not there yet , because last bit doesn't replace my dashes haha, yea good idea with Set
     
    K. Tsang
    Bartender
    Pie
    Posts: 3228
    12
    Android Java
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 229
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 492
    Firefox Browser Linux VI Editor
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 25
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 229
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 25
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    And the result I am getting is:


    ...
     
    Paul Yule
    Ranch Hand
    Posts: 229
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 25
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 3228
    12
    Android Java
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 47300
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The unoverridden toString() method does not return a meaningless String. It is hard to understand, but by no means meaningless.
     
    Kris Danielewski
    Greenhorn
    Posts: 25
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 492
    Firefox Browser Linux VI Editor
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 229
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 25
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic