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

How do I create a battlecode from the code I already created?  RSS feed

 
Mike Corona
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I became extremely confused after trying to create the code to initialize my hero class and die class. The following expectations were created for these objects:

Introduction

You will build a turn based battle game called BattleCode. The characters in the game are called Heroes - a data class detailed below. Heroes have four traits: strength, agility, health and stamina. These factors, and some luck, will ultimately decide the fate of a battle. In this assignment you will only build part of the larger game - more to come later

Game Play

Setup

The game starts by initializing Heroes. Heroes have 4 properties: strength, agility, stamina and health. Health and stamina are 100, but strength and agility must be initialized by randomness. Strength helps with attacking and agility helps with blocking. Roll four 6-sided die. The highest value is assigned to strength. the second highest is assigned to agility.

Battles

A battle consists of rounds. In each round, a player can choose how to move their hero - attack, block, or rest.

Attack - attacks the opponent; roll two six sided die, attack is strength + roll, stamina cost of 7
Block - block an attack;roll a ten sided die, If your roll is less than 4 then dodge is agility + the roll * 2. Otherwise dodge is agility + the roll. Stamina cost of 3
Rest - Gain stamina and health;Roll three 8-sided die. Drop the lowest roll. Add the highest roll to health, the other to stamina *Stamina less than move cost requires opponent to REST

