-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
224 lines (188 loc) · 5.18 KB
/
Copy pathmain.cpp
File metadata and controls
224 lines (188 loc) · 5.18 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <iostream>
#include <cstring>
/**
* \file main.cpp
* \authors Thibault AMETTLER, Enzo BEDOS, Enzo COMBELONGE, Francesco LIMOSANI, Raoul MAOULIDA
* \date Janvier 13 2023
* \brief Projet C++ "Voleur/Policier" sur le modele de PacMan
*/
using namespace std;
//Variable d'environnement (taille et Ghosts)
/**
* @brief ROW
* @return Variable definissant la dimention sur l'axe X (le nombre de caractère sur une ligne)
*/
const int ROW = 20;
/**
* @brief COL
* @return Variable definissant la dimention sur l'axe Y (le nombre de caractère sur une colonne)
*/
const int COL = 30;
/**
* @brief GHOST_COUNT
* @return Variable definissant le nombre de fantome sur la carte
*/
const int GHOST_COUNT = 4;
// Carte du jeu avec des murs et des points
char Pacmap[ROW][COL];
// Map
/**
* @brief generateRandomMap
* @return Génère une carte de façon aléatoire qui a pour paramètre les dimentions de la carte Map[ROW][COL], où ROW et COL variable int defini au préalable.
*/
void generateRandomMap(char Pacmap[ROW][COL]) {
srand(time(0));
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
// Générer un nombre aléatoire entre 0 et 100
int r = rand() % 20;
if (r <= 1) {
Pacmap[i][j] = '#';
} else if (r >= 2) {
Pacmap[i][j] = ' ';
}
}
}
}
void ClearScreen()
{
cout << "\033[H\033[2J";
}// ClearScreen ()
//Propriété Pac-Man
struct PacMan {
int x, y;
char c;
};
//Génération d'un Pac-Man
PacMan pacman;
//Propriété d'un Ghost
struct Ghost {char carte;
int x, y;
char c;
};
//Génération de n Ghost (En fonction de la variable GHOST_COUNT)
Ghost ghosts[GHOST_COUNT];
int score = 0;
void setup() {
pacman.x = 1;
pacman.y = 1;
pacman.c = 'P';
for (int i = 0; i <= GHOST_COUNT; i++) {
ghosts[i].x = rand()% ROW + 1;
ghosts[i].y = rand() % COL + 1;
ghosts[i].c = 'G';
}
}
void moveGhosts(Ghost & ghost){
//int dx = rand() % 3 - 1;
//int dy = rand() % 3 - 1;
//
//int x = ghost.x + dx;
//int y = ghost.y + dy;
int x = 0;
int y = 0;
if ((ghost.x >= pacman.x)and(ghost.x -1 != '#')) {
x = ghost.x - 1;
} else if ((ghost.x <= pacman.x)and(ghost.x +1 != '#')){
x = ghost.x + 1;
}
if ((ghost.y >= pacman.y)and(ghost.y -1 != '#')) {
y = ghost.y - 1;
} else if ((ghost.y <= pacman.y)and(ghost.y +1 != '#')){
y = ghost.y + 1;
}
if (Pacmap[x][y] != '#'){
if (Pacmap[x][y] != 'G'){
Pacmap[ghost.y][ghost.x] = ' ';
ghost.x = x;
ghost.y = y;
}
}
}
//Affiche les Ghost sur la map
void drawGhosts(char Pacmap[ROW][COL]) {
for (int i = 0; i < GHOST_COUNT; i++) {
Ghost ghost = ghosts[i];
// Vérifier que le fantôme ne se trouve pas sur un mur
if (Pacmap[ghost.y][ghost.x] != '#') {
Pacmap[ghost.y][ghost.x] = ghost.c;
}
}
}
void placeGhostsRandomly(char Pacmap[ROW][COL]) {
for (int i = 0; i <= GHOST_COUNT; i++) {
Ghost ghost = ghosts[i];
while (true) {
// Générer des coordonnées aléatoires
ghost.x = rand() % COL;
ghost.y = rand() % ROW;
// Vérifier que la case n'est pas un mur
if (Pacmap[ghost.y][ghost.x] != '#') {
break;
}
}
}
}
void draw() {
cout << string (COL+2,'_')<< endl;
for (int i = 0; i < ROW; i++) {
cout <<'|';
for (int j = 0; j < COL; j++) {
if (i == pacman.y && j == pacman.x) {
cout << pacman.c;
} else {
cout << Pacmap[i][j];
}
}
cout <<'|';
cout << endl;
}
cout << string(COL+2,'_')<< COL << endl;
cout << "Score: " << score << endl;
}
// ----------------
int main() {
generateRandomMap(Pacmap);
placeGhostsRandomly(Pacmap);
setup();
while (true) {
drawGhosts(Pacmap);
ClearScreen();
draw();
string verif;
cin >> verif;
char input = verif[0];
if (input == 'z') {
pacman.y--;
} else if (input == 'q') {
pacman.x--;
} else if (input == 's') {
pacman.y++;
} else if (input == 'd') {
pacman.x++;
}
if (Pacmap[pacman.y][pacman.x] == '#' || pacman.x == -1 || pacman.y == COL || pacman.y == -1 || pacman.x == ROW) {
// Annuler le mouvement
if (input == 'z') {
pacman.y++;
} else if (input == 'q') {
pacman.x++;
} else if (input == 's') {
pacman.y--;
} else if (input == 'd') {
pacman.x--;
}
}
for (int i = 0; i < GHOST_COUNT; i++){
moveGhosts(ghosts[i]);
}
for (int i = 0; i < GHOST_COUNT; i++) {
Ghost ghost = ghosts[i];
if (ghost.x == pacman.x && ghost.y == pacman.y) {
cout << "Game Over" << endl;
return 0;
}
}
}
return 0;
}