扫雷在科技历史上也扮演了相似的角色。
这个基于数字的逻辑谜题最早来自20实际六七十年代,当时候Jerimac Ratliff推出的名为“Cube”的游戏已经非常受人欢迎。几十年后的1992年,扫雷游戏被加入了Windows3.1,这并不是为了展示Windows是游戏操作系统专家,而是为了训练用户的鼠标左右键操作能力,让这些动作变得非常自然,并培养鼠标移动的速度和准确性。但是现在的win10系统中已经没有了这款小游戏。所以今天就用c语言实现扫雷游戏的代码编程!
扫雷的游戏规则:
扫雷就是要把所有非地雷的格子揭开即胜利;踩到地雷格子就算失败。
游戏主区域由很多个方格组成。使用鼠标左键随机点击一个方格,方格即被打开并显示出方格中的数字;方格中数字则表示其周围的8个方格隐藏了几颗雷;如果点开的格子为空白格,即其周围有0颗雷,则其周围格子自动打开;如果其周围还有空白格,则会引发连锁反应;在你认为有雷的格子上,点击右键即可标记雷;如果一个已打开格子周围所有的雷已经正确标出,则可以在此格上同时点击鼠标左右键以打开其周围剩余的无雷格。
1代表1的上下左右及斜角合计有一颗雷,依次轮推,2则有2颗,3则有3颗......
下面附上代码:
game.h
#ifndef __GAME_H__
#define __GAME_H__
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define ROW 9
#define COL 9
#define ROWS 9+2
#define COLS 9+2
#define COUNT 20 //雷数
void init_arr(char arr[ROWS][COLS], char ch, int row, int col);
void display_arr(char arr[ROWS][COLS], int row, int col);
void mine_set(char mine[ROWS][COLS], int count);
int count_mine(char mine[ROWS][COLS], int x, int y);
void move_mine(char arr[ROWS][COLS], int row, int col);
#endif //__GAME_H__
#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void init_arr(char arr[ROWS][COLS], char ch, int row, int col)
{
memset(arr, ch, sizeof(arr[0][0])*row*col);
}
void display_arr(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf(" ");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
}
void mine_set(char mine[ROWS][COLS], int count)
{
int num = count;
int x = 0;
int y = 0;
while (num > 0)
{
x = rand() % ROW + 1;
y = rand() % COL + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
num--;
}
}
}
int count_mine(char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y - 1] + mine[x][y - 1] +
mine[x + 1][y - 1] + mine[x + 1][y] +
mine[x + 1][y + 1] + mine[x][y + 1] +
mine[x - 1][y + 1] + mine[x - 1][y] - 8 * '0';
}
void move_mine(char arr[ROWS][COLS], int x, int y)
{
int num = 1;
while (num)
{
arr[x][y] = '0';
num--;
}
}
#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("**************************************\n");
printf("******** 扫雷小游戏 ********\n");
printf("******** 1.play 0.exit ********\n");
printf("**************************************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
int x = 0;
int y = 0;
int j = 0;
int count = 0;
init_arr(mine, '0', ROWS, COLS);
init_arr(show, '*', ROWS, COLS);
mine_set(mine, COUNT);
display_arr(mine, ROW, COL);
int blank = ROW*COL - COUNT;
while (blank)
{
printf("please input coordinate:> ");
fflush(stdin);
scanf("%d%d", &x, &y);
if (((x >= 1) && (x <= 9)) && ((y >= 1) && (y <= 9)))
{
if (mine[x][y] == '1')
{
++j;
if (j == 1)
{
init_arr(mine, '0', ROWS, COLS);
init_arr(show, '*', ROWS, COLS);
mine_set(mine, COUNT + 1);
move_mine(mine, x, y);//第一个是雷移走;
display_arr(mine, ROW, COL);
}
else
{
display_arr(mine, ROW, COL);
printf("boom!\n");
break;
}
}
else
{
count = count_mine(mine, x, y);//雷的个数
show[x][y] = count + '0';
display_arr(show, ROW, COL);
blank--;
}
}
else
{
printf("error ");
}
}
if (blank == 0)
{
printf("player sucess!\n");
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请输入你的选择:> ");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("error\n");
break;
}
} while (input);
return 0;
}
上面就是扫雷的基本代码,部分细节不到位的还望大家批评改正,新手上路,大神勿喷!