added lab14 ex1

This commit is contained in:
Louis Heredero 2024-11-11 10:44:58 +01:00
parent 64f7f56ffe
commit 23829388cb
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
11 changed files with 145 additions and 0 deletions

View File

@ -0,0 +1,8 @@
package lab14_strategy.ex1;
public class AxeBehavior implements WeaponBehavior {
@Override
public String useWeapon() {
return "chops with an axe";
}
}

View File

@ -0,0 +1,19 @@
package lab14_strategy.ex1;
public abstract class Character {
private WeaponBehavior weapon;
public Character(WeaponBehavior weapon) {
this.weapon = weapon;
}
public void setWeapon(WeaponBehavior weapon) {
this.weapon = weapon;
}
public abstract String getName();
public void fight() {
System.out.println(getName() + " " + weapon.useWeapon());
}
}

View File

@ -0,0 +1,25 @@
package lab14_strategy.ex1;
import lab14_strategy.ex1.characters.King;
import lab14_strategy.ex1.characters.Knight;
import lab14_strategy.ex1.characters.Troll;
import lab14_strategy.ex1.characters.Queen;
import lab14_strategy.ex1.weapons.BowAndArrowBehavior;
import lab14_strategy.ex1.weapons.KnifeBehavior;
public class Game {
public static void main(String[] args) {
Character king = new King(new KnifeBehavior());
Character queen = new Queen(new BowAndArrowBehavior());
Character knight = new Knight(new SwordBehavior());
Character troll = new Troll(new AxeBehavior());
king.fight();
queen.fight();
knight.fight();
troll.fight();
king.setWeapon(new SwordBehavior());
king.fight();
}
}

View File

@ -0,0 +1,8 @@
package lab14_strategy.ex1;
public class SwordBehavior implements WeaponBehavior {
@Override
public String useWeapon() {
return "swings a sword";
}
}

View File

@ -0,0 +1,5 @@
package lab14_strategy.ex1;
public interface WeaponBehavior {
String useWeapon();
}

View File

@ -0,0 +1,15 @@
package lab14_strategy.ex1.characters;
import lab14_strategy.ex1.Character;
import lab14_strategy.ex1.WeaponBehavior;
public class King extends Character {
public King(WeaponBehavior weapon) {
super(weapon);
}
@Override
public String getName() {
return "King";
}
}

View File

@ -0,0 +1,15 @@
package lab14_strategy.ex1.characters;
import lab14_strategy.ex1.Character;
import lab14_strategy.ex1.WeaponBehavior;
public class Knight extends Character {
public Knight(WeaponBehavior weapon) {
super(weapon);
}
@Override
public String getName() {
return "Knight";
}
}

View File

@ -0,0 +1,15 @@
package lab14_strategy.ex1.characters;
import lab14_strategy.ex1.Character;
import lab14_strategy.ex1.WeaponBehavior;
public class Queen extends Character {
public Queen(WeaponBehavior weapon) {
super(weapon);
}
@Override
public String getName() {
return "Queen";
}
}

View File

@ -0,0 +1,15 @@
package lab14_strategy.ex1.characters;
import lab14_strategy.ex1.Character;
import lab14_strategy.ex1.WeaponBehavior;
public class Troll extends Character {
public Troll(WeaponBehavior weapon) {
super(weapon);
}
@Override
public String getName() {
return "Troll";
}
}

View File

@ -0,0 +1,10 @@
package lab14_strategy.ex1.weapons;
import lab14_strategy.ex1.WeaponBehavior;
public class BowAndArrowBehavior implements WeaponBehavior {
@Override
public String useWeapon() {
return "shoots an arrow with a bow";
}
}

View File

@ -0,0 +1,10 @@
package lab14_strategy.ex1.weapons;
import lab14_strategy.ex1.WeaponBehavior;
public class KnifeBehavior implements WeaponBehavior {
@Override
public String useWeapon() {
return "cuts with a knife";
}
}