diff --git a/checkers.c b/checkers.c index ff93f30..0eb7424 100644 --- a/checkers.c +++ b/checkers.c @@ -18,6 +18,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t #include #include #include +#include #include "config.h" #define LIGHT -1 #define DARK 1 @@ -35,6 +36,7 @@ byte cy,cx;//selected(choosen) piece int dpt; byte game[8][8]; byte computer[2]={0,0}; +char sides[2]={'h','h'}; byte score[2];//set by header() bool endgame=false; byte jumpagainy , jumpagainx; @@ -374,7 +376,7 @@ double decide(byte side,byte depth,byte s){//s is the type of move, it doesn't s else{ if(nextturn==side) adv=decide(nextturn,depth,nexts); - else{ //best move is the one that gives least advantage to the opponet + else{ //best move is the one that gives least advantage to the opponent adv=decide(nextturn,depth-!fj,nexts); if(adv==WIN) adv=0; @@ -485,15 +487,15 @@ void gameplay(void){ move(4,0); printw("1) The game starts with each player having 12 men;\n"); printw(" men can only diagonally move forwards \n"); - printw(" (toward the opponet's side).\n\n"); - printw("2) Men can become kings by reaching the opponet's \n"); + printw(" (toward the opponent's side).\n\n"); + printw("2) Men can become kings by reaching the opponent's \n"); printw(" first rank; kings can diagonally move both forwards\n"); printw(" and backwards.\n\n"); - printw("3) Pieces can capture opponet's pieces by jumping over them\n"); + printw("3) Pieces can capture opponent's pieces by jumping over them\n"); printw(" also they can capture several pieces at once by doing a\n"); printw(" chain of jumps.\n\n"); printw("4) You have to do a jump if you can.\n\n"); - printw("5) A player wins when the opponet can't do a move e. g. \n"); + printw("5) A player wins when the opponent can't do a move e. g. \n"); printw(" all of their pieces are captured.\n\n"); refresh(); getch(); @@ -501,18 +503,43 @@ void gameplay(void){ } int main(int argc,char** argv){ dpt=4; - if(argc>2){ - printf("Usage: %s [AIpower]\n",argv[0]); - return EXIT_FAILURE; - } - if(argc==2){ - if(sscanf(argv[1],"%d",&dpt) && dpt<128 && dpt>0) - ; - else{ - puts("That should be a number from 1 to 127."); - return EXIT_FAILURE; + int opt; + bool sides_chosen=0,no_replay=0; + while( (opt= getopt(argc,argv,"hnp:1:2:"))!= -1 ){ + switch(opt){ + case '1': + case '2': + if(!strcmp("c",optarg) || !strcmp("h",optarg)){ + sides[opt-'1']=optarg[0]; + sides_chosen=1; + } + else{ + puts("That should be either h or c\n"); + return EXIT_FAILURE; + } + break; + case 'p': + if(sscanf(optarg,"%d",&dpt) && dpt<128 && dpt>0) + ; + else{ + puts("That should be a number from 1 to 127."); + return EXIT_FAILURE; + } + + break; + + case 'n': + no_replay=1; + break; + case 'h': + default: + printf("Usage: %s [options]\n -p ai power\n -1 type of player 1\n -2 type of player 2\n -h help\n -n dont ask for replay\n",argv[0]); + return EXIT_SUCCESS; + break; + } } + initscr(); #ifndef NO_MOUSE mousemask(ALL_MOUSE_EVENTS,NULL); @@ -521,28 +548,35 @@ int main(int argc,char** argv){ cbreak(); keypad(stdscr,1); int input ; - printw("Dark plays first.\nChoose type of the dark player(H/c)\n" ); - refresh(); - input=getch(); - if(input=='c'){ - computer[0]=dpt; - printw("Computer.\n"); - } - else{ - computer[0]=0; - printw("Human.\n"); - } - printw("Choose type of the bright player(h/C)\n"); - refresh(); - input=getch(); - if(input=='h'){ - computer[1]=0; - printw("Human.\n"); - } - else{ - computer[1]=dpt; - printw("Computer.\n"); + if(!sides_chosen){ + printw("Black plays first.\nChoose type of the black player(H/c)\n" ); + refresh(); + input=getch(); + if(input=='c'){ + computer[0]=dpt; + sides[0]='c'; + printw("Computer.\n"); + } + else{ + computer[0]=0; + sides[0]='h'; + printw("Human.\n"); + } + printw("Choose type of the white player(h/C)\n"); + refresh(); + input=getch(); + if(input=='h'){ + computer[1]=0; + sides[0]='h'; + printw("Human.\n"); + } + else{ + computer[1]=dpt; + sides[1]='c'; + printw("Computer.\n"); + } } + if(has_colors()){ start_color(); use_default_colors(); @@ -583,7 +617,11 @@ int main(int argc,char** argv){ else todraw=0; } - if(!score[0] || (turn==-1 && !fj && !can_move(-1))){ + if(score[0]==score[1] && !can_move(1) && !can_move(-1) && !forced_jump(1) && !forced_jump(-1)){ + result=0; + goto End; + } + else if(!score[0] || (turn==-1 && !fj && !can_move(-1))){ result=1; goto End; } @@ -598,11 +636,10 @@ int main(int argc,char** argv){ endgame= score[t]<=5 || score[!t]<=5; draw(3,0); refresh(); - while(computer[t]){ + while(sides[t]=='c'){ mvprintw(13,0,"Thinking..."); refresh(); - computer[t]=dpt+ (score[!t] != score[t]) + endgame; - decide(turn,computer[t],1); + decide(turn,dpt+(score[t]=0 && !kinged )){ goto Turn; } @@ -613,10 +650,10 @@ int main(int argc,char** argv){ header(); if(!(computer[0]||computer[1])){ if(t) - addstr(" Bright's turn"); + addstr(" White's turn"); else{ attron(COLOR_PAIR(1)); - addstr(" Dark's turn"); + addstr(" Black's turn"); attroff(COLOR_PAIR(1)); } } @@ -671,29 +708,36 @@ int main(int argc,char** argv){ move(13,0); switch(result){ case -1: - printw("The dark side has won the game."); + printw("Black side has won the game."); break; case 0: printw("Draw."); break; case 1: - printw("The bright side has won the game."); + printw("White side has won the game."); break; case 2: printw("You resigned."); } - printw(" Wanna rematch?(y/n)"); - curs_set(1); - input=getch(); - if(result==2){ - if (input=='Y' || input=='y') + if(!no_replay){ + printw(" Wanna rematch?(y/n)"); + refresh(); + curs_set(1); + input=getch(); + if(result==2){ + if (input=='Y' || input=='y') + goto Start; + } + else if(input!='n' && input!='N' && input!= 'q'){ + /*byte b=computer[0]; //switch sides, i don't know if it's necessary + computer[0]=computer[1]; + computer[1]=b;*/ goto Start; + } } - else if(input!='n' && input!='N' && input!= 'q'){ - /*byte b=computer[0]; //switch sides, i don't know if it's necessary - computer[0]=computer[1]; - computer[1]=b;*/ - goto Start; + else{ + printw("Press any key on your keyboard to continue."); + getch(); } endwin(); return EXIT_SUCCESS; diff --git a/fifteen.c b/fifteen.c index 1cd4c0a..12c067a 100644 --- a/fifteen.c +++ b/fifteen.c @@ -192,15 +192,23 @@ void gameplay(void){ int main(int argc, char** argv){ #ifndef NO_VLA size=4; - if(argc==2){ - if(!strcmp("help",argv[1])){ - printf("Usage: %s [size]\n",argv[0]); - return EXIT_SUCCESS; - } - size=atoi(argv[1]); - if(size<3 || size>7){ - fprintf(stderr,"3<=size<=7\n"); - return EXIT_FAILURE; + int opt; + bool no_replay=0; + while( (opt=getopt(argc,argv,"hns:"))!=-1){ + switch(opt){ + case 's': + size=atoi(optarg); + if(size<3 || size>7){ + fprintf(stderr,"3<=size<=7"); + } + break; + case 'n': + no_replay=1; + break; + case 'h': + default: + printf("Usage:%s [options]\n -s size\n -h help\n -n don't ask for replay\n",argv[0]); + break; } } #endif @@ -257,11 +265,19 @@ int main(int argc, char** argv){ slide_multi(board,py,px); } } - mvprintw(size+5,0,"You solved it! Wanna play again?(y/n)"); - curs_set(1); - input=getch(); - if(input != 'N' && input != 'n' && input != 'q') - goto Start; + mvprintw(size+5,0,"You solved it!"); + if(!no_replay){ + printw(" Wanna play again?(y/n)"); + refresh(); + curs_set(1); + input=getch(); + if(input != 'N' && input != 'n' && input != 'q') + goto Start; + } + else{ + printw(" Press any key on this computer's keyboard if you want to continue."); + getch(); + } endwin(); return EXIT_SUCCESS; } diff --git a/memoblocks.c b/memoblocks.c index 040cec8..dfb944c 100644 --- a/memoblocks.c +++ b/memoblocks.c @@ -19,13 +19,9 @@ You should have received a copy of the CC0 Public Domain Dedication along with t #include "config.h" typedef unsigned char ubyte; -/* The Plan9 compiler can not handle VLAs */ -#ifdef NO_VLA #define size 8 #define size2 16 -#else -byte size,size2;//size2 is there to avoid a lot of multiplications -#endif + byte py,px; byte fy,fx; //the first tile chtype colors[6]={0}; @@ -172,23 +168,7 @@ void gameplay(void){ getch(); erase(); } -int main(int argc, char** argv){ -#ifndef NO_VLA - size=8; -#endif - if(argc>=2){ -#ifndef NO_VLA - size=atoi(argv[1]); -#endif - if(size<3 || size>19){ - fprintf(stderr,"3<=size<=19\n"); - return EXIT_FAILURE; - } - if(!strcmp("help",argv[1])){ - printf("Usage: %s [size]\n",argv[0]); - return EXIT_SUCCESS; - } - } +int main(void){ signal(SIGINT,sigint_handler); srand(time(NULL)%UINT_MAX); initscr(); @@ -215,12 +195,7 @@ int main(int argc, char** argv){ } } } -#ifndef NO_VLA - else if(size>8)//big sizes depend on color display - size=8; - size2=size*2; -#endif - chtype board[size][size2]; + chtype board[size][size2]; bool show[size][size2]; int input; time_t tstart,now; @@ -267,11 +242,14 @@ int main(int argc, char** argv){ } now=time(NULL)-tstart; mvprintw(size+7,0,"Time spent: %d:%2d:%2d",now/3600,(now%3600)/60,now%60); - mvprintw(size+5,0,"You solved it! Wanna play again?(y/n)"); + mvprintw(size+5,0,"You solved it!"); + printw(" Wanna play again?(y/n)"); + refresh(); curs_set(1); input=getch(); if(input != 'N' && input != 'n' && input != 'q') goto Start; + endwin(); return EXIT_SUCCESS; } diff --git a/miketron.c b/miketron.c index 37f83d8..9da00fd 100644 --- a/miketron.c +++ b/miketron.c @@ -44,7 +44,7 @@ int usleep(long usec) { #endif #else -int len,wid; +int len=MINLEN,wid=MINWID; #endif//NO_VLA int py,px; @@ -296,7 +296,7 @@ void gameplay(void){ mvprintw(3,0," **** THE GAMEPLAY ****"); attroff(A_BOLD); move(4,0); - printw("You are controlling a strange vechile which can \n"); + printw("You are controlling a strange vehicle which can \n"); printw("survive explosions but cannot cross the trail it has\n"); printw("left behind. Keep it running as much as you can."); refresh(); @@ -309,48 +309,26 @@ void sigint_handler(int x){ puts("Quit."); exit(x); } -int main(int argc, char** argv){ +int main(void){ #ifndef NO_VLA - bool autoset=0; signal(SIGINT,sigint_handler); - 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(len500 || wid>500){ - 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){ - len=LINES-7; - if(lenMAXLEN) - len=MAXLEN; + len=LINES-7; + if(lenMAXLEN){ + len=MAXLEN; + } - wid=COLS-5; - if(widMAXWID) - wid=MAXWID; + wid=COLS-5; + if(widMAXWID){ + wid=MAXWID; } #endif srand(time(NULL)%UINT_MAX); @@ -360,6 +338,7 @@ int main(int argc, char** argv){ noecho(); cbreak(); keypad(stdscr,1); + if(has_colors()){ start_color(); use_default_colors(); diff --git a/mines.c b/mines.c index c9647de..8eeadd5 100644 --- a/mines.c +++ b/mines.c @@ -20,12 +20,15 @@ compile with -lncurses #include "config.h" #define FLAG 9 #define UNCLEAR 10 - +#define MINLEN 8 +#define MINWID 8 +#define MAXLEN 1000 +#define MAXWID 1000 #ifdef NO_VLA //The Plan9 compiler can not handle VLAs #define len 8 #define wid 8 #else -int len,wid; +int len=8,wid=8; #endif int py,px,flags; int untouched; @@ -221,40 +224,37 @@ void gameplay(void){ int main(int argc, char** argv){ signal(SIGINT,sigint_handler); #ifndef NO_VLA - if(argc>4 || (argc==2 && !strcmp("help",argv[1])) ){ - printf("Usage: %s [len wid [minescount]]\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>1000 || wid>1000){ - puts("At least one of your given dimensions is either too small or too big."); - return EXIT_FAILURE; - } - - } - else - len=wid=8; - if(argc==4){ - if( !sscanf(argv[3],"%d",&mscount)){ - puts("Invalid input."); - return EXIT_FAILURE; - } - if( mscount<5 || mscount>= len*wid){ - puts("Too few/many mines."); - return EXIT_FAILURE; + int opt; + while( (opt=getopt(argc,argv,"hnm:l:w:"))!=-1){ + switch(opt){ + case 'm': + mscount=atoi(optarg); + if(mscount<0 || mscount>len*wid){ + fprintf(stderr,"Too few/many mines.\n"); + } + break; + case 'l': + len=atoi(optarg); + if(lenMAXLEN){ + fprintf(stderr,"Length too high or low.\n"); + } + break; + case 'w': + wid=atoi(optarg); + if(widMAXWID){ + fprintf(stderr,"Width too high or low.\n"); + } + break; + case 'h': + default: + printf("Usage:%s [options]\n -l length\n -w width\n -m number of mines\n -h help\n",argv[0]); + return EXIT_FAILURE; + break; } } - else - mscount = len*wid/6; + if(!mscount){ + mscount=len*wid/6; + } #else mscount=len*wid/6; #endif @@ -313,23 +313,27 @@ int main(int argc, char** argv){ input = getch(); if( input==KEY_PPAGE && LINES< len+3){//the board starts in 3 sy+=10; - if(sy>0) + if(sy>0){ sy=0; + } } if( input==KEY_NPAGE && LINES< len+3){ sy-=10; - if(sy< -(len+3) ) + if(sy< -(len+3) ){ sy=-(len+3); + } } if( input=='<' && COLS< wid*2+1){ sx+=10; - if(sx>0) + if(sx>0){ sx=0; + } } if( input=='>' && COLS< wid*2+1){ sx-=10; - if(sx< -(wid*2+1)) + if(sx< -(wid*2+1)){ sx=-(wid*2+1); + } } if( input==KEY_F(1) || input=='?' ) help(); diff --git a/muncher.c b/muncher.c index f086362..329d33b 100644 --- a/muncher.c +++ b/muncher.c @@ -270,47 +270,20 @@ void sigint_handler(int x){ 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(len500 || wid>500){ - 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){ - len=LINES-7; - if(lenMAXLEN) - len=MAXLEN; + len=LINES-7; + if(lenMAXLEN) + len=MAXLEN; - wid=COLS-5; - if(widMAXWID) - wid=MAXWID; - } + wid=COLS-5; + if(widMAXWID) + wid=MAXWID; #endif srand(time(NULL)%UINT_MAX); byte board[len][wid]; diff --git a/pipes.c b/pipes.c index 06f65a8..729f979 100644 --- a/pipes.c +++ b/pipes.c @@ -33,12 +33,12 @@ You should have received a copy of the CC0 Public Domain Dedication along with t typedef unsigned char bitbox; /* The Plan9 compiler can not handle VLAs */ -#ifdef NO_VLA +//#ifdef NO_VLA who uses that len wid arguments for this one? removed them #define wid 20 #define len 14 -#else -int len,wid; -#endif +//#else +//int len,wid; +//#endif int py,px,fy,fx;//p: pointer f: fluid bitbox tocome[5]={0};//the row of pipes in the left side @@ -301,32 +301,6 @@ void gameplay(void){ } int main(int argc, char** argv){ 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>1000 || wid>1000){ - puts("At least one of your given dimensions is too small or too big."); - return EXIT_FAILURE; - } - - } - else{ - wid=20; - len=14; - } -#endif initscr(); #ifndef NO_MOUSE mousemask(ALL_MOUSE_EVENTS,NULL); diff --git a/rabbithole.c b/rabbithole.c index 597d334..17ccb37 100644 --- a/rabbithole.c +++ b/rabbithole.c @@ -178,46 +178,21 @@ void sigint_handler(int x){ puts("Quit."); exit(x); } -int main(int argc, char** argv){ - bool autoset=0; +int main(void){ 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; + if((LINES-7)/2 < 5){ + len=5; + } + else{ + len=(LINES-7)/2; + } + if((COLS-5)/2 < 20){ + wid=20; + } + else{ + wid=(COLS-5)/2; } #endif int carrot_count= (len*wid)/50; diff --git a/reversi.c b/reversi.c index 31ff16c..7c0a509 100644 --- a/reversi.c +++ b/reversi.c @@ -20,7 +20,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t byte py,px;//cursor const char piece[2] = {'O','X'}; char game[8][8];//main board -byte computer[2] = {0,0}; +char sides[2]={'h','h'}; byte score[2];//set by header() void rectangle(byte sy,byte sx){ @@ -263,25 +263,51 @@ void gameplay(void){ printw(" another piece of the current player's color would turn\n"); printw(" to the current player's color.\n\n"); printw("2) You can only put pieces if at least one of your \n"); - printw(" opponet's pieces turns into your color.\n\n"); + printw(" opponent's pieces turns into your color.\n\n"); printw("3) The game ends when neither side can do a move and\n"); printw(" the player with more pieces wins.\n"); getch(); } int main(int argc , char** argv){ int depth=2; - if(argc>2){ - printf("Usage:%s [AIpower]",argv[0]); - return EXIT_FAILURE; - } - if(argc==2){ - if(sscanf(argv[1],"%d",&depth) && depth<128 && 00) + ; + else{ + puts("That should be a number from 1 to 127."); + return EXIT_FAILURE; + } + + break; + + case 'n': + no_replay=1; + break; + case 'h': + default: + printf("Usage: %s [options]\n -p ai power\n -1 type of player 1\n -2 type of player 2\n -h help\n -n dont ask for replay\n",argv[0]); + return EXIT_SUCCESS; + break; + } - } + } + + signal(SIGINT,sigint_handler); initscr(); #ifndef NO_MOUSE @@ -291,29 +317,31 @@ int main(int argc , char** argv){ cbreak(); keypad(stdscr,1); int input; - printw("Black plays first:\n"); - printw("Choose type of the white player (H/c)\n"); - refresh(); - input=getch(); - if(input == 'c'){ - computer[0]=depth; - printw("Computer.\n"); - } - else{ - computer[1]=0; - printw("Human.\n"); - } - refresh(); - printw("Choose type of the black player(h/C)\n"); - refresh(); - input=getch(); - if(input == 'h'){ - computer[1]=0; - printw("Human.\n"); - } - else{ - computer[1]=depth; - printw("Computer.\n"); + if(!sides_chosen){ + printw("Black plays first:\n"); + printw("Choose type of the white player (H/c)\n"); + refresh(); + input=getch(); + if(input == 'c'){ + sides[0]='c'; + printw("Computer.\n"); + } + else{ + sides[0]='h'; + printw("Human.\n"); + } + refresh(); + printw("Choose type of the black player(h/C)\n"); + refresh(); + input=getch(); + if(input == 'h'){ + sides[1]='h'; + printw("Human.\n"); + } + else{ + sides[1]='c'; + printw("Computer.\n"); + } } Start: curs_set(0); @@ -337,11 +365,11 @@ int main(int argc , char** argv){ goto End; turn = !turn; - if(computer[turn]){ + if(sides[turn]=='c'){ if(can_move(game,piece[turn])){ mvprintw(13,0,"Thinking..."); refresh(); - decide(game,piece[turn],piece[!turn],computer[turn]); + decide(game,piece[turn],piece[!turn],depth); cantmove=0; } else @@ -360,8 +388,9 @@ int main(int argc , char** argv){ erase(); draw(3,0); header(); - if(!(computer[0]||computer[1])) - mvprintw(0,5,"%c's turn",piece[turn]); + if(sides[0]=='h' && sides[1] =='h'){ + mvprintw(2,10,"%c's turn",piece[turn]); + } refresh(); input=getch(); if( input==KEY_F(1) || input=='?' ) @@ -401,16 +430,21 @@ int main(int argc , char** argv){ mvprintw(13,0,"'%c' won.",piece[0]); else mvprintw(13,0,"'%c' won.",piece[1]); - - printw(" Wanna play again?(y/n)"); - curs_set(1); - input=getch(); - if( resign){ - if (input=='Y' || input=='y') + if(!no_replay){ + printw(" Wanna play again?(y/n)"); + curs_set(1); + input=getch(); + if( resign){ + if (input=='Y' || input=='y') + goto Start; + } + else if(input != 'N' && input != 'n' && input != 'q') goto Start; } - else if(input != 'N' && input != 'n' && input != 'q') - goto Start; + else{ + printw(" Press any key on your keyboard to continue:"); + getch(); + } endwin(); return EXIT_SUCCESS; } diff --git a/snakeduel.c b/snakeduel.c index 7e10028..49b9f9f 100644 --- a/snakeduel.c +++ b/snakeduel.c @@ -59,7 +59,7 @@ int usleep(long usec) { #else -int len,wid; +int len=MINLEN,wid=MINWID; #endif//NO_VLA typedef struct snake{ int y; @@ -617,31 +617,32 @@ int main(int argc, char** argv){ lol=fopen("lol","w"); fflush(lol); #endif - bool autoset=0; + bool autoset=1; 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; + int opt; + while( (opt=getopt(argc,argv,"hnl:w:"))!=-1){ + switch(opt){ + case 'l': + len=atoi(optarg); + if(lenMAXLEN){ + fprintf(stderr,"Length too high or low.\n"); + } + autoset=0; + break; + case 'w': + wid=atoi(optarg); + if(widMAXWID){ + fprintf(stderr,"Width too high or low.\n"); + } + autoset=0; + break; + case 'h': + default: + printf("Usage:%s [options]\n -l length\n -w width\n -h help\n",argv[0]); + return EXIT_FAILURE; + break; } - if(len500 || wid>500){ - puts("At least one of your given dimensions is either too small or too big."); - return EXIT_FAILURE; - } - - } - else{ - autoset=1; } #endif initscr(); diff --git a/sos.c b/sos.c index cab89e4..6f2baea 100644 --- a/sos.c +++ b/sos.c @@ -23,13 +23,13 @@ You should have received a copy of the CC0 Public Domain Dedication along with t #define len 5 #define wid 6 #else -int len,wid; +int len=5,wid=6; #endif int py,px; chtype colors[6]={A_BOLD}; int score[2] ={0}; -int computer[2]={0}; +int sides[2]={'h','h'}; char so[2] = {'S','O'}; char rd(char board[len][wid],int y, int x){ @@ -248,57 +248,42 @@ int main(int argc, char** argv){ int dpt=1; signal(SIGINT,sigint_handler); #ifndef NO_VLA - if(argc>4 || (argc==2 && !strcmp("help",argv[1])) ){ - printf("Usage: %s [len wid [AIpower]]\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<3 || wid<3 || len>300 || wid>300){ - puts("At least one of your given dimensions is either too small or too big."); - return EXIT_FAILURE; - } - - } - else{ - len=5; - wid=6; - } - if(argc==4){ - if( !sscanf(argv[3],"%d",&dpt)){ - puts("Invalid input."); - return EXIT_FAILURE; - } - if( dpt<1 || dpt>= 127){ - puts("That should be between 1 and 127."); - return EXIT_FAILURE; - } - } -#else - if(argc>2 || (argc==2 && !strcmp("help",argv[1])) ){ - printf("Usage: %s [AIpower]\n",argv[0]); - return EXIT_FAILURE; - } + int opt; + bool sides_chosen=0,no_replay=0; + while( (opt= getopt(argc,argv,"hnp:1:2:"))!= -1 ){ + switch(opt){ + case '1': + case '2': + if(!strcmp("c",optarg) || !strcmp("h",optarg)){ + sides[opt-'1']=optarg[0]; + sides_chosen=1; + } + else{ + puts("That should be either h or c\n"); + return EXIT_FAILURE; + } + break; + case 'p': + if(sscanf(optarg,"%d",&dpt) && dpt<128 && dpt>0) + ; + else{ + puts("That should be a number from 1 to 127."); + return EXIT_FAILURE; + } + + break; - if(argc==2){ - if( !sscanf(argv[1],"%d",&dpt)){ - puts("Invalid input."); - return EXIT_FAILURE; - } - if( dpt<1 || dpt>= 127){ - puts("That should be between 1 and 127."); - return EXIT_FAILURE; + case 'n': + no_replay=1; + break; + case 'h': + default: + printf("Usage: %s [options]\n -p ai power\n -1 type of player 1\n -2 type of player 2\n -h help\n -n dont ask for replay\n",argv[0]); + return EXIT_SUCCESS; + break; + } } - #endif srand(time(NULL)%UINT_MAX); int input; @@ -310,28 +295,30 @@ int main(int argc, char** argv){ noecho(); cbreak(); keypad(stdscr,1); - printw("Black plays first.\n Choose the type of the blue player(H/c)\n" ); - refresh(); - input=getch(); - if(input=='c'){ - computer[0]=dpt; - printw("Computer.\n"); - } - else{ - computer[0]=0; - printw("Human.\n"); - } - refresh(); - printw("Choose the type of the yellow player(h/C)\n"); - refresh(); - input=getch(); - if(input=='h'){ - computer[1]=0; - printw("Human.\n"); - } - else{ - computer[1]=dpt; - printw("Computer.\n"); + if(!sides_chosen){ + printw("Blue plays first.\n Choose the type of the blue player(H/c)\n" ); + refresh(); + input=getch(); + if(input=='c'){ + sides[0]='c'; + printw("Computer.\n"); + } + else{ + sides[0]='h'; + printw("Human.\n"); + } + refresh(); + printw("Choose the type of the yellow player(h/C)\n"); + refresh(); + input=getch(); + if(input=='h'){ + sides[1]=0; + printw("Human.\n"); + } + else{ + sides[1]=dpt; + printw("Computer.\n"); + } } if(has_colors()){ start_color(); @@ -364,7 +351,7 @@ int main(int argc, char** argv){ goto End; refresh(); t=!t; - if(computer[t]){ + if(sides[t]=='c'){ mvprintw(sy+len+5,sx+0,"Thinking..."); refresh(); decide(board,colored,dpt,t); @@ -430,13 +417,19 @@ int main(int argc, char** argv){ mvprintw(sy+len+5,sx+0,"Draw!!"); else mvprintw(sy+len+5,sx+0,"Player %d won the game!",(score[1]>score[0]) +1); - printw(" Wanna play again?(y/n)"); - curs_set(1); - flushinp(); - input=getch(); - curs_set(0); - if(input != 'N' && input != 'n' && input!='q') - goto Start; + if(!no_replay){ + printw(" Wanna play again?(y/n)"); + curs_set(1); + flushinp(); + input=getch(); + curs_set(0); + if(input != 'N' && input != 'n' && input!='q') + goto Start; + } + else{ + printw("Please press a key on your computer's keyboard to continue."); + getch(); + } endwin(); return EXIT_SUCCESS; } diff --git a/sudoku.c b/sudoku.c index 6e05edf..3d6dc83 100644 --- a/sudoku.c +++ b/sudoku.c @@ -324,48 +324,38 @@ void gameplay(void){ } int main(int argc,char** argv){ signal(SIGINT,sigint_handler); + bool fastgen; + int opt; + while( (opt=getopt(argc,argv,"hfs:d:"))!=-1){ + switch(opt){ #ifndef NO_VLA - if(argc>4 || (argc==2 && !strcmp("help",argv[1])) ){ - printf("Usage: %s [size [ difficulty]] \n",argv[0]); - return EXIT_FAILURE; - } - if(argc>1 ){ - if(strlen(argv[1])>1 || argv[1][0]-'0'>7 || argv[1][0]-'0'< 2){ - printf("2 <= size <= 7\n"); - return EXIT_FAILURE; + case 's': + size=atoi(optarg); + if(size>7 || size<2){ + printf("2 <= size <= 7\n"); + return EXIT_FAILURE; + } + break; +#endif //NO_VLA + case 'd': + diff=atoi(optarg); + if(diff>4 || diff<0){ + printf("0 <= difficulty <= 4\n"); + return EXIT_FAILURE; + } + break; + case 'f': + fastgen=1; + break; + case 'h': + default: + printf("Usage:%s [options]\n -s size\n -d difficulty\n -h help\n -f fast (flawed) generation\n",argv[0]); + return EXIT_FAILURE; + break; } - else - size = *argv[1]-'0'; - } - else - size=3; - if(argc>2){ - if (strlen(argv[2])>1 || argv[2][0]-'0'>4 || argv[2][0]-'0'<= 0 ){ - printf("1 <= diff <=4\n"); - return EXIT_FAILURE; - } - else - diff = *argv[2]-'0'+1; } - else - diff=2; -#else //Plan9 doesn't take size - if(argc>2 || (argc==2 && !strcmp("help",argv[1])) ){ - printf("Usage: %s [difficulty]\n",argv[0]); - return EXIT_FAILURE; - } - if(argc>1){ - if (strlen(argv[2])>1 || argv[2][0]-'0'>4 || argv[2][0]-'0'<= 0 ){ - printf("1 <= diff <=4\n"); - return EXIT_FAILURE; - } - else - diff = *argv[2]-'0'+1; - } - else - diff=2; -#endif - bool fastgen= !(!getenv("SUDOKU_FASTGEN")); + + bool fastgen= fastgen || !(!getenv("SUDOKU_FASTGEN")); initscr(); #ifndef NO_MOUSE mousemask(ALL_MOUSE_EVENTS,NULL);