Junilu Lacar wrote:If you do the math and figure out all the permutations of 3 different rolls, you'd have 3! permutations or 3 × 2 × 1 = 6 permutations.
You could certainly brute force all 6 permutations with a series of if-else-if-else-if-... statements but you have to do it with the consistency you have in your first if-statement:
The problem with the way you coded it is that you were not consistent in your formulation of relational expressions. You switched things up and therefore your logic train jumped the tracks.
It pays to remember your high school algebra when it comes to these kind of problems.
Also, you don't account for two or more rolls being equal to each other. But you can reason about that later, once you figure out how to formulate your relational logic properly.
If I release a program to the world, I'll have to include the coderanch as well as some select people in the credits. This place and the people are awesome. I can't wait to get to a point I'm more the helper than helpee
IntelliJ IDEA 16 wrote:
Die die = new Die( faces: 8);
int[] highTwo = die.highRolls( highest: 2, outOf: 4);
Junilu Lacar wrote:Honestly, I think it's unfair to give requirements like this to students who are just learning how to program and not let them use arrays or tell them about the standard Java library utilities that are at their disposal. It's like asking inexperienced mechanics to fix an engine with just a screwdriver and a hammer and not tell them or let them use wrenches, jacks, and other tools available in the shop. Even experienced mechanics will find it challenging and ridiculous to not use the tools that will make the job easier and get it done faster and cleaner.
Since you say that you're not going to use arrays for this, I'll show you just how easy it is to do this with an array and the java.util.Arrays utility class:
The logic is basically this:
1. put Y rolls of the die into an array with Y elements
2. sort the array
3. return the last X elements of the array
That's it. No muss, no fuss. Bim, bam, boom, you're done. It doesn't even matter if there are any rolls that are equal; the sort guarantees you'll get the highest X rolls at the end of the array, no matter if the highest X rolls are the same or even if all Y rolls are the same.
Junilu Lacar wrote:This is the reason I suggested that you put the logic for determining the high X rolls out of a total of Y rolls in the Die class. That way, you'd just have to reuse the method, highRolls(x, y) from wherever you needed to. Otherwise, you're going to end up duplicating a lot of this "find the highest X of Y rolls" in all your methods that need to pick X out of Y rolls.
Junilu Lacar wrote:You really should consider what these two parts of your requirements imply:
addHealth - add an int value to health, the value added cannot be negative
...
addStamina - add an int value to stamina, the value added cannot be negative
Consider also that your requirements tell you to use the results of rolling the dice to determine how much health and stamina to add.
Remember the Recruit from before? You can tell the jarhead to jump() and all it needs to know is how high? Well, these requirements say that a hero can addHealth() and addStamina() but you have to tell each of those methods how much.
Mike Corona wrote:Technically, I should be focusing on the issues in the addXXX and removeXXX methods first. Is the issue as simple as using && instead of ||? I kept thinking about that earlier but I can feel my brain melting again lol
Mike Corona wrote:I debugged and this seemed to work. Not sure to what extent it will work.
Junilu Lacar wrote:If you just talk through a few sample scenarios, you'd see how to properly do it.
"If I have 90 points of health, and you tried to give me 8 points of health, then I'd end up with 98 points of health"
"If I have 92 points of health, and you tried to give me 8 points of health, then I'd end up with 100 points of health"
"If I have 96 points of health, and you tried to give me 8 points of health, then I'd end up with 100 points of health"
"If I have 96 points of health, and you tried to give me 80 points of health, then I'd end up with 100 points of health"
Therefore, I can only accept as much health as it will take to get me to 100 points max.
If you want to cut to the chase, you can just imagine measuring a level cup of flour. If you just happen to pour too much flour, then you just take a spatula and run the edge across the rim of the cup, right? So, you're basically throwing away the excess.
See if you can program that logic. Don't get caught up in the language. THINK LOGIC.
Mike Corona wrote:I hope this is closer to what you are saying because I know exactly what you mean, translating to code is much more difficult than the thought.
Mike Corona wrote:No wait! This.
Junilu Lacar wrote:Good job. Now the challenge for bonus points: Do it in one line of code using Math.min()
Mike Corona wrote:
Junilu Lacar wrote:Good job. Now the challenge for bonus points: Do it in one line of code using Math.min()
I'm up for it. I didn't even know I could make it that simple and small lol I was really overthinking those methods.
Junilu Lacar wrote:The requirements certainly don't help you figure out that stuff very much. From what I understand, both block() and attack() have to be executed in response to another player's attack move. It's like this: Your hero gets attacked by a certain amount. Given whatever intensity of attack your hero is subjected, you can respond by blocking it, or counterattacking it. A block will give you a chance to avoid damage entirely since the move is purely defensive. A counterattack provides you no protection and you can incur damage as you are trying to inflict it to the enemy who is attacking you.
Because of this, your block() and attack() methods needs to know how much of an attack from an enemy your hero is responding to. Right now, your methods don't have that. That's the "subtle" hint.
The not so subtle hint, in case you didn't catch the subtle one, is this: addStamina(), removeStamina() all take a parameter that tells the methods by how much they should change stamina. Well, block() and attack() also need to be told "how much" except in their case, they need to be told how much of an attack they are responding to.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |