|
| 1 | +#ifndef HEADER_H |
| 2 | +#define HEADER_H |
| 3 | + |
| 4 | +#include <iostream> |
| 5 | +#include <string> |
| 6 | +#include <sstream> |
| 7 | +#include <vector> |
| 8 | +#include <cmath> |
| 9 | +#include <ctime> |
| 10 | + |
| 11 | +using namespace std; |
| 12 | + |
| 13 | +// Forward declaration |
| 14 | + |
| 15 | +// --- Types of soldier --- |
| 16 | +class Character; |
| 17 | +class Swordman; |
| 18 | +class Knight; |
| 19 | +class Spikeman; |
| 20 | + |
| 21 | +// --- Visitor design pattern --- |
| 22 | +class IVisitor; |
| 23 | + |
| 24 | +// --- State of soldier --- |
| 25 | +class State; |
| 26 | +class NormalState; |
| 27 | +class ThreeSwordmanState; |
| 28 | +class ThreeKnightState; |
| 29 | +class ThreeSpikemanState; |
| 30 | + |
| 31 | +// --- Observer design pattern (Game / Rule / Player) --- |
| 32 | +class Game; |
| 33 | +class Player; |
| 34 | +class Rule; |
| 35 | +class ThreeSwordmanRule; |
| 36 | +class ThreeKnightRule; |
| 37 | +class ThreeSpikemanRule; |
| 38 | + |
| 39 | +// --- Random Number --- |
| 40 | +class RandomIntegerGenerator; |
| 41 | + |
| 42 | + |
| 43 | +// Declaration |
| 44 | + |
| 45 | +class RandomIntegerGenerator { |
| 46 | +public: |
| 47 | + int next(); |
| 48 | + int next(int); |
| 49 | + int next(int, int); |
| 50 | +}; |
| 51 | + |
| 52 | +class Character { |
| 53 | +protected: |
| 54 | + int _hp; |
| 55 | + int _atk; |
| 56 | + State* _state; |
| 57 | +public: |
| 58 | + Character(int hp, int atk); |
| 59 | + |
| 60 | + int hp(); |
| 61 | + int atk(); |
| 62 | + |
| 63 | + void setState(State*); |
| 64 | + void setHp(int); |
| 65 | + void setAtk(int); |
| 66 | + bool isAlive(); |
| 67 | + |
| 68 | + virtual void attack(Character* opponent) = 0; |
| 69 | + virtual string toString() = 0; |
| 70 | +}; |
| 71 | + |
| 72 | +class IVisitor { |
| 73 | +public: |
| 74 | + virtual void attack(Swordman&) = 0; |
| 75 | + virtual void attack(Knight&) = 0; |
| 76 | + virtual void attack(Spikeman&) = 0; |
| 77 | + |
| 78 | + virtual void hitBy(Swordman&) = 0; |
| 79 | + virtual void hitBy(Knight&) = 0; |
| 80 | + virtual void hitBy(Spikeman&) = 0; |
| 81 | + |
| 82 | + void select(Character*); |
| 83 | +}; |
| 84 | + |
| 85 | +class Swordman : public Character, public IVisitor { |
| 86 | +public: |
| 87 | + Swordman(int, int); |
| 88 | + |
| 89 | + string toString(); |
| 90 | + |
| 91 | + void attack(Swordman&); |
| 92 | + void attack(Knight&); |
| 93 | + void attack(Spikeman&); |
| 94 | + void attack(Character*); |
| 95 | + |
| 96 | + void hitBy(Swordman&); |
| 97 | + void hitBy(Knight&); |
| 98 | + void hitBy(Spikeman&); |
| 99 | + |
| 100 | +}; |
| 101 | + |
| 102 | +class Knight : public Character, public IVisitor { |
| 103 | +public: |
| 104 | + Knight(int, int); |
| 105 | + |
| 106 | + string toString(); |
| 107 | + |
| 108 | + void attack(Swordman&); |
| 109 | + void attack(Knight&); |
| 110 | + void attack(Spikeman&); |
| 111 | + void attack(Character*); |
| 112 | + |
| 113 | + void hitBy(Swordman&); |
| 114 | + void hitBy(Knight&); |
| 115 | + void hitBy(Spikeman&); |
| 116 | +}; |
| 117 | + |
| 118 | +class Spikeman : public Character, public IVisitor { |
| 119 | +public: |
| 120 | + Spikeman(int, int); |
| 121 | + |
| 122 | + string toString(); |
| 123 | + |
| 124 | + void attack(Swordman&); |
| 125 | + void attack(Knight&); |
| 126 | + void attack(Spikeman&); |
| 127 | + void attack(Character*); |
| 128 | + |
| 129 | + void hitBy(Swordman&); |
| 130 | + void hitBy(Knight&); |
| 131 | + void hitBy(Spikeman&); |
| 132 | +}; |
| 133 | + |
| 134 | +class State { |
| 135 | +public: |
| 136 | + virtual int transfromHp(int old) = 0; |
| 137 | + virtual int transfromAtk(int old) = 0; |
| 138 | +}; |
| 139 | + |
| 140 | +class NormalState : public State { |
| 141 | +public: |
| 142 | + int transfromHp(int old); |
| 143 | + int transfromAtk(int old); |
| 144 | +}; |
| 145 | + |
| 146 | +class ThreeSwordmanState : public State { |
| 147 | +public: |
| 148 | + int transfromHp(int old); |
| 149 | + int transfromAtk(int old); |
| 150 | +}; |
| 151 | + |
| 152 | +class ThreeKnightState : public State { |
| 153 | +public: |
| 154 | + int transfromHp(int old); |
| 155 | + int transfromAtk(int old); |
| 156 | +}; |
| 157 | + |
| 158 | +class ThreeSpikemanState : public State { |
| 159 | +public: |
| 160 | + int transfromHp(int old); |
| 161 | + int transfromAtk(int old); |
| 162 | +}; |
| 163 | + |
| 164 | +class Player { |
| 165 | +public: |
| 166 | + vector<Character*> _pieces; |
| 167 | + |
| 168 | + typedef void (Game::* checkCallBack) (Player*); |
| 169 | + checkCallBack checker = NULL; |
| 170 | +public: |
| 171 | + void add(Character*); |
| 172 | + string toString(); |
| 173 | +}; |
| 174 | + |
| 175 | +class Game { |
| 176 | +private: |
| 177 | + Player _blueTeam; |
| 178 | + Player _redTeam; |
| 179 | + |
| 180 | + vector<Rule*> _rules; |
| 181 | +public: |
| 182 | + Game(); |
| 183 | + void start(); |
| 184 | + void play(); |
| 185 | + //string attack(Player&, Player&); |
| 186 | + |
| 187 | + void checkRule(Player*); |
| 188 | + void apply(Player*); |
| 189 | + string toString(); |
| 190 | +}; |
| 191 | + |
| 192 | +class Rule { |
| 193 | +public: |
| 194 | + virtual bool satisfyWith(Player*) = 0; |
| 195 | + virtual void apply(Player*) = 0; |
| 196 | +}; |
| 197 | + |
| 198 | +class ThreeSwordmanRule : public Rule { |
| 199 | +public: |
| 200 | + bool satisfyWith(Player*); |
| 201 | + void apply(Player*); |
| 202 | +}; |
| 203 | + |
| 204 | +class ThreeKnightRule : public Rule { |
| 205 | +public: |
| 206 | + bool satisfyWith(Player*); |
| 207 | + void apply(Player*); |
| 208 | +}; |
| 209 | + |
| 210 | +class ThreeSpikemanRule : public Rule { |
| 211 | +public: |
| 212 | + bool satisfyWith(Player*); |
| 213 | + void apply(Player*); |
| 214 | +}; |
| 215 | + |
| 216 | +#endif |
| 217 | + |
0 commit comments