Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java:A simple.Hangman Gamd  RSS feed

 
Udit K Agarwal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a beginner in Java Programming . Okay so I decided to make this Hangman Game, and as being a beginner in Java it was quite difficult for me. But , though it has no error , it is not running properly , in other words , not giving the expected Output
Okay so here is how the whole program works-

1) The Dictionary Method Picks Out A Random Word From an Array
2) The word2return Function Makes The Word picked by Dictionary Method in Blank Form e.g If the Word picked By Dictionary is "Mexico" Then It makes it look like this "M_e_ic_"
3) The realgame Method works like this --
User Enters A Character (in n) , then the program searches whether it is present in (nword) or not , if present it returns value greater than 0 , if not present then -1 displaying that the Entered guess is wrong and it reduces life by one ( in else loop), On the other hand if it is present in nword then It searches that charcter's index in nword and replaces it on the same index in the String gameword (only if there is a '_' present in Gameword on that index) and then if the life become 0 it comes out of the loop and Tell us if we've won or lost SourceCode:-


Problems
1)The string str is also not displaying the right output . If The word to be filled is "M__ic_", and if in the last try i input E .. , then after getting out of the loop it displays "Meeice"which actually should Display Mexico .. 2) If The Loop has to take Input from user 'X' imes , it takes input 'X-1' times . 3) Even Though User Inputs The Right Character , It executes both if and else loop. The output is like This :-
Problem
1)The string str is also not displaying the right output . If The word to be filled is "M__ic_", and if in the last try i input E .. , then after getting out of the loop it displays "Meeice"which actually should Display Mexico .. 2) If The Loop has to take Input from user 'X' imes , it takes input 'X-1' times . 3) Even Though User Inputs The Right Character , It executes both if and else loop. The output is like This-



each time it displays Unsuccessful Try even though The user has input the right characters

Please , Kindly Help me out with these Problems ..


P.S Sorry for Poor Grammar
 
Will Ashton
Greenhorn
Posts: 22
Java Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Udit,
Welcome at the ranch first
I took a brief look at your code, but I was not able to find much. This might be partially, cause I'm tired as hell right now, but a bigger problem is, that your code is not very clear. It would bring us huge steps further, if you would use indents. Like for example:



Additionally it would be great, if you could consider using some more descriptive names for your variables.
A common convention is furthermore, to write the first letter of a variable- or method-name in lower-case (e.g. realGame () instead of RealGame (). You should respect these conventions, if you want others to read your code. Programmers are very sensitive and are easily disturbed by things like these

Some changes like these make your code easier to read and to understand for others and helps us to help you

For your second problem you need to take a look at the evaluation part (the middle one in the loop declaration).
Surely you messed something up in there. OfByOneErrors happen very often. Just sleep a bit, relax and then take a second look at your code. I'm sure, you will find the error fast this way. Keep in mind, that the incrementation (the last part in the for-loop-declaration) always happens at the end of the loop-block, which means after the instructions, which you wrote in the loop body.

Your third problem will be solved by using indents. You used a high number of if's and the indents will show you clearly, which else belongs to which if. Do you use an IDE? I'm using NetBeans which has an option for formatting the source code automatically (very handy, if you ask me). In NB you find it under Source>Format.

Well, I just inserted your Code into NB, made a testrun and took a detailed look on the first 40 lines. I'm to tired to go on, but I can give you the hint, that the API-method String.replace() (line 37) is probably not doing, what you want it to do. Take a deeper look at the API-reference and run some tests to find a method that is better suited to your needs. If I were you I would try to create two substrings, the first containing everything before the "_" and the second everything behind it. GameWord = substring1 + n + substring2 should do the rest.

If you need more help, feel free to ask. I'm sorry, but at the moment, my bed is in a quite desperate situation and needs me more than you :P

Good night
 
Udit K Agarwal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well , i sorted out the first Problem of The Output String .
But for that OffByOne Error i tried using do..while and while loop instead of for loop, but Nothing worked .. Can you help me out with that?
And yea what to do with that 3rd problem , both the if and else conditions are executing ..Like if i have entered the right Character , it do says that i have Entered the right character, the loop then goes on normally i displays "guess next character" ,but now It do not take input from user and simply executes the Else loop Displaying "Wrong guess" (refer to the output) How to deal with this??

THANKS
 
Stephan van Hulst
Saloon Keeper
Posts: 6972
109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Udit,

You should break up your code in classes and methods. People used to write long procedures that did everything, but those days are long over. I suggest you create a class that keeps track of which letters have been tried, and returns information about the number of wrong guesses, the complete word that's been guessed so far, etc. Here's how it could look:

 
Stephan van Hulst
Saloon Keeper
Posts: 6972
109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't worry about console input yet. You can create the logic of a game without it:


 
Udit K Agarwal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey stephan ,
Thanks for that idea .I'll work out on it too ... but since you know i am just a beginner , i dont much , I am just in 10th grade
.Thus, It would be a great help if you could help me out with the problems i am encountering with my code
 
Stephan van Hulst
Saloon Keeper
Posts: 6972
109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing like a good challenge to learn more ;)

Anyway, the reason I posted this example, was to show you that breaking up your code in smaller pieces will make it much easier to find errors. This is because when a method doesn't work as you expect it to, you can look at the individual pieces it's composed of, and see if those work as expected. This process is called debugging.

The first thing you should do is change your code so the RealGame method (which should be starting with a lower case letter) is split into smaller pieces. I can see that is consists of a piece that asks the user to guess a letter, a piece that determines whether a letter is present in the word, a piece that replaces underscores with real letters, and a piece that tells the user the game is over. Make separate methods for those pieces, and show us your new code.
 
Will Ashton
Greenhorn
Posts: 22
Java Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The first thing you should do is change your code so the RealGame method (which should be starting with a lower case letter) is split into smaller pieces. I can see that is consists of a piece that asks the user to guess a letter, a piece that determines whether a letter is present in the word, a piece that replaces underscores with real letters, and a piece that tells the user the game is over. Make separate methods for those pieces, and show us your new code.


Approved!

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!