need help with hangman java program

Discussion in 'OT Technology' started by meow, Nov 5, 2007.

  1. meow

    meow OT Supporter

    Joined:
    Feb 12, 2006
    Messages:
    11,869
    Likes Received:
    73
    Location:
    PA
    i'm trying to make this little hangman type game and i don't know the logic of what to do. i am new to java so i can't figure it out on my own.

    so made it get the words from the file, then i used the word it grabbed and replaced it with underscores. so if it grabs the word "santa" it says like, your word is "_____". but now they have to input a character and if it is one of the characters that is in the hidden word, it will change it to like "_a__a" if they choose 'a' and keep doing that until they win or lose (max 7 tries). i have no clue how to have it search the real word with the inputed character and then replace the "_____" with the correct letters if they choose the right letter.

    so basically, how do i get it too search and replace with the inputed character.
     
  2. meow

    meow OT Supporter

    Joined:
    Feb 12, 2006
    Messages:
    11,869
    Likes Received:
    73
    Location:
    PA
    also i'm having another problem..when they lose or choose to get a new word i don't know how to get it to clear the past results.

    they get 7 chances so if they go seven times it will say like

    "You have guessed these characters: g d l r d s a"

    then it's supposed to restart with a new word (and it does) but the hiddenword is a StringBuffer and it appends..so say the first word is santa and 2nd is armor. it would be using santaarmor which is "__________" instead of being "_____"

    ^it does the same thing with the characters. if u try twice, it would be "You have gussed: g d l r d s a b v j f s f d" instead of "b v j f s f d"


    my code is pretty crappy so if i post it i don't think anyone will help me..it's in 3 different classes like it was supposed to be, but i dont know what the hell im doing..i just want to get this working and hand it in and deal with whatever style problems i have as is.
     
  3. Bruticus

    Bruticus half dead OT Supporter

    Joined:
    Apr 10, 2004
    Messages:
    4,608
    Likes Received:
    0
    Location:
    Melbourne
    OK logic-wise it shouldn't be too hard. One way to decide which letters to display would be to have an array the same length as the current word, fill it with x's to start and change to something else if the matching letter has been "hit". You should be able to search the word letter by letter without much problem, read up on some of the java libraries.

    Also I recommend you output the _'s with a space between them otherwise they'll look strange.

    The issue with restarting the program should be a simple one, you just have to call the classes in the right order etc and clear memory when it is required.

    It sounds like you have the code basics down, so I'd recommend actually sitting down with a pen and a piece of paper and mapping out the actual logical algorithm, THEN implementing it.
     
  4. meow

    meow OT Supporter

    Joined:
    Feb 12, 2006
    Messages:
    11,869
    Likes Received:
    73
    Location:
    PA
    i figured out those problems and got them working..my program had a few other minor problems but i handed it in anyways..i couldn't get it too now allow dupelicate of the same letter without screwing up the guesses StringBuffer. it seemed like it shoulda worked but it wasn't so i removed the code i had for that. the other problem was that i didn't get it too not get the same word sometimes..we were supposed to have it get a random word from a textfile and it did but it was supposed to not grab that word again and i didn't get that done.
     

Share This Page