Skip to content

Commit 2942a3c

Browse files
authored
Add files via upload
1 parent 1487407 commit 2942a3c

File tree

3 files changed

+294
-0
lines changed

3 files changed

+294
-0
lines changed

SoldierGame/Header.h

+217
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
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+

SoldierGame/Readme.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## **FULLNAME:**
2+
3+
Phạm Huy Cường Thịnh
4+
5+
## **STUDENT'S ID:**
6+
7+
20127335
8+
9+
## **ANY NOTICE ON HOW TO COMPILE AND RUN MY PROJECT ?**
10+
11+
- [x] Compiler: g++ 11.2.0 version
12+
13+
- [x] Text editor: Visual Studio Code
14+
15+
- [x] Operating system: Windows 11 version 21H2
16+
17+
- [x] C++ version: C++20
18+
19+
I also compiled and run my project on Visual Studio 2022 version and work perfectly.
20+
21+
## **WHAT I HAVE DONE:**
22+
23+
`READING TIME: 3 MIN READ`
24+
25+
I have created 17 classes for this homework. I used visitor design pattern and observer design pattern to handle this homework.
26+
27+
<img src="https://integu.net/wp-content/uploads/2020/11/INTEGU-visitor-design-pattern-overview-.png" width="300" height="180" /> <img src="https://laptrinhvb.net/uploads/source/vbnet/0umqa0oz6wf95h6aza4j.jpeg" width="300" height="180" />
28+
29+
- I created a **Character** class which is the abstract class of all characters and 3 inherited classes: **Swordman**, **Knight**, **Spikeman**.
30+
31+
- The class **IVisitor** is the visit pattern. It is used to visit all characters and print their information.
32+
33+
- The class **State** is the abstract class of all states. It is used to change the state of characters. It also has 4 inherited classes: **NormalState**, **ThreeSwordmanState**, **ThreeKnightState**, **ThreeSpikemanState**.
34+
35+
- The next 6 classes are the observer design pattern. They are used to have some rules, create players, and start the game.
36+
37+
- **Player** is the observer class. It is used to create players.
38+
39+
- **Game** is the observer class. It is used to start the game.
40+
41+
- **Rules** is the observer and abstract class. It is used to have some rules. It has 3 inherited classes:**ThreeSwordmanRules**, **ThreeKnightRules**, **ThreeSpikemanRules**.
42+
43+
- The last class is **RandomIntegerGenerator**. It is used to generate random integers in order to random the characters and choose which player attacks first.
44+
45+
In the main function, I created a **Game** object to start the game. First I print all the information of 2 teams. After 3 turns, the game will end and I print all the result of the game.
46+
47+
## **WHAT I HAVE NOT DONE:**
48+
49+
None.
50+
51+
## **WHAT SHOULD BE TAKEN INTO ACCOUNT FOR A BONUS ?**
52+
53+
I have created attack functions in 4 characters in order to make player attack each other.
54+
55+
## **EXPECTED GRADES:**
56+
57+
10.0

SoldierGame/main.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "Header.h"
2+
3+
int main() {
4+
srand(time(NULL));
5+
6+
Game game;
7+
game.start();
8+
9+
cout << "Get ready:" << endl;
10+
cout << game.toString();
11+
12+
game.play();
13+
cout << endl << "After 3 turns," << endl;
14+
cout << game.toString();
15+
16+
game.toString();
17+
18+
cout << endl << "Press any key to exit . . .";
19+
cin.get();
20+
}

0 commit comments

Comments
 (0)