扫雷 linux版
#include <stdio.h>#include <stdlib.h>
#include <time.h>
#include <termios.h>
#include <string.h>
#define SIZE 10
static struct termios stored_settings;
void set_keypress(void)
{
struct termios new_settings;
tcgetattr(0, &stored_settings);
new_settings = stored_settings;
/* Disable canonical mode, and set buffer size to 1 byte */
new_settings.c_lflag &= (~ICANON);
new_settings.c_cc = 0;
new_settings.c_cc = 1;
tcsetattr(0, TCSANOW, &new_settings);
return;
}
void reset_keypress(void)
{
tcsetattr(0, TCSANOW, &stored_settings);
return;
}
void initgame();
void drawgame();
void playgame(int* h,int* l,int* action);
void end();
int win();
int a={0};//防止溢出,1-10为游戏区,0,11为辅助区
int main()
{
initgame();
int h=1,l=1,action;
a+=30;
printf("hjkl移动,空格选择,g插旗");
set_keypress();
while(1){
system("clear");
printf("当前位置:%d,%d\n",h,l);
drawgame();
action=getchar();
if(action=='q')break;
playgame(&h,&l,&action);
if(win()==1)break;
}
reset_keypress();
}
void initgame()
{
srand(time(0));
int i,j,s;
for(s=0;s<10;)
{
i=rand()%10+1;
j=rand()%10+1;
if(a==0)
{
a=-1;//埋雷
s++;
}
}
for(i=1;i<=10;i++)
{
for(j=1;j<=10;j++)
{
int o,k;
for(o=-1;o<2;o++)
{
for(k=-1;k<2;k++)
if(a==-1&&a!=-1)//算周围几个雷
a++;
}
}
}
for(i=1;i<=10;i++)
{
for(j=1;j<=10;j++)
{
a+=20;
}
}
}
void drawgame()
{
/* 雷 -1 *
正常数字 0-8
-1 - 8
旗帜 +10 9 -18
加密 +20 19-28
用户操作 +30 29-38 39-4849-58
*/
int i,j;
for(i=1;i<=10;i++)
{
for(j=1;j<=10;j++)
{
if(a==-1){printf("*");}
else if (a<=8){printf("%-3d",a);}
else if (a<=18){printf("P");}
else if (a<=28){printf("#");}
else if (a<=38)
{
switch(a-30)
{
case -1:printf("*");end();break;
case 0:printf("⊙ ");break;
case 1:printf("① ");break;
case 2:printf("② ");break;
case 3:printf("③ ");break;
case 4:printf("④ ");break;
case 5:printf("⑤ ");break;
case 6:printf("⑥ ");break;
case 7:printf("⑦ ");break;
case 8:printf("⑧ ");break;
}
}
else if(a<=48)
{
printf("⊕ ");
}
else if(a<=58)
{
printf("⊙ ");
}
}
printf("\n");
}
}
void playgame(int* h,int* l,int* action)
{
/* 雷 -1 *
正常数字 0-8
-1 - 8
旗帜 +10 9 -18
加密 +20 19-28
用户操作 +30 29-38 39-4849-58
*/
switch(*action)
{
case 'h':
if(*l>1)
{
a[*h][*l]-=30;
a[*h][*l-1]+=30;
*l-=1;
}
break;
case 'l':
if(*l<10)
{
a[*h][*l]-=30;
a[*h][*l+1]+=30;
*l+=1;
}
break;
case 'j':
if(*h<10)
{
a[*h][*l]-=30;
a[*h+1][*l]+=30;
*h+=1;
}
break;
case 'k':
if(*h>1)
{
a[*h][*l]-=30;
a[*h-1][*l]+=30;
*h-=1;
}
break;
case ' ':
if(a[*h][*l]>=49&&a[*h][*l]<=58)
{
a[*h][*l]-=20;
}
case 'g':
if(a[*h][*l]>=49&&a[*h][*l]<=58)
{
a[*h][*l]-=10;
}
}
}
void end()
{
printf("you died");
exit(0);
}
int win()
{
int q=0;
int i,j;
for(i=0;i<12;i++)
{
for(j=0;j<12;j++)
{
if(a==9||a==39)q++;
}
}
if(q==10)
{
printf("you win");
return 1;
}
else
{
q=0;
}
return 0;
}
页:
[1]