Skip to content

Commit 196a64d

Browse files
committed
Added Scatter motion, Added Task info and Declaration
1 parent 5cd9c4d commit 196a64d

23 files changed

+261
-118
lines changed

prohlaseni.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Prohlašuji, že tuto semestrální práci jsem vytvořil sám, odnikud nezkopíroval, a případné části kódu,
2+
které jsou vypůjčené z jiných zdrojů nebo knihovny, jsou řádně okomentované a označené,
3+
včetně uvedení zdroje.
4+
5+
Jakub Dobrý, dobryjak, 2018/2019 FIT ČVUT

src/Entities/Players/Ghost.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,10 @@ bool Ghost::follow(GameWorld &world) {
2020
else
2121
return false;
2222
}
23+
24+
bool Ghost::scatter(GameWorld &world) {
25+
if (checkTimerMove())
26+
return m_MotionScatter.chase(world, this);
27+
else
28+
return false;
29+
}

src/Entities/Players/Ghost.h

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "Player.h"
44
#include "PacMan.h"
55
#include "Heuristics/FollowMotion.h"
6+
#include "Heuristics/ScatterMotion.h"
67

78
#include <memory>
89

@@ -19,6 +20,8 @@ class Ghost : public Player {
1920

2021
virtual bool frightened(GameWorld &world) = 0;
2122

23+
bool scatter(GameWorld &world);
24+
2225
bool follow(GameWorld &world);
2326

2427
EntityType getType() const override;
@@ -27,4 +30,5 @@ class Ghost : public Player {
2730
std::shared_ptr<Motion> m_MotionBase;
2831
std::shared_ptr<Motion> m_MotionFrightened;
2932
FollowMotion m_MotionFollow = FollowMotion();
33+
ScatterMotion m_MotionScatter = ScatterMotion();
3034
};

src/Entities/Players/Heuristics/CompletelyRandomMotion.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "../../../Random.h"
55
#include <vector>
66

7-
// Completely random potion
7+
// Completely random motion
88
bool CompletelyRandomMotion::chase(GameWorld &world, Player *ghost) {
99
std::vector<std::shared_ptr<Entity>> directions;
1010

src/Entities/Players/Heuristics/FollowMotion.cpp

-55
Original file line numberDiff line numberDiff line change
@@ -18,58 +18,3 @@ bool FollowMotion::chase(GameWorld &world, Player *ghost) {
1818

1919
return true;
2020
}
21-
22-
Pos FollowMotion::findNextPos(const GameWorld &world, const Pos &from, const Pos &to) {
23-
if (from == to) {
24-
return from;
25-
}
26-
27-
// Get Cached entities, in map
28-
std::map<Pos, std::shared_ptr<Entity>> entities = world.getScreenMap();
29-
30-
31-
std::list<std::shared_ptr<TPosFind>> toCheck;
32-
toCheck.push_back(std::make_shared<TPosFind>(TPosFind(from, nullptr)));
33-
34-
std::set<Pos> seen;
35-
36-
Pos posLeft(-1, 0);
37-
Pos posUp(0, -1);
38-
Pos posRight(1, 0);
39-
Pos posDown(0, 1);
40-
41-
while (!toCheck.empty()) {
42-
std::shared_ptr<TPosFind> posCheck = toCheck.front();
43-
44-
if (seen.find(posCheck->m_Pos) == seen.end()) {
45-
if (posCheck->m_Pos == to) {
46-
std::shared_ptr<TPosFind> parent = posCheck;
47-
while (parent->m_PosFrom != nullptr && parent->m_PosFrom->m_PosFrom != nullptr) {
48-
parent = parent->m_PosFrom;
49-
}
50-
return parent->m_Pos;
51-
}
52-
53-
std::shared_ptr<Entity> entLeft = GameMap::getScreenAt(entities, posCheck->m_Pos + posLeft);
54-
if (entLeft->getType() != Entity::EBorder)
55-
toCheck.push_back(std::make_shared<TPosFind>(TPosFind(entLeft->getPos(), posCheck)));
56-
57-
std::shared_ptr<Entity> entUp = GameMap::getScreenAt(entities, posCheck->m_Pos + posUp);
58-
if (entUp->getType() != Entity::EBorder)
59-
toCheck.push_back(std::make_shared<TPosFind>(TPosFind(entUp->getPos(), posCheck)));
60-
61-
std::shared_ptr<Entity> entRight = GameMap::getScreenAt(entities, posCheck->m_Pos + posRight);
62-
if (entRight->getType() != Entity::EBorder)
63-
toCheck.push_back(std::make_shared<TPosFind>(TPosFind(entRight->getPos(), posCheck)));
64-
65-
std::shared_ptr<Entity> entDown = GameMap::getScreenAt(entities, posCheck->m_Pos + posDown);
66-
if (entDown->getType() != Entity::EBorder)
67-
toCheck.push_back(std::make_shared<TPosFind>(TPosFind(entDown->getPos(), posCheck)));
68-
69-
seen.emplace(posCheck->m_Pos);
70-
}
71-
toCheck.pop_front();
72-
}
73-
74-
return {0, 0};
75-
}

src/Entities/Players/Heuristics/FollowMotion.h

-9
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,4 @@
77
class FollowMotion : public Motion {
88
public:
99
bool chase(GameWorld &world, Player *ghost) override;
10-
11-
private:
12-
struct TPosFind {
13-
TPosFind(const Pos & pos, std::shared_ptr<TPosFind> from) :m_Pos(pos), m_PosFrom(std::move(from)) {};
14-
Pos m_Pos;
15-
std::shared_ptr<TPosFind> m_PosFrom = nullptr;
16-
};
17-
18-
static Pos findNextPos(const GameWorld &world, const Pos &from, const Pos &to);
1910
};
+58
Original file line numberDiff line numberDiff line change
@@ -1 +1,59 @@
11
#include "Motion.h"
2+
3+
#include "../../../Game/GameWorld.h"
4+
#include <set>
5+
6+
Pos Motion::findNextPos(const GameWorld &world, const Pos &from, const Pos &to) {
7+
if (from == to) {
8+
return from;
9+
}
10+
11+
// Get Cached entities, in map
12+
std::map<Pos, std::shared_ptr<Entity>> entities = world.getScreenMap();
13+
14+
15+
std::list<std::shared_ptr<TPosFind>> toCheck;
16+
toCheck.push_back(std::make_shared<TPosFind>(TPosFind(from, nullptr)));
17+
18+
std::set<Pos> seen;
19+
20+
Pos posLeft(-1, 0);
21+
Pos posUp(0, -1);
22+
Pos posRight(1, 0);
23+
Pos posDown(0, 1);
24+
25+
while (!toCheck.empty()) {
26+
std::shared_ptr<TPosFind> posCheck = toCheck.front();
27+
28+
if (seen.find(posCheck->m_Pos) == seen.end()) {
29+
if (posCheck->m_Pos == to) {
30+
std::shared_ptr<TPosFind> parent = posCheck;
31+
while (parent->m_PosFrom != nullptr && parent->m_PosFrom->m_PosFrom != nullptr) {
32+
parent = parent->m_PosFrom;
33+
}
34+
return parent->m_Pos;
35+
}
36+
37+
std::shared_ptr<Entity> entLeft = GameMap::getScreenAt(entities, posCheck->m_Pos + posLeft);
38+
if (entLeft->getType() != Entity::EBorder)
39+
toCheck.push_back(std::make_shared<TPosFind>(TPosFind(entLeft->getPos(), posCheck)));
40+
41+
std::shared_ptr<Entity> entUp = GameMap::getScreenAt(entities, posCheck->m_Pos + posUp);
42+
if (entUp->getType() != Entity::EBorder)
43+
toCheck.push_back(std::make_shared<TPosFind>(TPosFind(entUp->getPos(), posCheck)));
44+
45+
std::shared_ptr<Entity> entRight = GameMap::getScreenAt(entities, posCheck->m_Pos + posRight);
46+
if (entRight->getType() != Entity::EBorder)
47+
toCheck.push_back(std::make_shared<TPosFind>(TPosFind(entRight->getPos(), posCheck)));
48+
49+
std::shared_ptr<Entity> entDown = GameMap::getScreenAt(entities, posCheck->m_Pos + posDown);
50+
if (entDown->getType() != Entity::EBorder)
51+
toCheck.push_back(std::make_shared<TPosFind>(TPosFind(entDown->getPos(), posCheck)));
52+
53+
seen.emplace(posCheck->m_Pos);
54+
}
55+
toCheck.pop_front();
56+
}
57+
58+
return {0, 0};
59+
}

src/Entities/Players/Heuristics/Motion.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,14 @@ class GameWorld;
88

99
class Motion {
1010
public:
11-
virtual bool chase(GameWorld &world, Player * ghost) = 0;
11+
virtual bool chase(GameWorld &world, Player *ghost) = 0;
12+
13+
static Pos findNextPos(const GameWorld &world, const Pos &from, const Pos &to);
14+
15+
private:
16+
struct TPosFind {
17+
TPosFind(const Pos &pos, std::shared_ptr<TPosFind> from) : m_Pos(pos), m_PosFrom(std::move(from)) {};
18+
Pos m_Pos;
19+
std::shared_ptr<TPosFind> m_PosFrom = nullptr;
20+
};
1221
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "ScatterMotion.h"
2+
3+
#include "../../../Game/GameWorld.h"
4+
#include "../../../Random.h"
5+
#include <vector>
6+
7+
// Home motion
8+
bool ScatterMotion::chase(GameWorld &world, Player *ghost) {
9+
PacMan &pac = world.getPacMan();
10+
if (ghost->getPos() == pac.getPos()) {
11+
return false;
12+
}
13+
14+
const Pos &nextPos = findNextPos(world, ghost->getPos(), ghost->getDefaultPos());
15+
16+
if (nextPos != Pos(0, 0))
17+
ghost->setPos(nextPos);
18+
19+
return true;
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
#include "Motion.h"
4+
5+
class ScatterMotion : public Motion {
6+
public:
7+
bool chase(GameWorld &world, Player *ghost) override;
8+
};

src/Entities/Players/Player.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@ bool Player::checkTimerMove(int speed) {
4444
return true;
4545
}
4646
}
47+
48+
const Pos &Player::getDefaultPos() const {
49+
return m_DefaultPos;
50+
}

src/Entities/Players/Player.h

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class Player : public Entity {
1212

1313
const Pos &getVec() const;
1414

15+
const Pos &getDefaultPos() const;
16+
1517
Pos getDirection() const;
1618

1719
Timer<Timer_Type_Millisecond> & getTimer();

src/Modes/CrazyMode.cpp

+2-17
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,18 @@ bool CrazyMode::update(GameBoard & board) {
1414
}
1515
}
1616

17-
solveConflicts(board);
17+
solveConflictsDeath(board);
1818

1919
// Move PacMan
2020
if (board.getPacMan().move(board)) {
2121
shouldUpdate = true;
2222
}
2323

24-
solveConflicts(board);
24+
solveConflictsDeath(board);
2525

2626
return shouldUpdate;
2727
}
2828

29-
bool CrazyMode::solveConflicts(GameBoard & board) {
30-
for (auto &ghost : board.getGhosts()) {
31-
if (ghost->getPos() == board.getPacMan().getPos()) {
32-
board.removeLives(1);
33-
board.getPacMan().setVec({0, 0});
34-
board.getPacMan().resetPos();
35-
for (auto &ghostRes : board.getGhosts()) {
36-
ghostRes->resetPos();
37-
}
38-
return true;
39-
}
40-
}
41-
return false;
42-
}
43-
4429
std::string CrazyMode::print() const {
4530
return "Crazy";
4631
}

src/Modes/CrazyMode.h

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
class CrazyMode : public Mode {
66
public:
77
bool update(GameBoard & board) override;
8-
bool solveConflicts(GameBoard & board) override;
98
ModeType getType() const override;
109
std::string print() const override;
1110
};

src/Modes/InvincibleMode.cpp

+2-12
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,18 @@ bool InvincibleMode::update(GameBoard & board) {
1414
}
1515
}
1616

17-
solveConflicts(board);
17+
solveConflictsKill(board);
1818

1919
// Move PacMan
2020
if (board.getPacMan().move(board)) {
2121
shouldUpdate = true;
2222
}
2323

24-
solveConflicts(board);
24+
solveConflictsKill(board);
2525

2626
return shouldUpdate;
2727
}
2828

