-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAvatar.cpp
145 lines (142 loc) · 3.32 KB
/
Avatar.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
#include "Avatar.h"
#include "Map.h"
Avatar::Avatar(int xBorder ,int yBorder){
x = 1;
y = 1;
this->xBorder = xBorder;
this->yBorder = yBorder;
map2dArray = NULL;
}
Avatar::~Avatar(){
}
void Avatar::move(const int KEY){
if(isKeyAllowed(KEY)){
switch(KEY){
case UP_W:
case UP_ARROW: moveUp();
break;
case DOWN_S:
case DOWN_ARROW: moveDown();
break;
case LEFT_A:
case LEFT_ARROW: moveLeft();
break;
case RIGHT_D:
case RIGHT_ARROW: moveRight();
break;
}
winCheck();
bombCheck();
}
}
void Avatar::winCheck(){
if((map2dArray[y])[x] == Map::BOMB){
ConsoleController::clearScreen();
cout << "You lose!";
exit(1);
}
}
void Avatar::bombCheck(){
if(x == xBorder-2 && y == yBorder-2){
ConsoleController::clearScreen();
printYouWin();
exit(1);
}
}
void Avatar::printYouWin(){
cout << " ## ## ### ## ## \n"
<< " ## ## ### ### ## ## \n"
<< " ## ## ## ## ## ## \n"
<< " #### ## ## ## ## \n"
<< " ## ## ## ## ## \n"
<< " ## ## ## ## ## \n"
<< " ## ## ## ## ## \n"
<< " ## ### ### ## ## \n"
<< " ## ### #### \n\n";
cout << "### ### #### #### ### \n"
<< " ## ## ## ## ### ## \n"
<< " ## #### ## ## ## ## ## \n"
<< " ## ## ## ## ## ## ## ## \n"
<< " ## ## ## ## ## ## ## ## \n"
<< " ## ## ## ## ## ## ## ## \n"
<< " ## ## ## ## ## ## ## ## \n"
<< " ### ### ## ## ### \n"
<< " # # #### ### #### \n";
}
void Avatar::moveUp(){
if(y != 0)
if(!isWallAt(Direction::TOP))
--y;
}
void Avatar::moveDown(){
bool condit = yBorder-1 != y;
if(condit)
if(!isWallAt(Direction::BOTTOM))
++y;
}
void Avatar::moveLeft(){
if(x != 0)
if(!isWallAt(Direction::LEFT))
--x;
}
void Avatar::moveRight(){
if(xBorder-1 != x)
if(!isWallAt(Direction::RIGHT))
++x;
}
bool Avatar::isKeyAllowed(const int KEY){
switch(KEY){
case UP_W:
case UP_ARROW:
case DOWN_S:
case DOWN_ARROW:
case LEFT_A:
case LEFT_ARROW:
case RIGHT_D:
case RIGHT_ARROW:
return true;
break;
default: return false;
break;
}
}
void Avatar::draw(){
ConsoleController::goTo(x, y);
cout << Avatar::ANDICATOR;
}
void Avatar::setMap(char** map2dArray){
this->map2dArray = map2dArray;
}
bool Avatar::isWallAt(Direction direction){
if(map2dArray != NULL){
switch (direction){
case Direction::TOP :{
if ((map2dArray[y-1])[x] == Map::WALL)
return true;
else
return false;
}
case Direction::BOTTOM :{
if ((map2dArray[y+1])[x] == Map::WALL)
return true;
else
return false;
}
case Direction::LEFT :{
if ((map2dArray[y])[x-1] == Map::WALL)
return true;
else
return false;
}
case Direction::RIGHT :{
if ((map2dArray[y])[x+1] == Map::WALL)
return true;
else
return false;
}
default: return false;
}
}else{
throw logic_error("didn't set map for avatar, use setMap()");
}
}