-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacman.cpp
More file actions
128 lines (97 loc) · 2.77 KB
/
Pacman.cpp
File metadata and controls
128 lines (97 loc) · 2.77 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
#include "Pacman.h"
bool Pacman::checkInit(const char curDir,const Board& board)const //check input
{
if (curDir == ESC)//if user pressed escape
{
do {
clear_screen();
gotoxy(40, 13);
std::cout << "Game paused, press ESC again to continue" << std::endl;
} while (_getch() != ESC);
clear_screen();
board.printBoard();
}
if (curDir == 'S' || curDir == 's')//if user press the stay key
return false;
gotoxy(0, 0);//print board from same starting place
return true;
}
bool Pacman::getDirectionPacman(char dir,const Board& board)
{
if ((dir <= 'Z' && dir >= 'A') || (dir <= 'z' && dir >= 'a')) //from small letters to capital letters
dir = toupper(dir);
switch (dir) { //switch on char value direction using enum
case UP: // up
key=Creature::UP;
break;
case DOWN: //down
key = Creature::DOWN;
break;
case LEFT: //left
key = Creature::LEFT;
break;
case RIGHT: //right
key = Creature::RIGHT;
break;
default:
return false;
}
checkCreatureObs(board,getDirection(board));
return true;
}
void Pacman::checkCreatureObs(const Board& board, int dir)
{
checkObstacle(board, dir);//check if pacman run into a wall
//check if its a secret pass
if ((coord.getY() == (board.getUp())) && (board.getMatGame(coord.getY(), coord.getX()) != '#'))
coord.setY(board.getDown());
else if (coord.getY() == (board.getDown()) && (board.getMatGame(coord.getY(), coord.getX()) != '#'))
coord.setY(board.getUp());
else if (coord.getX() == (board.getLeft()-1) && (board.getMatGame(coord.getY(), coord.getX()) != '#'))
coord.setX(board.getRight());
else if (coord.getX() == (board.getRight()+1) && (board.getMatGame(coord.getY(), coord.getX()) != '#'))
coord.setX(board.getLeft());
}
void Pacman:: moveCreature(Board& board, std::ofstream& stepsFile) {
bool check = true, stay = false;
if (!_kbhit || check)
{
if (_kbhit()) // if we get input
{
if (!stay)
{
prevDir = curDir;
curDir = _getch();
}
else
{
stay = false;
curDir = prevDir;
}
}
erase(); // erase prev step
board.setMatGame(' ', getCurY(), getCurX());// update the chars table
check = getDirectionPacman(curDir, board);
if (statusCreature == SAVE)
if (check)
updateStepsFile(stepsFile, "Pacman",key);
if (!check) //if it is a "special" move
{
check = checkInit(curDir, board);
if (check)
curDir = prevDir;
else
{
stay = true;
if (statusCreature == SAVE)
stepsFile << "Pacman STAY ";
check = true;
}
}
hideCursor();
draw(figure, color);
}
if(statusCreature==SAVE)
if (curDir != UNDEFINED)
numPointOfLives++;
}