-change name of Character to fighter or something? There is already a character. change fight to battle
-in fight, have them type out [fireball] or [attack] or something so i know what to type
-teach .equalsIgnoreCase( )
Due Sunday, October 29 at Noon.
Video of this lesson is here: https://www.youtube.com/watch?v=xaZnDGLy-gE
Please follow all of my directions closely. As always, name your methods exactly as I have done and make sure your inputs and outputs match what I have written. Please test your classes and ask questions if you are unsure if they work!
Note: you are always free to add more methods/code to your classes if you think it would be helpful!
1. Create a Coin class (Coin.java). This class should represent a coin that may (or may not) be biased. A fair coin (with a bias of 0.50) will come up heads 50% of the time and tails 50% of the time. A biased coin, perhaps with a 70% bias (.70), will come up heads 70% of the time and tails the other 30%. If it has a bias of .42 (42%), it should come up heads 42 percent of the time and tails the other 58%.
This class should have the following instance data:
private String face; //this represents the face of the coin which is up. Should always be "Heads" or "Tails" - notice these are capitalized
private double bias; //a number between 0 and 1 which represents that probability the coin will come up heads if flipped.
This class should have the following methods:
2 constructors (just set up the instance variables, don't flip anything here):
Coin() //with no inputs, should set up a fair coin (a bias of 0.5), set initial face to be "Heads"
Coin(double inputBias) //with a double input between 0 and 1, should set up a coin with the given bias, set initial face to be "Heads"
isHeads() – this accessor should return a boolean which tells if the face is "Heads"
isTails() – same thing, but tells if the face is "Tails"
flip() – this void method should "flip the coin" by randomly setting the face to be "Heads" or "Tails" and then printing out that result. HINT: Math.random() generates a random double between (0,1). Compare this with your bias to determine whether it should be heads or tails.
flip(int n) – this method should flip the coin n times and RETURN an int - representing the number of times heads comes up. For example, if I call flip(100) and there are 53 heads and 47 tails, this should return 53. Note that if your coin has a bias of 0.8, and you flip a large number of coins, like 10000, it should come up heads ABOUT (but not necessarily exactly) 80% of the time. (this is how you should test this method and your bias variable. Create a Coin object in a driver with a bias of .8 and call this method)
toString() – your toString method here should return the value of face.
2. Create a class named Character (Character.java). This class should represent a basic video game character. Your character should have the following instance variables:
String:
name
int:
health
maxHealth
mana //for the non-gamers: mana is a resource used to cast spells
maxMana
minDamage
maxDamage
Your character should have the following public methods which represent actions the character can take and things that can happen to him/her:
2 constructors:
-One constructor should input the name (a String), then initialize health, mana, maxHealth and maxMana to be 100 each, then set minDamage to be 12 and maxDamage to be 16 [feel free to fiddle with any of these numbers, they are just recommended for balance].
-The other constructor should have no inputs (this will represent a computer-controlled Character) . This version should set the name to be "CPU", set health and maxHealth to be 120, maxMana and mana to be 0, then set minDamage to 15 and maxDamage to 20.
Other methods:
heal(int n) // this void method heals the character by n points. If n is less than 0, it should do nothing, and this method should not increase the health beyond maxHealth.
heal() //this void method heals the character fully up to max health.
takeDamage(int n) //void method, reduces health by n points.
isDead() //returns a boolean representing if the health is less than or equal to zero. If so, it should return true.
attack(Character enemy) //this void method should lower enemy’s (the input to the method) health (use your takeDamage method!) by some random integer between minDamage and maxDamage (including the endpoints). It should also print the damage dealt.
//remember that if you want to do a random number between say, 5 and 10, you have to do 5 + 6*Math.random() and then cast it, because there are 6 options for the value: 5,6,7,8,9,10
//for the following methods, please feel free to change any of the following suggested numbers for mana, health, attacks, etc. These are just suggested numbers and you are free to tweak and change things if you like!
fireball(Character enemy) //if the user has at least 43 mana, this void method should lower the user’s mana by 43 and then cause enemy to take 30 damage. It should also print out the damage dealt. If the user doesn’t have enough mana, this method should do nothing but print out that the player doesn’t have enough mana! You may change the amount of mana and damage of this method.
healSpell() //if the user has at least 18 mana, this void method should lower the user’s mana by 18 and raise the user’s health by 35 (again, not raising health over the maximum health value). It should print out how much health was healed. If the user doesn’t have enough mana, this method should do nothing but print out that the player doesn’t have enough mana! You may change the mana and healing amount of this method.
//you may also want to write a toString() to test - you may design this however you want, but it should include at least the characters name, current health and current mana, and should ideally fit on one or two lines of text.
As always, you are free to write your own supporting methods or extra variables.
3. Create a program called Fight.java. Rather than being the blueprint for an object, this class will contain a public static void main method that I will run to test your program. In this main method, create two Characters - one to be controlled by the user, one to be controlled by the computer. Use both of your constructors. This program will then simulate a turn-based fight between the two characters, alternating turns between the two Characters. On the player's turn, they will be able to choose whether to attack, cast healSpell or cast fireball on the opponent each turn (using Scanner). The computer will simply attack you every turn.
It is up to you to decide whether the player or the computer should go first or whether you wish to make it random.
Before each move, your program should print out the current status (name, health, mana) of both characters.
After each move, your program should check if either character is dead, and if so, declare a victor and make sure no more moves will happen!
After each round, increase each character's health and mana by some amount (you may make this 0, 1, or whatever else you want - I would recommend a couple mana each turn).
For varying extra credit, add more functionality to your characters (more spells, potions, etc.) and/or give the computer an AI that allows it to do more than just attack. Feel free to mess with mana costs of spells and damage as well to balance your game however you would like. Have fun with it!
Make sure to submit Coin.java, Fight.java and Character.java!