/
main.cpp
192 lines (180 loc) · 4.22 KB
/
main.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
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
#include <vector>
#include <cstdio>
#include <unistd.h>
const int MAP_HEIGHT = 8;
const int MAP_WIDTH = 7;
const char &get_map_value(int x, int y){
static const char map[MAP_HEIGHT][MAP_WIDTH] = {
{'.','x','.','.','.','.','.'},
{'.','x','.','.','x','x','.'},
{'.','.','.','.','.','x','.'},
{'.','.','.','.','.','x','x'},
{'.','x','.','x','.','x','.'},
{'x','x','.','x','.','x','.'},
{'.','x','.','x','.','x','.'},
{'.','.','.','.','.','.','.'}
};
return map[y][x];
}
struct player {
player(int posx, int posy, int dirx, int diry):dead(false),x(posx),y(posy),direction_x(dirx), direction_y(diry) {}
void act(){
if (dead)
return;
int local_dirx = direction_x;
int local_diry = direction_y;
int local_x = x;
int local_y = y;
if (direction_x != 0){
local_x += local_dirx;
if (check_blocked(local_x, y)){
local_x = x;
local_dirx = 0;
local_diry = 1;
local_y = y+local_diry;
if (check_blocked(x,local_y)){
local_diry = -1;
local_y = y+local_diry;
if (check_blocked(x,local_y)){
local_dirx = direction_x * -1;
local_x = x + local_dirx;
if (check_blocked(local_x,y)){
return;
} else {
x = local_x;
direction_x = local_dirx;
}
} else {
direction_x = 0;
direction_y = local_diry;
y = local_y;
}
} else {
direction_x = 0;
direction_y = local_diry;
y = local_y;
}
}
else {
x = local_x;
}
}
else if (direction_y != 0){
local_y += direction_y;
if (check_blocked(x, local_y)){
local_y = y;
local_diry = 0;
local_dirx = 1;
local_x = x+local_dirx;
if (check_blocked(local_x,y)){
local_dirx = -1;
local_x = x+local_dirx;
if (check_blocked(local_x,y)){
local_diry = direction_y * -1;
local_y = y + local_diry;
if (check_blocked(x,local_y)){
return;
} else {
y = local_y;
direction_y = local_diry;
}
} else {
direction_y = 0;
direction_x = local_dirx;
x = local_x;
}
} else {
direction_y = 0;
direction_x = local_dirx;
x = local_x;
}
} else {
y = local_y;
}
}
}
void reset_direction(){
direction_x = 0;
direction_y = 0;
}
bool check_blocked(int x, int y){
if (x >= MAP_WIDTH || x < 0){
return true;
} else if (y >= MAP_HEIGHT || y < 0){
return true;
} else if (get_map_value(x,y) == 'x'){
return true;
} else {
return false;
}
}
int x;
int y;
int direction_x;
int direction_y;
bool dead;
};
std::vector<player> players;
player* &get_sprite_ptr(int x, int y){
static player* sprite_map[MAP_HEIGHT][MAP_WIDTH] = { NULL };
return sprite_map[y][x];
}
void add_player(const player& p){
new player(p);
}
void draw_player(const player* p){
if (p->dead){
printf("%c ", 237);
}
else if (p->direction_y < 0){
printf("< ");
}
else if (p->direction_y > 0){
printf("> ");
}
else if (p->direction_x < 0){
printf("^ ");
}
else if (p->direction_x > 0){
printf("v ");
}
else {
printf("o ");
}
}
void build_world(){
for (int i = 0; i < MAP_WIDTH; ++i){
for (int j = 0; j < MAP_HEIGHT; ++j){
get_sprite_ptr(i,j) = NULL;
}
}
for (int i = 0; i < players.size(); ++i){
players[i].act();
get_sprite_ptr(players[i].x,players[i].y) = &players[i];
}
}
void draw_world(){
for (int i = 0; i < MAP_WIDTH; ++i){
for (int j = 0; j < MAP_HEIGHT; ++j){
if (NULL != get_sprite_ptr(i,j)){
draw_player(get_sprite_ptr(i,j));
} else {
printf("%c ", get_map_value(i,j));
}
}
printf("\n");
}
printf("\n");
}
int main(){
players.push_back(player(3,6,0,1));
players.push_back(player(4,0,1,-1));
players.push_back(player(3,0,0,1));
players.push_back(player(0,6,0,-1));
while (true){
build_world();
draw_world();
sleep(1);
}
return 0;
}