前言
掃雷游戲是一款大眾類的益智小游戲,於1992年發行。游戲目標是在最短的時間內根據點擊格子出現的數字找出所有非雷格子,同時避免踩雷。我們可以通過C語言編程來實現游戲。我們可以把文件代碼分別分在三個不同的文件下,game.h:頭文件,game.c:用來具體完成函數代碼,test.c:主要寫邏輯函數。
寫掃雷程序剛開始很多人可能沒有思緒,我們可以先思考輸入一個坐標而屏幕顯示是否有雷,或者顯示這個點的附近有幾個雷,當然如果你還沒學到這里。建議可以先去小編的C++交流.裙 :九起久傘吧起傘留傘(數字的諧音)轉換下可以找到了,里面有最新C++教程項目,多跟里面的人交流,進步更快哦
仔細思考我們可以想到可以用兩個數組完成,一個數組存放當,一個數組用來顯示。當玩家輸入坐標后,需判斷這個坐標是有有雷然后把它信息存放在另一個數組顯示出來。
-
int get_num(int x,int y)//雷的坐標
-
{
-
return rand() % (y - x + 1) + x;//x-y的隨機數
-
}
-
void set_mine(char mine[ROWS][COLS])//初始化雷
-
{
-
int x = 0;
-
int y = 0;
-
int i = LEI;
-
srand((unsigned)time(NULL));
-
-
while (i)
-
{
-
x = get_num(1, 9);
-
y = get_num(1, 9);
-
-
if (mine[x][y] == '0')
-
{
-
mine[x][y] = '1';
-
i--;
-
}
-
}
-
}
-
void menu()
-
{
-
printf("**********************************\n");
-
printf("******** 1.PLAY *************\n");
-
printf("******** 0.EXIT *************\n");
-
printf("**********************************\n");
-
}
-
void init_game(char mine[ROWS][COLS], char show[ROWS][COLS])//數組初始化
-
{
-
memset(mine, '0', ROWS*COLS*sizeof(char));
-
set_mine(mine);//初始化雷的位置
-
memset(show, '*', ROWS*COLS*sizeof(char));
-
-
}

