-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacters.cpp
167 lines (152 loc) · 5.95 KB
/
characters.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "characters.h"
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <QElapsedTimer>
#include <cstdlib>
#include <algorithm>
coordinates::coordinates(int x, int y){
this->x = x;
this->y = y;
}
bool operator ==(const coordinates &lhs, const coordinates &rhs){
if ((lhs.x == rhs.x)&&(lhs.y == rhs.y)){
return 1;
}
else {
return 0;
}
}
/// aus ConsoleDemo:
int rrnd(int min_value, int max_value)
{
double v = std::rand();
v /= (static_cast<double>(RAND_MAX) + 1.0);
v *= (max_value - min_value);
v += min_value;
return static_cast<int>(v);
}
//Direction-Randomizer
move_direction rrndDirection(){
int random = rrnd(0,3);
switch(random){
case 0:
return directionUp;
case 1:
return directionDown;
case 2:
return directionLeft;
case 3:
return directionRight;
default:
return directionUp;
}
}
Pacman::Pacman(){
points = 0;
direction = directionNone;
};
void Meeple::SetPosition(int x, int y)
{
position = coordinates(x,y);
}
void Pacman::EatPoint()
{
points++;
}
coordinates Pacman::Move(const char& pressedKey, const std::vector<move_direction> &possibleDirections)
{
// Ansatz um das Drücken mehrer Tasten zu unterstützen(für Kurven) momentan bleibt Pacman aber stehen wenn man eine Taste loslässt
bool up = false;
bool down= false;
bool left= false;
bool right= false;
for (move_direction direction : possibleDirections){
if (direction == directionUp){up = true;}
if (direction == directionDown){down = true;}
if (direction == directionLeft){left = true;}
if (direction == directionRight){right = true;}
}
coordinates movingtoPosition = this->position;
if (pressedKey == 3){
if (up) { movingtoPosition = coordinates(movingtoPosition.x, (movingtoPosition.y - 1));}}
if (pressedKey == 4){
if (down){movingtoPosition = coordinates(movingtoPosition.x, (movingtoPosition.y + 1));}}
if (pressedKey == 1){
if (left) {movingtoPosition = coordinates((movingtoPosition.x - 1), movingtoPosition.y);}}
if (pressedKey == 2){
if (right){movingtoPosition = coordinates((movingtoPosition.x + 1), movingtoPosition.y);}}
return movingtoPosition;
}
StupidGhost::StupidGhost(int x, int y){
position = coordinates(x,y);
isParkedOnDot = true;
symbol = 'g';
isActive = true;
}
NotAsStupidGhost::NotAsStupidGhost(int x, int y)
{
position = coordinates(x,y);
isParkedOnDot = true;
everySecondMove = 0;
symbol = 'G';
isActive = true;
}
coordinates StupidGhost::Move(const std::vector<move_direction> &possibleDirections, Pacman &pacman, const bool& pillActive)
{
direction = possibleDirections[rrnd(0,possibleDirections.size())];
coordinates movingtoPosition = this->position;
if (direction == directionUp){
movingtoPosition = coordinates(this->position.x, (this->position.y - 1));}
if (direction == directionDown){
movingtoPosition = coordinates(this->position.x, (this->position.y + 1));}
if (direction == directionLeft){
movingtoPosition = coordinates((this->position.x - 1), this->position.y);}
if (direction == directionRight){
movingtoPosition = coordinates((this->position.x + 1), this->position.y);}
return movingtoPosition;
}
coordinates NotAsStupidGhost::Move(const std::vector< move_direction >& possibleDirections, Pacman& pacman, const bool& pillActive)
{
coordinates movingtoPosition = this->position;
int up_down = 0;
int left_right = 0;
bool done = false;
move_direction move = directionNone;
std::vector<move_direction> closerOrAwayFromPackman;
if (everySecondMove == 0){
everySecondMove++;
up_down = pacman.position.y - this->position.y;
left_right = pacman.position.x - this->position.x;
if (!pillActive){
if (left_right < 0) {closerOrAwayFromPackman.push_back(directionLeft);}
else if (left_right > 0) {closerOrAwayFromPackman.push_back(directionRight);}
if (up_down < 0) {closerOrAwayFromPackman.push_back(directionUp);}
else if (up_down > 0) {closerOrAwayFromPackman.push_back(directionDown);}
}
else if (pillActive){
if (left_right > 0) {closerOrAwayFromPackman.push_back(directionLeft);}
else if (left_right < 0) {closerOrAwayFromPackman.push_back(directionRight);}
else if (left_right == 0) {closerOrAwayFromPackman.push_back(directionRight);closerOrAwayFromPackman.push_back(directionLeft);}
if (up_down > 0) {closerOrAwayFromPackman.push_back(directionUp);}
else if (up_down < 0) {closerOrAwayFromPackman.push_back(directionDown);}
else if (up_down == 0) {closerOrAwayFromPackman.push_back(directionDown);closerOrAwayFromPackman.push_back(directionUp);}
}
for (move_direction direction : closerOrAwayFromPackman){
for (move_direction possible : possibleDirections){
if (direction == possible){move = direction;done = true;break;}
if (done) {break;}
}
}
}
else {
everySecondMove++;
if (everySecondMove == 2){ everySecondMove = 0; }
}
if (move == directionLeft) {movingtoPosition = coordinates(this->position.x-1, this->position.y);}
if (move == directionRight) {movingtoPosition = coordinates(this->position.x+1, this->position.y);}
if (move == directionUp) {movingtoPosition = coordinates(this->position.x, this->position.y-1);}
if (move == directionDown) {movingtoPosition = coordinates(this->position.x, this->position.y+1);}
return movingtoPosition;
}