簡單的掃雷小遊戲
阿新 • • 發佈:2019-02-09
掃雷小遊戲
只是簡單的實現了掃雷的過程,而且很多地方還可以優化,比如輸入一個位置如果周圍都沒有雷,就讓它展開,又比如第一次輸入的位置一定不是雷。
源程式:
sl.h
#ifndef __SL_H__#define __SL_H__ #define ROWS 12
#define COLS 12
void InitBoard(char arr[ROWS][COLS], int rows,int cols, char paint);
void PrintBoard(char arr[ROWS][COLS], int rows,int cols);
void Bomb(char arr[ROWS][COLS], int rows,int cols);
int Pmove(char arr1[ROWS][COLS],char arr2[ROWS][COLS],int rows, int cols);
int Win(char arr[ROWS][COLS],int rows,int cols); #endif
game.c
#include <time.h>
#include <stdlib.h>
#include "sl.h" void InitBoard(char arr[ROWS][COLS], int rows,int cols, char paint)
{
int i = 0;
int j = 0;
for(i = 0; i < rows; i++)
{
for(j =0; j<cols; j++)
{
if(i == 11)
{
arr[i][j] = ' ';
}else if(j== 11)
{
arr[i][j] = ' ';
}
else if (j == 0)
{
arr[i][j] = '0'+i;
}else if(i==0)
{
arr[i][j] = '0'+j;
}
else
{
arr[i][j] = paint;
}
}
}
} void PrintBoard(char arr[ROWS][COLS], int rows ,int cols)
{
int i = 0;
int j = 0;
for(i = 0; i < rows; i++)
{
for(j =0; j<cols; j++)
{
printf(" %c",arr[i][j]);
}
printf("\n");
}
} void Bomb(char arr[ROWS][COLS], int rows,int cols)
{
int i = 0;
int x = 0;
int y =0; while(i<10)
{
x= rand()%10+1;
y = rand()%10+1;
if(arr[x][y] == ' ')
{
arr[x][y] = 'X';
i++;
}
}
} int BombSum(char arr[ROWS][COLS], int x, int y)
{
char sum = '0';
if(arr[x-1][y-1] == 'X')sum++;
if(arr[x-1][y] == 'X')sum++;
if(arr[x-1][y+1] == 'X')sum++;
if(arr[x][y-1] == 'X')sum++;
if(arr[x][y+1] == 'X')sum++;
if(arr[x+1][y-1] == 'X')sum++;
if(arr[x+1][y] == 'X')sum++;
if(arr[x+1][y+1] == 'X')sum++;
return sum;
} int Pmove(char arr1[ROWS][COLS],char arr2[ROWS][COLS],int rows, int cols)
{
int i = 0;
int j = 0;
int x= 0;
int y = 0;
char count = '0';
printf("請輸入座標<x y>\n");
scanf("%d %d",&x,&y);
if(arr1[x][y] != 'X')
{
count =BombSum(arr1,x,y);
if(count == 0)
{
arr2[x][y] = arr1[x][y];
}
else
{
arr2[x][y] = count;
}
}
else
{
return 1;
}
return 0;
} int Win(char arr[ROWS][COLS],int rows,int cols)
{
int i = 0;
int j = 0;
int count = 0;
for(i = 1;i < rows; i++)
{
for(j = 1; j < cols; j++)
{
if((arr[i][j] != '*')&&(arr[i][j]!='X'))
{
count ++;
}
}
}
if(count == 90)
{
return 0;
}
else
{
return 1;
}
} tese.c #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "sl.h"
void menu()
{
printf("******************************\n");
printf("********* 掃雷 ***********\n");
printf("******************************\n");
printf("****** 1.game 0.exit *******\n");
printf("******************************\n");
printf("******************************\n");
}
void game()
{
char board1[ROWS][COLS]; //放雷
char board2[ROWS][COLS]; //地圖
int death = 0; InitBoard(board1,ROWS,COLS,' ');
InitBoard(board2,ROWS,COLS, '*');
PrintBoard(board2,ROWS,COLS);
Bomb(board1,ROWS,COLS ); do{
death = Pmove(board1,board2,ROWS,COLS);
if(death)
{
printf("很遺憾,你輸了!\n");
PrintBoard(board1,ROWS,COLS);
break;
}
PrintBoard(board2,ROWS,COLS);
}while(Win(board2,ROWS,COLS)); }
int main()
{
int input = 0;
srand((unsigned)time(NULL));
do
{
menu();
printf("請選擇->\n");
scanf("%d",&input);
switch(input)
{
case 1:
game();break;
case 0:
printf("遊戲結束!");
break;
default:
printf("輸入錯誤,重新輸入!");
break;
}
}while(input);
return 0;
} 執行結果: