1. 程式人生 > >掃雷小遊戲(粗略寫的,可以應該多用函式簡單明瞭)

掃雷小遊戲(粗略寫的,可以應該多用函式簡單明瞭)

#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
void menu()
{
	printf("**********************************\n");
	printf("*************掃雷遊戲*************\n");
	printf("***********1.開始遊戲*************\n");
	printf("***********0.結束遊戲*************\n");
	printf("**********************************\n");
} 
void print(char arr[12][12],int rows,int cols)//列印
{
	int i;
	int j;
	for(i=1;i<=rows;i++)
	{
		for(j=1;j<=cols;j++)
			printf(" %c",arr[i][j]);
		printf("\n");
	}
}
void game()
{ 
	char lei[12][12];
	char xianshi[12][12];
	int a=0;
	int b=0;
	int leishu=99;//雷的個數
	int count=leishu;
	int x;
	int y;
	int sum=0;
	srand((unsigned int)time(NULL));
	memset(lei,'0',sizeof(char)*12*12);//初始化字元0
        memset(xianshi,'*',sizeof(char)*12*12);//初始化*
	print(xianshi,10,10);
	while(leishu)
	{
		a=rand()%10+1;
	        b=rand()%10+1;
		if(lei[a][b]=='0')
		{
			lei[a][b]='1';
		        leishu--;
		}
	}
	while(10*10-count)//當雷排完後跳出迴圈
       {
	  //print(lei,10,10);
            printf("請輸入座標:>");
	    scanf("%d%d",&x,&y);
	    if(lei[x][y]=='1')
	    {
		    printf("你被炸死了,遊戲結束。\n");
		  //Sleep(2000);
		    break;
	    }
	    else
	    {
		   sum=lei[x-1][y-1]+lei[x][y-1]
		      +lei[x+1][y-1]+lei[x+1][y]
		      +lei[x+1][y+1]+lei[x][y+1]
		      +lei[x-1][y+1]+lei[x-1][y]
		      -'0'-'0'-'0'-'0'-'0'-'0'-'0'-'0';//字元1與數字1相差字元0   即‘1’-‘0’=1
	       xianshi[x][y]=sum+'0';
	       count++;
	    }
	    print(xianshi,10,10);
	}
	if(count==10*10)
	    printf("恭喜你,通關。\n");
	print(lei,10,10);
}
int main()
{
	int input=0;
	do
	{
		menu();
	    printf("請選擇:");
            scanf("%d",&input);
	    switch(input)
	   { 
	        case 1:
		       system("cls");//玩下一次清屏
		       game();
		       break;
	        case 0:
		       break;
	        default:
		       printf("輸入錯誤,請重新輸入。");
		       Sleep(1000);//停頓1秒
		       system("cls");
	   }
	}while(input);
	system("pause");
	return 0;
}