實現用C語言編寫一個掃雷游戲
大家想必都玩過掃雷,無論那個版本都有難度供已選擇,下面來分享一個自己用C語言編寫的掃雷游戲吧!
編寫語言:C語言
編寫軟件:visual studio 2017
1.首先是將游戲的測試模塊寫好,要有提示玩家進入的菜單函數以及選擇函數等各種需要編寫的函數想出一個整體框架來
//測試模塊test。c
#include "game.h"
void Menu()
{
printf("***********************************************************************\n");
printf("************************1.PLAY 0.EXIT*************************\n");
printf("***********************************************************************\n");
}
int Game()
{
int x = 0;
int y = 0;
int i = 0;
int j = 0;
int ret = 0;
int num = count;
char real[ROWS][COLS] = {'0'};
char show[ROWS][COLS] = {'*'};
srand((unsigned)time(NULL));
Init(real,ROWS, COLS, '0');
Init(show, ROWS, COLS, '*');
SetMine(real, ROWS, COLS, count);
Display(show, ROW, COL);
printf("Pelase Input x and y :\n");
scanf_s("%d%d", &x, &y);
if (real[x][y] == '1')//保證第一次不踩到雷
{
do
{
SetMine(real, ROWS, COLS, count);
} while (real[x][y] == '1');
}
show[x][y] = SearchMind(real, &x, &y, ROWS, COLS) + '0';
for (i = 0; i <ROWS; i++)//管理員監視器模塊
{
//printf("%3d", i);
for (j = 0; j < COLS; j++)
{
printf("%3c", real[i][j]);
}
printf("\n");
}
printf("\n");
Display(show, ROW, COL);
while ((ROW*COL) > (ROW*COL - num))
{
printf("Pelase Input x and y :\n");
scanf_s("%d%d", &x, &y);
ret = SearchMind(real, &x, &y, ROWS, COLS) + 1;
if (ret!=0)//不是踩到雷的情況進入
{
show[x][y] = SearchMind(real, &x, &y, ROWS, COLS) + '0';//轉化成字符型
Display(show, ROW, COL);
}
if (ret == 0)//踩到雷的情況進入
{
return 0;
}
num--;
}
return 1;
}
int main()
{
Menu();
while (1)
{
int m = 0;
scanf_s("%d", &m);
switch (m)
{
case 1:
{
if (Game())
{
printf("you win\n");
}
else
{
printf("you failed\n");
Menu();
}
break;
}
case 2:
return 0;
break;
default:
printf("請重新輸入\n");
break;
}
}
}
測試函數中的頭文件 #include "game.h",而不是#include <stdio.h>...的原因是因為我們在后續的頭文件game.h中已經包含我們所需要聲明的頭文件
接下來是頭文件game.h
#ifndef __GAME_H__
#define __GAME_H__
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define ROWS 11
#define COLS 11
#define ROW 9
#define COL 9
#define count 10
void Init(char arr[ROWS][COLS],int rows, int cols, const char ch);
void SetMine(char str[ROWS][COLS], int rows, int cols, int cot);
void Display(char str[ROWS][COLS], int rows, int cols);
int SearchMind(char str[ROWS][COLS], int* x, int* y, int rows, int cols);
#endif
接下來是最重要的部分也是該游戲的核心部分游戲函數文件game.c
#include "game.h"
void Init(char arr[ROWS][COLS], int rows, int cols, const char ch)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
arr[i][j] = ch;
}
}
}
//隨機埋雷 SetMine()
void SetMine(char str[ROWS][COLS], int rows, int cols, int cot)
{
int x = 0;
int y = 0;
while (cot > 0)
{
x = rand() % 9 + 1;
y = rand() % 9 + 1;
if (str[x][y]=='0')
{
str[x][y] = '1';
cot--;
}
}
}
//顯示棋盤 Display()
void Display(char str[ROWS][COLS], int rows, int cols)
{
int i = 0;
int j = 0;
for (i = -1; i < rows; i++)
{
printf("%3d", i+1);
}
printf("\n");
for (i = 1; i <=rows; i++)
{
printf("%3d", i);
for (j = 1; j <= cols; j++)
{
printf("%3c", str[i][j]);
}
printf("\n");
}
}
//輸入坐標,判斷周圍的雷數,並回饋數字,為0時遞歸SearchMine()
int SearchMind(char str[ROWS][COLS], int* x, int* y, int rows,int cols)
{
int number = 0;
//判斷是否踩到雷
if (str[*x][*y] == '1')
{
return -1;
}
number = str[*x - 1][*y - 1] + str[*x - 1][*y] + str[*x - 1][*y + 1]
+ str[*x][*y - 1] + str[*x][*y + 1]
+ str[*x + 1][*y - 1] + str[*x + 1][*y] + str[*x + 1][*y + 1] - 8 * '0';
//if (number == 0)
//{
// //改變x,y的值實現爆炸效果
// number = SearchMind(str, &x, &y, rows, cols);
//}//遞歸
return number;
}
//判斷輸贏 Distinguish()