1
0
mirror of https://github.com/abakh/nbsdgames.git synced 2024-06-08 17:20:41 +00:00
nbsdgames/mines.c

384 lines
7.8 KiB
C
Raw Normal View History

2020-06-20 16:26:54 +00:00
/*
|\/|
| |INES
Authored by abakh <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
*/
2019-03-20 14:16:33 +00:00
#include <curses.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#include <signal.h>
#include <stdbool.h>
2020-09-12 10:38:45 +00:00
#include "config.h"
2019-03-20 14:16:33 +00:00
#define FLAG 9
#define UNCLEAR 10
2020-06-20 16:26:54 +00:00
typedef signed char byte;
2020-09-12 10:38:45 +00:00
#ifdef NO_VLA //The Plan9 compiler can not handle VLAs
#define len 8
#define wid 8
2020-06-20 16:26:54 +00:00
#else
int len,wid;
#endif
int py,px,flags;
2019-03-20 14:16:33 +00:00
int untouched;
int mscount;
chtype colors[6]={0};
2020-06-20 16:26:54 +00:00
2019-03-20 14:16:33 +00:00
void rectangle(int sy,int sx){
2020-06-20 16:26:54 +00:00
for(int y=0;y<=len+1;++y){
2019-03-20 14:16:33 +00:00
mvaddch(sy+y,sx,ACS_VLINE);
mvaddch(sy+y,sx+wid*2,ACS_VLINE);
}
2020-06-20 16:26:54 +00:00
for(int x=0;x<=wid*2;++x){
2019-03-20 14:16:33 +00:00
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*2,ACS_URCORNER);
mvaddch(sy+len+1,sx+wid*2,ACS_LRCORNER);
}
//display
void draw(int sy,int sx,byte board[len][wid]){
rectangle(sy,sx);
chtype attr ;
char prnt;
int y,x;
2020-06-20 16:26:54 +00:00
for(y=0;y<len;++y){
for(x=0;x<wid;++x){
2019-03-20 14:16:33 +00:00
attr=A_NORMAL;
if(y==py && x==px)
attr |= A_STANDOUT;
if(board[y][x]<0)
prnt='.';
else if(!board[y][x])
prnt=' ';
else if(board[y][x] < 8){
attr |= colors[board[y][x]-1];
prnt='0'+board[y][x];
}
else if(board[y][x]==9){
attr |= colors[3];
prnt='P';
}
else if(board[y][x]>9)
prnt='?';
mvaddch(sy+1+y,sx+x*2+1,attr|prnt);
}
}
}
//show the mines
void drawmines(int sy,int sx,byte board[len][wid],bool mines[len][wid]){
int y,x;
2020-06-20 16:26:54 +00:00
for(y=0;y<len;++y){
for(x=0;x<wid;++x){
2019-03-20 14:16:33 +00:00
if(mines[y][x]){
if(y==py&&x==px)
mvaddch(sy+y+1,sx+x*2+1,'X');
else if(board[y][x]==9)
mvaddch(sy+y+1,sx+x*2+1,'%');
else
mvaddch(sy+y+1,sx+x*2+1,'*');
}
}
}
}
//place mines
void mine(bool mines[len][wid]){
2019-07-30 18:13:10 +00:00
int y=rand()%len;
int x=rand()%wid;
2020-06-20 16:26:54 +00:00
for(int n=0;n<mscount;++n){
2019-03-20 14:16:33 +00:00
while(mines[y][x]){
2019-07-30 18:13:10 +00:00
y=rand()%len;
x=rand()%wid;
2019-03-20 14:16:33 +00:00
}
mines[y][x]=true;
}
}
bool click(byte board[len][wid],bool mines[len][wid],int ty,int tx){
if(board[ty][tx]>=0 && board[ty][tx] <9)//it has been click()ed before
return 0;
else{//untouched
if(board[ty][tx]==FLAG)
2020-06-20 16:26:54 +00:00
--flags;
2019-03-20 14:16:33 +00:00
board[ty][tx]=0;
2020-06-20 16:26:54 +00:00
--untouched;
2019-03-20 14:16:33 +00:00
}
int y,x;
2020-06-20 16:26:54 +00:00
for(y=ty-1;y<ty+2;++y){
2019-03-20 14:16:33 +00:00
if(y<0)
y=0;
if(y>=len)
break;
2020-06-20 16:26:54 +00:00
for (x=tx-1;x<tx+2;++x){
2019-03-20 14:16:33 +00:00
if(x<0)
x=0;
if(x>=wid)
break;
if(mines[y][x])
board[ty][tx]++;
}
}
if(!board[ty][tx]){//there are no mines in the adjacent tiles
2020-06-20 16:26:54 +00:00
for(y=ty-1;y<ty+2;++y){
2019-03-20 14:16:33 +00:00
if(y<0)
y=0;
if(y>=len)
break;
2020-06-20 16:26:54 +00:00
for(x=tx-1;x<tx+2;++x){
2019-03-20 14:16:33 +00:00
if(x<0)
x=0;
if(x>=wid)
break;
click(board,mines,y,x);
}
}
}
return 0;
}
void sigint_handler(int x){
endwin();
puts("Quit.");
exit(x);
}
void mouseinput(int sy, int sx){
2020-09-12 10:38:45 +00:00
#ifndef NO_MOUSE
2019-03-20 14:16:33 +00:00
MEVENT minput;
2019-07-30 19:50:57 +00:00
#ifdef PDCURSES
nc_getmouse(&minput);
#else
2019-03-20 14:16:33 +00:00
getmouse(&minput);
2019-07-30 19:50:57 +00:00
#endif
2019-03-20 14:16:33 +00:00
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(' ');
2020-09-12 10:38:45 +00:00
#endif
2019-03-20 14:16:33 +00:00
}
void help(void){
2020-05-18 18:59:29 +00:00
erase();
2019-03-20 14:16:33 +00:00
mvprintw(1,0,"|\\/|");
mvprintw(2,0,"| |INES");
2020-05-18 18:59:29 +00:00
attron(A_BOLD);
mvprintw(3,0," **** THE CONTROLS ****");
2019-03-20 14:16:33 +00:00
mvprintw(10,0,"YOU CAN ALSO USE THE MOUSE!");
2020-05-18 18:59:29 +00:00
attroff(A_BOLD);
2019-03-20 14:16:33 +00:00
mvprintw(4,0,"RETURN/ENTER : Examine for bombs");
2020-05-18 18:59:29 +00:00
mvprintw(5,0,"SPACE : Flag/Unflag");
mvprintw(6,0,"hjkl/ARROW KEYS : Move cursor");
mvprintw(7,0,"q : Quit");
2019-03-20 14:16:33 +00:00
mvprintw(8,0,"F1 & F2 : Help on controls & gameplay");
mvprintw(9,0,"PgDn,PgUp,<,> : Scroll");
2020-05-18 18:59:29 +00:00
mvprintw(12,0,"Press a key to continue");
2019-03-20 14:16:33 +00:00
refresh();
2020-05-18 18:59:29 +00:00
getch();
2019-03-20 14:16:33 +00:00
erase();
}
void gameplay(void){
2020-05-18 18:59:29 +00:00
erase();
mvprintw(1,0,"|\\/|");
mvprintw(2,0,"| |INES");
attron(A_BOLD);
mvprintw(3,0," **** THE GAMEPLAY ****");
attroff(A_BOLD);
2019-03-20 14:16:33 +00:00
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.");
2020-05-18 18:59:29 +00:00
refresh();
getch();
erase();
2019-03-20 14:16:33 +00:00
}
int main(int argc, char** argv){
signal(SIGINT,sigint_handler);
2020-09-12 10:38:45 +00:00
#ifndef NO_VLA
2019-03-20 14:16:33 +00:00
if(argc>4 || (argc==2 && !strcmp("help",argv[1])) ){
printf("Usage: %s [len wid [minescount]]\n",argv[0]);
return EXIT_FAILURE;
}
if(argc==2){
puts("Give both dimensions.");
return EXIT_FAILURE;
}
if(argc>=3){
bool lool = sscanf(argv[1],"%d",&len) && sscanf(argv[2],"%d",&wid);
if(!lool){
puts("Invalid input.");
return EXIT_FAILURE;
}
if(len<5 || wid<5 || len>1000 || wid>1000){
puts("At least one of your given dimensions is either too small or too big.");
return EXIT_FAILURE;
}
}
else
len=wid=8;
if(argc==4){
if( !sscanf(argv[3],"%d",&mscount)){
puts("Invalid input.");
return EXIT_FAILURE;
}
if( mscount<5 || mscount>= len*wid){
puts("Too few/many mines.");
return EXIT_FAILURE;
}
}
else
mscount = len*wid/6;
2020-06-20 16:26:54 +00:00
#else
mscount=len*wid/6;
#endif
2019-07-30 18:13:10 +00:00
srand(time(NULL)%UINT_MAX);
2019-03-20 14:16:33 +00:00
initscr();
2020-09-12 10:38:45 +00:00
#ifndef NO_MOUSE
2020-05-18 18:59:29 +00:00
mousemask(ALL_MOUSE_EVENTS,NULL);
2020-09-12 10:38:45 +00:00
#endif
2020-05-18 18:59:29 +00:00
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);
2020-06-20 16:26:54 +00:00
for(byte b= 0;b<6;++b){
2020-05-18 18:59:29 +00:00
colors[b]=COLOR_PAIR(b+1);
}
2019-03-20 14:16:33 +00:00
2020-05-18 18:59:29 +00:00
}
byte board[len][wid];
bool mines[len][wid];
char result[70];
int input;
2019-03-20 14:16:33 +00:00
int sy,sx;
Start:
sy=sx=0;
py=px=0;
2020-05-18 18:59:29 +00:00
untouched=len*wid;
flags=0;
2019-03-20 14:16:33 +00:00
curs_set(0);
memset(board,-1,len*wid);
memset(mines,false,len*wid);
mine(mines);
while(1){
erase();
mvprintw(sy+1,sx+0,"|\\/| Flags:%d\n",flags);
mvprintw(sy+2,sx+0,"| |INES Mines:%d\n",mscount);
draw(sy+3,sx+0,board);
refresh();
if(untouched<=mscount){
strcpy(result,"YAY!!");
break;
}
input = getch();
if( input==KEY_PPAGE && LINES< len+3){//the board starts in 3
sy+=10;
if(sy>0)
sy=0;
}
if( input==KEY_NPAGE && LINES< len+3){
2020-05-18 18:59:29 +00:00
sy-=10;
if(sy< -(len+3) )
sy=-(len+3);
}
2019-03-20 14:16:33 +00:00
if( input=='<' && COLS< wid*2+1){
2020-05-18 18:59:29 +00:00
sx+=10;
if(sx>0)
sx=0;
}
2019-03-20 14:16:33 +00:00
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) )
gameplay();
if( input==KEY_MOUSE )
mouseinput(sy,sx);
if( (input=='k' || input==KEY_UP) && py>0)
2020-06-20 16:26:54 +00:00
--py;
2019-03-20 14:16:33 +00:00
if( (input=='j' || input==KEY_DOWN) && py<len-1)
2020-06-20 16:26:54 +00:00
++py;
2019-03-20 14:16:33 +00:00
if( (input=='h' || input==KEY_LEFT) && px>0)
2020-06-20 16:26:54 +00:00
--px;
2019-03-20 14:16:33 +00:00
if( (input=='l' || input==KEY_RIGHT) && px<wid-1)
2020-06-20 16:26:54 +00:00
++px;
2019-03-20 14:16:33 +00:00
if( input=='q')
sigint_handler(0);
if(input=='x' && getch()=='y' && getch()=='z' && getch()=='z' && getch()=='y' ){
strcpy(result,"It is now pitch dark. If you proceed you will likely fall into a pit.");
break;
}
if((input=='\n'||input==KEY_ENTER) && board[py][px] < 9){
2019-03-20 14:16:33 +00:00
if(mines[py][px]){
2019-07-30 18:13:10 +00:00
switch( rand()%3){
2019-03-20 14:16:33 +00:00
case 0:
strcpy(result,"You lost The Game.");
break;
case 1:
strcpy(result,"You exploded!");
break;
case 2:
strcpy(result,"Bring your MRAP with you next time!");
}
break;
}
click(board,mines,py,px);
}
if(input==' '){
if(board[py][px] == -1){
board[py][px]=FLAG;
2020-06-20 16:26:54 +00:00
++flags;
2019-03-20 14:16:33 +00:00
}
else if(board[py][px] == FLAG){
board[py][px]=UNCLEAR;
2020-06-20 16:26:54 +00:00
--flags;
2019-03-20 14:16:33 +00:00
}
else if(board[py][px] == UNCLEAR)
board[py][px]=-1;
}
}
drawmines(sy+3,sx+0,board,mines);
mvprintw(sy+len+5,sx+0,"%s Wanna play again?(y/n)",result);
curs_set(1);
input=getch();
if(input != 'N' && input != 'n' && input != 'q')
goto Start;
endwin();
return EXIT_SUCCESS;
}