A Player

Goals

Overview

A Player is a simplistic representation of a Player in a video game. Every Player has strength and points (both of which may vary). Every Player is either alive or dead. Given some code and a specification for a Player in the form of a written description and sample interactions with comments, you will complete writing the code for the Player class.

 

Directions

Download Player.java and complete it.

Details

When a Player is created, the maximum amount of strength that it can attain is passed as an argument to the constructor. However the maximum strength can not be less than 50 or greater than 200. If there is an attempt to create a Player with maximum strength outside this range, its maximum strength is set to 100. Upon creation a Player has maximum strength and 0 points and is alive.

A Player can earn points by fighting, although it loses strength when doing so. A Player's strength can not go below 0. When a player loses all strength, it dies. After that, it can not become alive again and neither its strength nor points may change. Comments within the sample interactions provide more details. The following interactions should work:

Sample Interactions

You are give sample interactions to test player class that can be performed in Dr Java's interaction pane. If you want to try these interactions without typing them in, you have two options:

1) load them in from an interactions history file. Download the file player.hist . Choose Tools->Load Interactions History As Script. Note: that four new buttons appear on the right side of the Interactions Pane. Hit the Next and Execute buttons one after the other.

2) Create file called playerSim.java. Write main method in that file. Write out instructions, compile and run. Note: you need to use print statements wherever you need to see the value e.g. System.out.println(p.getMaxStrength());

> Player p = new Player(80);
> p.getMaxStrength()
80
> p.getStrength()
80
> p.isAlive()
true
> p.getPoints()
0
> p.fight(20)  // this fight takes 20 strength units to win
true       // the Player had enough strength to win the fight (true means it won)
> p.getStrength()
60
> p.getPoints()
200   // every strength unit spent fighting earns 10 points 
> p.getMaxStrength()
80
> p.revive(30)   // Attempt to add 30 strength units. Since the Player is alive, it's revivable.
true   // returns true if the Player is alive and the revive method added at least some strength
> p.getStrength()
80   // Its strength would have gone up to 90 but its max strength is 80
> p.fight(100)
false       // It didn't have enough strength to win the fight (but it did use all its strength)
> p.getStrength()
0
> p.isAlive()
false
> p.getPoints()
1000     // even though it didn't win the fight and died, it earned points
> p.revive(50)
false    // A dead Player is not revivable.
> p.getStrength()
0
>//------------Create a new Player(not in Player.hist)----------------
> Player mario = new Player(250); 
>mario.getMaxStrength() 
100
> mario.getStrength()
100
> //Continue just like for Player p
> //See if the outcomes match the specifications with different inputs