Creature class assignment

From WLCS

Objective

  • You will learn to create a basic class that encapsulates several attributes and methods

Purpose

  • The purpose of this class is to encapsulate all the attributes and methods of a single Creature object

Attributes

The Creature class should use variables to represent the following attributes:

  • int hitPoints: the amount of damage a creature can take before it dies. A creature's hit points are reduced when an opponent scores a hit: when the warrior successfully attacks the monster, it decreases the monster's hit points, and if the monster takes a bite out of the warrior, the warrior's hit points go down. If hit points reach or drops below zero, the creature dies.
  • int strength: the maximum damage a creature can inflict. The strength represents the maximum number of hit points that will be deducted from the opponent if the creature scores a hit.
  • int skill: the likelihood of a creature scoring a hit. This is an integer between 0 and 25. The higher the skill, the more likely the creature is to score a hit.
  • int armorClass: the armor class or AC of a creature represents how hard it is to damage. It is an integer between 0 and 20. The higher the AC, the less likely an opponent is to score a hit.
  • String name: the name of the creature (e.g., "Frodo" or "Aaron Bloomfield").
  • String type: a word or phrase describing the race or class of the creature (e.g., "hobbit" or "mighty warrior").
  • String pronoun: the appropriate possessive pronoun to use for this creature (e.g., "his", "her", "its").
  • Weapon weapon: a reference to the Weapon object wielded by the creature.
  • boolean angry: should be default false

Methods

The Creature class should have the following methods:

Constructors:

Creature(): the default constructor should initialize hit points to 50, strength to 15, skill to 15, armor class to 5, name to "Somebody", type to "nondescript creature", pronoun to "its", and weapon to a new Weapon created with the Weapon() default constructor.

Creature(int hp, int str, int skl, int ac, String name, String type, String pronoun): a specific constructor that lets the programmer specify all the attributes of a creature except the weapon, which is still set a new Weapon object created with the Weapon() default constructor.

Accessors/Mutators:

  • All the attributes are declared private, so you should create accessor and mutator methods for each attribute. The name of the accessor/mutator should follow the standard Java naming conventions (i.e. getHitPoints(), setDamageModifier(), getName(), etc.).

Other Methods:

  • boolean isAlive(): returns true if the creature is alive (that is, if its hit points are greater than zero) and false otherwise.
  • boolean tryToAttack(int targetAC): First, generate a random number between 0 and skill, where skill is the skill of the creature (Hint: (int)(Math.random()*skill)). Returns true if the resulting random number is greater than or equal to the targetAC parameter (which represents the opponent's armor class) and false otherwise.
  • int calcHitDamage(): computes the damage done if the creature scores a hit. This is a function of the creature's strength, adjusted to account for the weapon being wielded (i.e. a sword does more damage than bare hands). Generates a random number between zero and strength, where strength is the strength of the creature (Hint: (int)(Math.random()*strength)) and adjusts it by adding the damage modifier of the weapon being wielded, then returns this adjusted number.
  • void takeDamage(int amount): decrements the creature's hit points by amount. Also, prints out the message "<creature name> is down to <creature hit points> hit points"
  • String constructHitString(Creature opponent): returns a string of the form: "<name> the <type> <weapon hit verb> <opponent name> the <opponent type> with <pronoun> <weapon name>". For example, "Frodo the hobbit slashes Aaron Bloomfield the mighty warrior with his sword".
  • String constructMissString(Creature opponent): like constructHitString() but using the weapon's miss verb. For example, "Gnash the orc misses Frodo the hobbit with its spear".

Testing

  1. Download Media:CombatSimulation.java to the same location as your Creature.java and Weapon.java files
  2. Compile and execute Media:CombatSimulation.java

This program creates and initializes two Creature objects, for example one called monster and the other called warrior. It then goes into a loop that repeats while both the monster and the warrior are alive. Each iteration of the loop the monster attempts to hit the warrior, and if the hit is successful, the warrior takes damage. The loop body then prints out an appropriate message using constructHitString() or constructMissString(). If still alive, the other creature then attacks back in the same way. You can use this class to see how the methods in your Creature and Weapon classes will be called, and as a test of the code you right. Though the outcome of the battle is different each time because of the random numbers, the output should look something like this:

Schmoo the gruesome monster slashes Gnusto Frotz the valiant warrior with its claws.
Gnusto Frotz the valiant warrior swings at but misses Schmoo the gruesome monster with his sword.
Schmoo the gruesome monster slashes Gnusto Frotz the valiant warrior with its claws.
Gnusto Frotz the valiant warrior swings at but misses Schmoo the gruesome monster with his sword.
Schmoo the gruesome monster slashes Gnusto Frotz the valiant warrior with its claws.
Gnusto Frotz the valiant warrior stabs Schmoo the gruesome monster with his sword.
Schmoo the gruesome monster swipes at but misses Gnusto Frotz the valiant warrior with its claws.
Gnusto Frotz the valiant warrior swings at but misses Schmoo the gruesome monster with his sword.
Schmoo the gruesome monster slashes Gnusto Frotz the valiant warrior with its claws.
Gnusto Frotz the valiant warrior stabs Schmoo the gruesome monster with his sword.
Schmoo the gruesome monster slashes Gnusto Frotz the valiant warrior with its claws.
Gnusto Frotz dies.  Schmoo is victorious!
Press any key to continue...