diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
| commit | c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch) | |
| tree | 3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/cpp/war | |
add: graphs et rushs
Diffstat (limited to 'graphs/cpp/war')
| -rw-r--r-- | graphs/cpp/war/assassin.cc | 5 | ||||
| -rw-r--r-- | graphs/cpp/war/assassin.hh | 8 | ||||
| -rw-r--r-- | graphs/cpp/war/knight.cc | 5 | ||||
| -rw-r--r-- | graphs/cpp/war/knight.hh | 8 | ||||
| -rw-r--r-- | graphs/cpp/war/regiment.cc | 38 | ||||
| -rw-r--r-- | graphs/cpp/war/regiment.hh | 17 | ||||
| -rw-r--r-- | graphs/cpp/war/soldier.cc | 24 | ||||
| -rw-r--r-- | graphs/cpp/war/soldier.hh | 16 |
8 files changed, 121 insertions, 0 deletions
diff --git a/graphs/cpp/war/assassin.cc b/graphs/cpp/war/assassin.cc new file mode 100644 index 0000000..090ec8b --- /dev/null +++ b/graphs/cpp/war/assassin.cc @@ -0,0 +1,5 @@ +#include "assassin.hh" + +Assassin::Assassin() + : Soldier(10, 9, "Out of the shadows!") +{} diff --git a/graphs/cpp/war/assassin.hh b/graphs/cpp/war/assassin.hh new file mode 100644 index 0000000..548fd8b --- /dev/null +++ b/graphs/cpp/war/assassin.hh @@ -0,0 +1,8 @@ +#pragma once +#include "soldier.hh" + +class Assassin : public Soldier +{ +public: + Assassin(); +};
\ No newline at end of file diff --git a/graphs/cpp/war/knight.cc b/graphs/cpp/war/knight.cc new file mode 100644 index 0000000..be52105 --- /dev/null +++ b/graphs/cpp/war/knight.cc @@ -0,0 +1,5 @@ +#include "knight.hh" + +Knight::Knight() + : Soldier(20, 5, "Be quick or be dead!") +{} diff --git a/graphs/cpp/war/knight.hh b/graphs/cpp/war/knight.hh new file mode 100644 index 0000000..d4f2b5f --- /dev/null +++ b/graphs/cpp/war/knight.hh @@ -0,0 +1,8 @@ +#pragma once +#include "soldier.hh" + +class Knight : public Soldier +{ +public: + Knight(); +};
\ No newline at end of file diff --git a/graphs/cpp/war/regiment.cc b/graphs/cpp/war/regiment.cc new file mode 100644 index 0000000..3534ced --- /dev/null +++ b/graphs/cpp/war/regiment.cc @@ -0,0 +1,38 @@ +#include "regiment.hh" + +#include <vector> + +void Regiment::join_by(Regiment& r) +{ + if (r.soldiers_.empty()) + return; + std::vector<Soldier*> v{ r.soldiers_.rbegin(), r.soldiers_.rend() }; + while (!v.empty()) + { + this->soldiers_.push_back(v.back()); + v.pop_back(); + } + r.soldiers_.clear(); +} +size_t Regiment::count() const +{ + return soldiers_.size(); +} +void Regiment::add_soldier(Soldier* s) +{ + soldiers_.push_back(s); +} +void Regiment::print_state() const +{ + for (auto soldier : soldiers_) + { + soldier->print_state(); + } +} +void Regiment::scream() const +{ + for (auto soldier : soldiers_) + { + soldier->scream(); + } +}
\ No newline at end of file diff --git a/graphs/cpp/war/regiment.hh b/graphs/cpp/war/regiment.hh new file mode 100644 index 0000000..ac54953 --- /dev/null +++ b/graphs/cpp/war/regiment.hh @@ -0,0 +1,17 @@ +#pragma once +#include <vector> + +#include "soldier.hh" + +class Regiment +{ +public: + void join_by(Regiment& r); + size_t count() const; + void add_soldier(Soldier* s); + void print_state() const; + void scream() const; + +private: + std::vector<Soldier*> soldiers_; +};
\ No newline at end of file diff --git a/graphs/cpp/war/soldier.cc b/graphs/cpp/war/soldier.cc new file mode 100644 index 0000000..01dc57c --- /dev/null +++ b/graphs/cpp/war/soldier.cc @@ -0,0 +1,24 @@ +#include "soldier.hh" + +#include <iostream> + +Soldier::Soldier(int hp, int dmg, std::string scream) + : health_points_{ hp } + , damage_{ dmg } + , scream_{ scream } +{} + +void Soldier::print_state() const +{ + std::cout << "I have " << health_points_ << " health points.\n"; +} + +void Soldier::scream() const +{ + std::cout << scream_ << "\n"; +} + +void Soldier::attack(Soldier& s) +{ + s.health_points_ -= this->damage_; +} diff --git a/graphs/cpp/war/soldier.hh b/graphs/cpp/war/soldier.hh new file mode 100644 index 0000000..1406022 --- /dev/null +++ b/graphs/cpp/war/soldier.hh @@ -0,0 +1,16 @@ +#pragma once +#include <string> + +class Soldier +{ +public: + Soldier(int hp, int dmg, std::string scream); + void attack(Soldier& s); + void print_state() const; + virtual void scream() const; + +private: + int health_points_; + int damage_; + std::string scream_; +};
\ No newline at end of file |
