forked from RuiSiang/color-block-rush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.cpp
178 lines (177 loc) · 4.56 KB
/
map.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
#include"common.h"
#include<fstream>
//#include<iostream>
void map::initialize(void)
{
std::string str_map_line;
std::string map_file_name;
map_file_name="./maps/default_";
map_file_name.push_back(map_selection+'0');
map_file_name+=".txt";
//std::cout<<map_file_name<<"\n";
std::ifstream fin;
fin.open(map_file_name.c_str());
int j=0;
while(fin>>str_map_line) {
//std::cout<<str_map_line<<"\n";
for(int i=0; i<18; i++) {
m.array[i][j]=str_map_line[i]-'0';
}
j++;
}
fin.close();
}
void map::set_bomb()
{
int randx, randy;
for(int i=0; i<18; i++) {
for(int j=0; j<18; j++) {
if(array[i][j]==6) {
array[i][j]=0;
}
}
}
for(int l=0; l<5; l++) {
randx=rand()%18;
randy=rand()%18;
while(!islegal(randx, randy)) {
randx=rand()%18;
randy=rand()%18;
}
array[randx][randy]=6;
}
}
void map::set_poison()
{
int randx, randy, randr;
int tempx, tempy, tempr;
for(int i=0; i<18; i++) {
for(int j=0; j<18; j++) {
if(array[i][j]==7) {
array[i][j]=0;
}
}
}
for(int l=0; l<2; l++) {
randx=rand()%18;
randy=rand()%18;
randr=rand()%10+10;
while(!islegal(randx, randy)) {
randx=rand()%18;
randy=rand()%18;
}
tempr=0;
tempx=randx;
tempy=randy;
int randdir[5][2]= {{0,-1 },{0,1 },{-1,0 },{1,0 },{0,0} };
while(tempr<=randr&&islegal(tempx, tempy)) {
int rnd;
rnd=rand()%4;
int justincase=0;
while(!islegal(tempx+randdir[rnd][0], tempy+randdir[rnd][1])&&justincase<20) {
rnd=rand()%4;
if(justincase==19) {
rnd=5;
}
justincase++;
}
array[tempx][tempy]=7;
tempx+=randdir[rnd][0];
tempy+=randdir[rnd][1];
tempr++;
}
}
}
bool map::islegal(int x,int y)
{
if(x<0||x>17||y<0||y>17) {
return 0;
}
if(m.array[x][y]!=0&&m.array[x][y]!=8) {
return 0;
}
return 1;
}
void map::operator+()
{
for(int i=0; i<18; i++) {
for(int j=0; j<18; j++)
if(array[i][j]==1||array[i][j]==2||array[i][j]==8) {
array[i][j] = 0;
}
}
array[_attacker.x][_attacker.y] = 1;
array[_defender.x][_defender.y] = 2;
int x_up=_attacker.x-1;
int y_up=_attacker.y;
while(x_up>=0&&(array[x_up][y_up]==0||array[x_up][y_up]==2)) { //¤W
if(array[x_up][y_up]==0) {
array[x_up][y_up]=8;
x_up--;
} else if(array[x_up][y_up]==2) {
x_up--;
} else {
break;
}
}
int x_down=_attacker.x+1;
int y_down=_attacker.y;
while(x_down<=17&&(array[x_down][y_down]==0||array[x_down][y_down]==2)) { //¤U
if(array[x_down][y_down]==0) {
array[x_down][y_down]=8;
x_down++;
} else if(array[x_down][y_down]==2) {
x_down++;
} else {
break;
}
}
int x_left=_attacker.x;
int y_left=_attacker.y-1;
while(y_left>=0&&(array[x_left][y_left]==0||array[x_left][y_left]==2)) { //¥ª
if(array[x_left][y_left]==0) {
array[x_left][y_left]=8;
y_left--;
} else if(array[x_left][y_left]==2) {
y_left--;
} else {
break;
}
}
int x_right=_attacker.x;
int y_right=_attacker.y+1;
while(y_right<=17&&(array[x_right][y_right]==0||array[x_right][y_right]==2)) { //¥k
if(array[x_right][y_right]==0) {
array[x_right][y_right]=8;
y_right++;
} else if(array[x_right][y_right]==2) {
y_right++;
} else {
break;
}
}
}
bool map::check_win()
{
if(islegal(_attacker.x, _attacker.y+1)==0&&islegal(_attacker.x, _attacker.y-1)==0&&islegal(_attacker.x+1, _attacker.y)==0&&islegal(_attacker.x-1, _attacker.y)==0) {
winner_is(4);
return 1;
} else if(istrapped(_defender.x, _defender.y+1)==0&&istrapped(_defender.x, _defender.y-1)==0&&istrapped(_defender.x+1, _defender.y)==0&&istrapped(_defender.x-1, _defender.y)==0) {
winner_is(1);
return 1;
} else {
return 0;
}
}
bool map::istrapped(int x, int y)
{
+m;
bool valid=1;
if(x<0||x>17||y<0||y>17) {
return 0;
}
if(m.array[x][y]!=0) {
return 0;
}
return 1;
}