29-
bool InvincibleMode::solveConflicts(GameBoard & board) {
30-
for (auto &ghost : board.getGhosts()) {
31-
if (ghost->getPos() == board.getPacMan().getPos()) {
32-
ghost->setVec({0,0});
33-
ghost->resetPos();
34-
}
35-
}
36-
return false;
37-
}
38-
3929
std::string InvincibleMode::print() const {
4030
return "Invincible";
4131
}

src/Modes/InvincibleMode.h

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
class InvincibleMode : public Mode {
66
public:
77
bool update(GameBoard & board) override;
8-
bool solveConflicts(GameBoard & board) override;
98
ModeType getType() const override;
109
std::string print() const override;
1110
};

src/Modes/Mode.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -1 +1,29 @@
11
#include "Mode.h"
2+
3+
#include "../Game/GameBoard.h"
4+
5+
bool Mode::solveConflictsKill(GameBoard &board) {
6+
for (auto &ghost : board.getGhosts()) {
7+
if (ghost->getPos() == board.getPacMan().getPos()) {
8+
ghost->setVec({0, 0});
9+
ghost->resetPos();
10+
}
11+
}
12+
return false;
13+
}
14+
15+
16+
bool Mode::solveConflictsDeath(GameBoard &board) {
17+
for (auto &ghost : board.getGhosts()) {
18+
if (ghost->getPos() == board.getPacMan().getPos()) {
19+
board.removeLives(1);
20+
board.getPacMan().setVec({0, 0});
21+
board.getPacMan().resetPos();
22+
for (auto &ghostRes : board.getGhosts()) {
23+
ghostRes->resetPos();
24+
}
25+
return true;
26+
}
27+
}
28+
return false;
29+
}

src/Modes/Mode.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ class Mode {
99
enum ModeType {
1010
MNormal,
1111
MInvincible,
12-
MCrazy
12+
MCrazy,
13+
MScatter
1314
};
1415

1516
virtual bool update(GameBoard &board) = 0;
1617

17-
virtual bool solveConflicts(GameBoard &board) = 0;
18-
1918
virtual ModeType getType() const = 0;
2019

2120
virtual std::string print() const = 0;
21+
22+
virtual bool solveConflictsKill(GameBoard &board);
23+
24+
virtual bool solveConflictsDeath(GameBoard &board);
2225
};

0 commit comments

Comments
 (0)