//lei.h
//设置雷
void set_mine(char mine[ROW + 2][COL + 2], int row, int col);
//显示界面
void display(char mine[ROW + 2][COL + 2], int row, int col);
//统计雷的个数
int get_mine_count(char board[ROW + 2][COL + 2], int x, int y);
//玩游戏
void play_game(char mine[ROW + 2][COL + 2], char board[ROW + 2][COL + 2]);
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "lei.h"
void menu()
{
printf("*************************\n");
printf("********* 1.play ********\n");
printf("********* 0.exit ********\n");
printf("*************************\n");
}
void game()
{
char mine[ROW + 2][COL + 2];
char board[ROW + 2][COL + 2];
int i;
int j;
for (i = 0; i < ROW + 2; i++)
{
for (j = 0; j < COL + 2; j++)
{
mine[i][j] = '0';
board[i][j] = '*';
}
}
set_mine(mine, ROW + 2, COL + 2);
display(board, ROW + 2, COL + 2);
play_game(mine, board);
}
int main()
{
int choose = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择>");
scanf("%d", &choose);
switch (choose)
{
case 1:
game();
break;
case 0:
printf("退出程序\n");
break;
default:
printf("输入错误,请重新输入!");
}
} while (choose);
system("pause");
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include "lei.h"
#define MINE_COUNT 10
void set_mine(char mine[ROW + 2][COL + 2], int row, int col)
{
int count = MINE_COUNT;
int x;
int y;
while (count)
{
x = rand() % 10 + 1;
y = rand() % 10 + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
void display(char board[ROW + 2][COL + 2], int row, int col)
{
int i;
int j;
printf(" ");
for (i = 1; i < row - 1; i++)
printf(" %d ", i);
printf("\n----------------------------------\n");
for (i = 1; i < row - 1; i++)
{
printf("%2d|", i);
for (j = 1; j < col - 1; j++)
{
printf(" %c ", board[i][j]);
}
printf("\n");
}
}
int get_mine_count(char mine[ROW + 2][COL + 2], int x, int y)
{
return (mine[x - 1][y - 1] - '0' + mine[x - 1][y] - '0' + mine[x - 1][y + 1] - '0'
+ mine[x][y - 1] - '0' + mine[x][y + 1] - '0' + mine[x + 1][y - 1] - '0' +
mine[x + 1][y] - '0' + mine[x + 1][y + 1] - '0');
}
void play_game(char mine[ROW + 2][COL + 2], char board[ROW + 2][COL + 2])
{
while (1)
{
printf("输入坐标(x,y):");
int x;
int y;
int count = 0;
scanf("%d%d", &x, &y);
if ((1 <= x && x <= ROW) && (1 <= y && y <= COL))
{
if (mine[x][y] == '1')
{
printf("你被炸死了\n");
display(mine, ROW + 2, COL + 2);
break;
}
else
{
int ret = get_mine_count(mine, x, y);
board[x][y] = ret + '0';
if (ret == 0)
{
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] = ' ';
}
count++;
display(board, ROW + 2, COL + 2);
if (count == MINE_COUNT)
{
printf("恭喜你,赢了\n");
display(mine, ROW + 2, COL + 2);
break;
}
}
}
else
printf("越界,输入错误\n");
}
}