Once both players select a move damage is calculated.

  • Attacking and resting provide no defense. If you are attacked in these states, you receive full damage

  • healing adds health, which mitigates damage, but it is not the same as blocking
  • Blocking provides defense. If a hero is attacked when blocking, damages could be averted

  • If the block value is greater than or equal to the attack value, the attack is dodged
    Otherwise the difference between the attack and the block is the damage.

    This completes a round.

    Hero Class

    A hero performs the following:

    getName - return the string name
    setName - assign a value to the string name
    getHealth - return the int health
    setHealth - assign a value to the int health
    addHealth - add an int value to health, the value added cannot be negative
    removeHealth - subtract an int value from health, the value subtracted cannot be negative
    setStrength - assign a value to strength, cannot be negative
    getStrength - return strength
    setAgility - assign a value to agility, cannot be negative
    getAgility - return the int agility
    setStamina - assign a value to stamina, cannot be negative
    getStamina - return the int stamina
    addStamina -  add an int value to stamina, the value added cannot be negative
    removeStamina - subtract an int value from stamina, the value subtracted cannot be negative
    rest - returns void, adds to health and stamina based on table above.
    block - returns the int value calculated by the table above - stamina less than move cost, opponent must rest
    attack - returns the int value calculated by the table above - stamina less than move cost, opponent must rest
    printStats - print the hero name, strength, agility, stamina and health to the screen

    Instructions

    1.Create a project called Project2
    2.Create a Hero class as specified above.
    3.Create a class called BattleCode that contains a main method that does the following
    1.Create two hero objects
    2.Use the gameplay instructions to setup the hero values.
    1.Use the Die class you created in the Labs to roll the dice
    3.Call all the methods of the hero object
    4.Write a static method called round that takes two hero objects as input and returns void
    1.this method should
    1.print each player's stats
    2.prompt the user to choose a move for one of the heroes
    3.use a die object to randomly choose a move for the other.
    4.report both moves and their results
    5.assign damage to any player that received damage
    5.Battle a few of rounds by calling the round method a few times. Be sure to test all combinations of moves

    Die Class



    Hero Class


    BattleCode Class
     
    Mike Corona
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I do not think I should be calling die rolls until the battlecode class, but some of the methods from the hero class rely on the dice roll. The real issue is I am still trying to understand object integration.
     
    Junilu Lacar
    Sheriff
    Posts: 10929
    158
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Look carefully at this Die constructor:

    On line 17, what happens to the value that you passed in to the constructor?
     
    Mike Corona
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Junilu Lacar wrote:Look carefully at this Die constructor:

    On line 17, what happens to the value that you passed in to the constructor?


    sides becomes 1. I take it that should be this.sides = sides;
     
    Junilu Lacar
    Sheriff
    Posts: 10929
    158
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And a nitpick:

    What's the point of the local variable value? You can cut the length of this method by half by just having a return statement.
     
    Campbell Ritchie
    Marshal
    Posts: 54886
    155
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, it shou‍ld. What does the if statement mean? I think it is incorrect.
     
    Mike Corona
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Junilu Lacar wrote:And a nitpick:

    What's the point of the local variable value? You can cut the length of this method by half by just having a return statement.

    you are completely right. I'll change that now.
     
    Dave Tolls
    Ranch Hand
    Posts: 2721
    30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Corona wrote:
    sides becomes 1. I take it that should be this.sides = sides;


    Yes, but at the moment that code (for a six sided die anyway) is working by pure fluke.

    That aside (and Junilu is good at this stuff), to answer your question, yes, you do need 4 die rolls.
    Not necessarily as 4 separate dice, but that's one way.

    The reason is, to create your Hero:
    "Roll four 6-sided die. The highest value is assigned to strength. the second highest is assigned to agility."

    So you need those 4 numbers so you can finish creating a Hero.
     
    Mike Corona
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Yes, it shou‍ld. What does the if statement mean? I think it is incorrect.

    the if statement was to make sure the sides could not be a negative integer or 0.
     
    Junilu Lacar
    Sheriff
    Posts: 10929
    158
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Corona wrote:sides becomes 1. I take it that should be this.sides = sides;

    Ok. So you default the Die to be showing a 1 on the next line. That's fine, too. What's your intent for lines 20 to 23? (I know what it is, I just want to see if you can express it in plain English--the code doesn't match the intent, that's why you have a bug)

    EDIT: Gah! too slow to post...
     
    Knute Snortum
    Sheriff
    Posts: 3834
    91
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You have several lines of code that look like this: That's fine, but there is a shortcut: There are many operators in that family, such as -=, *=, \=, and others.
     
    Junilu Lacar
    Sheriff
    Posts: 10929
    158
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Corona wrote:the if statement was to make sure the sides could not be a negative integer or 0.

    Let me help you express in plain English, since it's sometimes hard to switch back to regular language when you've been racking your brain for the Java-speak for so long:

    "If we're asked to create a die with negative or zero sides, we should ignore the request and just default to a regular 6-sided die."
     
    Mike Corona
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Junilu Lacar wrote:
    Mike Corona wrote:sides becomes 1. I take it that should be this.sides = sides;

    Ok. So you default the Die to be showing a 1 on the next line. That's fine, too. What's your intent for lines 20 to 23? (I know what it is, I just want to see if you can express it in plain English--the code doesn't match the intent, that's why you have a bug)

    well, if sides is less than or equal to 0, then set sides to the original variable.......which is 0? ugh that doesnt make much sense does it. that should be sides = sides + 1;?
     
    Mike Corona
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Junilu Lacar wrote:
    Mike Corona wrote:the if statement was to make sure the sides could not be a negative integer or 0.

    Let me help you express in plain English, since it's sometimes hard to switch back to regular language when you've been racking your brain for the Java-speak for so long:

    "If we're asked to create a die with negative or zero sides, we should ignore the request and just default to a regular 6-sided die."

    So, are you basically saying that constructor is useless and I should move the if statement to the empty contructor?
     
    Mike Corona
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Corona wrote:
    Junilu Lacar wrote:
    Mike Corona wrote:the if statement was to make sure the sides could not be a negative integer or 0.

    Let me help you express in plain English, since it's sometimes hard to switch back to regular language when you've been racking your brain for the Java-speak for so long:

    "If we're asked to create a die with negative or zero sides, we should ignore the request and just default to a regular 6-sided die."

    So, are you basically saying that constructor is useless and I should move the if statement to the empty contructor?

    My brain is fried now lol Sorry, then that would mean I could not set the sides and it would always be default so I just sounded dumb.
     
    Junilu Lacar
    Sheriff
    Posts: 10929
    158
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Corona wrote:So, are you basically saying that constructor is useless and I should move the if statement to the empty contructor?

    No, I'm saying that this:

    is not how you say this: "If we're asked to create a die with negative or zero sides, we should ignore the request and just default to a regular 6-sided die" in Java. It's not useless because the intent is valid; the implementation, however, is invalid. It's really saying, "If we're asked to create a Die that has negative or zero sides, then let this Die have sides number of sides."  Since you changed the value of sides prior to executing this code, this section of code becomes pointless.

    A more "techie" translation of "default to a regular 6-sided die" is "let the number of sides be 6" -- that's as plain as I can get without actually writing code for you.
     
    Mike Corona
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Junilu Lacar wrote:
    Mike Corona wrote:So, are you basically saying that constructor is useless and I should move the if statement to the empty contructor?

    No, I'm saying that this:

    is not how you say this: "If we're asked to create a die with negative or zero sides, we should ignore the request and just default to a regular 6-sided die" in Java. It's not useless because the intent is valid; the implementation, however, is invalid. It's really saying, "If we're asked to create a Die that has negative or zero sides, then let this Die have sides number of sides."  Since you changed the value of sides prior to executing this code, this section of code becomes pointless.

    A more "techie" translation of "default to a regular 6-sided die" is "let the number of sides be 6" -- that's as plain as I can get without actually writing code for you.


     
    Junilu Lacar
    Sheriff
    Posts: 10929
    158
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you wanted your second constructor to do what the no-argument constructor does first, you can always call it like this:

    I don't particularly like to do this kind of thing in general; just giving this to illustrate how you can call other constructors from a constructor.
     
    Mike Corona
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Now, how does my hero class look compared to the instructions I had to follow? btw I created a more updated version.

     
    Junilu Lacar
    Sheriff
    Posts: 10929
    158
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Corona wrote:

    You can do that but now you've lost the intent of checking for invalid values of sides and defaulting to a reasonable value.
     
    Mike Corona
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Junilu Lacar wrote:If you wanted your second constructor to do what the no-argument constructor does first, you can always call it like this:

    I don't particularly like to do this kind of thing in general; just giving this to illustrate how you can call other constructors from a constructor.

    Wow thats incredible. I had no idea that could be done
     
    Mike Corona
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Junilu Lacar wrote: You can do that but now you've lost the intent of checking for invalid values of sides and defaulting to a reasonable value.


    How about this?

     
    Liutauras Vilda
    Marshal
    Posts: 4262
    256
    BSD
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Corona wrote:the if statement was to make sure the sides could not be a negative integer or 0.
    You can't be sure it makes sure unless you have some reasonable proof. I suggest you start writing tests early on or at least tests, in fact, better probably start from them, so such mistakes might wouldn't even occur.
    Now I'm not saying this just from up thin air. Very recent experience, today - I finally found a bug in my little project (functional programming, Lisp family language, confusing enough, Sudoku solver, which I have putted away since Christmas) I was working on, because I've deleted what I had written previously (without testing) and approached it again, but this time, writing tests first and only then actual code - it saved me, hence I promised to myself from now on follow what Junilu always propagates - TDD approach. Ok, let's not takeover here..

    Write some unit tests to prove yourself your understanding what your methods do, otherwise tracking bugs can become very lengthy.
     
    Junilu Lacar
    Sheriff
    Posts: 10929
    158
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok, moving on to "roll 4 die and get the highest two rolls"

    You can translate that literally/mechanically and create four Die objects, that's certainly one option.  Dave Tolls seems to be starting to be able to read my mind though...

    It's not very elegant, nor is it very object-oriented.  You can achieve the same results by imagining a conversation with just ONE Die object:

    You: Hey, you there, Die with 6 faces... ( ) Roll yourself four times and give me the highest two rolls you get.

    Die: Ok... (turns around, rolls 4 random numbers, sorts those numbers, then picks out the two biggest ones)... (turning back to you) Here you go... the two highest rolls out of four.

    In Java, that might be:

    That's basically the Java "conversation", I'll let you figure out the details for how you'd declare the twoHighRolls variable.
     
    Junilu Lacar
    Sheriff
    Posts: 10929
    158
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Corona wrote:
    How about this?

    Well, first of all you should put away that rolling pin because your code looks like you just took one and smushed your nice indentations flat as a pizza. (I guess the expression is "flat as a pancake" but it's past breakfast time and I'm hankering for some melted cheese and anchovies on a hand-tossed crust right now)

    Now that code says "If sides is a valid value, then make this die have that many sides, then show the 1 face.  Otherwise, make this a one-sided die."  Is that what you really want, a one-sided die if you write new Die(0) or new Die(-1)? I thought you wanted a 6-sided Die by default?

    And by the way, when sides is not valid, that code will also leave the faceValue at the default value that Java assigns to all int instance variables: 0.
     
    Mike Corona
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Junilu Lacar wrote:
    Well, first of all you should put away that rolling pin because your code looks like you just took one and smushed your nice indentations flat as a pizza. (I guess the expression is "flat as a pancake" but it's past breakfast time and I'm hankering for some melted cheese and anchovies on a hand-tossed crust right now)

    Now that code says "If sides is a valid value, then make this die have that many sides, then show the 1 face.  Otherwise, make this a one-sided die."  Is that what you really want, a one-sided die if you write new Die(0) or new Die(-1)? I thought you wanted a 6-sided Die by default?

    And by the way, when sides is not valid, that code will also leave the faceValue at the default value that Java assigns to all int instance variables: 0.


    Now it looks like too much code for a simple method.

     
    Campbell Ritchie
    Marshal
    Posts: 54886
    155
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That isn't a method, but a constructor. You are setting face to 1 regardless, so that can be moved out of the if‑else. You can get if‑0‑then‑1‑otherwise‑n onto one line with the ?: operator or a method of the Math class.

    I don't think it is too long.
     
    Campbell Ritchie
    Marshal
    Posts: 54886
    155
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A few minutes ago, I wrote:. . . if‑0‑then‑1‑otherwise‑n . . .
    Change that to if‑0‑then‑6‑otherwise‑n.
     
    Junilu Lacar
    Sheriff
    Posts: 10929
    158
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Corona wrote:Now it looks like too much code for a simple method.

    That's the funny thing about programming. Very seldom do you write the best code the first time around. I suppose that's reasonable because after all, writers go through many rough drafts before they publish their books. Even great writers like Ernest Hemingway went through reams of paper, looking for just the right words to use in his books.

    Programmers are authors, too. There's even an @author tag for JavaDoc comments. As authors, we seldom come up with just the right words or phrases the first time we put pen to paper or fingers to keyboard, as the case may be. We have to read what we wrote, see if it says what we really want to say simply and clearly. The process of cleaning up after the initial mess we make is called refactoring. You should get used to it.

    As Campbell said, that constructor doesn't have to be that long. You can boil it down / refactor it to two lines of code.
     
    Mike Corona
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I know where I went wrong and what was trying to be pointed out. This is the new code and I am sticking with it.

     
    Mike Corona
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There is still the hero class I need help with though. I just need to understand how to integrate it into the battlecode class and I can take it from there
     
    Mike Corona
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Junilu Lacar wrote:
    Mike Corona wrote:Now it looks like too much code for a simple method.

    That's the funny thing about programming. Very seldom do you write the best code the first time around. I suppose that's reasonable because after all, writers go through many rough drafts before they publish their books. Even great writers like Ernest Hemingway went through reams of paper, looking for just the right words to use in his books.

    Programmers are authors, too. There's even an @author tag for JavaDoc comments. As authors, we seldom come up with just the right words or phrases the first time we put pen to paper or fingers to keyboard, as the case may be. We have to read what we wrote, see if it says what we really want to say simply and clearly. The process of cleaning up after the initial mess we make is called refactoring. You should get used to it.

    As Campbell said, that constructor doesn't have to be that long. You can boil it down / refactor it to two lines of code.

    I just moved a few things around from the original code I had and tested it with a previous dietester I created, which showed it to be working fine.
     
    Junilu Lacar
    Sheriff
    Posts: 10929
    158
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Corona wrote:There is still the hero class I need help with though. I just need to understand how to integrate it into the battlecode class and I can take it from there

    What you have right now is kind of heading in the right direction. What exactly don't you understand about the instructions? If you take them one item at a time and follow each to the letter, it seems like a pretty straightforward translation.
     
    Mike Corona
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Junilu Lacar wrote:
    Mike Corona wrote:There is still the hero class I need help with though. I just need to understand how to integrate it into the battlecode class and I can take it from there

    What you have right now is kind of heading in the right direction. What exactly don't you understand about the instructions? If you take them one item at a time and follow each to the letter, it seems like a pretty straightforward translation.

    Honestly, there are a few places where I am left wondering if they should only be included in the battlecode class such as:

    The if statement here. I keep thinking about how this may only need to be adding 10 health without an if statement.


    Block,rest, and attack all seem to be a bit weird to me. Maybe it's just in my head but I don't think I composed them correctly.


    And then there's the parts where I am expected to use a dice roll value to determine how the method works, but then I keep thinking I should not have the die class being called into the hero class.

     
    Campbell Ritchie
    Marshal
    Posts: 54886
    155
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Corona wrote:. . . This is the new code and I am sticking with it.  . . .
    It looks a lot better now
     
    Campbell Ritchie
    Marshal
    Posts: 54886
    155
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Corona wrote:. . .. . .
    Work out what that expression avaluates to. I don't think the addHealth method shou‍ld return anything.
     
    Mike Corona
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    Mike Corona wrote:. . .. . .
    Work out what that expression avaluates to. I don't think the addHealth method shou‍ld return anything.

    You are very right. This method should be void. Ill figure that out now and post the results.
     
    Mike Corona
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    How about this?

     
    Mike Corona
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Corona wrote:How about this?


    but not I run into a new issue. rest no longer makes sense considering it is also void and I am using the addHealth() method in it.
     
    Junilu Lacar
    Sheriff
    Posts: 10929
    158
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Corona wrote:
    Mike Corona wrote:

    but not I run into a new issue. rest no longer makes sense considering it is also void and I am using the addHealth() method in it.

    Why wouldn't it make sense? What did you write in your rest() method? 

    I imagine it the intent of rest() is to give the player a chance to recuperate, right? So every time the player rests, you want to add an increment of health and the most any player can recuperate would be to 100 percent health. No matter how much a player rests, the most they can get would be 100% healthy. Makes sense to me so what's the problem?
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!