/* _ _ |_) (_ | \ed_)quare 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 . */ #include #include #include #include #include #include #include "config.h" #define LEN 35 #define WID 50 #define RLEN LEN //real #define RWID WID #define DEAD 0 #define ALIVE 1 #define RED 2 typedef signed char byte; int level; byte py,px; byte cy,cx;//cross bool coherent;//square's coherence int anum,rnum;//reds and otherwise alive cell counts chtype colors[6]={0}; void cp(byte a[RLEN][RWID],byte b[RLEN][RWID]){ byte y,x; for(y=0;yalives) board[y][x]=RED; else if(alives>reds) board[y][x]=ALIVE; } else{ if(coherent && board[y][x]==RED) coherent=0; board[y][x]=DEAD; } } else if(alives+reds==3){ if(alives>reds) board[y][x]=ALIVE; else board[y][x]=RED; } } } } void add_line(byte board[LEN][WID],byte line,const char* str){ for(byte x=0;str[x]!='\0';++x){ if(str[x]=='#') board[line][x]=ALIVE; /*else board[line][x]=0;*/ } } void new_level(byte board[LEN][WID]){ ++level; memset(board,0,RLEN*RWID); switch(level){ case 0: cy=12; cx=RWID/2; add_line(board,5, " #### #"); add_line(board,6, " #### #"); add_line(board,7, " # # "); add_line(board,8, " # ## # ## # ##"); add_line(board,9, " # # # ## # ## #"); add_line(board,10," # # # # # # # #"); add_line(board,11," ### ## # # # #"); add_line(board,15," #### "); add_line(board,16," # # "); add_line(board,17," # ## # ## # # ## # #"); add_line(board,18," # # # ## # # # # # # # #"); add_line(board,19," # # # # # # # # # # # # #"); add_line(board,20," #### ## # # # # ## # ###"); add_line(board,21," #"); add_line(board,22," # #"); add_line(board,23," ##"); break; case 1: cy=12; cx=RWID/2; add_line(board,5, " # # # #"); add_line(board,6, " # # ## # "); add_line(board,7, " # # # ## ### # # ## ## # # ##"); add_line(board,8, " # # # # # # # # ## # # # ##"); add_line(board,9, " # # # # # # # # # # # # #"); add_line(board,10," # # ## # ## # # # # # # #"); add_line(board,15," #### # "); add_line(board,16," # # # "); add_line(board,17," # # # ## # # # ## # ## # #"); add_line(board,18," ##### ## # # # # # # # # # #"); add_line(board,19," # # # # # # # # # # # #"); add_line(board,20," # # # # ## # # ## #"); break; case 2: cy= 12; cx= 10; add_line(board,3, " ## # #"); add_line(board,4, " ## # # "); add_line(board,5, " # # "); add_line(board,6, " # # # # "); add_line(board,7, " ### ### "); add_line(board,17," ## ## "); add_line(board,18," # # # #"); add_line(board,19," # # # # "); add_line(board,20," # # "); add_line(board,21," ### ### "); add_line(board,22," ### ### "); add_line(board,23," ## ## "); add_line(board,24," ## ## "); add_line(board,25," # ## ## # "); add_line(board,26," ### ###"); add_line(board,27," # #"); add_line(board,30," ##"); add_line(board,31," ##"); break; case 3: cy=RLEN/2; cx=RWID/2; add_line(board,0, " "); add_line(board,1, " # # "); add_line(board,2, " # # "); add_line(board,3, " ### ### "); add_line(board,4, " # # "); add_line(board,5, " # # "); add_line(board,6, " ### ### "); add_line(board,7, " # # "); add_line(board,8, " # # "); add_line(board,9, " ### ### "); add_line(board,10," # # "); add_line(board,11," # # "); add_line(board,12," ### ### "); add_line(board,13," # # "); add_line(board,14," # #"); add_line(board,15," ### ###"); add_line(board,17," "); add_line(board,18," # "); add_line(board,19," # "); add_line(board,20," ### "); add_line(board,21," # "); add_line(board,22," # "); add_line(board,23," ### "); add_line(board,24," # "); add_line(board,25," # "); add_line(board,26," ### "); add_line(board,27," # "); add_line(board,28," # "); add_line(board,29," ### "); add_line(board,30," # "); add_line(board,31," # "); add_line(board,32," ### "); break; case 4: cy=rand()%(RLEN/2); cx=rand()%(RWID/2); add_line(board,0, " "); add_line(board,1, " "); add_line(board,2, " "); add_line(board,3, " "); add_line(board,4, " "); add_line(board,5, " "); add_line(board,6, " "); add_line(board,0, " # # # # "); add_line(board,1, " # | | # # # "); add_line(board,2, " # # | | # # # # # # "); add_line(board,3 ," #### | | #### #### #### "); add_line(board,11," "); add_line(board,12," "); add_line(board,13," "); add_line(board,8 ," # # # # "); add_line(board,9 ," # # # # "); add_line(board,10," # # # # # # # # "); add_line(board,11," #### #### #### #### "); add_line(board,19," "); add_line(board,20," "); add_line(board,16," # # # # "); add_line(board,17," #| | # # # "); add_line(board,18," # #| | # # # # # # "); add_line(board,19," ####| | #### #### #### "); add_line(board,25," "); add_line(board,26," "); add_line(board,27," "); add_line(board,28," "); add_line(board,25," # # "); add_line(board,26," # # "); add_line(board,27," # # # # "); add_line(board,28," #### #### "); //add_line(board,5," #"); //add_line(board,6," ##"); //add_line(board,7," ##"); break; default: srand(level); rand_level(board); } } void rm_square(byte board[LEN][WID],byte prey,byte prex){ byte dy,dx,ry,rx; for(dy=0;dy<2;++dy){ for(dx=0;dx<2;++dx){ ry=prey+dy; if(ry==-1) ry=LEN-1; else if(ry==LEN) ry=0; rx=prex+dx; if(rx==-1) rx=WID-1; else if(rx==WID) rx=0; board[ry][rx]=DEAD; } } } void mk_square(byte board[LEN][WID]){ byte dy,dx,ry,rx; for(dy=0;dy<2;++dy){ for(dx=0;dx<2;++dx){ ry=py+dy; if(ry==-1) ry=LEN-1; else if(ry==LEN) ry=0; rx=px+dx; if(rx==-1) rx=WID-1; else if(rx==WID) rx=0; board[ry][rx]=RED; } } } //detect if there is a square and enable the player to move void reemerge(byte board[LEN][WID]){ byte y,x,dy,dx,ry,rx; for(y=0;yanum||cinred==2){ mvprintw(LEN+5,0,"Well done! Press a key to continue:"); curs_set(1); getch(); curs_set(0); new_level(board); py=LEN*3/4; px=WID/2; mk_square(board); continue; } else if(!rnum){ move(LEN+5,0); printw("You have lost The Game"); if(rand()%5==0) printw(" (and RedSquare)"); printw(". "); break; } halfdelay(9); input = getch(); live(board); count(board);//apparently this should come at both sides of live+draw. resulting from trial and error. if(rnum!=4)//the square has participated in life reactions if so coherent=0; if(!coherent && rnum==4)//there can be a square reemerge(board); if( input==KEY_F(1) || input=='?' ) help(); if( input==KEY_F(2) ) gameplay(); prey=py; prex=px; if(input=='k' || input==KEY_UP){ --py; if(py==-1) py=LEN-1; } else if(input=='j' || input==KEY_DOWN){ ++py; if(py==LEN) py=0; } else if(input=='h' || input==KEY_LEFT){ --px; if(px==-1) px=WID-1; } else if(input=='l' || input==KEY_RIGHT){ ++px; if(px==WID) px=0; } else goto DidntMove; if(coherent){ rm_square(board,prey,prex); mk_square(board); } DidntMove: if( input=='q') sigint_handler(0); if( input=='p'){ nocbreak(); cbreak(); erase(); logo(); attron(A_BOLD); addstr("\n PAUSED"); attroff(A_BOLD); refresh(); getch(); halfdelay(9); } } printw("Wanna play again?(y/n)"); nocbreak(); cbreak(); curs_set(1); flushinp(); input=getch(); if(input != 'N' && input != 'n' && input != 'q') goto Start; endwin(); return EXIT_SUCCESS; }