-
void display(char show[ROWS][COLS])//打印數組
-
{
-
int i = 0;
-
int j = 0;
-
// for (i = 0; i < ROWS; i++)
-
// {
-
//
-
// for (j = 0; j < COLS; j++)
-
// {
-
// printf(" %c ", mine[i][j]);
-
//
-
// }
-
// printf("\n");
-
// }
-
printf(" ");
-
for (i = 1; i < COLS - 1; i++)
-
{
-
printf(" %d ", i);
-
}
-
printf("\n");
-
for (i = 1; i < ROWS-1; i++)
-
{
-
printf("%d ", i);
-
-
for (j = 1; j < COLS - 1; j++)
-
{
-
-
printf(" %c ", show[i][j]);
-
}
-
printf("\n");
-
}
-
-
}
-
void sweep(char mine[ROWS][COLS], char show[ROWS][COLS])//開始掃雷
-
{
-
int x = 0;
-
int y = 0;
-
int i = (ROWS-2)*(COLS-2)-LEI;
-
-
while (i)
-
{
-
printf("請輸入坐標:");
-
scanf("%d%d", &x, &y);
-
int ret = get_lei_nums(mine, x, y);
-
if ((x > 0 && x <ROWS - 1) && (y > 0 && y<COLS-1))
-
{
-
if (mine[x][y] == '1')
-
{
-
printf("您炸了,游戲結束\n");
-
return;
-
}
-
else
-
{
-
show[x][y]=ret+'0';
-
display(show);
-
i--;
-
}
-
}
-
else
-
{
-
printf("您輸入的坐標不合法");
-
}
-
}
-
printf("Win!!!!!!!!!!!\n");
-
}
-
int get_lei_nums(char mine[ROWS][COLS],int x,int y)//判斷附近雷的個數
-
{
-
-
int count = 0;
-
if (mine[x - 1][y - 1] == '1')
-
count++;
-
if (mine[x][y - 1] == '1')
-
count++;
-
if (mine[x + 1][y - 1] == '1')
-
count++;
-
if (mine[x - 1][y] == '1')
-
count++;
-
if (mine[x + 1][y] == '1')
-
count++;
-
if (mine[x - 1][y + 1] == '1')
-
count++;
-
if (mine[x][y + 1] == '1')
-
count++;
-
if (mine[x + 1][y + 1] == '1')
-
count++;
-
-
return count;
-
}
game.h
-
#define _CRT_SECURE_NO_WARNINGS 1
-
#ifndef __GAME__H__
-
#define __GAME__H__
-
#include <stdio.h>
-
#include <string.h>
-
#include <stdlib.h>
-
#include <time.h>
-
enum CH
-
{
-
EXIT,
-
PLAY
-
};
-
#define ROWS 11
-
#define COLS 11
-
#define LEI 10
-
-
-
void set_mine(char mine[ROWS][COLS]);
-
void init_game(char mine[ROWS][COLS], char show[ROWS][COLS]);
-
void display(char show[ROWS][COLS]);
-
void play_game(char mine[ROWS][COLS], char show[ROWS][COLS]);
-
void sweep(char mine[ROWS][COLS], char show[ROWS][COLS]);
-
int get_lei_nums(char mine[ROWS][COLS], int x,int y);
-
-
#endif
game.c
-
#define _CRT_SECURE_NO_WARNINGS 1
-
#include "game.h"
-
-
int get_num(int x,int y)//雷的坐標
-
{
-
return rand() % (y - x + 1) + x;//x-y的隨機數
-
}
-
void set_mine(char mine[ROWS][COLS])//初始化雷
-
{
-
int x = 0;
-
int y = 0;
-
int i = LEI;
-
srand((unsigned)time(NULL));
-
-
while (i)
-
{
-
x = get_num(1, 9);
-
y = get_num(1, 9);
-
-
if (mine[x][y] == '0')
-
{
-
mine[x][y] = '1';
-
i--;
-
}
-
}
-
}
-
void init_game(char mine[ROWS][COLS], char show[ROWS][COLS])//數組初始化
-
{
-
memset(mine, '0', ROWS*COLS*sizeof(char));
-
set_mine(mine);//初始化雷的位置
-
memset(show, '*', ROWS*COLS*sizeof(char));
-
-
}
-
-
void display(char show[ROWS][COLS])
-
{
-
int i = 0;
-
int j = 0;
-
// for (i = 0; i < ROWS; i++)
-
// {
-
//
-
// for (j = 0; j < COLS; j++)
-
// {
-
// printf(" %c ", mine[i][j]);
-
//
-
// }
-
// printf("\n");
-
// }
-
printf(" ");
-
for (i = 1; i < COLS - 1; i++)
-
{
-
printf(" %d ", i);
-
}
-
printf("\n");
-
for (i = 1; i < ROWS-1; i++)
-
{
-
printf("%d ", i);
-
-
for (j = 1; j < COLS - 1; j++)
-
{
-
-
printf(" %c ", show[i][j]);
-
}
-
printf("\n");
-
}
-
-
}
-
void sweep(char mine[ROWS][COLS], char show[ROWS][COLS])//開始掃雷
-
{
-
int x = 0;
-
int y = 0;
-
int i = (ROWS-2)*(COLS-2)-LEI;
-
-
while (i)
-
{
-
printf("請輸入坐標:");
-
scanf("%d%d", &x, &y);
-
int ret = get_lei_nums(mine, x, y);
-
if ((x > 0 && x<ROWS - 1) && (y > 0 && y<COLS-1))
-
{
-
if (mine[x][y] == '1')
-
{
-
printf("您炸了,游戲結束\n");
-
return;
-
}
-
else
-
{
-
show[x][y]=ret+'0';
-
display(show);
-
i--;
-
}
-
}
-
else
-
{
-
printf("您輸入的坐標不合法");
-
}
-
}
-
printf("Win!!!!!!!!!!!\n");
-
}
-
int get_lei_nums(char mine[ROWS][COLS],int x,int y)//判斷附近雷的個數
-
{
-
-
int count = 0;
-
if (mine[x - 1][y - 1] == '1')
-
count++;
-
if (mine[x][y - 1] == '1')
-
count++;
-
if (mine[x + 1][y - 1] == '1')
-
count++;
-
if (mine[x - 1][y] == '1')
-
count++;
-
if (mine[x + 1][y] == '1')
-
count++;
-
if (mine[x - 1][y + 1] == '1')
-
count++;
-
if (mine[x][y + 1] == '1')
-
count++;
-
if (mine[x + 1][y + 1] == '1')
-
count++;
-
-
return count;
-
}
test.c
-
#define _CRT_SECURE_NO_WARNINGS 1
-
#include"game.h"
-
-
void play_game()
-
{
-
char mine[ROWS][COLS] = { 0 };
-
char show[ROWS][COLS] = { 0 };
-
init_game(mine, show);
-
display(show);
-
sweep(mine, show);
-
-
}
-
-
void menu()
-
{
-
printf("**********************************\n");
-
printf("******** 1.PLAY *************\n");
-
printf("******** 0.EXIT *************\n");
-
printf("**********************************\n");
-
}
-
-
void game()
-
{
-
-
-
int input = 0;
-
menu();
-
printf("請選擇:");
-
scanf("%d", &input);
-
-
while (input)
-
{
-
switch (input)
-
{
-
case PLAY:
-
play_game();
-
break;
-
case EXIT:
-
break;
-
}
-
menu();
-
printf("請選擇:");
-
scanf("%d", &input);
-
}
-
}
-
-
int main()
-
{
-
game();
-
system("pause");
-
return 0;
-
}
1.在選擇是否游戲時引用了枚舉類型,方便以后對程序擴展
-
enum CH
-
{
-
EXIT,
-
PLAY
-
};
srand((unsigned)time(NULL));
rand() % (y - x + 1) + x;//生成x-y的隨機數。
3.再把數組初始化時用到了memset()函數:頭文件是<string.h>
memset(mine, '0', ROWS*COLS*sizeof(char));
數組 要賦值的值 數組的大小