1
0
mirror of https://github.com/abakh/nbsdgames.git synced 2025-02-02 15:07:27 -05:00

getopt, etc.

This commit is contained in:
Your Name 2021-08-16 19:24:22 +04:30
parent 83e2b35548
commit 3f6a8f696c
12 changed files with 428 additions and 467 deletions

View File

@ -18,6 +18,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#include <signal.h> #include <signal.h>
#include <math.h> #include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <unistd.h>
#include "config.h" #include "config.h"
#define LIGHT -1 #define LIGHT -1
#define DARK 1 #define DARK 1
@ -35,6 +36,7 @@ byte cy,cx;//selected(choosen) piece
int dpt; int dpt;
byte game[8][8]; byte game[8][8];
byte computer[2]={0,0}; byte computer[2]={0,0};
char sides[2]={'h','h'};
byte score[2];//set by header() byte score[2];//set by header()
bool endgame=false; bool endgame=false;
byte jumpagainy , jumpagainx; 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{ else{
if(nextturn==side) if(nextturn==side)
adv=decide(nextturn,depth,nexts); 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); adv=decide(nextturn,depth-!fj,nexts);
if(adv==WIN) if(adv==WIN)
adv=0; adv=0;
@ -485,15 +487,15 @@ void gameplay(void){
move(4,0); move(4,0);
printw("1) The game starts with each player having 12 men;\n"); printw("1) The game starts with each player having 12 men;\n");
printw(" men can only diagonally move forwards \n"); printw(" men can only diagonally move forwards \n");
printw(" (toward the opponet's side).\n\n"); printw(" (toward the opponent's side).\n\n");
printw("2) Men can become kings by reaching the opponet's \n"); printw("2) Men can become kings by reaching the opponent's \n");
printw(" first rank; kings can diagonally move both forwards\n"); printw(" first rank; kings can diagonally move both forwards\n");
printw(" and backwards.\n\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(" also they can capture several pieces at once by doing a\n");
printw(" chain of jumps.\n\n"); printw(" chain of jumps.\n\n");
printw("4) You have to do a jump if you can.\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"); printw(" all of their pieces are captured.\n\n");
refresh(); refresh();
getch(); getch();
@ -501,18 +503,43 @@ void gameplay(void){
} }
int main(int argc,char** argv){ int main(int argc,char** argv){
dpt=4; dpt=4;
if(argc>2){ int opt;
printf("Usage: %s [AIpower]\n",argv[0]); bool sides_chosen=0,no_replay=0;
return EXIT_FAILURE; while( (opt= getopt(argc,argv,"hnp:1:2:"))!= -1 ){
} switch(opt){
if(argc==2){ case '1':
if(sscanf(argv[1],"%d",&dpt) && dpt<128 && dpt>0) case '2':
; if(!strcmp("c",optarg) || !strcmp("h",optarg)){
else{ sides[opt-'1']=optarg[0];
puts("That should be a number from 1 to 127."); sides_chosen=1;
return EXIT_FAILURE; }
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(); initscr();
#ifndef NO_MOUSE #ifndef NO_MOUSE
mousemask(ALL_MOUSE_EVENTS,NULL); mousemask(ALL_MOUSE_EVENTS,NULL);
@ -521,28 +548,35 @@ int main(int argc,char** argv){
cbreak(); cbreak();
keypad(stdscr,1); keypad(stdscr,1);
int input ; int input ;
printw("Dark plays first.\nChoose type of the dark player(H/c)\n" ); if(!sides_chosen){
refresh(); printw("Black plays first.\nChoose type of the black player(H/c)\n" );
input=getch(); refresh();
if(input=='c'){ input=getch();
computer[0]=dpt; if(input=='c'){
printw("Computer.\n"); computer[0]=dpt;
} sides[0]='c';
else{ printw("Computer.\n");
computer[0]=0; }
printw("Human.\n"); else{
} computer[0]=0;
printw("Choose type of the bright player(h/C)\n"); sides[0]='h';
refresh(); printw("Human.\n");
input=getch(); }
if(input=='h'){ printw("Choose type of the white player(h/C)\n");
computer[1]=0; refresh();
printw("Human.\n"); input=getch();
} if(input=='h'){
else{ computer[1]=0;
computer[1]=dpt; sides[0]='h';
printw("Computer.\n"); printw("Human.\n");
}
else{
computer[1]=dpt;
sides[1]='c';
printw("Computer.\n");
}
} }
if(has_colors()){ if(has_colors()){
start_color(); start_color();
use_default_colors(); use_default_colors();
@ -583,7 +617,11 @@ int main(int argc,char** argv){
else else
todraw=0; 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; result=1;
goto End; goto End;
} }
@ -598,11 +636,10 @@ int main(int argc,char** argv){
endgame= score[t]<=5 || score[!t]<=5; endgame= score[t]<=5 || score[!t]<=5;
draw(3,0); draw(3,0);
refresh(); refresh();
while(computer[t]){ while(sides[t]=='c'){
mvprintw(13,0,"Thinking..."); mvprintw(13,0,"Thinking...");
refresh(); refresh();
computer[t]=dpt+ (score[!t] != score[t]) + endgame; decide(turn,dpt+(score[t]<score[!t])+endgame,1);
decide(turn,computer[t],1);
if(!(fj && jumpagainy>=0 && !kinged )){ if(!(fj && jumpagainy>=0 && !kinged )){
goto Turn; goto Turn;
} }
@ -613,10 +650,10 @@ int main(int argc,char** argv){
header(); header();
if(!(computer[0]||computer[1])){ if(!(computer[0]||computer[1])){
if(t) if(t)
addstr(" Bright's turn"); addstr(" White's turn");
else{ else{
attron(COLOR_PAIR(1)); attron(COLOR_PAIR(1));
addstr(" Dark's turn"); addstr(" Black's turn");
attroff(COLOR_PAIR(1)); attroff(COLOR_PAIR(1));
} }
} }
@ -671,29 +708,36 @@ int main(int argc,char** argv){
move(13,0); move(13,0);
switch(result){ switch(result){
case -1: case -1:
printw("The dark side has won the game."); printw("Black side has won the game.");
break; break;
case 0: case 0:
printw("Draw."); printw("Draw.");
break; break;
case 1: case 1:
printw("The bright side has won the game."); printw("White side has won the game.");
break; break;
case 2: case 2:
printw("You resigned."); printw("You resigned.");
} }
printw(" Wanna rematch?(y/n)"); if(!no_replay){
curs_set(1); printw(" Wanna rematch?(y/n)");
input=getch(); refresh();
if(result==2){ curs_set(1);
if (input=='Y' || input=='y') 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; goto Start;
}
} }
else if(input!='n' && input!='N' && input!= 'q'){ else{
/*byte b=computer[0]; //switch sides, i don't know if it's necessary printw("Press any key on your keyboard to continue.");
computer[0]=computer[1]; getch();
computer[1]=b;*/
goto Start;
} }
endwin(); endwin();
return EXIT_SUCCESS; return EXIT_SUCCESS;

View File

@ -192,15 +192,23 @@ void gameplay(void){
int main(int argc, char** argv){ int main(int argc, char** argv){
#ifndef NO_VLA #ifndef NO_VLA
size=4; size=4;
if(argc==2){ int opt;
if(!strcmp("help",argv[1])){ bool no_replay=0;
printf("Usage: %s [size]\n",argv[0]); while( (opt=getopt(argc,argv,"hns:"))!=-1){
return EXIT_SUCCESS; switch(opt){
} case 's':
size=atoi(argv[1]); size=atoi(optarg);
if(size<3 || size>7){ if(size<3 || size>7){
fprintf(stderr,"3<=size<=7\n"); fprintf(stderr,"3<=size<=7");
return EXIT_FAILURE; }
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 #endif
@ -257,11 +265,19 @@ int main(int argc, char** argv){
slide_multi(board,py,px); slide_multi(board,py,px);
} }
} }
mvprintw(size+5,0,"You solved it! Wanna play again?(y/n)"); mvprintw(size+5,0,"You solved it!");
curs_set(1); if(!no_replay){
input=getch(); printw(" Wanna play again?(y/n)");
if(input != 'N' && input != 'n' && input != 'q') refresh();
goto Start; 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(); endwin();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -19,13 +19,9 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#include "config.h" #include "config.h"
typedef unsigned char ubyte; typedef unsigned char ubyte;
/* The Plan9 compiler can not handle VLAs */
#ifdef NO_VLA
#define size 8 #define size 8
#define size2 16 #define size2 16
#else
byte size,size2;//size2 is there to avoid a lot of multiplications
#endif
byte py,px; byte py,px;
byte fy,fx; //the first tile byte fy,fx; //the first tile
chtype colors[6]={0}; chtype colors[6]={0};
@ -172,23 +168,7 @@ void gameplay(void){
getch(); getch();
erase(); erase();
} }
int main(int argc, char** argv){ int main(void){
#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;
}
}
signal(SIGINT,sigint_handler); signal(SIGINT,sigint_handler);
srand(time(NULL)%UINT_MAX); srand(time(NULL)%UINT_MAX);
initscr(); initscr();
@ -215,12 +195,7 @@ int main(int argc, char** argv){
} }
} }
} }
#ifndef NO_VLA chtype board[size][size2];
else if(size>8)//big sizes depend on color display
size=8;
size2=size*2;
#endif
chtype board[size][size2];
bool show[size][size2]; bool show[size][size2];
int input; int input;
time_t tstart,now; time_t tstart,now;
@ -267,11 +242,14 @@ int main(int argc, char** argv){
} }
now=time(NULL)-tstart; now=time(NULL)-tstart;
mvprintw(size+7,0,"Time spent: %d:%2d:%2d",now/3600,(now%3600)/60,now%60); 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); curs_set(1);
input=getch(); input=getch();
if(input != 'N' && input != 'n' && input != 'q') if(input != 'N' && input != 'n' && input != 'q')
goto Start; goto Start;
endwin(); endwin();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -44,7 +44,7 @@ int usleep(long usec) {
#endif #endif
#else #else
int len,wid; int len=MINLEN,wid=MINWID;
#endif//NO_VLA #endif//NO_VLA
int py,px; int py,px;
@ -296,7 +296,7 @@ void gameplay(void){
mvprintw(3,0," **** THE GAMEPLAY ****"); mvprintw(3,0," **** THE GAMEPLAY ****");
attroff(A_BOLD); attroff(A_BOLD);
move(4,0); 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("survive explosions but cannot cross the trail it has\n");
printw("left behind. Keep it running as much as you can."); printw("left behind. Keep it running as much as you can.");
refresh(); refresh();
@ -309,48 +309,26 @@ void sigint_handler(int x){
puts("Quit."); puts("Quit.");
exit(x); exit(x);
} }
int main(int argc, char** argv){ int main(void){
#ifndef NO_VLA #ifndef NO_VLA
bool autoset=0;
signal(SIGINT,sigint_handler); 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(len<MINLEN || wid<MINWID || len>500 || wid>500){
puts("At least one of your given dimensions is either too small or too big.");
return EXIT_FAILURE;
}
}
else{
autoset=1;
}
#endif #endif
initscr(); initscr();
#ifndef NO_VLA #ifndef NO_VLA
if(autoset){ len=LINES-7;
len=LINES-7; if(len<MINLEN){
if(len<MINLEN) len=MINLEN;
len=MINLEN; }
else if(len>MAXLEN) else if(len>MAXLEN){
len=MAXLEN; len=MAXLEN;
}
wid=COLS-5; wid=COLS-5;
if(wid<MINWID) if(wid<MINWID){
wid=MINWID; wid=MINWID;
else if(wid>MAXWID) }
wid=MAXWID; else if(wid>MAXWID){
wid=MAXWID;
} }
#endif #endif
srand(time(NULL)%UINT_MAX); srand(time(NULL)%UINT_MAX);
@ -360,6 +338,7 @@ int main(int argc, char** argv){
noecho(); noecho();
cbreak(); cbreak();
keypad(stdscr,1); keypad(stdscr,1);
if(has_colors()){ if(has_colors()){
start_color(); start_color();
use_default_colors(); use_default_colors();

80
mines.c
View File

@ -20,12 +20,15 @@ compile with -lncurses
#include "config.h" #include "config.h"
#define FLAG 9 #define FLAG 9
#define UNCLEAR 10 #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 #ifdef NO_VLA //The Plan9 compiler can not handle VLAs
#define len 8 #define len 8
#define wid 8 #define wid 8
#else #else
int len,wid; int len=8,wid=8;
#endif #endif
int py,px,flags; int py,px,flags;
int untouched; int untouched;
@ -221,40 +224,37 @@ void gameplay(void){
int main(int argc, char** argv){ int main(int argc, char** argv){
signal(SIGINT,sigint_handler); signal(SIGINT,sigint_handler);
#ifndef NO_VLA #ifndef NO_VLA
if(argc>4 || (argc==2 && !strcmp("help",argv[1])) ){ int opt;
printf("Usage: %s [len wid [minescount]]\n",argv[0]); while( (opt=getopt(argc,argv,"hnm:l:w:"))!=-1){
return EXIT_FAILURE; switch(opt){
} case 'm':
if(argc==2){ mscount=atoi(optarg);
puts("Give both dimensions."); if(mscount<0 || mscount>len*wid){
return EXIT_FAILURE; fprintf(stderr,"Too few/many mines.\n");
} }
if(argc>=3){ break;
bool lool = sscanf(argv[1],"%d",&len) && sscanf(argv[2],"%d",&wid); case 'l':
if(!lool){ len=atoi(optarg);
puts("Invalid input."); if(len<MINLEN || len>MAXLEN){
return EXIT_FAILURE; fprintf(stderr,"Length too high or low.\n");
} }
if(len<5 || wid<5 || len>1000 || wid>1000){ break;
puts("At least one of your given dimensions is either too small or too big."); case 'w':
return EXIT_FAILURE; wid=atoi(optarg);
} if(wid<MINWID || wid>MAXWID){
fprintf(stderr,"Width too high or low.\n");
} }
else break;
len=wid=8; case 'h':
if(argc==4){ default:
if( !sscanf(argv[3],"%d",&mscount)){ printf("Usage:%s [options]\n -l length\n -w width\n -m number of mines\n -h help\n",argv[0]);
puts("Invalid input."); return EXIT_FAILURE;
return EXIT_FAILURE; break;
}
if( mscount<5 || mscount>= len*wid){
puts("Too few/many mines.");
return EXIT_FAILURE;
} }
} }
else if(!mscount){
mscount = len*wid/6; mscount=len*wid/6;
}
#else #else
mscount=len*wid/6; mscount=len*wid/6;
#endif #endif
@ -313,23 +313,27 @@ int main(int argc, char** argv){
input = getch(); input = getch();
if( input==KEY_PPAGE && LINES< len+3){//the board starts in 3 if( input==KEY_PPAGE && LINES< len+3){//the board starts in 3
sy+=10; sy+=10;
if(sy>0) if(sy>0){
sy=0; sy=0;
}
} }
if( input==KEY_NPAGE && LINES< len+3){ if( input==KEY_NPAGE && LINES< len+3){
sy-=10; sy-=10;
if(sy< -(len+3) ) if(sy< -(len+3) ){
sy=-(len+3); sy=-(len+3);
}
} }
if( input=='<' && COLS< wid*2+1){ if( input=='<' && COLS< wid*2+1){
sx+=10; sx+=10;
if(sx>0) if(sx>0){
sx=0; sx=0;
}
} }
if( input=='>' && COLS< wid*2+1){ if( input=='>' && COLS< wid*2+1){
sx-=10; sx-=10;
if(sx< -(wid*2+1)) if(sx< -(wid*2+1)){
sx=-(wid*2+1); sx=-(wid*2+1);
}
} }
if( input==KEY_F(1) || input=='?' ) if( input==KEY_F(1) || input=='?' )
help(); help();

View File

@ -270,47 +270,20 @@ void sigint_handler(int x){
exit(x); exit(x);
} }
int main(int argc, char** argv){ int main(int argc, char** argv){
bool autoset=0;
signal(SIGINT,sigint_handler); 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<MINLEN || wid<MINWID || len>500 || 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(); initscr();
#ifndef NO_VLA #ifndef NO_VLA
if(autoset){ len=LINES-7;
len=LINES-7; if(len<MINLEN)
if(len<MINLEN) len=MINLEN;
len=MINLEN; else if(len>MAXLEN)
else if(len>MAXLEN) len=MAXLEN;
len=MAXLEN;
wid=COLS-5; wid=COLS-5;
if(wid<MINWID) if(wid<MINWID)
wid=MINWID; wid=MINWID;
else if(wid>MAXWID) else if(wid>MAXWID)
wid=MAXWID; wid=MAXWID;
}
#endif #endif
srand(time(NULL)%UINT_MAX); srand(time(NULL)%UINT_MAX);
byte board[len][wid]; byte board[len][wid];

34
pipes.c
View File

@ -33,12 +33,12 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
typedef unsigned char bitbox; typedef unsigned char bitbox;
/* The Plan9 compiler can not handle VLAs */ /* 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 wid 20
#define len 14 #define len 14
#else //#else
int len,wid; //int len,wid;
#endif //#endif
int py,px,fy,fx;//p: pointer f: fluid int py,px,fy,fx;//p: pointer f: fluid
bitbox tocome[5]={0};//the row of pipes in the left side 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){ int main(int argc, char** argv){
signal(SIGINT,sigint_handler); 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(); initscr();
#ifndef NO_MOUSE #ifndef NO_MOUSE
mousemask(ALL_MOUSE_EVENTS,NULL); mousemask(ALL_MOUSE_EVENTS,NULL);

View File

@ -178,46 +178,21 @@ void sigint_handler(int x){
puts("Quit."); puts("Quit.");
exit(x); exit(x);
} }
int main(int argc, char** argv){ int main(void){
bool autoset=0;
signal(SIGINT,sigint_handler); 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(); initscr();
#ifndef NO_VLA #ifndef NO_VLA
if(autoset){ if((LINES-7)/2 < 5){
if((LINES-7)/2 < 10) len=5;
len=10; }
else else{
len=(LINES-7)/2; len=(LINES-7)/2;
}
if((COLS-5)/2 < 20) if((COLS-5)/2 < 20){
wid=20; wid=20;
else }
wid=(COLS-5)/2; else{
wid=(COLS-5)/2;
} }
#endif #endif
int carrot_count= (len*wid)/50; int carrot_count= (len*wid)/50;

130
reversi.c
View File

@ -20,7 +20,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
byte py,px;//cursor byte py,px;//cursor
const char piece[2] = {'O','X'}; const char piece[2] = {'O','X'};
char game[8][8];//main board char game[8][8];//main board
byte computer[2] = {0,0}; char sides[2]={'h','h'};
byte score[2];//set by header() byte score[2];//set by header()
void rectangle(byte sy,byte sx){ 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(" another piece of the current player's color would turn\n");
printw(" to the current player's color.\n\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("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("3) The game ends when neither side can do a move and\n");
printw(" the player with more pieces wins.\n"); printw(" the player with more pieces wins.\n");
getch(); getch();
} }
int main(int argc , char** argv){ int main(int argc , char** argv){
int depth=2; int depth=2;
if(argc>2){ int opt;
printf("Usage:%s [AIpower]",argv[0]); bool sides_chosen=0,no_replay=0;
return EXIT_FAILURE; while( (opt= getopt(argc,argv,"hnp:1:2:"))!= -1 ){
} switch(opt){
if(argc==2){ case '1':
if(sscanf(argv[1],"%d",&depth) && depth<128 && 0<depth) case '2':
;//already done in sscanf if(!strcmp("c",optarg) || !strcmp("h",optarg)){
else{ sides[opt-'1']=optarg[0];
printf("That should be a number from 1 to 127.\n"); sides_chosen=1;
return EXIT_FAILURE; }
else{
puts("That should be either h or c\n");
return EXIT_FAILURE;
}
break;
case 'p':
if(sscanf(optarg,"%d",&depth) && depth<128 && depth>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;
} }
} }
signal(SIGINT,sigint_handler); signal(SIGINT,sigint_handler);
initscr(); initscr();
#ifndef NO_MOUSE #ifndef NO_MOUSE
@ -291,29 +317,31 @@ int main(int argc , char** argv){
cbreak(); cbreak();
keypad(stdscr,1); keypad(stdscr,1);
int input; int input;
printw("Black plays first:\n"); if(!sides_chosen){
printw("Choose type of the white player (H/c)\n"); printw("Black plays first:\n");
refresh(); printw("Choose type of the white player (H/c)\n");
input=getch(); refresh();
if(input == 'c'){ input=getch();
computer[0]=depth; if(input == 'c'){
printw("Computer.\n"); sides[0]='c';
} printw("Computer.\n");
else{ }
computer[1]=0; else{
printw("Human.\n"); sides[0]='h';
} printw("Human.\n");
refresh(); }
printw("Choose type of the black player(h/C)\n"); refresh();
refresh(); printw("Choose type of the black player(h/C)\n");
input=getch(); refresh();
if(input == 'h'){ input=getch();
computer[1]=0; if(input == 'h'){
printw("Human.\n"); sides[1]='h';
} printw("Human.\n");
else{ }
computer[1]=depth; else{
printw("Computer.\n"); sides[1]='c';
printw("Computer.\n");
}
} }
Start: Start:
curs_set(0); curs_set(0);
@ -337,11 +365,11 @@ int main(int argc , char** argv){
goto End; goto End;
turn = !turn; turn = !turn;
if(computer[turn]){ if(sides[turn]=='c'){
if(can_move(game,piece[turn])){ if(can_move(game,piece[turn])){
mvprintw(13,0,"Thinking..."); mvprintw(13,0,"Thinking...");
refresh(); refresh();
decide(game,piece[turn],piece[!turn],computer[turn]); decide(game,piece[turn],piece[!turn],depth);
cantmove=0; cantmove=0;
} }
else else
@ -360,8 +388,9 @@ int main(int argc , char** argv){
erase(); erase();
draw(3,0); draw(3,0);
header(); header();
if(!(computer[0]||computer[1])) if(sides[0]=='h' && sides[1] =='h'){
mvprintw(0,5,"%c's turn",piece[turn]); mvprintw(2,10,"%c's turn",piece[turn]);
}
refresh(); refresh();
input=getch(); input=getch();
if( input==KEY_F(1) || input=='?' ) if( input==KEY_F(1) || input=='?' )
@ -401,16 +430,21 @@ int main(int argc , char** argv){
mvprintw(13,0,"'%c' won.",piece[0]); mvprintw(13,0,"'%c' won.",piece[0]);
else else
mvprintw(13,0,"'%c' won.",piece[1]); mvprintw(13,0,"'%c' won.",piece[1]);
if(!no_replay){
printw(" Wanna play again?(y/n)"); printw(" Wanna play again?(y/n)");
curs_set(1); curs_set(1);
input=getch(); input=getch();
if( resign){ if( resign){
if (input=='Y' || input=='y') if (input=='Y' || input=='y')
goto Start;
}
else if(input != 'N' && input != 'n' && input != 'q')
goto Start; goto Start;
} }
else if(input != 'N' && input != 'n' && input != 'q') else{
goto Start; printw(" Press any key on your keyboard to continue:");
getch();
}
endwin(); endwin();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -59,7 +59,7 @@ int usleep(long usec) {
#else #else
int len,wid; int len=MINLEN,wid=MINWID;
#endif//NO_VLA #endif//NO_VLA
typedef struct snake{ typedef struct snake{
int y; int y;
@ -617,31 +617,32 @@ int main(int argc, char** argv){
lol=fopen("lol","w"); lol=fopen("lol","w");
fflush(lol); fflush(lol);
#endif #endif
bool autoset=0; bool autoset=1;
signal(SIGINT,sigint_handler); signal(SIGINT,sigint_handler);
#ifndef NO_VLA #ifndef NO_VLA
if(argc>3 || (argc==2 && !strcmp("help",argv[1])) ){ int opt;
printf("Usage: %s [len wid]\n",argv[0]); while( (opt=getopt(argc,argv,"hnl:w:"))!=-1){
return EXIT_FAILURE; switch(opt){
} case 'l':
if(argc==2){ len=atoi(optarg);
puts("Give both dimensions."); if(len<MINLEN || len>MAXLEN){
return EXIT_FAILURE; fprintf(stderr,"Length too high or low.\n");
} }
if(argc==3){ autoset=0;
bool lool = sscanf(argv[1],"%d",&len) && sscanf(argv[2],"%d",&wid); break;
if(!lool){ case 'w':
puts("Invalid input."); wid=atoi(optarg);
return EXIT_FAILURE; if(wid<MINWID || wid>MAXWID){
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(len<MINLEN || wid<MINWID || len>500 || wid>500){
puts("At least one of your given dimensions is either too small or too big.");
return EXIT_FAILURE;
}
}
else{
autoset=1;
} }
#endif #endif
initscr(); initscr();

153
sos.c
View File

@ -23,13 +23,13 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#define len 5 #define len 5
#define wid 6 #define wid 6
#else #else
int len,wid; int len=5,wid=6;
#endif #endif
int py,px; int py,px;
chtype colors[6]={A_BOLD}; chtype colors[6]={A_BOLD};
int score[2] ={0}; int score[2] ={0};
int computer[2]={0}; int sides[2]={'h','h'};
char so[2] = {'S','O'}; char so[2] = {'S','O'};
char rd(char board[len][wid],int y, int x){ char rd(char board[len][wid],int y, int x){
@ -248,57 +248,42 @@ int main(int argc, char** argv){
int dpt=1; int dpt=1;
signal(SIGINT,sigint_handler); signal(SIGINT,sigint_handler);
#ifndef NO_VLA #ifndef NO_VLA
if(argc>4 || (argc==2 && !strcmp("help",argv[1])) ){ int opt;
printf("Usage: %s [len wid [AIpower]]\n",argv[0]); bool sides_chosen=0,no_replay=0;
return EXIT_FAILURE; while( (opt= getopt(argc,argv,"hnp:1:2:"))!= -1 ){
} switch(opt){
if(argc==2){ case '1':
puts("Give both dimensions."); case '2':
return EXIT_FAILURE; if(!strcmp("c",optarg) || !strcmp("h",optarg)){
} sides[opt-'1']=optarg[0];
if(argc>=3){ sides_chosen=1;
bool lool = sscanf(argv[1],"%d",&len) && sscanf(argv[2],"%d",&wid); }
if(!lool){ else{
puts("Invalid input."); puts("That should be either h or c\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if(len<3 || wid<3 || len>300 || wid>300){ break;
puts("At least one of your given dimensions is either too small or too big."); case 'p':
return EXIT_FAILURE; if(sscanf(optarg,"%d",&dpt) && dpt<128 && dpt>0)
} ;
else{
} puts("That should be a number from 1 to 127.");
else{ return EXIT_FAILURE;
len=5; }
wid=6;
} break;
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;
}
if(argc==2){ case 'n':
if( !sscanf(argv[1],"%d",&dpt)){ no_replay=1;
puts("Invalid input."); break;
return EXIT_FAILURE; case 'h':
} default:
if( dpt<1 || dpt>= 127){ 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]);
puts("That should be between 1 and 127."); return EXIT_SUCCESS;
return EXIT_FAILURE; break;
} }
} }
#endif #endif
srand(time(NULL)%UINT_MAX); srand(time(NULL)%UINT_MAX);
int input; int input;
@ -310,28 +295,30 @@ int main(int argc, char** argv){
noecho(); noecho();
cbreak(); cbreak();
keypad(stdscr,1); keypad(stdscr,1);
printw("Black plays first.\n Choose the type of the blue player(H/c)\n" ); if(!sides_chosen){
refresh(); printw("Blue plays first.\n Choose the type of the blue player(H/c)\n" );
input=getch(); refresh();
if(input=='c'){ input=getch();
computer[0]=dpt; if(input=='c'){
printw("Computer.\n"); sides[0]='c';
} printw("Computer.\n");
else{ }
computer[0]=0; else{
printw("Human.\n"); sides[0]='h';
} printw("Human.\n");
refresh(); }
printw("Choose the type of the yellow player(h/C)\n"); refresh();
refresh(); printw("Choose the type of the yellow player(h/C)\n");
input=getch(); refresh();
if(input=='h'){ input=getch();
computer[1]=0; if(input=='h'){
printw("Human.\n"); sides[1]=0;
} printw("Human.\n");
else{ }
computer[1]=dpt; else{
printw("Computer.\n"); sides[1]=dpt;
printw("Computer.\n");
}
} }
if(has_colors()){ if(has_colors()){
start_color(); start_color();
@ -364,7 +351,7 @@ int main(int argc, char** argv){
goto End; goto End;
refresh(); refresh();
t=!t; t=!t;
if(computer[t]){ if(sides[t]=='c'){
mvprintw(sy+len+5,sx+0,"Thinking..."); mvprintw(sy+len+5,sx+0,"Thinking...");
refresh(); refresh();
decide(board,colored,dpt,t); decide(board,colored,dpt,t);
@ -430,13 +417,19 @@ int main(int argc, char** argv){
mvprintw(sy+len+5,sx+0,"Draw!!"); mvprintw(sy+len+5,sx+0,"Draw!!");
else else
mvprintw(sy+len+5,sx+0,"Player %d won the game!",(score[1]>score[0]) +1); mvprintw(sy+len+5,sx+0,"Player %d won the game!",(score[1]>score[0]) +1);
printw(" Wanna play again?(y/n)"); if(!no_replay){
curs_set(1); printw(" Wanna play again?(y/n)");
flushinp(); curs_set(1);
input=getch(); flushinp();
curs_set(0); input=getch();
if(input != 'N' && input != 'n' && input!='q') curs_set(0);
goto Start; if(input != 'N' && input != 'n' && input!='q')
goto Start;
}
else{
printw("Please press a key on your computer's keyboard to continue.");
getch();
}
endwin(); endwin();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -324,48 +324,38 @@ void gameplay(void){
} }
int main(int argc,char** argv){ int main(int argc,char** argv){
signal(SIGINT,sigint_handler); signal(SIGINT,sigint_handler);
bool fastgen;
int opt;
while( (opt=getopt(argc,argv,"hfs:d:"))!=-1){
switch(opt){
#ifndef NO_VLA #ifndef NO_VLA
if(argc>4 || (argc==2 && !strcmp("help",argv[1])) ){ case 's':
printf("Usage: %s [size [ difficulty]] \n",argv[0]); size=atoi(optarg);
return EXIT_FAILURE; if(size>7 || size<2){
} printf("2 <= size <= 7\n");
if(argc>1 ){ return EXIT_FAILURE;
if(strlen(argv[1])>1 || argv[1][0]-'0'>7 || argv[1][0]-'0'< 2){ }
printf("2 <= size <= 7\n"); break;
return EXIT_FAILURE; #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; bool fastgen= fastgen || !(!getenv("SUDOKU_FASTGEN"));
#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"));
initscr(); initscr();
#ifndef NO_MOUSE #ifndef NO_MOUSE
mousemask(ALL_MOUSE_EVENTS,NULL); mousemask(ALL_MOUSE_EVENTS,NULL);