From 23829388cb189b58bdac7a8bc2e7077e8e8ebe47 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Mon, 11 Nov 2024 10:44:58 +0100 Subject: [PATCH] added lab14 ex1 --- src/lab14_strategy/ex1/AxeBehavior.java | 8 ++++++ src/lab14_strategy/ex1/Character.java | 19 ++++++++++++++ src/lab14_strategy/ex1/Game.java | 25 +++++++++++++++++++ src/lab14_strategy/ex1/SwordBehavior.java | 8 ++++++ src/lab14_strategy/ex1/WeaponBehavior.java | 5 ++++ src/lab14_strategy/ex1/characters/King.java | 15 +++++++++++ src/lab14_strategy/ex1/characters/Knight.java | 15 +++++++++++ src/lab14_strategy/ex1/characters/Queen.java | 15 +++++++++++ src/lab14_strategy/ex1/characters/Troll.java | 15 +++++++++++ .../ex1/weapons/BowAndArrowBehavior.java | 10 ++++++++ .../ex1/weapons/KnifeBehavior.java | 10 ++++++++ 11 files changed, 145 insertions(+) create mode 100644 src/lab14_strategy/ex1/AxeBehavior.java create mode 100644 src/lab14_strategy/ex1/Character.java create mode 100644 src/lab14_strategy/ex1/Game.java create mode 100644 src/lab14_strategy/ex1/SwordBehavior.java create mode 100644 src/lab14_strategy/ex1/WeaponBehavior.java create mode 100644 src/lab14_strategy/ex1/characters/King.java create mode 100644 src/lab14_strategy/ex1/characters/Knight.java create mode 100644 src/lab14_strategy/ex1/characters/Queen.java create mode 100644 src/lab14_strategy/ex1/characters/Troll.java create mode 100644 src/lab14_strategy/ex1/weapons/BowAndArrowBehavior.java create mode 100644 src/lab14_strategy/ex1/weapons/KnifeBehavior.java diff --git a/src/lab14_strategy/ex1/AxeBehavior.java b/src/lab14_strategy/ex1/AxeBehavior.java new file mode 100644 index 0000000..4ca3832 --- /dev/null +++ b/src/lab14_strategy/ex1/AxeBehavior.java @@ -0,0 +1,8 @@ +package lab14_strategy.ex1; + +public class AxeBehavior implements WeaponBehavior { + @Override + public String useWeapon() { + return "chops with an axe"; + } +} diff --git a/src/lab14_strategy/ex1/Character.java b/src/lab14_strategy/ex1/Character.java new file mode 100644 index 0000000..3b2213f --- /dev/null +++ b/src/lab14_strategy/ex1/Character.java @@ -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()); + } +} diff --git a/src/lab14_strategy/ex1/Game.java b/src/lab14_strategy/ex1/Game.java new file mode 100644 index 0000000..7afc2d4 --- /dev/null +++ b/src/lab14_strategy/ex1/Game.java @@ -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(); + } +} diff --git a/src/lab14_strategy/ex1/SwordBehavior.java b/src/lab14_strategy/ex1/SwordBehavior.java new file mode 100644 index 0000000..3d17937 --- /dev/null +++ b/src/lab14_strategy/ex1/SwordBehavior.java @@ -0,0 +1,8 @@ +package lab14_strategy.ex1; + +public class SwordBehavior implements WeaponBehavior { + @Override + public String useWeapon() { + return "swings a sword"; + } +} diff --git a/src/lab14_strategy/ex1/WeaponBehavior.java b/src/lab14_strategy/ex1/WeaponBehavior.java new file mode 100644 index 0000000..b952bb4 --- /dev/null +++ b/src/lab14_strategy/ex1/WeaponBehavior.java @@ -0,0 +1,5 @@ +package lab14_strategy.ex1; + +public interface WeaponBehavior { + String useWeapon(); +} diff --git a/src/lab14_strategy/ex1/characters/King.java b/src/lab14_strategy/ex1/characters/King.java new file mode 100644 index 0000000..7a60487 --- /dev/null +++ b/src/lab14_strategy/ex1/characters/King.java @@ -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"; + } +} diff --git a/src/lab14_strategy/ex1/characters/Knight.java b/src/lab14_strategy/ex1/characters/Knight.java new file mode 100644 index 0000000..98e2045 --- /dev/null +++ b/src/lab14_strategy/ex1/characters/Knight.java @@ -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"; + } +} diff --git a/src/lab14_strategy/ex1/characters/Queen.java b/src/lab14_strategy/ex1/characters/Queen.java new file mode 100644 index 0000000..0a00fc5 --- /dev/null +++ b/src/lab14_strategy/ex1/characters/Queen.java @@ -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"; + } +} diff --git a/src/lab14_strategy/ex1/characters/Troll.java b/src/lab14_strategy/ex1/characters/Troll.java new file mode 100644 index 0000000..fa191c4 --- /dev/null +++ b/src/lab14_strategy/ex1/characters/Troll.java @@ -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"; + } +} diff --git a/src/lab14_strategy/ex1/weapons/BowAndArrowBehavior.java b/src/lab14_strategy/ex1/weapons/BowAndArrowBehavior.java new file mode 100644 index 0000000..b2d51cb --- /dev/null +++ b/src/lab14_strategy/ex1/weapons/BowAndArrowBehavior.java @@ -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"; + } +} diff --git a/src/lab14_strategy/ex1/weapons/KnifeBehavior.java b/src/lab14_strategy/ex1/weapons/KnifeBehavior.java new file mode 100644 index 0000000..a3f801c --- /dev/null +++ b/src/lab14_strategy/ex1/weapons/KnifeBehavior.java @@ -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"; + } +}