Winston Gutkowski wrote:
Vivienne Ryan wrote:Ok so I fixed the DiceRoller(I think)so it should return a number between 1 - 6. I also changed the if statements to switch and while I still get the negative numbers my code is much more readable and look much more better. Anyways So I added 2 if statements where they check if the characters hp is <= 0 and if so to assign them to 0.
Aaargh! Do NOT do this.
Do things step by step, and test thoroughly after every single change you make; otherwise how can you ever know what caused a problem if you get one?
However I'm still getting the negative numbers, is this down to variable scope?
Possibly, but TBH it's difficult to tell, so I certainly wouldn't assume it.
The main problem, as I see it, is that you're doing all your logic in main(), which is a bad way to go. I suspect that most of this code should actually be in your Player class.
Try this: Turn off your computer and explain to me (or us) IN DETAIL and in English, exactly what an "attack" is supposed to do?
Winston
Winston Gutkowski wrote:
Vivienne Ryan wrote:Ok, so I have edited my code so the loop does end but...
It's still wrong. It will now produce a result between 0 and 6 (inclusive).
Also my variable names are temporary they will be changed to better ones(I just want to test the thing out and get it working)
Unfortunately, if your names are bad, it may well hamper your testing as well.
My advice: fix them NOW (you might want to look at my signature quote for inspiration ).
Also: Your if statements are not exclusive, so there's a lot of redundant (and probably incorrect) code being executed. You might want to have a look at the switch statement.
Winston
Stephan van Hulst wrote:Hi Vivienne.
The reason you're seeing negative values is because your loop will probably never terminate. Let's say one player has 10 hp left over, and you score a hit that deals 30 damage. This player will then have -20 hp, and the condition in the while loop will not reach 0.
There are also some other matters you should address. Right now the loop is going on while EITHER player has hitpoints other than 0. So if the loop terminates (IF it terminates indeed), both players are guaranteed to have 0 hitpoints. Combined with the following if statement, this means P1 will never win.
The inside of your loop does *lots* of things you probably didn't intend it to do. You're re-rolling the die for each condition (you probably meant to save the first result) and some if statements probably should only be executed if the previous ones failed. Did you mean to use if-else?
Also, you should NOT create a class named Character. Character is already defined in the Standard API. Name it something like Person, Fighter, Player or something similar instead.
Your DiceRoller generates values between 0 and 5, included. This may be confusing, when you think of what most dice look like. Your variable names are similarly confusing. Don't call player 1 "c", and player 2 "c1". Call them "player1" and "player2".
Finally, I'd suggest you'd write your loop in such a way that in its body, one player gets to attack, and at the end of it, the turn goes to the other player.
Henry Wong wrote:
Vivienne Ryan wrote:
Could somebody explain to me what is going on? I don't want to be spoonfed, I just cannot wrap my head around this bit.
Why is the method not overloaded?
Also why doesn't the second version of main not output the expected output(false, true)?
Actually, the two methods are overloaded. What they are not, and what you were expecting, were for them to be overridden. Choosing between overloaded methods is done at compile time, and the compiler does so with the reference types.
Henry
p1 equals p2 is false
p1 equals p3 is true
p1 equals p2 is false
p1 equals p3 is false
Campbell Ritchie wrote:
I didn't say, “red herring”.Jayesh A Lalwani wrote: . . . You will see what Campbell is saying about a "red herring"
I said, “red herrings”.
Challenge to Vivienne Ryan: count the red herrings.
Jayesh A Lalwani wrote:The answer is rather elementary if you know how to read the code. I am suspecting that your instructor wants you to learn how to read code. So, I'm goi9g to give you tips on how to read code rather than spoon feed the answer.