#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
const int M = 20;
const int N = 20;
const int S = M * N;
int dx[4] = {1,-1,0,0},dy[4] = {0,0,-1,1};
void print (const char* s, int color){
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | color);
printf(s); SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | 7);
}
int df;
struct node{
int x,y;
}q[N * N];
struct Node{
int x,y,z;
}p[N * N];
int rr;
bool st1;
bool st[N][N];
int zd = 100;
char s[N][M];
int dir,r;
void sx(){
r++;
for(int i = 1;i <= N * M;i++){
int l1 = rand();
l1 %= 4;
int a1 = rand() % 17 + 1;
if(l1 == 3&&s[1][a1] == ' ')q[r] = {1,a1};
else if(l1 == 2&&s[N - 3][a1] == ' ') q[r] = {N - 3,a1};
else if(l1 == 1&&s[a1][M - 3] == ' ') q[r] = {a1,M - 3};
else if(s[a1][1] == ' ')q[r] = {a1,1};
}
}
void cl(){
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coordScreen = {0, 0};
SetConsoleCursorPosition(hConsole, coordScreen);
}
int xl,yl;
void printScreen() {
cl();
for (int i = 0;i <= N - 1;i++)
for(int j = 0;j < M - 1;j++)
s[i][j] = ' ';
for (int i = 1;i <= N - 3;i++)s[0][i] = s[M - 2][i] = '-';
for(int j = 1;j <= M - 3;j++) s[j][0] = s[j][N - 2] = '|';
int a = xl + dx[dir],b = yl + dy[dir];
for(int i = 1;i <= r;i++) s[q[i].x][q[i].y] = '$';
for(int i = 1;i <= rr;i++) s[p[i].x][p[i].y] = '.';
if(st1&&a > 0 && a < N - 2 && b > 0 && b < M - 2) {
s[a][b] = '*';
xl = a;
yl = b;
}
else s[xl][yl] = '*';
for(int i = 0;i < N;i++) puts(s[i]);
printf("还剩%d颗子弹\n",zd);
}
void gj() {
for(int i = 1;i <= rr;i++){
int z = p[i].z;
int x = p[i].x + dx[z] , y = p[i].y + dy[z];
if(x > N - 2|| x < 1 || y > M - 2|| y < 1){
rr --;
for(int j = i;j <= rr;j++) p[j] = p[j + 1];
i--;
}
else if(s[x][y] == '$') {
rr --;
for(int j = i;j <= rr;j++) p[j] = p[j + 1];
i--;
st[x][y] = 1;
}
else{
p[i] = {x,y,z};
}
}
for(int i = 1;i <= r;i++){
int x = q[i].x,y = q[i].y;
if(st[x][y] == 1){
df++;
zd += 10;
r --;
for(int j = i;j <= r;j++) q[j] = q[j + 1];
i --;
st[x][y] = 0;
}
}
}
int px[N * M],py[N * M];
bool st2[N][M];
node d[N][M];
void bfs(){
memset(st2,0,sizeof st2);
int l = 1,r = 0;
r++;
px[r] = xl;
py[r] = yl;
d[xl][yl] = {xl,yl};
while(l <= r){
int xx = px[l],yy = py[l];
l++;
for(int i = 0;i < 4;i++){
int xxx = xx + dx[i],yyy = yy + dy[i];
if(xxx >= 1 && xxx < N - 1&&yyy >= 1 &&yyy < N - 1&&!st2[xxx][yyy]){
st2[xxx][yyy] = 1;
d[xxx][yyy] = {xx,yy};
r++;
px[r] = xxx;
py[r] = yyy;
}
}
}
}
bool yd(){
bfs();
for(int i = 1;i <= r;i++){
node aaa = d[q[i].x][q[i].y];
int x = aaa.x,y = aaa.y;
if(s[x][y] == '*'){
return 1;
}else if(s[x][y] == ' '){
q[i].x = x;
q[i].y = y;
}
}
return 0;
}
int main() {
xl = 10,yl = 10;
int tl = 0;
srand(time(0));
while (true) {
printScreen();
Sleep(10);
tl += 10;
st1 = 0;
if (kbhit()) {
char c = getch();
switch (c) {
case 'w':
dir = 1;
st1 = 1;
break;
case 'a':
dir = 2;
st1 = 1;
break;
case 's':
dir = 0;
st1 = 1;
break;
case 'd':
dir = 3;
st1 = 1;
break;
case ' ':
if(zd > 0){
p[++rr] = {xl,yl,dir};
zd--;
}
break;
}
}
if (tl >= 200) gj();
if (tl >= 1000) {
if(yd()){
printf("你死了,得分:%d\n",df);
return 0;
}
sx();
tl = 0;
}
}
return 0;
}