/* _ |_) | \ABBITHOLE Authored by abakh To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty. You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see . compile with -lncurses */ #include #include #include #include #include #include #include "config.h" #define UP 1 #define RIGHT 2 #define DOWN 4 #define LEFT 8 #define VISITED 16 #define CARROT 32 typedef signed char byte; typedef unsigned char bitbox; /* The Plan9 compiler can not handle VLAs */ #ifdef NO_VLA #define len 10 #define wid 20 #else int len,wid; #endif int py,px; chtype colors[6]={0}; typedef struct point{ int y; int x; } point; point MID(int y,int x,bitbox direction){//move in direction point pt = {y,x}; switch(direction){ case UP: --pt.y; return pt; case DOWN: ++pt.y; return pt; case LEFT: --pt.x; return pt; case RIGHT: ++pt.x; return pt; } return pt; } void rectangle(int sy,int sx){ for(int y=0;y<=len*2;++y){ mvaddch(sy+y,sx,ACS_VLINE); mvaddch(sy+y,sx+wid*2,ACS_VLINE); } for(int x=0;x<=wid*2;++x){ mvaddch(sy,sx+x,ACS_HLINE); mvaddch(sy+len*2,sx+x,ACS_HLINE); } mvaddch(sy,sx,ACS_ULCORNER); mvaddch(sy+len*2,sx,ACS_LLCORNER); mvaddch(sy,sx+wid*2,ACS_URCORNER); mvaddch(sy+len*2,sx+wid*2,ACS_LRCORNER); } void draw(int sy,int sx,bitbox board[len][wid]){ int y,x; bitbox d; chtype prnt; point pt; for(y=0;y : Scroll"); mvprintw(9,0,"Press a key to continue"); refresh(); while ( getch()==ERR ); erase(); } void gameplay(void){ erase(); mvprintw(0,0," _ "); mvprintw(1,0,"|_)"); mvprintw(2,0,"| \\ABBITHOLE"); attron(A_BOLD); mvprintw(3,0," **** THE GAMEPLAY ****"); attroff(A_BOLD); move(4,0); printw("Try to gather all the carrots in the maze\n"); printw("in the given time. The determining factors\n"); printw("are your choice of paths and the speed of\n "); printw("your fingers.\n"); refresh(); while ( getch()==ERR ); erase(); } void sigint_handler(int x){ endwin(); puts("Quit."); exit(x); } int main(int argc, char** argv){ bool autoset=0; signal(SIGINT,sigint_handler); #ifndef NO_VLA if(argc>3 || (argc==2 && !strcmp("help",argv[1])) ){ printf("Usage: %s [len wid]\n",argv[0]); return EXIT_FAILURE; } if(argc==2){ puts("Give both dimensions."); return EXIT_FAILURE; } if(argc==3){ bool lool = sscanf(argv[1],"%d",&len) && sscanf(argv[2],"%d",&wid); if(!lool){ puts("Invalid input."); return EXIT_FAILURE; } if(len<5 || wid<5 || len>800 || wid>800){ puts("At least one of your given dimensions is either too small or too big."); return EXIT_FAILURE; } } else{ autoset=1; } #endif initscr(); #ifndef NO_VLA if(autoset){ if((LINES-7)/2 < 10) len=10; else len=(LINES-7)/2; if((COLS-5)/2 < 20) wid=20; else wid=(COLS-5)/2; } #endif int carrot_count= (len*wid)/50; int carrots_found; time_t tstart , now, giventime=len*wid/5; srand(time(NULL)%UINT_MAX); point start={0,0}; bitbox board[len][wid]; int sy,sx; Start: tstart = time(NULL); carrots_found=0; initscr(); curs_set(0); noecho(); cbreak(); halfdelay(3); keypad(stdscr,1); if(has_colors()){ start_color(); use_default_colors(); init_pair(1,COLOR_BLUE,-1); init_pair(2,COLOR_GREEN,-1); init_pair(3,COLOR_YELLOW,-1); init_pair(4,COLOR_RED,-1); init_pair(5,COLOR_RED,COLOR_YELLOW); init_pair(6,COLOR_RED,COLOR_MAGENTA); for(byte b= 0;b<6;++b){ colors[b]=COLOR_PAIR(b+1); } } sy=sx=0; py=px=0; memset(board,0,len*wid); make_maze(board,start); carrotify(board,carrot_count); int input; while(1){ board[py][px] |= VISITED; if( board[py][px] & CARROT ){ ++carrots_found; board[py][px] &= ~CARROT; } now=time(NULL); erase(); mvprintw(sy+0,sx+0," _ "); mvprintw(sy+1,sx+0,"|_) Time left :%ld",giventime-(now-tstart)); mvprintw(sy+2,sx+0,"| \\ABBITHOLE Carrots left :%d",carrot_count-carrots_found); draw(sy+3,sx+0,board); refresh(); if(carrots_found==carrot_count || now-tstart == giventime){ flushinp(); break; } input = getch(); if( input==KEY_PPAGE && LINES< len+3){//the board starts in 3 sy+=10; if(sy>0) sy=0; } if( input==KEY_NPAGE && LINES< len+3){ sy-=10; if(sy< -(len+3) ) sy=-(len+3); } if( input=='<' && COLS< wid*2+1){ sx+=10; if(sx>0) sx=0; } if( input=='>' && COLS< wid*2+1){ sx-=10; if(sx< -(wid*2+1)) sx=-(wid*2+1); } if( input == KEY_F(2) ) gameplay(); if( input == KEY_F(1) || input=='?' ) help(); if( (input=='k' || input==KEY_UP) && py>0 && (board[py][px]&UP) ) --py; if( (input=='j' || input==KEY_DOWN) && py0 && (board[py][px]&LEFT) ) --px; if( (input=='l' || input==KEY_RIGHT) && px