扫雷——1.第一步不炸死2.坐标周围没雷,可以实现展开
通过二维数组来编写一个扫雷游戏
实现的功能:
1、布置雷
2、排雷
3、踩到雷被炸死
4、排雷成功
game.h
#ifndef __GAME_H__
#define __GAME_H__
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define COUNT 10
void Initboard(char board[ROWS][COLS], int row, int col, char ret);
void Displayboard(char board[ROWS][COLS], int row, int col);
void Bombboard(char board[ROWS][COLS], int row, int col);
int Countbomb(char show[ROWS][COLS], int x, int y);
char first_step(char board[ROWS][COLS], int x, int y, int row, int col);
#endif
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void Initboard(char board[ROWS][COLS], int row, int col, char ret)//初始化数组
{
int i = 0, j = 0;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
board[i][j] = ret;
}
}
}
void Displayboard(char board[ROWS][COLS], int row, int col)//打印雷盘界面
{
int i = 0, j = 0;
for (i = 0; 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 ", board[i][j]);
}
printf("\n");
}
}
void Bombboard(char board[ROWS][COLS], int row, int col)//设置雷
{
int count = COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
void first_step(char board[ROWS][COLS], int x, int y, int row, int col)//第一步不睬雷
{
if (board[x][y] == '1')
{
board[x][y] = '0';
int count = 1;
while (count)
{
int x1 = rand() % row + 1;
int y1 = rand() % col + 1;
if (board[x][y] == '0'&&x1 != x&&y1 != y)
{
board[x1][y1] = '1';
count--;
}
}
}
return board[x][y];
}
int Countbomb(char board[ROWS][COLS], int x, int y)//统计周围雷数
{
return board[x][y] = board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1] +
board[x][y - 1] + board[x][y + 1] +
board[x + 1][y - 1] + board[x + 1][y] + board[x + 1][y + 1] - 8 * '0';
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void game()
{
int x = 0, y = 0,i=0;
char board[ROWS][COLS] = {'0'};
char show[ROWS][COLS] = { '0' };
Initboard(board, ROW, COL,'0');
Initboard(show, ROW, COL,'*');
Bombboard(board, ROW, COL);
//Displayboard(board, ROW, COL);//显示雷的分布点
Displayboard(show, ROW, COL);
while (i<ROW*COL - COUNT)
{
int ret = 0;
printf("请选择:\n");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= ROW&&y >= 1 && y <= COL)
{
if (board[x][y] == '1'&&i == 0)
{
board[x][y]=first_step(board, x, y, ROW, COL);
Displayboard(board, ROW, COL);
ret = Countbomb(board, x, y);
show[x][y] = ret + '0';
Displayboard(show, ROW, COL);
i++;
}
else
{
if (board[x][y] == '0')
{
ret = Countbomb(board, x, y);
show[x][y] = ret + '0';
if (ret == 0)
{
show[x - 1][y - 1] = ' ';
show[x - 1][y] = ' ';
show[x - 1][y + 1] = ' ';
show[x][y - 1] = ' ';
show[x][y + 1] = ' ';
show[x + 1][y - 1] = ' ';
show[x + 1][y] = ' ';
show[x + 1][y + 1] = ' ';
}
Displayboard(show, ROW, COL);
i=i+8;
}
else
{
printf("很遗憾,你被炸死了\n");
break;
}
}
}
else
{
printf("输入坐标有误\n");
}
}
if (i==ROW*COL - COUNT)
printf("恭喜你,排雷成功!\n");
}
void menu()
{
printf("*********************\n");
printf("***1.开始 0.退出***\n");
printf("*********************\n");
}
int main()
{
menu();
int input = 0;
srand((unsigned)time(NULL));
do
{
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
}
} while (1);
system("pause");
return 0;
}