File tree Expand file tree Collapse file tree 3 files changed +33
-5
lines changed
Expand file tree Collapse file tree 3 files changed +33
-5
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,9 @@ public void addWeapon(Weapon newWeapon) {
5151 * Gets the equipped weapon of this Character
5252 */
5353 public Weapon getEquippedWeapon () {
54+ if (equippedWeapon == -1 ) {
55+ return null ;
56+ }
5457 return weapons .get (equippedWeapon );
5558 }
5659
Original file line number Diff line number Diff line change 1+ /**
2+ * Default weapon for when you are unarmed
3+ */
4+ public class Fists extends Weapon {
5+ public Fists () {
6+ super ("fists" , "your hands" , 3 );
7+ }
8+
9+
10+ @ Override
11+ public boolean useOn (Character target ) {
12+ target .health -= getBaseDamage ();
13+ System .out .println ("Slapped for " + getBaseDamage () + " damage." );
14+ return true ;
15+ }
16+ }
Original file line number Diff line number Diff line change 22
33public class Main {
44 public static void main (String [] args ) {
5+ Weapon defaultWeapon = new Fists ();
56 ArrayList <Character > characters = new ArrayList <>();
67 /*
78 Adding Your Character to the Arena, by Avery
@@ -34,12 +35,20 @@ public static void main(String[] args) {
3435 continue ;
3536 }
3637 int targetIndex = curr .pickTarget (characters );
37- if (targetIndex != i ) {
38- System .out .println (curr .getName () + " attacked " +
39- characters .get (targetIndex ).getName () +
40- " with their " + curr .getEquippedWeapon ().getName ());
38+ if (targetIndex == -1 ) {
39+ System .out .println (curr .getName () + "'s attack failed because" +
40+ " it couldn't find a good target." );
41+ continue ;
42+ }
43+ Character target = characters .get (targetIndex );
44+ Weapon weapon = curr .getEquippedWeapon ();
45+ if (weapon == null ) {
46+ weapon = defaultWeapon ;
4147 }
42- curr .getEquippedWeapon ().useOn (characters .get (targetIndex ));
48+ System .out .println (curr .getName () + " attacks " +
49+ target .getName () +
50+ " with their " + weapon .getName ());
51+ weapon .useOn (target );
4352 }
4453 System .out .println (characters .get (0 ).getName () + " won!" );
4554 }
You can’t perform that action at this time.
0 commit comments