头文件
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define numlei 10
do while可以循环玩
两个板子,内板子放0,外板子放*
set函数初始化两个板子
lei函数,x,y随机两数,把内板子某些0改为1
print函数打印内或外板子
board返回周围8个格子雷的个数
#include "game.h"
void set(char arr[ROWS][COLS], int row, int col,char set)
{
for (int i = 0;i < ROWS;i++)
{
for (int j = 0;j < COLS;j++)
{
arr[i][j] = set;
}
}
}
void lei(char arr[ROWS][COLS], int row, int col)
{
int count = numlei;
while (count)
{
int x = rand() % 9 + 1;
int y = rand() % 9 + 1;
if (arr[x][y]=='0')
{
arr[x][y] = '1';
count--;
}
}
}
void print(char arr[ROWS][COLS], int row, int col)
{
for (int i=0;i<=row;i++)
{
printf("%d ",i);
}
printf("\n");
for (int i = 1;i <= row;i++)
{
printf("%d ", i);
for (int j = 1;j <= col;j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
}
int board(char mine[ROWS][COLS], int x, int y)
{
return (mine[x - 1][y] + 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]-8*'0');
}
void game()
{
printf("********************\n");
printf("**** 1.游戏开始 ****\n");
printf("**** 0.退出游戏 ****\n");
printf("********************\n");
int input;
char show[ROWS][COLS];
char inside[ROWS][COLS];
do
{
scanf("%d", &input);
switch (input)
{
case 1:
printf("游戏开始\n");
set(show, ROW, COL, '*');
print(show, ROW, COL);
set(inside, ROW, COL, '0');
lei(inside, ROW, COL);
int win = 0;
while (win<ROW*COL-numlei)
{
int x, y;
scanf("%d %d", &x, &y);
if (x >= 1 && y >= 1 && x <= ROW && y <= COL)
{
if (inside[x][y] == '1')
{
printf("你被炸了\n");
print(inside, ROW, COL);
break;
}
else
{
int c = board(inside, x, y);
show[x][y] = c + '0';
print(show, ROW, COL);
win++;
}
}
else
printf("输入坐标错误,请重新输入\n");
}
if (win == ROW * COL - numlei)
{
printf("排雷成功\n");
print(inside, ROW, COL);
}
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("重新输入\n");
break;
}
} while (input);
}
int main()
{
srand((unsigned) time(NULL));
game();
return 0;
}
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » C语言最简单的扫雷实现(解析加原码)
发表评论 取消回复