#include
#include
#include
#include
#include
#include
#include
#include
#include
int c_long,c_high;
int cr[10000][3]; //cr[a][b],a表示位置,b表示类别,b为2时有四种取值,0是未访问,1是路,2是墙,3是空,4是围墙
int n,n_0,n3,n4;
int h,m,s;
int direct;
int Enter[2],Exit[2];
int last[2],new[2];
bool game_over,game_again;
WINDOW *win_wall;
WINDOW *win_title;
WINDOW *win_end;
void cr_creat(void);
int xor(int a,int b);
void road_show(void);
void road_wall(void);
void enter_exit(void);
void move_control(void);
void title(void);
void time_show(void);
void game_end(void);
int main()
{
initscr();
keypad(stdscr,TRUE); //curses接管键盘转义
noecho(); //输入不回显
curs_set(0);
begin:
game_over=false;
game_again=false;
n=1;
cr_creat();
road_wall();
road_show();
enter_exit();
title();
pthread_t time;
pthread_t control;
pthread_create(&time,NULL ,(void *)time_show,NULL );
pthread_create(&control,NULL ,(void *)move_control ,NULL );
pthread_join(control,NULL );
game_end();
if(game_again) goto begin;
endwin();
return 0;
}
void cr_creat(void)
{
int i,j;
struct winsize size; //获取界面大小
ioctl(STDIN_FILENO,TIOCGWINSZ,&size);
c_long=size.ws_col; //屏幕长
c_high=size.ws_row;
c_long=c_long-50;
win_wall=newwin(c_high, c_long, 0, 2);
wrefresh(win_wall);
for(j=0;j { for(i=0;i { cr[n][0]=j; cr[n][1]=i; if(i==0||i==c_long-1||j==c_high-1||j==0) cr[n][2]=4; else cr[n][2]=0; n++; } } n--; } void road_wall(void) { int n1,n2=0,jude1,jude2,jude3; srand((int)time(NULL)); do{ do { n1=1+rand()%(n+1); }while(cr[n1][2]!=n2); jude1=xor(cr[n1+1][2],cr[n1-1][2]); jude2=xor(cr[n1+c_long][2],cr[n1-c_long][2]); if(jude1==2||jude2==2) cr[n1][2]=3; else{ jude3=xor(jude1,jude2); if(jude3==0&&n2!=0) cr[n1][2]=3; else if(jude3==1||n2==0) cr[n1][2]=1; else cr[n1][2]=3; } if(cr[n1][2]==1) { cr[n1+1][2]=cr[n1+1][2]==0?2:cr[n1+1][2]; cr[n1-1][2]=cr[n1-1][2]==0?2:cr[n1-1][2]; cr[n1-c_long][2]=cr[n1-c_long][2]==0?2:cr[n1-c_long][2]; cr[n1+c_long][2]=cr[n1+c_long][2]==0?2:cr[n1+c_long][2]; } n2=2; int i; for(i=0,n_0=0;i<=n;i++) { if(cr[i][2]==2) n_0++; } }while(n_0!=0); } int xor(int a,int b) { int c; if((a==1&&b!=1)||(a!=1&&b==1)) c=1; else c=0; if(c==0) c=(a==1&&b==1)?2:0; return c; } void road_show() { int i; for(i=0;i<=n;i++) { if(cr[i][2]!=1) { wmove(win_wall,cr[i][0],cr[i][1]); waddch(win_wall,ACS_CKBOARD); } } refresh(); wrefresh(win_wall); } void enter_exit(void) { int i; for(i=0;i<2;i++) { do { n3=1+rand()%(n+1); }while(cr[n3][2]!=4||(cr[n3-1][2]!=1&&cr[n3+1][2]!=1&&cr[n3-c_long][2]!=1&&cr[n3+c_long][2]!=1)||n3==0||n3==c_long||n3==n||n3==n-c_long); if(i==0) { Exit[0]=cr[n3][0]; Exit[1]=cr[n3][1]; } if(i==1) { Enter[0]=cr[n3][0]; Enter[1]=cr[n3][1]; } } wmove(win_wall,Enter[0],Enter[1]); wprintw(win_wall,"I"); wmove(win_wall,Exit[0],Exit[1]); wprintw(win_wall, "O"); wrefresh(win_wall); refresh(); } void move_control(void) { n4=n3; while(!game_over) { direct=getch(); switch (direct) { case KEY_LEFT:n3--;break; case KEY_RIGHT:n3++;break; case KEY_UP:n3=n3-c_long;break; case KEY_DOWN:n3=n3+c_long;break; case 'q':game_over=true;break; case 'Q':game_over=true;break; default:break; } if(cr[n3][2]==1) { wmove(win_wall, cr[n4][0], cr[n4][1]); waddch(win_wall, ' '); wmove(win_wall, cr[n3][0], cr[n3][1]); waddch(win_wall,'#'); wrefresh(win_wall); n4=n3; } else if(cr[n3][2]==4&&Exit[0]==cr[n3][0]&&Exit[1]==cr[n3][1]) game_over=true; else n3=n4; } } void title(void) { win_title=newwin( c_high, 40,0,c_long+4); box(win_title, ACS_VLINE, ACS_HLINE); wmove(win_title, 10, 20); wprintw(win_title,"maze"); wmove(win_title, 50, 8); wprintw(win_title,"Input 'q' to end the game"); wrefresh(win_title); refresh(); } void time_show(void) { h=0; m=0; s=0; for(h=0;h<=24&&!game_over;h++) { for(m=0;m<=60&&!game_over;m++) { for(s=0;s<=60&&!game_over;s++) { wmove(win_title, 30, 15); wprintw(win_title, "time %d:%d:%d",h,m,s); wrefresh(win_title); refresh(); sleep(1); } } } } void game_end(void) { char c='p'; win_end=newwin(20,30,20,35 ); box(win_end, ACS_VLINE, ACS_HLINE); wmove(win_end, 5, 10); wprintw(win_end, "time %d:%d:%d",h,m,s); wmove(win_end, 10, 10); wprintw(win_end, "Try again?"); wmove(win_end, 15, 10); wprintw(win_end, "Y N"); wrefresh(win_end); do { c=getch(); }while(c!='Y'&&c!='y'&&c!='n'&&c!='N'); if(c=='y'||c=='Y') game_again=true; else game_again=false; delwin(win_wall); delwin(win_end); delwin(win_title); } 运行结果展示