#include"YWS2.h"
#include<time.h>
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<windows.h>
#include<malloc.h>
#define End 3
#define Player 2
#define Wall 1
#define Road 0
#define YN1 41
//41
#define YN2 19
//19
#define W (YN1*2+1)
#define L (YN2*2+1)
int map[L+2][W+2];
void gotoxy(HANDLE hOut, int x, int y){
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(hOut, pos);
}
void yws_action(char* p,int x,int y){
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
gotoxy(hOut,x,y);
printf("%s",p);
return;
}
void yDraw(int i,int j){
if(map[i][j]==Wall)
yws_action("█",(j-1)*2,i-1);
else if(map[i][j]==Road)
yws_action(" ",(j-1)*2,i-1);
else if(map[i][j]==Player)
yws_action("☆",(j-1)*2,i-1);
else
yws_action("出",(j-1)*2,i-1);
}
void f(int i,int j){
if(i!=0&&i!=L+1&&j!=0&&j!=W+1){
yDraw(i,j);
Sleep(11);
}
}
void create(int x,int y){
int c[4][2]={0,1,1,0,0,-1,-1,0}; //right down left up
int i,j,t;
for(i=0;i<4;i++){
j=rand()%4;
t=c[i][0],c[i][0]=c[j][0],c[j][0]=t;
t=c[i][1],c[i][1]=c[j][1],c[j][1]=t;
}
map[x][y]=Road;
f(x,y);
for(i=0;i<4;i++){
j = x+2*c[i][0];
t = y+2*c[i][1];
if(map[j][t]==Wall){
map[x+c[i][0]][y+c[i][1]]=Road;
f(x+c[i][0],y+c[i][1]);
create(j,t);
}}}
void mapcreate(){
int i,j;
for(i=0;i<=L+1;i++){
for(j=0;j<=W+1;j++){
if(i==0||i==L+1||j==0||j==W+1){
map[i][j]=Road;
yDraw(i,j);
}
else{
map[i][j]=Wall;
yDraw(i,j);
}}}
i= 2*(rand()%(L/2)+1);
j= 2*(rand()%(W/2)+1);
create(i,j);
}
void main(){
system(SYSTEMMODECON_EX COLS SYSTEMMODECON_EY LINES);
system("color f2");
char c;
int m,n,i,j;
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
START:
m=2,n=1;
srand(time(NULL));
mapcreate();
for(i=0;i<=L+1;i++){
map[i][0]=Wall;
f(i,0);
map[i][W+1]=Wall;
f(i,W+1);
}
for(j=0;j<=W+1;j++){
map[0][j]=Wall;
f(0,j);
map[L+1][j]=Wall;
f(L+1,j);
}
map[L-1][W]=End;
f(L-1,W);
map[m][n]=Player;
f(m,n);
for(i=1;i<L+1;i++){
for(j=1;j<W+1;j++){
yDraw(i,j);
}}
while(1){
if(m==L-1&&n==W){
goto START;
}
while(!kbhit());
do{
c=getch();
}while(c!='a'&&c!='s'&&c!='d'&&c!='w');
if(c == 'a'){
if(map[m][n-1]==Road||map[m][n-1]==End){
map[m][n]=Road;
yDraw(m,n);
n--;
}}
else if(c == 'd'){
if(map[m][n+1]==Road||map[m][n+1]==End){
map[m][n]=Road;
yDraw(m,n);
n++;
}}
else if(c == 'w'){
if(map[m-1][n]==Road||map[m-1][n]==End){
map[m][n]=Road;
yDraw(m,n);
m--;
}}
else{
if(map[m+1][n]==Road||map[m+1][n]==End){
map[m][n]=Road;
yDraw(m,n);
m++;
}}
map[m][n]=Player;
yDraw(m,n);
}
}