mirror of
https://github.com/abakh/nbsdgames.git
synced 2025-02-02 15:07:27 -05:00
Going for version 3!
This commit is contained in:
parent
b8928c7832
commit
a5e83d6a56
31
README.md
31
README.md
@ -24,22 +24,10 @@ These include:
|
||||
* Pipes (Same as the famous Pipe Mania, unplayable on the environments that don't support the line characters)
|
||||
* Fifteen
|
||||
* Memoblocks (or Memory blocks. A similar game was included in Windows 7)
|
||||
|
||||
## Features
|
||||
* Modern looking TUI
|
||||
* Color
|
||||
* Mouse support
|
||||
* Multiplayer (not network)
|
||||
* Each game containing help pages on the controls and the gameplay
|
||||
* Very permissive license (Public Domain equivalent)
|
||||
* Clean code with documentation
|
||||
* Small size, with the deb package being less than 50 KB as I write this
|
||||
* Low in dependencies and very simple build, even in comparison to bsdgames
|
||||
* Low in CPU usage (Low if you are playing against AI, virtually nothing otherwise)
|
||||
* Low as possible in memory footprint
|
||||
* Diversity, you will like one at least
|
||||
* Portability
|
||||
* Flexiblity
|
||||
* Fisher
|
||||
* Muncher
|
||||
* Miketron
|
||||
* Redsquare
|
||||
|
||||
They natively run on Linux, BSD, MacOS and are known to work on Windows as well (using PDCurses, thanks to Laura Michaels for providing advice).
|
||||
|
||||
@ -81,10 +69,9 @@ It's been made available for openSUSE thanks to Jan Brezina: https://build.opens
|
||||
|
||||
|
||||
## How to contribute
|
||||
Oh, so kind! You can...
|
||||
* Share these with your friends
|
||||
* Tell me your feature requests, bug reports, games you want to be added etc. (Think of games in spirit of those already included and look if there is not a good terminal game for it already)
|
||||
* Make a package for your distro (or put it on repos and tell me afterwards)
|
||||
* Tell me if you're interested in porting it to non-Unix (Possible in theory since there is PDCurses for SDL, and SDL for everything)
|
||||
* With mouse support, PDCurses, SDL and NDK existing, now it is possible to port it to Android proper, feel free to tell me if you are doing it.
|
||||
* Pull requests are of course, welcome too!
|
||||
* Tell me your feature requests, bug reports, etc.
|
||||
* Tell me the games you want to be added (in the same genre, i can't port Angry Birds to curses! :)
|
||||
* Make a package for your distro (or put it on repos if it's not there)
|
||||
|
||||
Also thank to all the people who helped in the previous versions, i didn't expect such assistance <3
|
||||
|
1
sources/.#mines.c
Symbolic link
1
sources/.#mines.c
Symbolic link
@ -0,0 +1 @@
|
||||
user@laptop.7227:1577913591
|
BIN
sources/.jewels.c.swp
Normal file
BIN
sources/.jewels.c.swp
Normal file
Binary file not shown.
@ -1,6 +1,6 @@
|
||||
# -*- Makefile -*-
|
||||
|
||||
all: jewels sudoku mines reversi checkers battleship rabbithole sos pipes fifteen memoblocks
|
||||
all: jewels sudoku mines reversi checkers battleship rabbithole sos pipes fifteen memoblocks fisher muncher miketron redsquare
|
||||
|
||||
jewels: jewels.c config.h
|
||||
$(CC) jewels.c -lncurses -o ./jewels
|
||||
@ -24,12 +24,20 @@ fifteen: fifteen.c
|
||||
$(CC) fifteen.c -lncurses -o ./fifteen
|
||||
memoblocks: memoblocks.c
|
||||
$(CC) memoblocks.c -lncurses -o ./memoblocks
|
||||
fisher: fisher.c config.h
|
||||
$(CC) fisher.c -lncurses -o ./fisher
|
||||
muncher: muncher.c config.h
|
||||
$(CC) muncher.c -lncurses -o ./muncher
|
||||
miketron: miketron.c config.h
|
||||
$(CC) miketron.c -lncurses -o ./miketron
|
||||
redsquare: redsquare.c
|
||||
$(CC) redsquare.c -lncurses -o ./redsquare
|
||||
clean:
|
||||
rm ./jewels ./sudoku ./checkers ./mines ./reversi ./battleship ./rabbithole ./sos ./pipes ./fifteen ./memoblocks
|
||||
rm ./jewels ./sudoku ./checkers ./mines ./reversi ./battleship ./rabbithole ./sos ./pipes ./fifteen ./memoblocks ./fisher ./muncher ./miketron ./redsquare
|
||||
uninstall:
|
||||
rm $(PREFIX)/jewels $(PREFIX)/sudoku $(PREFIX)/checkers $(PREFIX)/mines $(PREFIX)/reversi $(PREFIX)/battleship $(PREFIX)/rabbithole $(PREFIX)/sos $(PREFIX)/pipes $(PREFIX)/fifteen $(PREFIX)/memoblocks
|
||||
rm $(PREFIX)/jewels $(PREFIX)/sudoku $(PREFIX)/checkers $(PREFIX)/mines $(PREFIX)/reversi $(PREFIX)/battleship $(PREFIX)/rabbithole $(PREFIX)/sos $(PREFIX)/pipes $(PREFIX)/fifteen $(PREFIX)/memoblocks $(PREFIX)/fisher $(PREFIX)/muncher $(PREFIX)/miketron $(PREFIX)/redsquare
|
||||
copy_sources:
|
||||
cp Makefile config.h jewels.c sudoku.c mines.c reversi.c checkers.c battleship.c rabbithole.c sos.c pipes.c fifteen.c memoblocks.c $(PREFIX)
|
||||
install: jewels sudoku mines reversi checkers battleship rabbithole sos pipes fifteen memoblocks
|
||||
cp jewels sudoku mines reversi checkers battleship rabbithole sos pipes fifteen memoblocks $(PREFIX)
|
||||
cp Makefile config.h jewels.c sudoku.c mines.c reversi.c checkers.c battleship.c rabbithole.c sos.c pipes.c fifteen.c memoblocks.c fisher.c muncher.c miketron.c redsquare.c $(PREFIX)
|
||||
install: jewels sudoku mines reversi checkers battleship rabbithole sos pipes fifteen memoblocks fisher muncher miketron redsquare
|
||||
cp jewels sudoku mines reversi checkers battleship rabbithole sos pipes fifteen memoblocks fisher muncher miketron redsquare $(PREFIX)
|
||||
|
||||
|
@ -40,31 +40,31 @@ byte lastinrowy ,lastinrowx;
|
||||
byte goindirection;
|
||||
byte shotinvain;
|
||||
void sigint_handler(int x){
|
||||
endwin();
|
||||
puts("Quit.");
|
||||
exit(x);
|
||||
endwin();
|
||||
puts("Quit.");
|
||||
exit(x);
|
||||
}
|
||||
void mouseinput(bool ingame){
|
||||
MEVENT minput;
|
||||
MEVENT minput;
|
||||
#ifdef PDCURSES
|
||||
nc_getmouse(&minput);
|
||||
#else
|
||||
getmouse(&minput);
|
||||
#endif
|
||||
if(minput.bstate & (BUTTON1_CLICKED|BUTTON1_RELEASED)){
|
||||
if(minput.bstate & (BUTTON1_CLICKED|BUTTON1_RELEASED)){
|
||||
if( minput.y-4 < 10){
|
||||
if( (ingame && minput.x-23<20 && minput.x-23>=0 ) || (!ingame && minput.x-1<20) ){//it most be on the trackboard if ingame is true
|
||||
py=minput.y-4;
|
||||
px=(minput.x-1-(ingame*2)) /2;
|
||||
py=minput.y-4;
|
||||
px=(minput.x-1-(ingame*2)) /2;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
return;
|
||||
}
|
||||
if(minput.bstate & (BUTTON1_CLICKED|BUTTON1_RELEASED))
|
||||
ungetch('\n');
|
||||
if(minput.bstate & (BUTTON2_CLICKED|BUTTON2_RELEASED|BUTTON3_CLICKED|BUTTON3_RELEASED) )
|
||||
ungetch('r');
|
||||
if(minput.bstate & (BUTTON1_CLICKED|BUTTON1_RELEASED))
|
||||
ungetch('\n');
|
||||
if(minput.bstate & (BUTTON2_CLICKED|BUTTON2_RELEASED|BUTTON3_CLICKED|BUTTON3_RELEASED) )
|
||||
ungetch('r');
|
||||
}
|
||||
void rectangle(byte sy,byte sx){
|
||||
for(byte y=0;y<=10+1;y++){
|
||||
@ -138,9 +138,17 @@ void header(bool side){
|
||||
mvaddch(0,1, '_');
|
||||
mvprintw(1,0,"|_) %2d:%2d",score[side],score[!side]);
|
||||
mvprintw(2,0,"|_)ATTLESHIP ");
|
||||
if(multiplayer){
|
||||
attron(colors[side]);
|
||||
if(side)
|
||||
printw("Yellow's turn");
|
||||
else
|
||||
printw("Green's turn");
|
||||
attroff(colors[side]);
|
||||
}
|
||||
}
|
||||
|
||||
void draw(bool side,byte sy,byte sx){//the game's board
|
||||
void draw(bool side,byte sy,byte sx,bool regular){//the game's board
|
||||
rectangle(sy,sx);
|
||||
chtype ch ;
|
||||
byte y,x;
|
||||
@ -151,12 +159,14 @@ void draw(bool side,byte sy,byte sx){//the game's board
|
||||
ch |= A_STANDOUT;
|
||||
if(game[side][y][x] == HIT)
|
||||
ch |= 'X'|colors[RED];
|
||||
else if(game[side][y][x] > 0 )
|
||||
else if(game[side][y][x] > 0 && !(multiplayer&®ular) )
|
||||
ch |= ACS_BLOCK|colors[side];
|
||||
else if(game[side][y][x]== MISS)
|
||||
ch |= 'O'|colors[CYAN];
|
||||
else
|
||||
else if(!(multiplayer&®ular))
|
||||
ch |= '~'|colors[CYAN];
|
||||
else
|
||||
ch |=' ';
|
||||
|
||||
mvaddch(sy+1+y,sx+x*2+1,ch);
|
||||
}
|
||||
@ -164,25 +174,25 @@ void draw(bool side,byte sy,byte sx){//the game's board
|
||||
}
|
||||
void draw_trackboard(bool side,byte sy,byte sx){
|
||||
rectangle(sy,sx);
|
||||
chtype ch ;
|
||||
byte y,x;
|
||||
for(y=0;y<10;y++){
|
||||
for(x=0;x<10;x++){
|
||||
ch =A_NORMAL;
|
||||
if(y==py && x==px-10)
|
||||
ch |= A_STANDOUT;
|
||||
chtype ch ;
|
||||
byte y,x;
|
||||
for(y=0;y<10;y++){
|
||||
for(x=0;x<10;x++){
|
||||
ch =A_NORMAL;
|
||||
if(y==py && x==px-10)
|
||||
ch |= A_STANDOUT;
|
||||
|
||||
if(game[!side][y][x] == HIT)
|
||||
ch |= '*'|colors[RED];
|
||||
else if(game[!side][y][x]== MISS)
|
||||
ch |= '~'|colors[CYAN];
|
||||
else
|
||||
ch |= '.';
|
||||
if(game[!side][y][x] == HIT)
|
||||
ch |= '*'|colors[RED];
|
||||
else if(game[!side][y][x]== MISS)
|
||||
ch |= '~'|colors[CYAN];
|
||||
else
|
||||
ch |= '.';
|
||||
|
||||
mvaddch(sy+1+y,sx+x*2+1,ch);
|
||||
}
|
||||
}
|
||||
refresh();
|
||||
mvaddch(sy+1+y,sx+x*2+1,ch);
|
||||
}
|
||||
}
|
||||
refresh();
|
||||
}
|
||||
|
||||
void autoset(bool side){
|
||||
@ -237,19 +247,19 @@ void set_the_board(bool side){
|
||||
addstr(" in its position: ");
|
||||
SetLocation:
|
||||
while(1){
|
||||
draw(side,3,0);
|
||||
draw(side,3,0,false);
|
||||
refresh();
|
||||
input = getch();
|
||||
if( input == KEY_MOUSE )
|
||||
mouseinput(0);
|
||||
if( (input=='k' || input==KEY_UP) && py>0)
|
||||
py--;
|
||||
if( (input=='j' || input==KEY_DOWN) && py<9)
|
||||
py++;
|
||||
if( (input=='h' || input==KEY_LEFT) && px>0)
|
||||
px--;
|
||||
if( (input=='l' || input==KEY_RIGHT) && px<9)
|
||||
px++;
|
||||
py--;
|
||||
if( (input=='j' || input==KEY_DOWN) && py<9)
|
||||
py++;
|
||||
if( (input=='h' || input==KEY_LEFT) && px>0)
|
||||
px--;
|
||||
if( (input=='l' || input==KEY_RIGHT) && px<9)
|
||||
px++;
|
||||
if( input=='\n' )
|
||||
break;
|
||||
if( input=='q' )
|
||||
@ -280,7 +290,7 @@ void set_the_board(bool side){
|
||||
}
|
||||
while(1){
|
||||
invain=0;
|
||||
draw(side,3,0);
|
||||
draw(side,3,0,false);
|
||||
input=getch();
|
||||
if( input== 'r' || input == 'R' ){
|
||||
genocide(side,type);
|
||||
@ -480,41 +490,41 @@ void decide(bool side){// sink_announce is responsible for unsetting the global
|
||||
}
|
||||
}
|
||||
void help(bool side){//side is only there to feed header()
|
||||
erase();
|
||||
erase();
|
||||
header(side);
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE CONTROLS ****");
|
||||
mvprintw(9,0,"YOU CAN ALSO USE THE MOUSE!");
|
||||
attroff(A_BOLD);
|
||||
mvprintw(4,0,"RETURN/ENTER : Shoot");
|
||||
mvprintw(5,0,"R : Rotate");
|
||||
mvprintw(6,0,"hjkl/ARROW KEYS : Move cursor");
|
||||
mvprintw(7,0,"q : Quit");
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE CONTROLS ****");
|
||||
mvprintw(9,0,"YOU CAN ALSO USE THE MOUSE!");
|
||||
attroff(A_BOLD);
|
||||
mvprintw(4,0,"RETURN/ENTER : Shoot");
|
||||
mvprintw(5,0,"R : Rotate");
|
||||
mvprintw(6,0,"hjkl/ARROW KEYS : Move cursor");
|
||||
mvprintw(7,0,"q : Quit");
|
||||
mvprintw(8,0,"F1 & F2 : Help on controls & gameplay");
|
||||
mvprintw(11,0,"Press a key to continue");
|
||||
getch();
|
||||
mvprintw(11,0,"Press a key to continue");
|
||||
getch();
|
||||
erase();
|
||||
}
|
||||
void gameplay(bool side){//side is only there to feed header()
|
||||
erase();
|
||||
header(side);
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE GAMEPLAY ****");
|
||||
attroff(A_BOLD);
|
||||
erase();
|
||||
header(side);
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE GAMEPLAY ****");
|
||||
attroff(A_BOLD);
|
||||
move(4,0);
|
||||
printw("Guess the location of your opponet's\n");
|
||||
printw("ships and sink them! The player\n");
|
||||
printw("who sinks all the opponet's ships wins.");
|
||||
getch();
|
||||
erase();
|
||||
getch();
|
||||
erase();
|
||||
}
|
||||
int main(void){
|
||||
initscr();
|
||||
mousemask(ALL_MOUSE_EVENTS,NULL);
|
||||
curs_set(0);
|
||||
noecho();
|
||||
cbreak();
|
||||
keypad(stdscr,1);
|
||||
curs_set(0);
|
||||
noecho();
|
||||
cbreak();
|
||||
keypad(stdscr,1);
|
||||
if( has_colors() ){
|
||||
start_color();
|
||||
use_default_colors();
|
||||
@ -526,23 +536,23 @@ int main(void){
|
||||
colors[b]=COLOR_PAIR(b+1);
|
||||
}
|
||||
int input;
|
||||
printw("Choose type of the game:\n");
|
||||
printw("Choose type of the game:\n");
|
||||
printw("1 : Single Player*\n");
|
||||
printw("2 : Multi Player\n");
|
||||
refresh();
|
||||
input=getch();
|
||||
if(input == '2'){
|
||||
input=getch();
|
||||
if(input == '2'){
|
||||
multiplayer=1;
|
||||
computer[1]=computer[0]=0;
|
||||
}
|
||||
else{
|
||||
computer[1]=computer[0]=0;
|
||||
}
|
||||
else{
|
||||
multiplayer=0;
|
||||
computer[1]=1;
|
||||
computer[1]=1;
|
||||
computer[0]=0;
|
||||
}
|
||||
}
|
||||
Start:
|
||||
firstinrowy=firstinrowx=lastinrowy=lastinrowx=goindirection=NOTHING;
|
||||
shotinvain=0;
|
||||
shotinvain=0;
|
||||
sunk[0]=sunk[1]=0;
|
||||
memset(game,SEA,200);
|
||||
srand(time(NULL)%UINT_MAX);
|
||||
@ -558,16 +568,16 @@ int main(void){
|
||||
py=0;
|
||||
sink_announce(turn);
|
||||
if( sunk[0]==ALL ){
|
||||
won=1;
|
||||
goto End;
|
||||
}
|
||||
else if( sunk[1]==ALL ){
|
||||
won=0;
|
||||
goto End;
|
||||
}
|
||||
won=1;
|
||||
goto End;
|
||||
}
|
||||
else if( sunk[1]==ALL ){
|
||||
won=0;
|
||||
goto End;
|
||||
}
|
||||
//the turn starts HERE
|
||||
turn=!turn;
|
||||
turn_shift();
|
||||
//turn_shift();
|
||||
if( computer[turn] ){
|
||||
decide(turn);
|
||||
goto Turn;
|
||||
@ -577,7 +587,7 @@ int main(void){
|
||||
you_sunk(turn);
|
||||
while(1){
|
||||
header(turn);
|
||||
draw(turn,3,0);
|
||||
draw(turn,3,0,true);
|
||||
draw_trackboard(turn,3,22);
|
||||
refresh();
|
||||
input=getch();
|
||||
@ -588,14 +598,14 @@ int main(void){
|
||||
if(input == KEY_MOUSE)
|
||||
mouseinput(1);
|
||||
if( (input=='k' || input==KEY_UP) && py>0)
|
||||
py--;
|
||||
if( (input=='j' || input==KEY_DOWN) && py<9)
|
||||
py++;
|
||||
if( (input=='h' || input==KEY_LEFT) && px>10)
|
||||
px--;
|
||||
if( (input=='l' || input==KEY_RIGHT) && px<19)
|
||||
px++;
|
||||
if( input=='q')
|
||||
py--;
|
||||
if( (input=='j' || input==KEY_DOWN) && py<9)
|
||||
py++;
|
||||
if( (input=='h' || input==KEY_LEFT) && px>10)
|
||||
px--;
|
||||
if( (input=='l' || input==KEY_RIGHT) && px<19)
|
||||
px++;
|
||||
if( input=='q')
|
||||
sigint_handler(EXIT_SUCCESS);
|
||||
if( input=='\n'){
|
||||
byte r=shoot(turn,py,px-10);
|
||||
@ -608,7 +618,7 @@ int main(void){
|
||||
End:
|
||||
erase();
|
||||
header(won);
|
||||
draw(won,3,0);
|
||||
draw(won,3,0,false);
|
||||
draw_trackboard(won,3,22);
|
||||
if( computer[won] )
|
||||
mvaddstr(15,0,"Hahaha! I won! ");
|
||||
|
@ -37,10 +37,10 @@ byte jumpagainy , jumpagainx;
|
||||
bool kinged;//if a piece jumps over multiple others and becomes a king it cannot continue jumping
|
||||
|
||||
bool in(byte A[4],byte B[4],byte a,byte b){
|
||||
for(byte c=0;c<4;c++)
|
||||
if(A[c]==a && B[c]==b)
|
||||
return true;
|
||||
return false;
|
||||
for(byte c=0;c<4;c++)
|
||||
if(A[c]==a && B[c]==b)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
void rectangle(byte sy,byte sx){
|
||||
byte y,x;
|
||||
@ -97,7 +97,7 @@ void draw(byte sy,byte sx){//the game's board
|
||||
else
|
||||
ch |='K';
|
||||
}
|
||||
else if( (y%2) != (x%2) )
|
||||
else if( (y%2) != (x%2) )
|
||||
ch|='.';
|
||||
else
|
||||
ch|=' ';
|
||||
@ -439,44 +439,44 @@ void sigint_handler(int x){
|
||||
}
|
||||
|
||||
void mouseinput(void){
|
||||
MEVENT minput;
|
||||
MEVENT minput;
|
||||
#ifdef PDCURSES
|
||||
nc_getmouse(&minput);
|
||||
#else
|
||||
getmouse(&minput);
|
||||
#endif
|
||||
if( minput.y-4 <8 && minput.x-1<16){
|
||||
py=minput.y-4;
|
||||
px=(minput.x-1)/2;
|
||||
}
|
||||
else
|
||||
return;
|
||||
if(minput.bstate & (BUTTON1_CLICKED|BUTTON1_PRESSED|BUTTON1_RELEASED) )
|
||||
ungetch('\n');
|
||||
if( minput.y-4 <8 && minput.x-1<16){
|
||||
py=minput.y-4;
|
||||
px=(minput.x-1)/2;
|
||||
}
|
||||
else
|
||||
return;
|
||||
if(minput.bstate & (BUTTON1_CLICKED|BUTTON1_PRESSED|BUTTON1_RELEASED) )
|
||||
ungetch('\n');
|
||||
}
|
||||
void help(void){
|
||||
erase();
|
||||
erase();
|
||||
header();
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE CONTROLS ****");
|
||||
mvprintw(9,0,"YOU CAN ALSO USE THE MOUSE!");
|
||||
attroff(A_BOLD);
|
||||
mvprintw(4,0,"RETURN/ENTER : Select or move the piece");
|
||||
mvprintw(5,0,"SPACE : Flag/Unflag");
|
||||
mvprintw(6,0,"hjkl/ARROW KEYS : Move cursor");
|
||||
mvprintw(7,0,"q : quit");
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE CONTROLS ****");
|
||||
mvprintw(9,0,"YOU CAN ALSO USE THE MOUSE!");
|
||||
attroff(A_BOLD);
|
||||
mvprintw(4,0,"RETURN/ENTER : Select or move the piece");
|
||||
mvprintw(5,0,"SPACE : Flag/Unflag");
|
||||
mvprintw(6,0,"hjkl/ARROW KEYS : Move cursor");
|
||||
mvprintw(7,0,"q : quit");
|
||||
mvprintw(8,0,"F1 & F2 : Help on controls & gameplay");
|
||||
mvprintw(11,0,"Press a key to continue");
|
||||
mvprintw(11,0,"Press a key to continue");
|
||||
refresh();
|
||||
getch();
|
||||
getch();
|
||||
erase();
|
||||
}
|
||||
void gameplay(void){
|
||||
erase();
|
||||
header();
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE GAMEPLAY ****");
|
||||
attroff(A_BOLD);
|
||||
erase();
|
||||
header();
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE GAMEPLAY ****");
|
||||
attroff(A_BOLD);
|
||||
move(4,0);
|
||||
printw("1) The game starts with each player having 12 men;\n");
|
||||
printw(" men can only diagonally move forwards \n");
|
||||
@ -490,9 +490,9 @@ void gameplay(void){
|
||||
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(" all of their pieces are captured.\n\n");
|
||||
refresh();
|
||||
getch();
|
||||
erase();
|
||||
refresh();
|
||||
getch();
|
||||
erase();
|
||||
}
|
||||
int main(int argc,char** argv){
|
||||
dpt=4;
|
||||
@ -525,7 +525,7 @@ int main(int argc,char** argv){
|
||||
computer[0]=0;
|
||||
printw("Human.\n");
|
||||
}
|
||||
printw("Choose type of the light player(h/C)\n");
|
||||
printw("Choose type of the bright player(h/C)\n");
|
||||
refresh();
|
||||
input=getch();
|
||||
if(input=='h'){
|
||||
@ -604,6 +604,15 @@ int main(int argc,char** argv){
|
||||
erase();
|
||||
draw(3,0);
|
||||
header();
|
||||
if(!(computer[0]||computer[1])){
|
||||
if(t)
|
||||
addstr(" Bright's turn");
|
||||
else{
|
||||
attron(COLOR_PAIR(1));
|
||||
addstr(" Dark's turn");
|
||||
attroff(COLOR_PAIR(1));
|
||||
}
|
||||
}
|
||||
refresh();
|
||||
input=getch();
|
||||
if( input == KEY_F(1) || input=='?' )
|
||||
@ -661,7 +670,7 @@ int main(int argc,char** argv){
|
||||
printw("Draw.");
|
||||
break;
|
||||
case 1:
|
||||
printw("The light side has won the game.");
|
||||
printw("The bright side has won the game.");
|
||||
break;
|
||||
case 2:
|
||||
printw("You resigned.");
|
||||
|
@ -1,3 +1,6 @@
|
||||
#define PP_SCORES "/usr/games/pp_scores"
|
||||
#define JW_SCORES "/usr/games/jw_scores"
|
||||
#define FSH_SCORES "/usr/games/fsh_scores"
|
||||
#define MNCH_SCORES "/usr/games/mnch_scores"
|
||||
#define MT_SCORES "/usr/games/mt_scores"
|
||||
//for easier access
|
||||
|
@ -42,13 +42,13 @@ void logo(byte sy,byte sx){
|
||||
char int2sgn(byte num){
|
||||
if(!num)
|
||||
return ' ';
|
||||
else if(0< num && num <= 9)
|
||||
return num+'0';
|
||||
else if(10<=num && num <=35)
|
||||
return num-10+'a';
|
||||
else if(36<=num && num <=51)
|
||||
return num-36+'A';
|
||||
return 0;
|
||||
else if(0< num && num <= 9)
|
||||
return num+'0';
|
||||
else if(10<=num && num <=35)
|
||||
return num-10+'a';
|
||||
else if(36<=num && num <=51)
|
||||
return num-36+'A';
|
||||
return 0;
|
||||
}
|
||||
/*bool isinorder(byte board[size][size],byte y,byte x){ using check[][] is much cheaper
|
||||
return (board[y][x] == y*size+x+1);
|
||||
@ -150,38 +150,38 @@ void mouseinput(void){
|
||||
ungetch('\n');
|
||||
}
|
||||
void help(void){
|
||||
erase();
|
||||
erase();
|
||||
logo(0,0);
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE CONTROLS ****");
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE CONTROLS ****");
|
||||
mvprintw(8,0,"YOU CAN ALSO USE THE MOUSE!");
|
||||
attroff(A_BOLD);
|
||||
attroff(A_BOLD);
|
||||
mvprintw(4,0,"RETURN/ENTER : Slide");
|
||||
mvprintw(5,0,"hjkl/ARROW KEYS : Move cursor");
|
||||
mvprintw(6,0,"q : Quit");
|
||||
mvprintw(5,0,"hjkl/ARROW KEYS : Move cursor");
|
||||
mvprintw(6,0,"q : Quit");
|
||||
mvprintw(7,0,"F1 & F2 : Help on controls & gameplay");
|
||||
mvprintw(10,0,"Press a key to continue");
|
||||
mvprintw(10,0,"Press a key to continue");
|
||||
refresh();
|
||||
getch();
|
||||
getch();
|
||||
erase();
|
||||
}
|
||||
void gameplay(void){
|
||||
erase();
|
||||
erase();
|
||||
logo(0,0);
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE GAMEPLAY ****");
|
||||
attroff(A_BOLD);
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE GAMEPLAY ****");
|
||||
attroff(A_BOLD);
|
||||
mvprintw(4,0,"Slide the tiles until the numbers and characters are\n");
|
||||
printw("in the right order.\n");
|
||||
refresh();
|
||||
getch();
|
||||
erase();
|
||||
refresh();
|
||||
getch();
|
||||
erase();
|
||||
}
|
||||
int main(int argc, char** argv){
|
||||
size=4;
|
||||
if(argc==2){
|
||||
if(!strcmp("help",argv[1])){
|
||||
printf("Usage: %s [size]\n",argv[0]);
|
||||
printf("Usage: %s [size]\n",argv[0]);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
size=atoi(argv[1]);
|
||||
@ -193,20 +193,20 @@ int main(int argc, char** argv){
|
||||
signal(SIGINT,sigint_handler);
|
||||
srand(time(NULL)%UINT_MAX);
|
||||
initscr();
|
||||
mousemask(ALL_MOUSE_EVENTS,NULL);
|
||||
noecho();
|
||||
cbreak();
|
||||
keypad(stdscr,1);
|
||||
if(has_colors()){
|
||||
start_color();
|
||||
use_default_colors();
|
||||
init_pair(1,COLOR_GREEN,-1);
|
||||
green=COLOR_PAIR(1);
|
||||
}
|
||||
char board[size][size];
|
||||
mousemask(ALL_MOUSE_EVENTS,NULL);
|
||||
noecho();
|
||||
cbreak();
|
||||
keypad(stdscr,1);
|
||||
if(has_colors()){
|
||||
start_color();
|
||||
use_default_colors();
|
||||
init_pair(1,COLOR_GREEN,-1);
|
||||
green=COLOR_PAIR(1);
|
||||
}
|
||||
char board[size][size];
|
||||
char check[size][size];
|
||||
fill(check);
|
||||
int input;
|
||||
int input;
|
||||
Start:
|
||||
py=px=0;
|
||||
ey=ex=size-1;
|
||||
|
415
sources/fisher.c
Normal file
415
sources/fisher.c
Normal file
@ -0,0 +1,415 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#include <curses.h>
|
||||
#include <unistd.h>
|
||||
#include "config.h"
|
||||
#define SAVE_TO_NUM 11
|
||||
#define HOOKS 10
|
||||
#define LEN 24
|
||||
#define HLEN LEN/2
|
||||
#define WID 80
|
||||
#define HWID WID/2
|
||||
typedef signed char byte;
|
||||
chtype colors[4]={A_NORMAL,A_STANDOUT};
|
||||
byte fish[10]={0};//positions
|
||||
byte caught=-1;
|
||||
bool stop[10]={0};
|
||||
unsigned int count[10]={0};
|
||||
unsigned long score=0;
|
||||
const char sym[]="~:=!@+><;&";
|
||||
byte hook=0, hooknum=0;
|
||||
char error [150]={0};
|
||||
byte clb,clbtime=0;
|
||||
FILE* scorefile;
|
||||
|
||||
int input;
|
||||
/*
|
||||
O__/|
|
||||
___|_/ | __
|
||||
| / | |__
|
||||
| ISHER*/
|
||||
// 12 lines of water
|
||||
// 80 columns
|
||||
byte digit_count(int num){
|
||||
byte ret=0;
|
||||
do{
|
||||
ret++;
|
||||
num/=10;
|
||||
}while(num);
|
||||
return ret;
|
||||
}
|
||||
void filled_rect(byte sy,byte sx,byte ey,byte ex){
|
||||
byte y,x;
|
||||
for(y=sy;y<ey;y++)
|
||||
for(x=sx;x<ex;x++)
|
||||
mvaddch(y,x,' ');
|
||||
}
|
||||
void green_border(void){
|
||||
byte y,x;
|
||||
for(y=0;y<LEN;y++){
|
||||
mvaddch(y,WID-1,' '|colors[2]);
|
||||
mvaddch(y,0,' '|colors[2]);
|
||||
}
|
||||
for(x=0;x<WID;x++){
|
||||
mvaddch(LEN-1,x,' '|colors[2]);
|
||||
mvaddch(0,x,' '|colors[2]);
|
||||
}
|
||||
|
||||
}
|
||||
void star_line(byte y){
|
||||
for(byte x=1;x<WID-1;x++)
|
||||
mvaddch(y,x,'.');
|
||||
}
|
||||
void draw(void){
|
||||
/*while(LEN< 15 || COL<80)
|
||||
mvprintw(0,0,"Screen size should at least be 80*15 characters");*/
|
||||
attron(colors[0]);
|
||||
byte y,x;
|
||||
filled_rect(0,0,12,80);
|
||||
mvprintw(0,0," __ Hooks:%d",hooknum);
|
||||
mvprintw(1,0,"|__ Score:%d",score);
|
||||
mvprintw(2,0,"| ISHER");
|
||||
mvprintw(9,32, " O__/");
|
||||
mvprintw(10,32," ___|_/ ");
|
||||
mvprintw(11,32,"| / ");
|
||||
|
||||
if(clbtime){
|
||||
if(count[clb]!=1){
|
||||
mvprintw(2,10,"%d ",count[clb]);
|
||||
switch(clb){
|
||||
case 0:
|
||||
addstr("plastic bags!");
|
||||
break;
|
||||
case 1:
|
||||
addstr("PVC bottles!");
|
||||
break;
|
||||
case 2:
|
||||
addstr("face masks!");
|
||||
break;
|
||||
case 3:
|
||||
addstr("shrimp!");
|
||||
break;
|
||||
case 4:
|
||||
addstr("algae!");
|
||||
break;
|
||||
case 5:
|
||||
addstr("jellyfish!");
|
||||
break;
|
||||
case 6:
|
||||
addstr("molluscs!");
|
||||
break;
|
||||
case 7:
|
||||
addstr("actual fish!");
|
||||
break;
|
||||
case 8:
|
||||
addstr("salmon!");
|
||||
break;
|
||||
case 9:
|
||||
addstr("tuna!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
else{
|
||||
switch(clb){
|
||||
case 0:
|
||||
addstr("A plastic bag!");
|
||||
break;
|
||||
case 1:
|
||||
addstr("A PVC bottle!");
|
||||
break;
|
||||
case 2:
|
||||
addstr("A face mask!");
|
||||
break;
|
||||
case 3:
|
||||
addstr("A shrimp!");
|
||||
break;
|
||||
case 4:
|
||||
addstr("Algae!");
|
||||
break;
|
||||
case 5:
|
||||
addstr("A jellyfish!");
|
||||
break;
|
||||
case 6:
|
||||
addstr("A mollusc!");
|
||||
break;
|
||||
case 7:
|
||||
addstr("Actual fish!");
|
||||
break;
|
||||
case 8:
|
||||
addstr("A salmon!");
|
||||
break;
|
||||
case 9:
|
||||
addstr("A tuna!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for(y=-3;y<0;y++)
|
||||
mvaddch(HLEN+y,HWID,ACS_VLINE);
|
||||
attroff(colors[0]);
|
||||
attron(colors[1]);
|
||||
filled_rect(HLEN,0,LEN,WID);
|
||||
for(y=0;y<hook;y++)
|
||||
mvaddch(HLEN+y,HWID,ACS_VLINE);
|
||||
if(caught==-1)
|
||||
mvaddch(HLEN+hook,HWID,')');
|
||||
else
|
||||
mvaddch(HLEN+hook,HWID,sym[caught]);
|
||||
for(y=0;y<10;y++)
|
||||
mvaddch(HLEN+1+y,fish[y],sym[y]);
|
||||
attroff(colors[1]);
|
||||
|
||||
}
|
||||
byte scorewrite(void){// only saves the top 10, returns the place in the chart
|
||||
bool deforno;
|
||||
if( !getenv("FSH_SCORES") && (scorefile= fopen(FSH_SCORES,"r")) ){
|
||||
deforno=1;
|
||||
}
|
||||
else{
|
||||
deforno=0;
|
||||
if( !(scorefile = fopen(getenv("FSH_SCORES"),"r")) ){
|
||||
sprintf(error,"No accessible score files found. You can make an empty text file in %s or set FSH_SCORES to such a file to solve this.",FSH_SCORES);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
char namebuff[SAVE_TO_NUM][60];
|
||||
long scorebuff[SAVE_TO_NUM];
|
||||
|
||||
memset(namebuff,0,SAVE_TO_NUM*60*sizeof(char) );
|
||||
memset(scorebuff,0,SAVE_TO_NUM*sizeof(long) );
|
||||
|
||||
long fuckingscore =0;
|
||||
char fuckingname[60]={0};
|
||||
byte location=0;
|
||||
|
||||
while( fscanf(scorefile,"%59s : %ld\n",fuckingname,&fuckingscore) == 2 && location<SAVE_TO_NUM ){
|
||||
strcpy(namebuff[location],fuckingname);
|
||||
scorebuff[location] = fuckingscore;
|
||||
location++;
|
||||
|
||||
memset(fuckingname,0,60);
|
||||
fuckingscore=0;
|
||||
}
|
||||
if(deforno)
|
||||
scorefile = fopen(FSH_SCORES,"w+");//get rid of the previous text first
|
||||
else
|
||||
scorefile = fopen(getenv("FSH_SCORES"), "w+") ;
|
||||
if(!scorefile){
|
||||
strcpy(error, "The file cannot be opened in w+. ");
|
||||
return -1;
|
||||
}
|
||||
|
||||
byte itreached=location;
|
||||
byte ret = -1;
|
||||
bool wroteit=0;
|
||||
for(location=0;location<=itreached && location<SAVE_TO_NUM-wroteit;location++){
|
||||
if(!wroteit && (location>=itreached || score>=scorebuff[location]) ){
|
||||
fprintf(scorefile,"%s : %ld\n",getenv("USER"),score);
|
||||
ret=location;
|
||||
wroteit=1;
|
||||
}
|
||||
if(location<SAVE_TO_NUM-wroteit && location<itreached)
|
||||
fprintf(scorefile,"%s : %ld\n",namebuff[location],scorebuff[location]);
|
||||
}
|
||||
fflush(scorefile);
|
||||
return ret;
|
||||
}
|
||||
void showscores(byte playerrank){
|
||||
byte y,x;
|
||||
attron(colors[3]);
|
||||
filled_rect(0,0,LEN,WID);
|
||||
green_border();
|
||||
if(*error){
|
||||
mvaddstr(1,0,error);
|
||||
mvprintw(2,0,"However, your score is %ld.",score);
|
||||
refresh();
|
||||
return;
|
||||
}
|
||||
if(playerrank == 0){
|
||||
char formername[60]={0};
|
||||
long formerscore=0;
|
||||
rewind(scorefile);
|
||||
fscanf(scorefile,"%*s : %*d");
|
||||
if ( fscanf(scorefile,"%s : %ld",formername,&formerscore)==2 && formerscore>0){
|
||||
byte a = (LEN-9)/2;
|
||||
star_line(1);
|
||||
star_line(LEN-2);
|
||||
mvaddstr(1,WID/2-8,"CONGRATULATIONS!!");
|
||||
mvprintw(a+1,HWID-10," _____ You bet the");
|
||||
mvprintw(a+2,HWID-10," .' | previous");
|
||||
mvprintw(a+3,HWID-10," .' | record");
|
||||
mvprintw(a+4,HWID-10," | .| | of");
|
||||
mvprintw(a+5,HWID-10," |.' | |%11ld",formerscore);
|
||||
mvprintw(a+6,HWID-10," | | held by");
|
||||
mvprintw(a+7,HWID-10," ___| |___%7s!",formername);
|
||||
mvprintw(a+8,HWID-10," | |");
|
||||
mvprintw(a+9,HWID-10," |____________|");
|
||||
mvprintw(LEN-3,HWID-11,"Press a key to continue");
|
||||
refresh();
|
||||
do{
|
||||
input=getch();
|
||||
}while(input==KEY_UP || input==KEY_DOWN);
|
||||
filled_rect(0,0,LEN,WID);
|
||||
green_border();
|
||||
}
|
||||
|
||||
}
|
||||
//scorefile is still open with w+
|
||||
char pname[60] = {0};
|
||||
long pscore=0;
|
||||
byte rank=0;
|
||||
rewind(scorefile);
|
||||
mvaddstr(1,WID/2-4,"HIGH SCORES");
|
||||
attron(colors[3]);
|
||||
while( rank<SAVE_TO_NUM && fscanf(scorefile,"%s : %ld\n",pname,&pscore) == 2){
|
||||
star_line(2+2*rank);
|
||||
move(2+2*rank,1);
|
||||
if(rank == playerrank)
|
||||
printw(">>>");
|
||||
printw("%s",pname);
|
||||
mvprintw(2+2*rank,WID-1-digit_count(pscore),"%d",pscore);
|
||||
rank++;
|
||||
}
|
||||
attroff(colors[3]);
|
||||
refresh();
|
||||
}
|
||||
void help(void){
|
||||
nocbreak();
|
||||
cbreak();
|
||||
attron(colors[3]);
|
||||
filled_rect(0,0,LEN,WID);
|
||||
green_border();
|
||||
mvprintw(1,HWID-4,"GAME PLAY");
|
||||
mvprintw(3,1,"Catch a fish and reel it in for points");
|
||||
mvprintw(4,1,"The deeper the fish, the more points it is worth.");
|
||||
mvprintw(5,1,"If a fish hits your line, you lose a hook.");
|
||||
mvprintw(6,1,"When you run out of hooks, the game is over!");
|
||||
mvprintw(8,HWID-4,"CONTROLS");
|
||||
mvprintw(10,1,"UP & DOWN: Control the hook");
|
||||
mvprintw(11,1,"q: Quit");
|
||||
mvprintw(13,1,"This is a port of \"Deep Sea Fisher\", a MikeOS game.");
|
||||
attroff(colors[3]);
|
||||
refresh();
|
||||
getch();
|
||||
halfdelay(1);
|
||||
}
|
||||
void sigint_handler(int x){
|
||||
endwin();
|
||||
puts("Quit.");
|
||||
exit(x);
|
||||
}
|
||||
int main(void){
|
||||
signal(SIGINT,sigint_handler);
|
||||
initscr();
|
||||
noecho();
|
||||
cbreak();
|
||||
keypad(stdscr,1);
|
||||
srand(time(NULL)%UINT_MAX);
|
||||
for(byte n=0;n<10;n++)
|
||||
fish[n]=rand()%80;
|
||||
if(has_colors()){
|
||||
start_color();
|
||||
init_pair(1,COLOR_BLACK,COLOR_CYAN);
|
||||
init_pair(2,COLOR_BLACK,COLOR_BLUE);
|
||||
init_pair(3,COLOR_WHITE,COLOR_GREEN);
|
||||
init_pair(4,COLOR_BLACK,COLOR_WHITE);
|
||||
for(byte b=0;b<4;b++)
|
||||
colors[b]=COLOR_PAIR(b+1);
|
||||
}
|
||||
byte n;
|
||||
Start:
|
||||
halfdelay(1);
|
||||
curs_set(0);
|
||||
hook=0;
|
||||
hooknum=HOOKS;
|
||||
score=0;
|
||||
while(1){
|
||||
draw();
|
||||
refresh();
|
||||
input=getch();
|
||||
for(n=0;n<10;n++){
|
||||
if(stop[n]){
|
||||
if(rand()%(n+15)==0)//this is to make the fish move
|
||||
stop[n]=0;
|
||||
continue;
|
||||
}
|
||||
if(n%2)
|
||||
fish[n]--;
|
||||
else
|
||||
fish[n]++;
|
||||
if(fish[n]<0)
|
||||
fish[n]=79;
|
||||
if(fish[n]>79)
|
||||
fish[n]=0;//appear on the other end
|
||||
if(fish[n]==40){
|
||||
if(hook>n+1){
|
||||
caught= -1;
|
||||
hook=0;
|
||||
hooknum--;
|
||||
}
|
||||
if(hook==n+1 && caught==-1){
|
||||
caught=n;
|
||||
if(n%2)
|
||||
fish[n]=79;
|
||||
else
|
||||
fish[n]=0;
|
||||
}
|
||||
}
|
||||
if(rand()%(14-n)==0)//this is to make it stop
|
||||
stop[n]=1;
|
||||
}
|
||||
if(input==KEY_UP)
|
||||
if(hook>0)
|
||||
hook--;
|
||||
if(hook==0 && caught!=-1){
|
||||
count[caught]++;
|
||||
score+=(caught+1)*(caught+1);
|
||||
clb=caught;
|
||||
clbtime=10;//celebrate catching the fish
|
||||
caught=-1;
|
||||
}
|
||||
|
||||
if(input==KEY_DOWN){
|
||||
if(hook<11)
|
||||
hook++;
|
||||
if(fish[hook-1]==40 && caught==-1){
|
||||
caught=hook-1;
|
||||
if(n%2)
|
||||
fish[hook-1]=0;
|
||||
else
|
||||
fish[hook-1]=79;
|
||||
}
|
||||
}
|
||||
if(input=='?' || input==KEY_F(1))
|
||||
help();
|
||||
if(input=='q')
|
||||
break;
|
||||
if(!hooknum)
|
||||
break;
|
||||
if(input!=ERR){
|
||||
usleep(100000);
|
||||
flushinp();
|
||||
}
|
||||
}
|
||||
End:
|
||||
flushinp();
|
||||
nocbreak();
|
||||
cbreak();
|
||||
curs_set(1);
|
||||
showscores(scorewrite());
|
||||
attron(colors[2]);
|
||||
mvprintw(LEN-1,HWID-11,"Wanna play again? (y/n)");
|
||||
attroff(colors[2]);
|
||||
do{
|
||||
input=getch();
|
||||
}while(input==KEY_UP || input==KEY_DOWN);
|
||||
if(input!='q' && input!='n' && input!='N')
|
||||
goto Start;
|
||||
endwin();
|
||||
}
|
@ -33,7 +33,7 @@ char* controls = "j,l-Move k-Rotate p-Pause q-Quit";
|
||||
FILE* scorefile;
|
||||
byte scorewrite(long score){// only saves the top 10
|
||||
bool deforno;
|
||||
if( !getenv("JW_SCORES") && (scorefile= fopen(JW_SCORES,"r")) ){
|
||||
if( !getenv("JW_SCORES") && (scorefile= fopen(JW_SCORES,"r")) ){
|
||||
deforno=1;
|
||||
}
|
||||
else{
|
||||
@ -44,26 +44,26 @@ byte scorewrite(long score){// only saves the top 10
|
||||
}
|
||||
}
|
||||
|
||||
char namebuff[SAVE_TO_NUM][60];
|
||||
long scorebuff[SAVE_TO_NUM];
|
||||
char namebuff[SAVE_TO_NUM][60];
|
||||
long scorebuff[SAVE_TO_NUM];
|
||||
|
||||
memset(namebuff,0,SAVE_TO_NUM*60*sizeof(char) );
|
||||
memset(scorebuff,0,SAVE_TO_NUM*sizeof(long) );
|
||||
memset(namebuff,0,SAVE_TO_NUM*60*sizeof(char) );
|
||||
memset(scorebuff,0,SAVE_TO_NUM*sizeof(long) );
|
||||
|
||||
long fuckingscore =0;
|
||||
char fuckingname[60]={0};
|
||||
byte location=0;
|
||||
long fuckingscore =0;
|
||||
char fuckingname[60]={0};
|
||||
byte location=0;
|
||||
|
||||
while( fscanf(scorefile,"%59s : %ld\n",fuckingname,&fuckingscore) == 2 && location<SAVE_TO_NUM ){
|
||||
strcpy(namebuff[location],fuckingname);
|
||||
scorebuff[location] = fuckingscore;
|
||||
location++;
|
||||
while( fscanf(scorefile,"%59s : %ld\n",fuckingname,&fuckingscore) == 2 && location<SAVE_TO_NUM ){
|
||||
strcpy(namebuff[location],fuckingname);
|
||||
scorebuff[location] = fuckingscore;
|
||||
location++;
|
||||
|
||||
memset(fuckingname,0,60);
|
||||
fuckingscore=0;
|
||||
}
|
||||
memset(fuckingname,0,60);
|
||||
fuckingscore=0;
|
||||
}
|
||||
if(deforno)
|
||||
scorefile = fopen(JW_SCORES,"w+");//get rid of the text
|
||||
scorefile = fopen(JW_SCORES,"w+");//get rid of the text
|
||||
else
|
||||
scorefile = fopen(getenv("JW_SCORES"), "w+") ;
|
||||
if(!scorefile){
|
||||
@ -71,21 +71,21 @@ byte scorewrite(long score){// only saves the top 10
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
byte itreached=location;
|
||||
byte ret = -1;
|
||||
bool wroteit=0;
|
||||
byte itreached=location;
|
||||
byte ret = -1;
|
||||
bool wroteit=0;
|
||||
|
||||
for(location=0;location<=itreached && location<SAVE_TO_NUM-wroteit;location++){
|
||||
if(!wroteit && (location>=itreached || score>=scorebuff[location]) ){
|
||||
fprintf(scorefile,"%s : %ld\n",getenv("USER"),score);
|
||||
ret=location;
|
||||
wroteit=1;
|
||||
}
|
||||
if(location<SAVE_TO_NUM-wroteit && location<itreached)
|
||||
fprintf(scorefile,"%s : %ld\n",namebuff[location],scorebuff[location]);
|
||||
}
|
||||
fflush(scorefile);
|
||||
return ret;
|
||||
for(location=0;location<=itreached && location<SAVE_TO_NUM-wroteit;location++){
|
||||
if(!wroteit && (location>=itreached || score>=scorebuff[location]) ){
|
||||
fprintf(scorefile,"%s : %ld\n",getenv("USER"),score);
|
||||
ret=location;
|
||||
wroteit=1;
|
||||
}
|
||||
if(location<SAVE_TO_NUM-wroteit && location<itreached)
|
||||
fprintf(scorefile,"%s : %ld\n",namebuff[location],scorebuff[location]);
|
||||
}
|
||||
fflush(scorefile);
|
||||
return ret;
|
||||
}
|
||||
void showscores(byte playerrank){
|
||||
if(playerrank == 0){
|
||||
@ -97,12 +97,12 @@ void showscores(byte playerrank){
|
||||
printf("\n*****CONGRATULATIONS!****\n");
|
||||
printf(" _____ You bet the\n");
|
||||
printf(" .' | previous\n");
|
||||
printf(" .' | record\n");
|
||||
printf(" | .| | of\n");
|
||||
printf(" .' | record\n");
|
||||
printf(" | .| | of\n");
|
||||
printf(" |.' | |%14ld\n",formerscore);
|
||||
printf(" | | held by\n");
|
||||
printf(" ___| |___%11s\n",formername);
|
||||
printf(" | |\n");
|
||||
printf(" | |\n");
|
||||
printf(" |____________|\n");
|
||||
printf("*************************\n");
|
||||
}
|
||||
@ -154,13 +154,14 @@ void clockwise(byte* y,byte* x){
|
||||
o x
|
||||
x xo o ox*/
|
||||
chtype fx,fy;
|
||||
if(*y)
|
||||
if(*y){
|
||||
fy=0;
|
||||
fx=-*y;
|
||||
|
||||
if(*x)
|
||||
}
|
||||
if(*x){
|
||||
fx=0;
|
||||
fy=*x;
|
||||
}
|
||||
*y=fy;
|
||||
*x=fx;
|
||||
}
|
||||
@ -243,13 +244,13 @@ bool explode(byte combo){
|
||||
uc=c;
|
||||
c = board[y][x];
|
||||
if(c && c == uc){
|
||||
n++;
|
||||
n++;
|
||||
if(n>=3 && y==LEN-1){
|
||||
y++;
|
||||
goto VrExplsn;
|
||||
}
|
||||
}
|
||||
else if(n>=3){
|
||||
else if(n>=3){
|
||||
VrExplsn:
|
||||
score+=n*10*(n-2)*combo;
|
||||
ret=1;
|
||||
|
@ -41,15 +41,15 @@ void logo(byte sy,byte sx){
|
||||
}
|
||||
//convert integer to representing sign
|
||||
char int2sgn(byte num){
|
||||
if(0< num && num <= 9)
|
||||
return num+'0';
|
||||
else if(10<=num && num <=35)
|
||||
return num-10+'a';
|
||||
else if(36<=num && num <=51)
|
||||
return num-36+'A';
|
||||
if(0< num && num <= 9)
|
||||
return num+'0';
|
||||
else if(10<=num && num <=35)
|
||||
return num-10+'a';
|
||||
else if(36<=num && num <=51)
|
||||
return num-36+'A';
|
||||
else if(52<=num && num<=64)
|
||||
return num-52+'!';
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
//display
|
||||
void draw(byte sy,byte sx,chtype board[size][size2],bool show[size][size2]){
|
||||
@ -131,79 +131,79 @@ void mouseinput(void){
|
||||
ungetch('\n');
|
||||
}
|
||||
void help(void){
|
||||
erase();
|
||||
erase();
|
||||
logo(0,0);
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE CONTROLS ****");
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE CONTROLS ****");
|
||||
mvprintw(8,0,"YOU CAN ALSO USE THE MOUSE!");
|
||||
attroff(A_BOLD);
|
||||
attroff(A_BOLD);
|
||||
mvprintw(4,0,"RETURN/ENTER : Reveal");
|
||||
mvprintw(5,0,"hjkl/ARROW KEYS : Move cursor");
|
||||
mvprintw(6,0,"q : Quit");
|
||||
mvprintw(5,0,"hjkl/ARROW KEYS : Move cursor");
|
||||
mvprintw(6,0,"q : Quit");
|
||||
mvprintw(7,0,"F1 & F2 : Help on controls & gameplay");
|
||||
mvprintw(10,0,"Press a key to continue");
|
||||
mvprintw(10,0,"Press a key to continue");
|
||||
refresh();
|
||||
getch();
|
||||
getch();
|
||||
erase();
|
||||
}
|
||||
void gameplay(void){
|
||||
erase();
|
||||
erase();
|
||||
logo(0,0);
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE GAMEPLAY ****");
|
||||
attroff(A_BOLD);
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE GAMEPLAY ****");
|
||||
attroff(A_BOLD);
|
||||
mvprintw(4,0,"Click on a tile to see the gylph it contains,\n");
|
||||
printw( "then try to find a matching gylph the same way.\n");
|
||||
printw( "They form a pair only when you click a tile\n");
|
||||
printw( "directly after the match. The game ends when \n");
|
||||
printw( "you have found all the matching pairs.\n");
|
||||
refresh();
|
||||
getch();
|
||||
erase();
|
||||
refresh();
|
||||
getch();
|
||||
erase();
|
||||
}
|
||||
int main(int argc, char** argv){
|
||||
size=8;
|
||||
if(argc>=2){
|
||||
size=atoi(argv[1]);
|
||||
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]);
|
||||
size=atoi(argv[1]);
|
||||
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);
|
||||
srand(time(NULL)%UINT_MAX);
|
||||
initscr();
|
||||
mousemask(ALL_MOUSE_EVENTS,NULL);
|
||||
noecho();
|
||||
cbreak();
|
||||
keypad(stdscr,1);
|
||||
if(has_colors()){
|
||||
start_color();
|
||||
use_default_colors();
|
||||
mousemask(ALL_MOUSE_EVENTS,NULL);
|
||||
noecho();
|
||||
cbreak();
|
||||
keypad(stdscr,1);
|
||||
if(has_colors()){
|
||||
start_color();
|
||||
use_default_colors();
|
||||
if( has_colors() ){
|
||||
start_color();
|
||||
use_default_colors();
|
||||
init_pair(1,COLOR_YELLOW,-1);
|
||||
init_pair(2,COLOR_GREEN,-1);
|
||||
init_pair(3,COLOR_BLUE,-1);
|
||||
init_pair(4,COLOR_CYAN,-1);
|
||||
init_pair(5,COLOR_MAGENTA,-1);
|
||||
init_pair(6,COLOR_RED,-1);
|
||||
for(byte b=0;b<6;b++){
|
||||
colors[b]=COLOR_PAIR(b+1);
|
||||
}
|
||||
start_color();
|
||||
use_default_colors();
|
||||
init_pair(1,COLOR_YELLOW,-1);
|
||||
init_pair(2,COLOR_GREEN,-1);
|
||||
init_pair(3,COLOR_BLUE,-1);
|
||||
init_pair(4,COLOR_CYAN,-1);
|
||||
init_pair(5,COLOR_MAGENTA,-1);
|
||||
init_pair(6,COLOR_RED,-1);
|
||||
for(byte b=0;b<6;b++){
|
||||
colors[b]=COLOR_PAIR(b+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(size>8)//big sizes depend on color display
|
||||
size=8;
|
||||
size2=size*2;
|
||||
chtype board[size][size2];
|
||||
chtype board[size][size2];
|
||||
bool show[size][size2];
|
||||
int input;
|
||||
int input;
|
||||
time_t tstart,now;
|
||||
Start:
|
||||
tstart=time(NULL);
|
||||
|
538
sources/miketron.c
Normal file
538
sources/miketron.c
Normal file
@ -0,0 +1,538 @@
|
||||
#include <curses.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
#include <time.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include "config.h"
|
||||
#define SAVE_TO_NUM 10
|
||||
#define MINLEN 10
|
||||
#define MAXLEN 24
|
||||
#define MINWID 40
|
||||
#define MAXWID 80
|
||||
enum {UP=1,RIGHT,DOWN,LEFT,FLIGHT,NOTRAIL,BOMB,SPAWN,STOP,SUPERFOOD,TRAIL};
|
||||
/*
|
||||
|\/|
|
||||
| |IKETRON
|
||||
|
||||
Authored by Hossein Bakhtiarifar <abakh@tuta.io>
|
||||
No rights are reserved and this software comes with no warranties of any kind to the extent permitted by law.
|
||||
|
||||
compile with -lncurses
|
||||
*/
|
||||
typedef signed char byte;
|
||||
int len,wid,py,px;
|
||||
int immunity,flight,notrail;
|
||||
byte direction;
|
||||
long score;
|
||||
chtype colors[6]={0};
|
||||
FILE *scorefile,*lol;
|
||||
char error[150]={0};
|
||||
void move_tron(void){
|
||||
switch(direction){
|
||||
case UP:
|
||||
py--;
|
||||
break;
|
||||
case DOWN:
|
||||
py++;
|
||||
break;
|
||||
case LEFT:
|
||||
px--;
|
||||
break;
|
||||
case RIGHT:
|
||||
px++;
|
||||
break;
|
||||
}
|
||||
if(py==-1)
|
||||
py=len-1;
|
||||
else if(py==len)
|
||||
py=0;
|
||||
if(px==-1)
|
||||
px=wid-1;
|
||||
else if(py==len)
|
||||
py=0;
|
||||
}
|
||||
void logo(void){
|
||||
mvaddstr(1,0,"|\\/|");
|
||||
mvaddstr(2,0,"| |IKETRON");
|
||||
}
|
||||
byte scorewrite(void){// only saves the top 10, returns the place in the chart
|
||||
bool deforno;
|
||||
if( !getenv("MT_SCORES") && (scorefile= fopen(MT_SCORES,"r")) ){
|
||||
deforno=1;
|
||||
}
|
||||
else{
|
||||
deforno=0;
|
||||
if( !(scorefile = fopen(getenv("MT_SCORES"),"r")) ){
|
||||
sprintf(error,"No accessible score files found. You can make an empty text file in %s or set MT_SCORES to such a file to solve this.",MT_SCORES);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
char namebuff[SAVE_TO_NUM][60];
|
||||
long scorebuff[SAVE_TO_NUM];
|
||||
|
||||
memset(namebuff,0,SAVE_TO_NUM*60*sizeof(char) );
|
||||
memset(scorebuff,0,SAVE_TO_NUM*sizeof(long) );
|
||||
|
||||
long fuckingscore=0;
|
||||
char fuckingname[60]={0};
|
||||
byte location=0;
|
||||
|
||||
while( fscanf(scorefile,"%59s : %ld\n",fuckingname,&fuckingscore) == 2 && location<SAVE_TO_NUM ){
|
||||
strcpy(namebuff[location],fuckingname);
|
||||
scorebuff[location] = fuckingscore;
|
||||
location++;
|
||||
|
||||
memset(fuckingname,0,60);
|
||||
fuckingscore=0;
|
||||
}
|
||||
if(deforno)
|
||||
scorefile = fopen(MT_SCORES,"w+");//get rid of the previous text first
|
||||
else
|
||||
scorefile = fopen(getenv("MT_SCORES"), "w+") ;
|
||||
if(!scorefile){
|
||||
strcpy(error, "The file cannot be opened in w+. ");
|
||||
return -1;
|
||||
}
|
||||
|
||||
byte itreached=location;
|
||||
byte ret = -1;
|
||||
bool wroteit=0;
|
||||
|
||||
for(location=0;location<=itreached && location<SAVE_TO_NUM-wroteit;location++){
|
||||
if(!wroteit && (location>=itreached || score>=scorebuff[location]) ){
|
||||
fprintf(scorefile,"%s : %ld\n",getenv("USER"),score);
|
||||
ret=location;
|
||||
wroteit=1;
|
||||
}
|
||||
if(location<SAVE_TO_NUM-wroteit && location<itreached)
|
||||
fprintf(scorefile,"%s : %ld\n",namebuff[location],scorebuff[location]);
|
||||
}
|
||||
fflush(scorefile);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void showscores(byte playerrank){
|
||||
erase();
|
||||
logo();
|
||||
if(*error){
|
||||
mvaddstr(3,0,error);
|
||||
printw("\nHowever, your score is %ld.",score);
|
||||
refresh();
|
||||
return;
|
||||
}
|
||||
if(playerrank == 0){
|
||||
char formername[60]={0};
|
||||
long formerscore=0;
|
||||
rewind(scorefile);
|
||||
fscanf(scorefile,"%*s : %*d\n");
|
||||
move(3,0);
|
||||
byte b=0;
|
||||
if ( fscanf(scorefile,"%s : %ld\n",formername,&formerscore)==2){
|
||||
halfdelay(2);
|
||||
printw("*****CONGRATULATIONS!****\n");
|
||||
printw(" You bet the\n");
|
||||
printw(" previous\n");
|
||||
printw(" record\n");
|
||||
printw(" of\n");
|
||||
printw(" %14ld\n",formerscore);
|
||||
printw(" held by\n");
|
||||
printw(" %11s\n",formername);
|
||||
printw(" \n");
|
||||
printw(" \n");
|
||||
printw("*************************\n");
|
||||
printw("Press a key to proceed:");
|
||||
Effect:
|
||||
move(4,0);
|
||||
attron(colors[b]);
|
||||
mvprintw(4,0, " _____ ");
|
||||
mvprintw(5,0, " .' |");
|
||||
mvprintw(6,0, " .' |");
|
||||
mvprintw(7,0, " | .| |");
|
||||
mvprintw(8,0, " |.' | |");
|
||||
mvprintw(9,0, " | |");
|
||||
mvprintw(10,0," ___| |___");
|
||||
mvprintw(11,0," | |");
|
||||
mvprintw(12,0," |____________|");
|
||||
attroff(colors[b]);
|
||||
b=(b+1)%6;
|
||||
if(getch()==ERR)
|
||||
goto Effect;
|
||||
nocbreak();
|
||||
cbreak();
|
||||
erase();
|
||||
logo();
|
||||
}
|
||||
}
|
||||
//scorefile is still open with w+
|
||||
move(3,0);
|
||||
char pname[60] = {0};
|
||||
long pscore=0;
|
||||
byte rank=0;
|
||||
rewind(scorefile);
|
||||
printw(">*>*>Top %d<*<*<\n",SAVE_TO_NUM);
|
||||
while( rank<SAVE_TO_NUM && fscanf(scorefile,"%s : %ld\n",pname,&pscore) == 2){
|
||||
if(rank == playerrank)
|
||||
printw(">>>");
|
||||
printw("%d) %s : %ld\n",rank+1,pname,pscore);
|
||||
rank++;
|
||||
}
|
||||
addch('\n');
|
||||
refresh();
|
||||
}
|
||||
void put_stuff(byte board[len][wid],byte num){
|
||||
byte y,x;
|
||||
for(byte n=0;n<num;n++){
|
||||
do{
|
||||
y=rand()%len;
|
||||
x=rand()%wid;
|
||||
}while(y==py&&x==px);
|
||||
board[y][x]=STOP;
|
||||
if(!rand())
|
||||
board[y][x]=SUPERFOOD;
|
||||
else if(!(rand()%20))
|
||||
board[y][x]=SPAWN;
|
||||
else if(!(rand()%10))
|
||||
board[y][x]=BOMB;
|
||||
else if(!(rand()%10))
|
||||
board[y][x]=NOTRAIL;
|
||||
else if(!(rand()%4))
|
||||
board[y][x]=FLIGHT;
|
||||
}
|
||||
}
|
||||
void rectangle(void){
|
||||
for(int y=0;y<=len;y++){
|
||||
mvaddch(3+y,0,ACS_VLINE);
|
||||
mvaddch(4+y,1+wid,ACS_VLINE);
|
||||
}
|
||||
for(int x=0;x<=wid;x++){
|
||||
mvaddch(3,x,ACS_HLINE);
|
||||
mvaddch(4+len,x,ACS_HLINE);
|
||||
}
|
||||
mvaddch(3,0,ACS_ULCORNER);
|
||||
mvaddch(4+len,0,ACS_LLCORNER);
|
||||
mvaddch(3,1+wid,ACS_URCORNER);
|
||||
mvaddch(4+len,1+wid,ACS_LRCORNER);
|
||||
}
|
||||
void draw(byte board[len][wid]){
|
||||
int y,x;
|
||||
static byte effect=0;
|
||||
chtype prnt;
|
||||
rectangle();
|
||||
for(y=0;y<len;y++){
|
||||
for(x=0;x<wid;x++){
|
||||
if(board[y][x]<0){
|
||||
prnt=' '|A_STANDOUT|colors[abs(board[y][x])%6];
|
||||
board[y][x]++;
|
||||
}
|
||||
else if(y==py && x==px){
|
||||
prnt=ACS_PLUS;
|
||||
if(immunity)
|
||||
prnt|=colors[effect];
|
||||
else if(flight)
|
||||
prnt|=A_BOLD;
|
||||
else if(!notrail)
|
||||
prnt|=A_STANDOUT;
|
||||
}
|
||||
else if(board[y][x]==TRAIL)
|
||||
prnt=ACS_PLUS;
|
||||
else if(board[y][x]==NOTRAIL)
|
||||
prnt='&';
|
||||
else if(board[y][x]==FLIGHT)
|
||||
prnt='?';
|
||||
else if(board[y][x]==SUPERFOOD)
|
||||
prnt='%'|colors[effect];
|
||||
else if(board[y][x]==STOP)
|
||||
prnt='S';
|
||||
else if(board[y][x]==SPAWN)
|
||||
prnt='B';
|
||||
else if(board[y][x]==BOMB)
|
||||
prnt='8';
|
||||
else
|
||||
prnt= ' ';
|
||||
mvaddch(4+y,x+1,prnt);
|
||||
}
|
||||
}
|
||||
effect=(effect+1)%6;
|
||||
}
|
||||
void explode(byte board[len][wid],int by,int bx){
|
||||
board[by][bx]=0;//prevent endless recursion
|
||||
int sy=by-2;
|
||||
int sx=bx-4;
|
||||
int ey=by+2;
|
||||
int ex=bx+4;
|
||||
if(ey>=len)
|
||||
ey-=len;
|
||||
if(ex>=wid)
|
||||
ex-=wid;
|
||||
if(sy<0)
|
||||
sy+=len;
|
||||
if(sx<0)
|
||||
sx+=wid;
|
||||
int y=sy;
|
||||
int x=sx;
|
||||
while(y!=ey){
|
||||
while(x!=ex){
|
||||
x++;
|
||||
if(x==wid)
|
||||
x=0;
|
||||
if(board[y][x]==BOMB)
|
||||
explode(board,y,x);
|
||||
board[y][x]=-10;
|
||||
fprintf(lol,"y:%d x:%d\n",y,x);
|
||||
fflush(lol);
|
||||
}
|
||||
x=sx;
|
||||
y++;
|
||||
if(y==len)
|
||||
y=0;
|
||||
fprintf(lol,"y:%d x:%d\n",y,x);
|
||||
fflush(lol);
|
||||
|
||||
}
|
||||
}
|
||||
void help(void){
|
||||
nocbreak();
|
||||
cbreak();
|
||||
erase();
|
||||
logo();
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE CONTROLS ****");
|
||||
attroff(A_BOLD);
|
||||
mvprintw(4,0,"hjkl/ARROW KEYS : Change direction");
|
||||
mvprintw(5,0,"q : Quit");
|
||||
mvprintw(6,0,"F1 & F2: Help on controls & gameplay");
|
||||
mvprintw(8,0,"Press a key to continue");
|
||||
refresh();
|
||||
getch();
|
||||
erase();
|
||||
halfdelay(1);
|
||||
}
|
||||
void gameplay(void){
|
||||
nocbreak();
|
||||
cbreak();
|
||||
erase();
|
||||
logo();
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE GAMEPLAY ****");
|
||||
attroff(A_BOLD);
|
||||
move(4,0);
|
||||
printw("You are controlling a strange vechile 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();
|
||||
erase();
|
||||
getch();
|
||||
halfdelay(1);
|
||||
}
|
||||
void sigint_handler(int x){
|
||||
endwin();
|
||||
puts("Quit.");
|
||||
exit(x);
|
||||
}
|
||||
int main(int argc, char** argv){
|
||||
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(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;
|
||||
}
|
||||
initscr();
|
||||
if(autoset){
|
||||
len=LINES-7;
|
||||
if(len<MINLEN)
|
||||
len=MINLEN;
|
||||
else if(len>MAXLEN)
|
||||
len=MAXLEN;
|
||||
|
||||
wid=COLS-5;
|
||||
if(wid<MINWID)
|
||||
wid=MINWID;
|
||||
else if(wid>MAXWID)
|
||||
wid=MAXWID;
|
||||
}
|
||||
srand(time(NULL)%UINT_MAX);
|
||||
byte board[len][wid];
|
||||
byte predirection;
|
||||
int prex,prey;
|
||||
bool halfspeed=0;
|
||||
int constant=150*(80*24)/(len*wid);
|
||||
initscr();
|
||||
noecho();
|
||||
cbreak();
|
||||
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_CYAN,-1);
|
||||
init_pair(5,COLOR_MAGENTA,-1);
|
||||
init_pair(6,COLOR_RED,-1);
|
||||
for(byte b= 0;b<6;b++){
|
||||
colors[b]=COLOR_PAIR(b+1);
|
||||
}
|
||||
|
||||
}
|
||||
lol=fopen("lol","w");
|
||||
Start:
|
||||
curs_set(0);
|
||||
halfdelay(1);
|
||||
score=0;
|
||||
direction=LEFT;
|
||||
py=len/2;
|
||||
px=wid/2;
|
||||
memset(board,0,len*wid);
|
||||
put_stuff(board,20);
|
||||
int prepreinput,preinput,input;
|
||||
while(1){
|
||||
erase();
|
||||
logo();
|
||||
mvprintw(1,12,"Score:%ld",score);
|
||||
draw(board);
|
||||
refresh();
|
||||
prepreinput=preinput;
|
||||
preinput=input;
|
||||
input = getch();
|
||||
if(immunity)
|
||||
mvprintw(2,12,"Immunity:%ld",immunity);
|
||||
else if(flight)
|
||||
mvprintw(2,12,"Flight:%ld",flight);
|
||||
else if(notrail)
|
||||
mvprintw(2,12,"NoTrail:%ld",notrail);
|
||||
if(board[py][px]==SPAWN)
|
||||
put_stuff(board,5);
|
||||
else if(board[py][px]==BOMB){
|
||||
explode(board,py,px);
|
||||
for(byte b=0;b<10;b++){
|
||||
draw(board);
|
||||
refresh();
|
||||
usleep(100000);
|
||||
}
|
||||
}
|
||||
else if(board[py][px]==STOP){
|
||||
nocbreak();
|
||||
cbreak();
|
||||
ungetch(getch());
|
||||
halfdelay(1);
|
||||
}
|
||||
else if(board[py][px]==SUPERFOOD)
|
||||
immunity+=len+wid;
|
||||
else if(board[py][px]==FLIGHT)
|
||||
flight+=5;
|
||||
else if(board[py][px]==NOTRAIL)
|
||||
notrail+=15;
|
||||
else
|
||||
goto ElseJump;
|
||||
board[py][px]=0;//if one of conditions is true, it executes! keep nagging about goto being redundant!
|
||||
ElseJump:
|
||||
if(board[py][px]==TRAIL){
|
||||
if(immunity)
|
||||
board[py][px]=0;
|
||||
else if(!flight)
|
||||
break;
|
||||
}
|
||||
if( input == KEY_F(1) || input=='?' )
|
||||
help();
|
||||
halfspeed=!halfspeed;
|
||||
predirection=direction;
|
||||
if( (input=='k' || input==KEY_UP) ){
|
||||
direction=UP;
|
||||
halfspeed=1;
|
||||
}
|
||||
else if( (input=='j' || input==KEY_DOWN) ){
|
||||
direction=DOWN;
|
||||
halfspeed=1;
|
||||
}
|
||||
else if( (input=='h' || input==KEY_LEFT) )
|
||||
direction=LEFT;
|
||||
else if( (input=='l' || input==KEY_RIGHT) )
|
||||
direction=RIGHT;
|
||||
if( input=='q')
|
||||
sigint_handler(0);
|
||||
|
||||
if(input!=ERR){
|
||||
if(preinput==input){//if it wasn't there, hitting two keys in less than 0.1 sec would not work
|
||||
//the i part is there to prevent it from working too often =) you could just move diagonally that way and get a big score
|
||||
//just hitting UP and LEFT repeatedly. the trick works in the original, but not very well because of it's limitations.
|
||||
usleep(100000);
|
||||
flushinp();
|
||||
}
|
||||
if(prepreinput==input)
|
||||
usleep(100000);
|
||||
|
||||
}
|
||||
|
||||
if( !((direction==UP||direction==DOWN)&&!halfspeed) && !immunity && !flight && !notrail)
|
||||
board[py][px]=TRAIL;
|
||||
|
||||
if(direction==UP && halfspeed){
|
||||
py--;
|
||||
if(py==-1)
|
||||
py=len-1;
|
||||
halfspeed=1;
|
||||
}
|
||||
else if(direction==DOWN && halfspeed){
|
||||
py++;
|
||||
if(py==len)
|
||||
py=0;
|
||||
}
|
||||
else if(direction==LEFT){
|
||||
px--;
|
||||
if(px==-1)
|
||||
px=wid-1;
|
||||
}
|
||||
else if(direction==RIGHT){
|
||||
px++;
|
||||
if(px==wid)
|
||||
px=0;
|
||||
}
|
||||
score++;
|
||||
if(!(score%100))
|
||||
put_stuff(board,5);
|
||||
if(immunity)
|
||||
immunity--;
|
||||
else if(flight)
|
||||
flight--;
|
||||
else if(notrail)
|
||||
notrail--;
|
||||
}
|
||||
nocbreak();
|
||||
cbreak();
|
||||
draw(board);
|
||||
refresh();
|
||||
mvprintw(len+5,0,"Game over! Press a key to see the high scores:");
|
||||
getch();
|
||||
showscores(scorewrite());
|
||||
printw("Game over! Wanna play again?(y/n)");
|
||||
curs_set(1);
|
||||
input=getch();
|
||||
if( input!= 'N' && input!= 'n' && input!='q')
|
||||
goto Start;
|
||||
endwin();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
@ -165,31 +165,31 @@ void mouseinput(int sy, int sx){
|
||||
ungetch(' ');
|
||||
}
|
||||
void help(void){
|
||||
erase();
|
||||
erase();
|
||||
mvprintw(1,0,"|\\/|");
|
||||
mvprintw(2,0,"| |INES");
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE CONTROLS ****");
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE CONTROLS ****");
|
||||
mvprintw(10,0,"YOU CAN ALSO USE THE MOUSE!");
|
||||
attroff(A_BOLD);
|
||||
attroff(A_BOLD);
|
||||
mvprintw(4,0,"RETURN/ENTER : Examine for bombs");
|
||||
mvprintw(5,0,"SPACE : Flag/Unflag");
|
||||
mvprintw(6,0,"hjkl/ARROW KEYS : Move cursor");
|
||||
mvprintw(7,0,"q : Quit");
|
||||
mvprintw(5,0,"SPACE : Flag/Unflag");
|
||||
mvprintw(6,0,"hjkl/ARROW KEYS : Move cursor");
|
||||
mvprintw(7,0,"q : Quit");
|
||||
mvprintw(8,0,"F1 & F2 : Help on controls & gameplay");
|
||||
mvprintw(9,0,"PgDn,PgUp,<,> : Scroll");
|
||||
mvprintw(12,0,"Press a key to continue");
|
||||
mvprintw(12,0,"Press a key to continue");
|
||||
refresh();
|
||||
getch();
|
||||
getch();
|
||||
erase();
|
||||
}
|
||||
void gameplay(void){
|
||||
erase();
|
||||
mvprintw(1,0,"|\\/|");
|
||||
mvprintw(2,0,"| |INES");
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE GAMEPLAY ****");
|
||||
attroff(A_BOLD);
|
||||
erase();
|
||||
mvprintw(1,0,"|\\/|");
|
||||
mvprintw(2,0,"| |INES");
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE GAMEPLAY ****");
|
||||
attroff(A_BOLD);
|
||||
mvprintw(4,0,"Try to find the landmines in the field\n");
|
||||
printw("with logical reasoning: When you click\n");
|
||||
printw("on a tile ( a '.' here), numbers may show\n");
|
||||
@ -198,9 +198,9 @@ void gameplay(void){
|
||||
printw("avoid the landmines based on them; and\n");
|
||||
printw("clicking on a landmine would make you\n");
|
||||
printw("lose the game.");
|
||||
refresh();
|
||||
getch();
|
||||
erase();
|
||||
refresh();
|
||||
getch();
|
||||
erase();
|
||||
}
|
||||
int main(int argc, char** argv){
|
||||
signal(SIGINT,sigint_handler);
|
||||
@ -240,34 +240,34 @@ int main(int argc, char** argv){
|
||||
mscount = len*wid/6;
|
||||
srand(time(NULL)%UINT_MAX);
|
||||
initscr();
|
||||
mousemask(ALL_MOUSE_EVENTS,NULL);
|
||||
noecho();
|
||||
cbreak();
|
||||
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);
|
||||
}
|
||||
mousemask(ALL_MOUSE_EVENTS,NULL);
|
||||
noecho();
|
||||
cbreak();
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
byte board[len][wid];
|
||||
bool mines[len][wid];
|
||||
char result[70];
|
||||
int input;
|
||||
}
|
||||
byte board[len][wid];
|
||||
bool mines[len][wid];
|
||||
char result[70];
|
||||
int input;
|
||||
int sy,sx;
|
||||
Start:
|
||||
sy=sx=0;
|
||||
py=px=0;
|
||||
untouched=len*wid;
|
||||
flags=0;
|
||||
untouched=len*wid;
|
||||
flags=0;
|
||||
curs_set(0);
|
||||
memset(board,-1,len*wid);
|
||||
memset(mines,false,len*wid);
|
||||
@ -290,15 +290,15 @@ int main(int argc, char** argv){
|
||||
sy=0;
|
||||
}
|
||||
if( input==KEY_NPAGE && LINES< len+3){
|
||||
sy-=10;
|
||||
if(sy< -(len+3) )
|
||||
sy=-(len+3);
|
||||
}
|
||||
sy-=10;
|
||||
if(sy< -(len+3) )
|
||||
sy=-(len+3);
|
||||
}
|
||||
if( input=='<' && COLS< wid*2+1){
|
||||
sx+=10;
|
||||
if(sx>0)
|
||||
sx=0;
|
||||
}
|
||||
sx+=10;
|
||||
if(sx>0)
|
||||
sx=0;
|
||||
}
|
||||
if( input=='>' && COLS< wid*2+1){
|
||||
sx-=10;
|
||||
if(sx< -(wid*2+1))
|
||||
|
451
sources/muncher.c
Normal file
451
sources/muncher.c
Normal file
@ -0,0 +1,451 @@
|
||||
#include <curses.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
#include <time.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include "config.h"
|
||||
#define SAVE_TO_NUM 10
|
||||
#define MINLEN 10
|
||||
#define MAXLEN 24
|
||||
#define MINWID 40
|
||||
#define MAXWID 80
|
||||
enum {UP=1,RIGHT,DOWN,LEFT,FOOD,SUPERFOOD,TRAP};
|
||||
/*
|
||||
|\/|
|
||||
| |UNCHER
|
||||
|
||||
Authored by Hossein Bakhtiarifar <abakh@tuta.io>
|
||||
No rights are reserved and this software comes with no warranties of any kind to the extent permitted by law.
|
||||
|
||||
compile with -lncurses
|
||||
*/
|
||||
typedef signed char byte;
|
||||
int len,wid,py,px;
|
||||
int immunity;
|
||||
byte direction;
|
||||
long score;
|
||||
chtype colors[6]={0};
|
||||
FILE* scorefile;
|
||||
char error[150]={0};
|
||||
void logo(void){
|
||||
mvaddstr(1,0,"|\\/|");
|
||||
mvaddstr(2,0,"| |UNCHER");
|
||||
}
|
||||
byte scorewrite(void){// only saves the top 10, returns the place in the chart
|
||||
bool deforno;
|
||||
if( !getenv("MNCH_SCORES") && (scorefile= fopen(MNCH_SCORES,"r")) ){
|
||||
deforno=1;
|
||||
}
|
||||
else{
|
||||
deforno=0;
|
||||
if( !(scorefile = fopen(getenv("MNCH_SCORES"),"r")) ){
|
||||
sprintf(error,"No accessible score files found. You can make an empty text file in %s or set MNCH_SCORES to such a file to solve this.",MNCH_SCORES);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
char namebuff[SAVE_TO_NUM][60];
|
||||
long scorebuff[SAVE_TO_NUM];
|
||||
|
||||
memset(namebuff,0,SAVE_TO_NUM*60*sizeof(char) );
|
||||
memset(scorebuff,0,SAVE_TO_NUM*sizeof(long) );
|
||||
|
||||
long fuckingscore =0;
|
||||
char fuckingname[60]={0};
|
||||
byte location=0;
|
||||
|
||||
while( fscanf(scorefile,"%59s : %ld\n",fuckingname,&fuckingscore) == 2 && location<SAVE_TO_NUM ){
|
||||
strcpy(namebuff[location],fuckingname);
|
||||
scorebuff[location] = fuckingscore;
|
||||
location++;
|
||||
|
||||
memset(fuckingname,0,60);
|
||||
fuckingscore=0;
|
||||
}
|
||||
if(deforno)
|
||||
scorefile = fopen(MNCH_SCORES,"w+");//get rid of the previous text first
|
||||
else
|
||||
scorefile = fopen(getenv("MNCH_SCORES"), "w+") ;
|
||||
if(!scorefile){
|
||||
strcpy(error, "The file cannot be opened in w+. ");
|
||||
return -1;
|
||||
}
|
||||
|
||||
byte itreached=location;
|
||||
byte ret = -1;
|
||||
bool wroteit=0;
|
||||
|
||||
for(location=0;location<=itreached && location<SAVE_TO_NUM-wroteit;location++){
|
||||
if(!wroteit && (location>=itreached || score>=scorebuff[location]) ){
|
||||
fprintf(scorefile,"%s : %ld\n",getenv("USER"),score);
|
||||
ret=location;
|
||||
wroteit=1;
|
||||
}
|
||||
if(location<SAVE_TO_NUM-wroteit && location<itreached)
|
||||
fprintf(scorefile,"%s : %ld\n",namebuff[location],scorebuff[location]);
|
||||
}
|
||||
fflush(scorefile);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void showscores(byte playerrank){
|
||||
erase();
|
||||
logo();
|
||||
if(*error){
|
||||
mvaddstr(3,0,error);
|
||||
printw("\nHowever, your score is %ld.",score);
|
||||
refresh();
|
||||
return;
|
||||
}
|
||||
if(playerrank == 0){
|
||||
char formername[60]={0};
|
||||
long formerscore=0;
|
||||
rewind(scorefile);
|
||||
fscanf(scorefile,"%*s : %*d\n");
|
||||
move(3,0);
|
||||
byte b=0;
|
||||
if ( fscanf(scorefile,"%s : %ld\n",formername,&formerscore)==2){
|
||||
halfdelay(2);
|
||||
printw("*****CONGRATULATIONS!****\n");
|
||||
printw(" You bet the\n");
|
||||
printw(" previous\n");
|
||||
printw(" record\n");
|
||||
printw(" of\n");
|
||||
printw(" %14ld\n",formerscore);
|
||||
printw(" held by\n");
|
||||
printw(" %11s\n",formername);
|
||||
printw(" \n");
|
||||
printw(" \n");
|
||||
printw("*************************\n");
|
||||
printw("Press a key to proceed:");
|
||||
Effect:
|
||||
move(4,0);
|
||||
attron(colors[b]);
|
||||
mvprintw(4,0, " _____ ");
|
||||
mvprintw(5,0, " .' |");
|
||||
mvprintw(6,0, " .' |");
|
||||
mvprintw(7,0, " | .| |");
|
||||
mvprintw(8,0, " |.' | |");
|
||||
mvprintw(9,0, " | |");
|
||||
mvprintw(10,0," ___| |___");
|
||||
mvprintw(11,0," | |");
|
||||
mvprintw(12,0," |____________|");
|
||||
attroff(colors[b]);
|
||||
b=(b+1)%6;
|
||||
if(getch()==ERR)
|
||||
goto Effect;
|
||||
nocbreak();
|
||||
cbreak();
|
||||
erase();
|
||||
logo();
|
||||
}
|
||||
}
|
||||
//scorefile is still open with w+
|
||||
move(3,0);
|
||||
char pname[60] = {0};
|
||||
long pscore=0;
|
||||
byte rank=0;
|
||||
rewind(scorefile);
|
||||
printw(">*>*>Top %d<*<*<\n",SAVE_TO_NUM);
|
||||
while( rank<SAVE_TO_NUM && fscanf(scorefile,"%s : %ld\n",pname,&pscore) == 2){
|
||||
if(rank == playerrank)
|
||||
printw(">>>");
|
||||
printw("%d) %s : %ld\n",rank+1,pname,pscore);
|
||||
rank++;
|
||||
}
|
||||
addch('\n');
|
||||
refresh();
|
||||
}
|
||||
void rectangle(void){
|
||||
for(int y=0;y<=len;y++){
|
||||
mvaddch(3+y,0,ACS_VLINE);
|
||||
mvaddch(4+y,1+wid,ACS_VLINE);
|
||||
}
|
||||
for(int x=0;x<=wid;x++){
|
||||
mvaddch(3,x,ACS_HLINE);
|
||||
mvaddch(4+len,x,ACS_HLINE);
|
||||
}
|
||||
mvaddch(3,0,ACS_ULCORNER);
|
||||
mvaddch(4+len,0,ACS_LLCORNER);
|
||||
mvaddch(3,1+wid,ACS_URCORNER);
|
||||
mvaddch(4+len,1+wid,ACS_LRCORNER);
|
||||
}
|
||||
void place_food(byte board[len][wid]){
|
||||
int y,x;
|
||||
do{
|
||||
y=rand()%len;
|
||||
x=rand()%wid;
|
||||
}while(y==py && x==px);
|
||||
board[y][x]=FOOD;
|
||||
|
||||
byte num;
|
||||
if(score<300)
|
||||
num=rand()%2;
|
||||
else if(score<500)
|
||||
num=1+rand()%2;
|
||||
else if(score<1000)
|
||||
num=2+rand()%4;
|
||||
else if(score<2000)
|
||||
num=5+rand()%6;
|
||||
else
|
||||
num=10+rand()%11;
|
||||
|
||||
while(num){
|
||||
Again:
|
||||
y=rand()%len;
|
||||
x=rand()%wid;
|
||||
if(abs(y-py)<4 && abs(x-px)<7)
|
||||
goto Again;
|
||||
if(board[y][x]==FOOD)
|
||||
goto Again;
|
||||
board[y][x]=TRAP;
|
||||
|
||||
num--;
|
||||
}
|
||||
if(score>2000 && !(rand()%5)){
|
||||
do{
|
||||
y=rand()%len;
|
||||
x=rand()%wid;
|
||||
}while(y==py && x==px && board[y][x]!=FOOD);
|
||||
board[y][x]=SUPERFOOD;
|
||||
}
|
||||
}
|
||||
void draw(byte board[len][wid]){
|
||||
int y,x;
|
||||
static byte effect=0;
|
||||
chtype prnt;
|
||||
rectangle();
|
||||
for(y=0;y<len;y++){
|
||||
for(x=0;x<wid;x++){
|
||||
if(y==py && x==px){
|
||||
prnt='r'|colors[2]|A_STANDOUT;
|
||||
if(immunity)
|
||||
prnt='r'|colors[effect]|A_BOLD;
|
||||
}
|
||||
else if(board[y][x]==TRAP)
|
||||
prnt='^'|colors[((y*x)/15)%6];
|
||||
else if(board[y][x]==FOOD)
|
||||
prnt='%'|colors[(y+x)%6];
|
||||
else if(board[y][x]==SUPERFOOD)
|
||||
prnt='%'|colors[effect];
|
||||
else
|
||||
prnt= ' ';
|
||||
mvaddch(4+y,x+1,prnt);
|
||||
}
|
||||
}
|
||||
effect=(effect+1)%6;
|
||||
}
|
||||
void help(void){
|
||||
nocbreak();
|
||||
cbreak();
|
||||
erase();
|
||||
logo();
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE CONTROLS ****");
|
||||
attroff(A_BOLD);
|
||||
mvprintw(4,0,"hjkl/ARROW KEYS : Change direction");
|
||||
mvprintw(5,0,"q : Quit");
|
||||
mvprintw(6,0,"F1 & F2: Help on controls & gameplay");
|
||||
mvprintw(8,0,"Press a key to continue");
|
||||
refresh();
|
||||
getch();
|
||||
erase();
|
||||
halfdelay(1);
|
||||
}
|
||||
void gameplay(void){
|
||||
nocbreak();
|
||||
cbreak();
|
||||
erase();
|
||||
logo();
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE GAMEPLAY ****");
|
||||
attroff(A_BOLD);
|
||||
move(4,0);
|
||||
printw("Eat the food and avoid the traps.\n");
|
||||
refresh();
|
||||
erase();
|
||||
getch();
|
||||
halfdelay(1);
|
||||
}
|
||||
void sigint_handler(int x){
|
||||
endwin();
|
||||
puts("Quit.");
|
||||
exit(x);
|
||||
}
|
||||
int main(int argc, char** argv){
|
||||
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(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;
|
||||
}
|
||||
initscr();
|
||||
if(autoset){
|
||||
len=LINES-7;
|
||||
if(len<MINLEN)
|
||||
len=MINLEN;
|
||||
else if(len>MAXLEN)
|
||||
len=MAXLEN;
|
||||
|
||||
wid=COLS-5;
|
||||
if(wid<MINWID)
|
||||
wid=MINWID;
|
||||
else if(wid>MAXWID)
|
||||
wid=MAXWID;
|
||||
}
|
||||
srand(time(NULL)%UINT_MAX);
|
||||
byte board[len][wid];
|
||||
bool halfspeed=0;
|
||||
int constant=150*(80*24)/(len*wid);
|
||||
initscr();
|
||||
noecho();
|
||||
cbreak();
|
||||
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_CYAN,-1);
|
||||
init_pair(5,COLOR_MAGENTA,-1);
|
||||
init_pair(6,COLOR_RED,-1);
|
||||
for(byte b= 0;b<6;b++){
|
||||
colors[b]=COLOR_PAIR(b+1);
|
||||
}
|
||||
|
||||
}
|
||||
Start:
|
||||
curs_set(0);
|
||||
halfdelay(1);
|
||||
score=direction=0;
|
||||
py=len/2;
|
||||
px=wid/2;
|
||||
memset(board,0,len*wid);
|
||||
place_food(board);
|
||||
int input;
|
||||
while(1){
|
||||
erase();
|
||||
logo();
|
||||
mvprintw(1,11,"Score:%ld",score);
|
||||
if(immunity)
|
||||
mvprintw(2,11,"Immunity:%d",immunity);
|
||||
draw(board);
|
||||
refresh();
|
||||
if( board[py][px]==FOOD ){
|
||||
score+= constant;
|
||||
board[py][px]=0;
|
||||
for(byte b=0;b<6;b++){
|
||||
mvaddch(4+py,px+1,'r'|colors[b]|A_STANDOUT);
|
||||
refresh();
|
||||
usleep(100000/5);
|
||||
}
|
||||
place_food(board);
|
||||
}
|
||||
if( board[py][px]==SUPERFOOD ){
|
||||
immunity+=(len+wid)/2;
|
||||
board[py][px]=0;
|
||||
}
|
||||
if(board[py][px]==TRAP){
|
||||
if(immunity)
|
||||
board[py][px]=0;
|
||||
else
|
||||
break;
|
||||
}
|
||||
if(px<0 || px>=wid)
|
||||
break;
|
||||
halfspeed=!halfspeed;
|
||||
input = getch();
|
||||
if( input == KEY_F(1) || input=='?' )
|
||||
help();
|
||||
if( input==KEY_F(2) )
|
||||
gameplay();
|
||||
if( (input=='k' || input==KEY_UP) && py>0 ){
|
||||
direction=UP;
|
||||
halfspeed=1;
|
||||
}
|
||||
if( (input=='j' || input==KEY_DOWN) && py<len-1 ){
|
||||
direction=DOWN;
|
||||
halfspeed=1;
|
||||
}
|
||||
if( (input=='h' || input==KEY_LEFT) && px>0 )
|
||||
direction=LEFT;
|
||||
if( (input=='l' || input==KEY_RIGHT) && px<wid-1 )
|
||||
direction=RIGHT;
|
||||
|
||||
if( input=='q')
|
||||
sigint_handler(0);
|
||||
if( input=='p'){
|
||||
nocbreak();
|
||||
cbreak();
|
||||
erase();
|
||||
logo();
|
||||
attron(A_BOLD);
|
||||
mvaddstr(1,11,"PAUSED");
|
||||
attroff(A_BOLD);
|
||||
getch();
|
||||
halfdelay(1);
|
||||
}
|
||||
if(input!=ERR){
|
||||
usleep(100000);
|
||||
flushinp();
|
||||
}
|
||||
if(direction==UP && halfspeed){
|
||||
if(!py)
|
||||
break;
|
||||
py--;
|
||||
}
|
||||
else if(direction==DOWN && halfspeed){
|
||||
if(py==len-1)
|
||||
break;
|
||||
py++;
|
||||
}
|
||||
else if(direction==LEFT){
|
||||
if(!px)
|
||||
break;
|
||||
px--;
|
||||
}
|
||||
else if(direction==RIGHT){
|
||||
if(px==wid-1)
|
||||
break;
|
||||
px++;
|
||||
}
|
||||
if(immunity)
|
||||
immunity--;
|
||||
}
|
||||
nocbreak();
|
||||
cbreak();
|
||||
draw(board);
|
||||
refresh();
|
||||
mvprintw(len+5,0,"Game over! Press a key to see the high scores:");
|
||||
getch();
|
||||
showscores(scorewrite());
|
||||
printw("Game over! Wanna play again?(y/n)");
|
||||
curs_set(1);
|
||||
input=getch();
|
||||
if( input!= 'N' && input!= 'n' && input!='q')
|
||||
goto Start;
|
||||
endwin();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
230
sources/pipes.c
230
sources/pipes.c
@ -36,66 +36,66 @@ FILE* scorefile;
|
||||
char error [150]={0};
|
||||
|
||||
void logo(void){
|
||||
mvprintw(0,0," _ ");
|
||||
mvprintw(1,0,"|_)");
|
||||
mvprintw(2,0,"| IPES");
|
||||
mvprintw(0,0," _ ");
|
||||
mvprintw(1,0,"|_)");
|
||||
mvprintw(2,0,"| IPES");
|
||||
}
|
||||
|
||||
byte scorewrite(void){// only saves the top 10, returns the place in the chart
|
||||
bool deforno;
|
||||
if( !getenv("PP_SCORES") && (scorefile= fopen(PP_SCORES,"r")) ){
|
||||
deforno=1;
|
||||
}
|
||||
else{
|
||||
deforno=0;
|
||||
if( !(scorefile = fopen(getenv("PP_SCORES"),"r")) ){
|
||||
bool deforno;
|
||||
if( !getenv("PP_SCORES") && (scorefile= fopen(PP_SCORES,"r")) ){
|
||||
deforno=1;
|
||||
}
|
||||
else{
|
||||
deforno=0;
|
||||
if( !(scorefile = fopen(getenv("PP_SCORES"),"r")) ){
|
||||
sprintf(error,"No accessible score files found. You can make an empty text file in %s or set PP_SCORES to such a file to solve this.",PP_SCORES);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
char namebuff[SAVE_TO_NUM][60];
|
||||
long scorebuff[SAVE_TO_NUM];
|
||||
char namebuff[SAVE_TO_NUM][60];
|
||||
long scorebuff[SAVE_TO_NUM];
|
||||
|
||||
memset(namebuff,0,SAVE_TO_NUM*60*sizeof(char) );
|
||||
memset(scorebuff,0,SAVE_TO_NUM*sizeof(long) );
|
||||
memset(namebuff,0,SAVE_TO_NUM*60*sizeof(char) );
|
||||
memset(scorebuff,0,SAVE_TO_NUM*sizeof(long) );
|
||||
|
||||
long fuckingscore =0;
|
||||
char fuckingname[60]={0};
|
||||
byte location=0;
|
||||
long fuckingscore =0;
|
||||
char fuckingname[60]={0};
|
||||
byte location=0;
|
||||
|
||||
while( fscanf(scorefile,"%59s : %ld\n",fuckingname,&fuckingscore) == 2 && location<SAVE_TO_NUM ){
|
||||
strcpy(namebuff[location],fuckingname);
|
||||
scorebuff[location] = fuckingscore;
|
||||
location++;
|
||||
while( fscanf(scorefile,"%59s : %ld\n",fuckingname,&fuckingscore) == 2 && location<SAVE_TO_NUM ){
|
||||
strcpy(namebuff[location],fuckingname);
|
||||
scorebuff[location] = fuckingscore;
|
||||
location++;
|
||||
|
||||
memset(fuckingname,0,60);
|
||||
fuckingscore=0;
|
||||
}
|
||||
if(deforno)
|
||||
scorefile = fopen(PP_SCORES,"w+");//get rid of the previous text first
|
||||
else
|
||||
scorefile = fopen(getenv("PP_SCORES"), "w+") ;
|
||||
if(!scorefile){
|
||||
strcpy(error, "The file cannot be opened in w+. ");
|
||||
memset(fuckingname,0,60);
|
||||
fuckingscore=0;
|
||||
}
|
||||
if(deforno)
|
||||
scorefile = fopen(PP_SCORES,"w+");//get rid of the previous text first
|
||||
else
|
||||
scorefile = fopen(getenv("PP_SCORES"), "w+") ;
|
||||
if(!scorefile){
|
||||
strcpy(error, "The file cannot be opened in w+. ");
|
||||
return -1;
|
||||
}
|
||||
|
||||
byte itreached=location;
|
||||
byte ret = -1;
|
||||
bool wroteit=0;
|
||||
byte itreached=location;
|
||||
byte ret = -1;
|
||||
bool wroteit=0;
|
||||
|
||||
for(location=0;location<=itreached && location<SAVE_TO_NUM-wroteit;location++){
|
||||
if(!wroteit && (location>=itreached || score>=scorebuff[location]) ){
|
||||
fprintf(scorefile,"%s : %ld\n",getenv("USER"),score);
|
||||
ret=location;
|
||||
wroteit=1;
|
||||
}
|
||||
if(location<SAVE_TO_NUM-wroteit && location<itreached)
|
||||
fprintf(scorefile,"%s : %ld\n",namebuff[location],scorebuff[location]);
|
||||
}
|
||||
fflush(scorefile);
|
||||
return ret;
|
||||
for(location=0;location<=itreached && location<SAVE_TO_NUM-wroteit;location++){
|
||||
if(!wroteit && (location>=itreached || score>=scorebuff[location]) ){
|
||||
fprintf(scorefile,"%s : %ld\n",getenv("USER"),score);
|
||||
ret=location;
|
||||
wroteit=1;
|
||||
}
|
||||
if(location<SAVE_TO_NUM-wroteit && location<itreached)
|
||||
fprintf(scorefile,"%s : %ld\n",namebuff[location],scorebuff[location]);
|
||||
}
|
||||
fflush(scorefile);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void showscores(byte playerrank){
|
||||
@ -107,57 +107,57 @@ void showscores(byte playerrank){
|
||||
refresh();
|
||||
return;
|
||||
}
|
||||
if(playerrank == 0){
|
||||
char formername[60]={0};
|
||||
long formerscore=0;
|
||||
rewind(scorefile);
|
||||
fscanf(scorefile,"%*s : %*d");
|
||||
if ( fscanf(scorefile,"%s : %ld",formername,&formerscore)==2 && formerscore>0){
|
||||
if(playerrank == 0){
|
||||
char formername[60]={0};
|
||||
long formerscore=0;
|
||||
rewind(scorefile);
|
||||
fscanf(scorefile,"%*s : %*d");
|
||||
if ( fscanf(scorefile,"%s : %ld",formername,&formerscore)==2 && formerscore>0){
|
||||
byte a = (len-9)/2;
|
||||
attron(A_BOLD);
|
||||
mvprintw(SY,SX, "**** ***");
|
||||
mvprintw(SY,SX, "**** ***");
|
||||
mvprintw(SY+len+1,SX,"***********************");
|
||||
attroff(A_BOLD);
|
||||
attron(green);
|
||||
mvprintw(SY,SX+4,"CONGRATULATIONS!");
|
||||
attroff(green);
|
||||
mvprintw(SY,SX+4,"CONGRATULATIONS!");
|
||||
attroff(green);
|
||||
|
||||
mvprintw(SY+a+1,SX," _____ You bet the");
|
||||
mvprintw(SY+a+2,SX," .' | previous");
|
||||
mvprintw(SY+a+3,SX," .' | record");
|
||||
mvprintw(SY+a+4,SX," | .| | of");
|
||||
mvprintw(SY+a+5,SX," |.' | |%11ld",formerscore);
|
||||
mvprintw(SY+a+6,SX," | | held by");
|
||||
mvprintw(SY+a+7,SX," ___| |___%7s!",formername);
|
||||
mvprintw(SY+a+8,SX," | |");
|
||||
mvprintw(SY+a+9,SX," |____________|");
|
||||
mvprintw(SY+a+1,SX," _____ You bet the");
|
||||
mvprintw(SY+a+2,SX," .' | previous");
|
||||
mvprintw(SY+a+3,SX," .' | record");
|
||||
mvprintw(SY+a+4,SX," | .| | of");
|
||||
mvprintw(SY+a+5,SX," |.' | |%11ld",formerscore);
|
||||
mvprintw(SY+a+6,SX," | | held by");
|
||||
mvprintw(SY+a+7,SX," ___| |___%7s!",formername);
|
||||
mvprintw(SY+a+8,SX," | |");
|
||||
mvprintw(SY+a+9,SX," |____________|");
|
||||
mvprintw(len+2,0,"Game over! Press a key to proceed:");
|
||||
refresh();
|
||||
getch();
|
||||
erase();
|
||||
logo();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," HIGH");
|
||||
mvprintw(4,0,"SCORES");
|
||||
attroff(A_BOLD);
|
||||
//scorefile is still open with w+
|
||||
char pname[60] = {0};
|
||||
long pscore=0;
|
||||
byte rank=0;
|
||||
rewind(scorefile);
|
||||
while( rank<SAVE_TO_NUM && fscanf(scorefile,"%s : %ld\n",pname,&pscore) == 2){
|
||||
//scorefile is still open with w+
|
||||
char pname[60] = {0};
|
||||
long pscore=0;
|
||||
byte rank=0;
|
||||
rewind(scorefile);
|
||||
while( rank<SAVE_TO_NUM && fscanf(scorefile,"%s : %ld\n",pname,&pscore) == 2){
|
||||
move(SY+1+rank,SX+1);
|
||||
attron(green);
|
||||
if(rank == playerrank)
|
||||
printw(">>>");
|
||||
printw("%d",rank+1);
|
||||
if(rank == playerrank)
|
||||
printw(">>>");
|
||||
printw("%d",rank+1);
|
||||
attroff(green);
|
||||
printw(") %s : %ld",pname,pscore);
|
||||
rank++;
|
||||
}
|
||||
rank++;
|
||||
}
|
||||
refresh();
|
||||
}
|
||||
//move in direction
|
||||
@ -276,20 +276,20 @@ void draw(bitbox board[len][wid]){
|
||||
}
|
||||
|
||||
void mouseinput(void){
|
||||
MEVENT minput;
|
||||
MEVENT minput;
|
||||
#ifdef PDCURSES
|
||||
nc_getmouse(&minput);
|
||||
#else
|
||||
getmouse(&minput);
|
||||
#endif
|
||||
if( minput.y-4 <len && minput.x-1<wid*2){
|
||||
py=minput.y-(1+SY);
|
||||
px=minput.x-(1+SX);
|
||||
}
|
||||
else
|
||||
return;
|
||||
if(minput.bstate & BUTTON1_CLICKED)
|
||||
ungetch('\n');
|
||||
if( minput.y-4 <len && minput.x-1<wid*2){
|
||||
py=minput.y-(1+SY);
|
||||
px=minput.x-(1+SX);
|
||||
}
|
||||
else
|
||||
return;
|
||||
if(minput.bstate & BUTTON1_CLICKED)
|
||||
ungetch('\n');
|
||||
}
|
||||
//peacefully close when ^C is pressed
|
||||
void sigint_handler(int x){
|
||||
@ -299,44 +299,44 @@ void sigint_handler(int x){
|
||||
}
|
||||
void help(void){
|
||||
erase();
|
||||
logo();
|
||||
attron(A_BOLD);
|
||||
mvprintw(SY,SX+5,"-* *-");
|
||||
mvprintw(3,0," HELP");
|
||||
logo();
|
||||
attron(A_BOLD);
|
||||
mvprintw(SY,SX+5,"-* *-");
|
||||
mvprintw(3,0," HELP");
|
||||
mvprintw(4,0," PAGE");
|
||||
mvprintw(SY+7,SX,"YOU CAN ALSO USE THE MOUSE!");
|
||||
attroff(A_BOLD);
|
||||
attron(green);
|
||||
mvprintw(SY,SX+7,"THE CONTROLS");
|
||||
attroff(green);
|
||||
mvprintw(SY+1,SX,"RETURN/ENTER : Place/Replace a pipe");
|
||||
mvprintw(SY+2,SX,"hjkl/ARROW KEYS : Move cursor");
|
||||
mvprintw(SY+1,SX,"RETURN/ENTER : Place/Replace a pipe");
|
||||
mvprintw(SY+2,SX,"hjkl/ARROW KEYS : Move cursor");
|
||||
mvprintw(SY+3,SX,"p : Pause");
|
||||
mvprintw(SY+4,SX,"q : Quit");
|
||||
mvprintw(SY+4,SX,"q : Quit");
|
||||
mvprintw(SY+5,SX,"f : Toggle fast flow");
|
||||
mvprintw(SY+6,SX,"g : Go! (End the countdown.)");
|
||||
mvprintw(SY+6,SX,"F1 & F2 : Help on controls & gameplay");
|
||||
mvprintw(SY+9,SX,"Press a key to continue");
|
||||
refresh();
|
||||
while(getch()==ERR);
|
||||
erase();
|
||||
mvprintw(SY+6,SX,"F1 & F2 : Help on controls & gameplay");
|
||||
mvprintw(SY+9,SX,"Press a key to continue");
|
||||
refresh();
|
||||
while(getch()==ERR);
|
||||
erase();
|
||||
}
|
||||
void gameplay(void){
|
||||
erase();
|
||||
logo();
|
||||
attron(A_BOLD);
|
||||
mvprintw(SY,SX+5,"-* *-");
|
||||
mvprintw(3,0," HELP");
|
||||
erase();
|
||||
logo();
|
||||
attron(A_BOLD);
|
||||
mvprintw(SY,SX+5,"-* *-");
|
||||
mvprintw(3,0," HELP");
|
||||
mvprintw(4,0," PAGE");
|
||||
attroff(A_BOLD);
|
||||
attron(green);
|
||||
mvprintw(SY,SX+7,"THE GAMEPLAY");
|
||||
attroff(green);
|
||||
mvprintw(SY+1,SX,"Keep maintaining the pipeline and");
|
||||
mvprintw(SY+2,SX,"don't let the sewage leak.");
|
||||
refresh();
|
||||
while(getch()==ERR);
|
||||
erase();
|
||||
attroff(A_BOLD);
|
||||
attron(green);
|
||||
mvprintw(SY,SX+7,"THE GAMEPLAY");
|
||||
attroff(green);
|
||||
mvprintw(SY+1,SX,"Keep maintaining the pipeline and");
|
||||
mvprintw(SY+2,SX,"don't let the sewage leak.");
|
||||
refresh();
|
||||
while(getch()==ERR);
|
||||
erase();
|
||||
}
|
||||
int main(int argc, char** argv){
|
||||
signal(SIGINT,sigint_handler);
|
||||
@ -447,7 +447,7 @@ int main(int argc, char** argv){
|
||||
if( input == KEY_F(1) || input=='?' ){
|
||||
help();
|
||||
if(!flow)
|
||||
tstart += time(NULL)-now;
|
||||
tstart += time(NULL)-now;
|
||||
}
|
||||
if( input == KEY_F(2) ){
|
||||
gameplay();
|
||||
@ -520,8 +520,8 @@ int main(int argc, char** argv){
|
||||
cbreak();
|
||||
curs_set(1);
|
||||
attron(A_BOLD|green);
|
||||
mvprintw(3,0," OOPS!");
|
||||
attroff(A_BOLD|green);
|
||||
mvprintw(3,0," OOPS!");
|
||||
attroff(A_BOLD|green);
|
||||
draw(board);
|
||||
mvprintw(len+2,0,"Game over! Press a key to see the high scores:");
|
||||
getch();
|
||||
|
@ -127,40 +127,39 @@ void carrotify(bitbox board[len][wid],int count){
|
||||
}
|
||||
}
|
||||
void help(void){
|
||||
erase();
|
||||
erase();
|
||||
mvprintw(0,0," _ ");
|
||||
mvprintw(1,0,"|_)");
|
||||
mvprintw(2,0,"| \\ABBITHOLE");
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE CONTROLS ****");
|
||||
mvprintw(8,0,"YOU CAN ALSO USE THE MOUSE!");
|
||||
attroff(A_BOLD);
|
||||
mvprintw(4,0,"hjkl/ARROW KEYS : Move cursor");
|
||||
mvprintw(5,0,"q : Quit");
|
||||
mvprintw(6,0,"F1 & F2: Help on controls & gameplay (viewing these pages doesn't pause the timer!)");
|
||||
mvprintw(1,0,"|_)");
|
||||
mvprintw(2,0,"| \\ABBITHOLE");
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE CONTROLS ****");
|
||||
attroff(A_BOLD);
|
||||
mvprintw(4,0,"hjkl/ARROW KEYS : Move cursor");
|
||||
mvprintw(5,0,"q : Quit");
|
||||
mvprintw(6,0,"F1 & F2: Help on controls & gameplay (viewing these pages doesn't pause the timer!)");
|
||||
mvprintw(7,0,"PgDn,PgUp,<,> : Scroll");
|
||||
mvprintw(10,0,"Press a key to continue");
|
||||
mvprintw(9,0,"Press a key to continue");
|
||||
|
||||
refresh();
|
||||
while ( getch()==ERR );
|
||||
erase();
|
||||
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);
|
||||
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();
|
||||
refresh();
|
||||
while ( getch()==ERR );
|
||||
erase();
|
||||
}
|
||||
void sigint_handler(int x){
|
||||
endwin();
|
||||
@ -244,13 +243,13 @@ int main(int argc, char** argv){
|
||||
while(1){
|
||||
board[py][px] |= VISITED;
|
||||
if( board[py][px] & CARROT ){
|
||||
carrots_found++;
|
||||
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+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();
|
||||
@ -260,25 +259,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)
|
||||
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);
|
||||
}
|
||||
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) )
|
||||
|
650
sources/redsquare.c
Normal file
650
sources/redsquare.c
Normal file
@ -0,0 +1,650 @@
|
||||
#include <curses.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
#include <time.h>
|
||||
#include <signal.h>
|
||||
#define LEN 35
|
||||
#define WID 50
|
||||
#define RLEN LEN //real
|
||||
#define RWID WID
|
||||
#define DEAD 0
|
||||
#define ALIVE 1
|
||||
#define RED 2
|
||||
/*
|
||||
_ _
|
||||
|_) (_
|
||||
| \ED_)QUARE
|
||||
|
||||
Authored by Hossein Bakhtiarifar <abakh@tuta.io>
|
||||
No rights are reserved and this software comes with no warranties of any kind to the extent permitted by law.
|
||||
|
||||
compile with -lncurses
|
||||
*/
|
||||
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;y<RLEN;y++)
|
||||
for(x=0;x<RWID;x++)
|
||||
b[y][x]=a[y][x];
|
||||
}
|
||||
void logo(void){
|
||||
move(0,0);
|
||||
addstr(" _ _\n");
|
||||
addstr("|_) (_\n");
|
||||
addstr("| \\ED_)QUARE");
|
||||
}
|
||||
void rectangle(int sy,int sx){
|
||||
for(int y=0;y<=LEN;y++){
|
||||
mvaddch(sy+y,sx,ACS_VLINE);
|
||||
mvaddch(sy+y,sx+WID+1,ACS_VLINE);
|
||||
}
|
||||
for(int x=0;x<=WID;x++){
|
||||
mvaddch(sy,sx+x,ACS_HLINE);
|
||||
mvaddch(sy+LEN+1,sx+x,ACS_HLINE);
|
||||
}
|
||||
mvaddch(sy,sx,ACS_ULCORNER);
|
||||
mvaddch(sy+LEN+1,sx,ACS_LLCORNER);
|
||||
mvaddch(sy,sx+WID+1,ACS_URCORNER);
|
||||
mvaddch(sy+LEN+1,sx+WID+1,ACS_LRCORNER);
|
||||
}
|
||||
void count(byte board[LEN][WID]){
|
||||
byte y,x;
|
||||
anum=rnum=0;
|
||||
for(y=0;y<LEN;y++){
|
||||
for(x=0;x<WID;x++){
|
||||
if(board[y][x]==ALIVE)
|
||||
anum++;
|
||||
else if(board[y][x]==RED)
|
||||
rnum++;
|
||||
}
|
||||
}
|
||||
}
|
||||
//display
|
||||
void draw(byte board[RLEN][RWID]){
|
||||
rectangle(3,0);
|
||||
chtype prnt;
|
||||
byte y,x;
|
||||
for(y=0;y<LEN;y++){
|
||||
for(x=0;x<WID;x++){
|
||||
if(y==cy && x==cx){
|
||||
prnt='X';
|
||||
if(board[y][x]==ALIVE)
|
||||
prnt|=A_STANDOUT;
|
||||
else if(board[y][x]==RED)
|
||||
prnt|=colors[3]|A_STANDOUT;
|
||||
}
|
||||
else{
|
||||
if(board[y][x]==ALIVE)
|
||||
prnt=ACS_BLOCK;
|
||||
else if(board[y][x]==RED){
|
||||
if(coherent)
|
||||
prnt=' '|A_STANDOUT|colors[3];
|
||||
else
|
||||
prnt='O'|colors[3];
|
||||
}
|
||||
else
|
||||
prnt=' ';
|
||||
}
|
||||
mvaddch(4+y,x+1,prnt);
|
||||
}
|
||||
}
|
||||
}
|
||||
void rand_level(byte board[RLEN][RWID]){
|
||||
byte y,x;
|
||||
for(y=0;y<LEN/2;y++){
|
||||
for(x=0;x<WID;x++){
|
||||
if(rand()%2){
|
||||
if(rand()%3)
|
||||
board[y][x]=ALIVE;
|
||||
}
|
||||
else
|
||||
board[y][x]=DEAD;
|
||||
}
|
||||
}
|
||||
}
|
||||
void live(byte board[RLEN][RWID]){
|
||||
byte y,x;
|
||||
byte dy,dx;//delta
|
||||
byte ry,rx;
|
||||
byte alives,reds;
|
||||
byte preboard[RLEN][RWID];
|
||||
cp(board,preboard);
|
||||
for(y=0;y<LEN;y++){
|
||||
for(x=0;x<WID;x++){
|
||||
alives=reds=0;
|
||||
for(dy=-1;dy<2;dy++){
|
||||
for(dx=-1;dx<2;dx++){
|
||||
if(!dy && !dx)
|
||||
continue;
|
||||
ry=y+dy;
|
||||
rx=x+dx;
|
||||
if(ry==-1)
|
||||
ry=LEN-1;
|
||||
else if(ry==LEN)
|
||||
ry=0;
|
||||
if(rx==-1)
|
||||
rx=WID-1;
|
||||
else if(rx==WID)
|
||||
rx=0;
|
||||
|
||||
if(preboard[ry][rx]==ALIVE)
|
||||
alives++;
|
||||
else if(preboard[ry][rx]==RED)
|
||||
reds++;
|
||||
}
|
||||
}
|
||||
if(board[y][x]){
|
||||
if(alives+reds==2 || alives+reds==3){
|
||||
if(reds>alives)
|
||||
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;y<LEN;y++)
|
||||
for(x=0;x<WID;x++)
|
||||
if(board[y][x]==RED)
|
||||
goto FoundTheFirst;
|
||||
FoundTheFirst:
|
||||
for(dy=0;dy<2;dy++){
|
||||
for(dx=0;dx<2;dx++){
|
||||
ry=y+dy;
|
||||
if(ry==-1)
|
||||
ry=LEN-1;
|
||||
else if(ry==LEN)
|
||||
ry=0;
|
||||
rx=x+dx;
|
||||
if(rx==-1)
|
||||
rx=WID-1;
|
||||
else if(rx==WID)
|
||||
rx=0;
|
||||
if(board[ry][rx]!=RED){
|
||||
if(!y){
|
||||
y=LEN-1;//it can be divided at both sides of the border, this prevents failing
|
||||
//it goes to look from the upper-left corner of the square as it would for other squares
|
||||
goto FoundTheFirst;
|
||||
}
|
||||
if(!x){
|
||||
x=WID-1;
|
||||
goto FoundTheFirst;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
py=y;
|
||||
px=x;
|
||||
coherent=1;
|
||||
}
|
||||
void sigint_handler(int x){
|
||||
endwin();
|
||||
puts("Quit.");
|
||||
exit(x);
|
||||
}
|
||||
/*void mouseinput(int sy, int sx){
|
||||
MEVENT minput;
|
||||
#ifdef PDCURSES
|
||||
nc_getmouse(&minput);
|
||||
#else
|
||||
getmouse(&minput);
|
||||
#endif
|
||||
if( minput.y-4-sy<LEN && minput.x-1-sx<WID*2){
|
||||
py=minput.y-4-sy;
|
||||
px=(minput.x-1-sx)/2;
|
||||
}
|
||||
else
|
||||
return;
|
||||
if(minput.bstate & BUTTON1_CLICKED)
|
||||
ungetch('\n');
|
||||
if(minput.bstate & (BUTTON2_CLICKED|BUTTON3_CLICKED) )
|
||||
ungetch(' ');
|
||||
}*/
|
||||
void help(void){
|
||||
erase();
|
||||
logo();
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE CONTROLS ****");
|
||||
mvprintw(10,0,"YOU CAN ALSO USE THE MOUSE!");
|
||||
attroff(A_BOLD);
|
||||
mvprintw(4,0,"RETURN/ENTER : Examine for bombs");
|
||||
mvprintw(5,0,"SPACE : Flag/Unflag");
|
||||
mvprintw(6,0,"hjkl/ARROW KEYS : Move cursor");
|
||||
mvprintw(7,0,"q : Quit");
|
||||
mvprintw(8,0,"F1 & F2 : Help on controls & gameplay");
|
||||
mvprintw(9,0,"PgDn,PgUp,<,> : Scroll");
|
||||
mvprintw(12,0,"Press a key to continue");
|
||||
refresh();
|
||||
getch();
|
||||
erase();
|
||||
}
|
||||
void gameplay(void){
|
||||
erase();
|
||||
logo();
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE GAMEPLAY ****");
|
||||
attroff(A_BOLD);
|
||||
mvprintw(4,0,"Try to find the landmines in the field\n");
|
||||
printw("with logical reasoning: When you click\n");
|
||||
printw("on a tile ( a '.' here), numbers may show\n");
|
||||
printw("up that indicate the number of landmines\n");
|
||||
printw("in adjacent tiles; you should find and \n");
|
||||
printw("avoid the landmines based on them; and\n");
|
||||
printw("clicking on a landmine would make you\n");
|
||||
printw("lose the game.");
|
||||
refresh();
|
||||
getch();
|
||||
erase();
|
||||
}
|
||||
int main(void){
|
||||
signal(SIGINT,sigint_handler);
|
||||
srand(time(NULL)%UINT_MAX);
|
||||
initscr();
|
||||
mousemask(ALL_MOUSE_EVENTS,NULL);
|
||||
noecho();
|
||||
cbreak();
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
byte board[RLEN][RWID];
|
||||
memset(board,0,RLEN*RWID);
|
||||
char result[70];
|
||||
int input;
|
||||
int prey,prex;
|
||||
int cinred;
|
||||
Start:
|
||||
cinred=0;
|
||||
py=LEN*3/4;
|
||||
px=WID/2;
|
||||
curs_set(0);
|
||||
level=-1;
|
||||
new_level(board);
|
||||
mk_square(board);
|
||||
while(1){
|
||||
switch(rand()%5){//move the cross
|
||||
case 0:
|
||||
cx++;
|
||||
if(cx==WID)
|
||||
cx=0;
|
||||
break;
|
||||
case 1:
|
||||
cy--;
|
||||
if(cy==-1)
|
||||
cy=LEN-1;
|
||||
break;
|
||||
case 2:
|
||||
cx--;
|
||||
if(cx==-1)
|
||||
cx=WID-1;
|
||||
break;
|
||||
case 3:
|
||||
cy++;
|
||||
if(cy==LEN)
|
||||
cy=0;
|
||||
break;
|
||||
case 4:
|
||||
;//stay there
|
||||
}
|
||||
if(board[cy][cx]==RED)
|
||||
cinred++;
|
||||
else
|
||||
cinred=0;
|
||||
/*if(rnum>anum||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);
|
||||
}*/
|
||||
count(board);
|
||||
if(rnum!=4)
|
||||
coherent=0;
|
||||
if(!coherent && rnum==4)
|
||||
reemerge(board);
|
||||
erase();
|
||||
logo();
|
||||
draw(board);
|
||||
refresh();
|
||||
if(rnum>anum||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;
|
||||
}
|
||||
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(board[cy][cx]==RED)
|
||||
cinred++;
|
||||
else
|
||||
cinred=0;*/
|
||||
|
||||
/*if(rnum>anum||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);
|
||||
}*/
|
||||
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:
|
||||
/*switch(rand()%5){//move the cross
|
||||
case 0:
|
||||
cx++;
|
||||
if(cx==WID)
|
||||
cx=0;
|
||||
break;
|
||||
case 1:
|
||||
cy--;
|
||||
if(cy==-1)
|
||||
cy=LEN-1;
|
||||
break;
|
||||
case 2:
|
||||
cx--;
|
||||
if(cx==-1)
|
||||
cx=WID-1;
|
||||
break;
|
||||
case 3:
|
||||
cy++;
|
||||
if(cy==LEN)
|
||||
cy=0;
|
||||
break;
|
||||
case 4:
|
||||
;//stay there
|
||||
}*/
|
||||
/*if(board[cy][cx]==RED)
|
||||
cinred++;
|
||||
else
|
||||
cinred=0;
|
||||
if(rnum>anum||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);
|
||||
}*/
|
||||
if( input=='q')
|
||||
sigint_handler(0);
|
||||
}
|
||||
mvprintw(LEN+5,0,"Wanna play again?(y/n)");
|
||||
curs_set(1);
|
||||
input=getch();
|
||||
if(input != 'N' && input != 'n' && input != 'q')
|
||||
goto Start;
|
||||
endwin();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
@ -14,7 +14,7 @@ compile with -lncurses
|
||||
*/
|
||||
typedef signed char byte;
|
||||
byte py,px;//cursor
|
||||
const char piece[2] = "OX";
|
||||
const char piece[2] = {'O','X'};
|
||||
char game[8][8];//main board
|
||||
byte computer[2] = {0,0};
|
||||
byte score[2];//set by header()
|
||||
@ -61,7 +61,7 @@ void draw(byte sy,byte sx){//the game's board
|
||||
attr |= A_STANDOUT;
|
||||
if(game[y][x])
|
||||
mvaddch(sy+1+y,sx+x*2+1,attr|game[y][x]);
|
||||
else
|
||||
else
|
||||
mvaddch(sy+1+y,sx+x*2+1,attr|'.');
|
||||
}
|
||||
}
|
||||
@ -214,42 +214,42 @@ void sigint_handler(int x){
|
||||
exit(x);
|
||||
}
|
||||
void mouseinput(void){
|
||||
MEVENT minput;
|
||||
MEVENT minput;
|
||||
#ifdef PDCURSES
|
||||
nc_getmouse(&minput);
|
||||
#else
|
||||
getmouse(&minput);
|
||||
#endif
|
||||
if( minput.y-4 <8 && minput.x-1<16){
|
||||
py=minput.y-4;
|
||||
px=(minput.x-1)/2;
|
||||
}
|
||||
else
|
||||
return;
|
||||
if(minput.bstate & BUTTON1_CLICKED)
|
||||
ungetch('\n');
|
||||
if( minput.y-4 <8 && minput.x-1<16){
|
||||
py=minput.y-4;
|
||||
px=(minput.x-1)/2;
|
||||
}
|
||||
else
|
||||
return;
|
||||
if(minput.bstate & BUTTON1_CLICKED)
|
||||
ungetch('\n');
|
||||
}
|
||||
void help(void){
|
||||
erase();
|
||||
erase();
|
||||
header();
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE CONTROLS ****");
|
||||
mvprintw(8,0,"YOU CAN ALSO USE THE MOUSE!");
|
||||
attroff(A_BOLD);
|
||||
mvprintw(4,0,"RETURN/ENTER : Put the piece");
|
||||
mvprintw(5,0,"hjkl/ARROW KEYS : Move cursor");
|
||||
mvprintw(6,0,"q : Quit");
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE CONTROLS ****");
|
||||
mvprintw(8,0,"YOU CAN ALSO USE THE MOUSE!");
|
||||
attroff(A_BOLD);
|
||||
mvprintw(4,0,"RETURN/ENTER : Put the piece");
|
||||
mvprintw(5,0,"hjkl/ARROW KEYS : Move cursor");
|
||||
mvprintw(6,0,"q : Quit");
|
||||
mvprintw(7,0,"F1 & F2 : Help on controls & gameplay");
|
||||
mvprintw(10,0,"Press a key to continue");
|
||||
mvprintw(10,0,"Press a key to continue");
|
||||
curs_set(1);
|
||||
getch();
|
||||
getch();
|
||||
}
|
||||
void gameplay(void){
|
||||
erase();
|
||||
header();
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE GAMEPLAY ****");
|
||||
attroff(A_BOLD);
|
||||
erase();
|
||||
header();
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE GAMEPLAY ****");
|
||||
attroff(A_BOLD);
|
||||
move(4,0);
|
||||
printw("Players take turns placing disks on the board:\n\n");
|
||||
printw("1) Any pieces of the opponet's color that is bounded\n");
|
||||
@ -260,7 +260,7 @@ void gameplay(void){
|
||||
printw(" opponet'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();
|
||||
getch();
|
||||
}
|
||||
int main(int argc , char** argv){
|
||||
int depth=2;
|
||||
@ -322,8 +322,8 @@ int main(int argc , char** argv){
|
||||
Turn:
|
||||
erase();
|
||||
flushinp();
|
||||
header();
|
||||
draw(3,0);
|
||||
header();
|
||||
refresh();
|
||||
if(cantmove >=2)//both sides cant move, the game ends
|
||||
goto End;
|
||||
@ -350,8 +350,11 @@ int main(int argc , char** argv){
|
||||
cantmove=0;
|
||||
while(1){ //human control
|
||||
erase();
|
||||
header();
|
||||
draw(3,0);
|
||||
header();
|
||||
if(!(computer[0]||computer[1]))
|
||||
mvprintw(0,5,"%c's turn",piece[turn]);
|
||||
refresh();
|
||||
input=getch();
|
||||
if( input==KEY_F(1) || input=='?' )
|
||||
help();
|
||||
|
184
sources/sos.c
184
sources/sos.c
@ -95,28 +95,28 @@ byte did_sos(char board[len][wid], int y , int x ){
|
||||
}
|
||||
void color_sos(char board[len][wid],byte colored[len][wid], int y , int x ,bool side){
|
||||
byte dy,dx;
|
||||
if(board[y][x]== 'S'){
|
||||
for(dy=-1;dy<2;dy++){
|
||||
for(dx=-1;dx<2;dx++){
|
||||
if(rd(board,y+dy,x+dx)=='O' && rd(board,y+2*dy,x+2*dx) == 'S' ){
|
||||
if(board[y][x]== 'S'){
|
||||
for(dy=-1;dy<2;dy++){
|
||||
for(dx=-1;dx<2;dx++){
|
||||
if(rd(board,y+dy,x+dx)=='O' && rd(board,y+2*dy,x+2*dx) == 'S' ){
|
||||
color(colored,y,x,side);
|
||||
color(colored,y+dy,x+dx,side);
|
||||
color(colored,y+2*dy,x+2*dx,side);
|
||||
color(colored,y+dy,x+dx,side);
|
||||
color(colored,y+2*dy,x+2*dx,side);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(board[y][x]== 'O'){
|
||||
for(dy=-1;dy<2;dy++){
|
||||
for(dx=-1;dx<2;dx++){
|
||||
if(rd(board,y+dy,x+dx)=='S' && rd(board,y-dy,x-dx) =='S'){
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(board[y][x]== 'O'){
|
||||
for(dy=-1;dy<2;dy++){
|
||||
for(dx=-1;dx<2;dx++){
|
||||
if(rd(board,y+dy,x+dx)=='S' && rd(board,y-dy,x-dx) =='S'){
|
||||
color(colored,y,x,side);
|
||||
color(colored,y+dy,x+dx,side);
|
||||
color(colored,y-dy,x-dx,side);
|
||||
color(colored,y+dy,x+dx,side);
|
||||
color(colored,y-dy,x-dx,side);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void randmove(int* y,int* x,byte* c){
|
||||
*y=rand()%len;
|
||||
@ -178,59 +178,59 @@ void sigint_handler(int x){
|
||||
exit(x);
|
||||
}
|
||||
void mouseinput(int sy,int sx){
|
||||
MEVENT minput;
|
||||
MEVENT minput;
|
||||
#ifdef PDCURSES
|
||||
nc_getmouse(&minput);
|
||||
#else
|
||||
getmouse(&minput);
|
||||
#endif
|
||||
if( minput.y-4-sy <len && minput.x-1-sx<wid*2){
|
||||
py=minput.y-4-sy;
|
||||
px=(minput.x-1-sx)/2;
|
||||
}
|
||||
else
|
||||
return;
|
||||
if(minput.bstate & BUTTON1_CLICKED)
|
||||
ungetch('S');
|
||||
if(minput.bstate & (BUTTON2_CLICKED|BUTTON3_CLICKED) )
|
||||
ungetch('O');
|
||||
if( minput.y-4-sy <len && minput.x-1-sx<wid*2){
|
||||
py=minput.y-4-sy;
|
||||
px=(minput.x-1-sx)/2;
|
||||
}
|
||||
else
|
||||
return;
|
||||
if(minput.bstate & BUTTON1_CLICKED)
|
||||
ungetch('S');
|
||||
if(minput.bstate & (BUTTON2_CLICKED|BUTTON3_CLICKED) )
|
||||
ungetch('O');
|
||||
}
|
||||
void help(void){
|
||||
erase();
|
||||
erase();
|
||||
mvprintw(0,0," _ _ _");
|
||||
mvprintw(1,0,"(_'| |(_' ");
|
||||
mvprintw(2,0,"._):_:._) ");
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE CONTROLS ****");
|
||||
mvprintw(9,0,"YOU CAN ALSO USE THE MOUSE!");
|
||||
attroff(A_BOLD);
|
||||
mvprintw(1,0,"(_'| |(_' ");
|
||||
mvprintw(2,0,"._):_:._) ");
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE CONTROLS ****");
|
||||
mvprintw(9,0,"YOU CAN ALSO USE THE MOUSE!");
|
||||
attroff(A_BOLD);
|
||||
mvprintw(4,0,"hjkl/ARROW KEYS : Move cursor");
|
||||
mvprintw(5,0,"S & O : Write S or O");
|
||||
mvprintw(6,0,"q : Quit");
|
||||
mvprintw(7,0,"F1 & F2: Help on controls & gameplay");
|
||||
mvprintw(7,0,"F1 & F2: Help on controls & gameplay");
|
||||
mvprintw(8,0,"PgDn,PgUp,<,> : Scroll");
|
||||
mvprintw(11,0,"Press a key to continue");
|
||||
refresh();
|
||||
getch();
|
||||
erase();
|
||||
mvprintw(11,0,"Press a key to continue");
|
||||
refresh();
|
||||
getch();
|
||||
erase();
|
||||
}
|
||||
void gameplay(void){
|
||||
erase();
|
||||
mvprintw(0,0," _ _ _");
|
||||
mvprintw(1,0,"(_'| |(_' ");
|
||||
mvprintw(2,0,"._):_:._) ");
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE GAMEPLAY ****");
|
||||
attroff(A_BOLD);
|
||||
erase();
|
||||
mvprintw(0,0," _ _ _");
|
||||
mvprintw(1,0,"(_'| |(_' ");
|
||||
mvprintw(2,0,"._):_:._) ");
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE GAMEPLAY ****");
|
||||
attroff(A_BOLD);
|
||||
move(4,0);
|
||||
printw("The game is similiar to Tic Tac Toe:\n");
|
||||
printw("The players write S and O in the squares\n");
|
||||
printw("and making the straight connected sequence\n");
|
||||
printw("S-O-S makes you a score; obviously, the\n");
|
||||
printw("player with a higher score wins.");
|
||||
refresh();
|
||||
getch();
|
||||
erase();
|
||||
refresh();
|
||||
getch();
|
||||
erase();
|
||||
}
|
||||
int main(int argc, char** argv){
|
||||
int dpt=1;
|
||||
@ -279,27 +279,27 @@ int main(int argc, char** argv){
|
||||
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");
|
||||
}
|
||||
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");
|
||||
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");
|
||||
}
|
||||
input=getch();
|
||||
if(input=='h'){
|
||||
computer[1]=0;
|
||||
printw("Human.\n");
|
||||
}
|
||||
else{
|
||||
computer[1]=dpt;
|
||||
printw("Computer.\n");
|
||||
}
|
||||
if(has_colors()){
|
||||
start_color();
|
||||
use_default_colors();
|
||||
@ -340,32 +340,32 @@ int main(int argc, char** argv){
|
||||
//else
|
||||
while(1){
|
||||
erase();
|
||||
mvprintw(sy+0,sx+0," _ _ _");
|
||||
mvprintw(sy+1,sx+0,"(_'| |(_' %d vs %d \n",score[0],score[1]);
|
||||
mvprintw(sy+2,sx+0,"._):_:._) \n");
|
||||
mvprintw(sy+0,sx+0," _ _ _");
|
||||
mvprintw(sy+1,sx+0,"(_'| |(_' %d vs %d \n",score[0],score[1]);
|
||||
mvprintw(sy+2,sx+0,"._):_:._) \n");
|
||||
draw(sy+3,sx+0,board,colored);
|
||||
refresh();
|
||||
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);
|
||||
}
|
||||
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(1) || input=='?')
|
||||
help();
|
||||
if( input==KEY_F(2) )
|
||||
|
118
sources/sudoku.c
118
sources/sudoku.c
@ -55,7 +55,7 @@ byte sgn2int(char sgn){
|
||||
return sgn-'a'+10;
|
||||
if('A'<=sgn && sgn <= 'Z')
|
||||
return sgn-'A'+36;
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
char int2sgn(byte num){// convert integer to representing sign
|
||||
@ -142,7 +142,7 @@ void fill(char board[s][s]){
|
||||
if ( fill_with(board,int2sgn(num) ) ){
|
||||
memset(board,0,s*s);
|
||||
num=0;
|
||||
mvaddstr(0,0,"My algorithm sucks, so you need to wait a bit ");//with animated dots to entertain the waiter
|
||||
mvaddstr(0,0,"My algorithm sucks, so you need to wait a bit ");//with animated dots to entertain the waiter
|
||||
if(waitcycles==3)
|
||||
mvaddstr(2,0,"(You can set SUDOKU_FASTGEN if you just want to see if it works)");
|
||||
move(0,45);
|
||||
@ -231,22 +231,22 @@ void sigint_handler(int x){
|
||||
exit(x);
|
||||
}
|
||||
void mouseinput(int sy, int sx){
|
||||
MEVENT m;
|
||||
MEVENT m;
|
||||
#ifdef PDCURSES
|
||||
nc_getmouse(&m);
|
||||
nc_getmouse(&m);
|
||||
#else
|
||||
getmouse(&m);
|
||||
#endif
|
||||
if( m.y < (3+1+size+s)-sy && m.x<(2*s+1)-sx ){//it's a shame to include math.h only for round() but it was the only moral way to make gcc shut up
|
||||
py= round( (float)(size*(m.y-4-sy))/(size+1) );//these are derived from the formulas in draw() by simple algebra
|
||||
px=(m.x-1-sx)/2;
|
||||
}
|
||||
else
|
||||
return;
|
||||
if(m.bstate & BUTTON1_CLICKED)
|
||||
ungetch('\n');
|
||||
if(m.bstate & (BUTTON2_CLICKED|BUTTON3_CLICKED) )
|
||||
ungetch(' ');
|
||||
if( m.y < (3+1+size+s)-sy && m.x<(2*s+1)-sx ){//it's a shame to include math.h only for round() but it was the only moral way to make gcc shut up
|
||||
py= round( (float)(size*(m.y-4-sy))/(size+1) );//these are derived from the formulas in draw() by simple algebra
|
||||
px=(m.x-1-sx)/2;
|
||||
}
|
||||
else
|
||||
return;
|
||||
if(m.bstate & BUTTON1_CLICKED)
|
||||
ungetch('\n');
|
||||
if(m.bstate & (BUTTON2_CLICKED|BUTTON3_CLICKED) )
|
||||
ungetch(' ');
|
||||
}
|
||||
void help(void){
|
||||
erase();
|
||||
@ -272,10 +272,10 @@ void help(void){
|
||||
erase();
|
||||
}
|
||||
void gameplay(void){
|
||||
erase();
|
||||
header(0,0);
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE GAMEPLAY ****");
|
||||
erase();
|
||||
header(0,0);
|
||||
attron(A_BOLD);
|
||||
mvprintw(3,0," **** THE GAMEPLAY ****");
|
||||
attroff(A_BOLD);
|
||||
move(4,0);
|
||||
printw("Fill the table with digits");
|
||||
@ -295,9 +295,9 @@ void gameplay(void){
|
||||
printw(" and all\nthe alphabet letters from 'a' to '%c'.",int2sgn(s));
|
||||
}
|
||||
printw("\n\nPress a key to continue.");
|
||||
refresh();
|
||||
getch();
|
||||
erase();
|
||||
refresh();
|
||||
getch();
|
||||
erase();
|
||||
}
|
||||
int main(int argc,char** argv){
|
||||
signal(SIGINT,sigint_handler);
|
||||
@ -327,28 +327,28 @@ int main(int argc,char** argv){
|
||||
diff=2;
|
||||
bool fastgen= !(!getenv("SUDOKU_FASTGEN"));
|
||||
initscr();
|
||||
mousemask(ALL_MOUSE_EVENTS,NULL);
|
||||
noecho();
|
||||
cbreak();
|
||||
keypad(stdscr,1);
|
||||
srand(time(NULL)%UINT_MAX);
|
||||
mousemask(ALL_MOUSE_EVENTS,NULL);
|
||||
noecho();
|
||||
cbreak();
|
||||
keypad(stdscr,1);
|
||||
srand(time(NULL)%UINT_MAX);
|
||||
if( has_colors() ){
|
||||
start_color();
|
||||
use_default_colors();
|
||||
init_pair(1,COLOR_YELLOW,-1);
|
||||
init_pair(2,COLOR_GREEN,-1);
|
||||
init_pair(3,COLOR_BLUE,-1);
|
||||
init_pair(4,COLOR_CYAN,-1);
|
||||
init_pair(5,COLOR_MAGENTA,-1);
|
||||
init_pair(6,COLOR_RED,-1);
|
||||
for(byte b=0;b<6;b++){
|
||||
colors[b]=COLOR_PAIR(b+1);
|
||||
}
|
||||
}
|
||||
start_color();
|
||||
use_default_colors();
|
||||
init_pair(1,COLOR_YELLOW,-1);
|
||||
init_pair(2,COLOR_GREEN,-1);
|
||||
init_pair(3,COLOR_BLUE,-1);
|
||||
init_pair(4,COLOR_CYAN,-1);
|
||||
init_pair(5,COLOR_MAGENTA,-1);
|
||||
init_pair(6,COLOR_RED,-1);
|
||||
for(byte b=0;b<6;b++){
|
||||
colors[b]=COLOR_PAIR(b+1);
|
||||
}
|
||||
}
|
||||
s= size*size;
|
||||
char board[s][s];
|
||||
char empty[s][s];
|
||||
char game[s][s];
|
||||
char empty[s][s];
|
||||
char game[s][s];
|
||||
int input=0;
|
||||
int sy,sx;
|
||||
Start:
|
||||
@ -375,25 +375,25 @@ int main(int argc,char** argv){
|
||||
break;
|
||||
input = getch();
|
||||
if( input==KEY_PPAGE && LINES< s+size+3){//the board starts in 3
|
||||
sy+=10;
|
||||
if(sy>0)
|
||||
sy=0;
|
||||
}
|
||||
if( input==KEY_NPAGE && LINES< s+size+3){
|
||||
sy-=10;
|
||||
if(sy< -(s+size+3) )
|
||||
sy=-(s+size+3);
|
||||
}
|
||||
if( input=='<' && COLS< s*2){
|
||||
sx+=10;
|
||||
if(sx>0)
|
||||
sx=0;
|
||||
}
|
||||
if( input=='>' && COLS< s*2){
|
||||
sx-=10;
|
||||
if(sx< -(s*2))
|
||||
sx=-(s*2);
|
||||
}
|
||||
sy+=10;
|
||||
if(sy>0)
|
||||
sy=0;
|
||||
}
|
||||
if( input==KEY_NPAGE && LINES< s+size+3){
|
||||
sy-=10;
|
||||
if(sy< -(s+size+3) )
|
||||
sy=-(s+size+3);
|
||||
}
|
||||
if( input=='<' && COLS< s*2){
|
||||
sx+=10;
|
||||
if(sx>0)
|
||||
sx=0;
|
||||
}
|
||||
if( input=='>' && COLS< s*2){
|
||||
sx-=10;
|
||||
if(sx< -(s*2))
|
||||
sx=-(s*2);
|
||||
}
|
||||
if(input == KEY_F(1))
|
||||
help();
|
||||
if(input == KEY_F(2))
|
||||
|
Loading…
x
Reference in New Issue
Block a user