帮我用c写一个扫雷游戏
时间: 2023-03-29 11:00:32 浏览: 80
好的,我可以为您提供一个基本的扫雷游戏代码,如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 9
#define COL 9
#define MINE_NUM 10
void init_board(char board[ROW][COL], int row, int col, char set);
void display_board(char board[ROW][COL], int row, int col);
void set_mine(char board[ROW][COL], int row, int col, int num);
int get_mine_count(char board[ROW][COL], int x, int y);
int main()
{
char board[ROW][COL];
int x, y, count = ;
srand((unsigned int)time(NULL));
init_board(board, ROW, COL, ' ');
set_mine(board, ROW, COL, MINE_NUM);
display_board(board, ROW, COL);
while (1)
{
printf("请输入坐标(x,y):");
scanf("%d,%d", &x, &y);
if (board[x][y] == '*')
{
printf("很遗憾,你踩到了地雷!\n");
break;
}
else
{
count = get_mine_count(board, x, y);
board[x][y] = count + '';
display_board(board, ROW, COL);
}
}
return ;
}
void init_board(char board[ROW][COL], int row, int col, char set)
{
int i, j;
for (i = ; i < row; i++)
{
for (j = ; j < col; j++)
{
board[i][j] = set;
}
}
}
void display_board(char board[ROW][COL], int row, int col)
{
int i, j;
printf(" ");
for (i = ; i < col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = ; i < row; i++)
{
printf("%d ", i);
for (j = ; j < col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void set_mine(char board[ROW][COL], int row, int col, int num)
{
int i, x, y;
for (i = ; i < num; i++)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == '*')
{
i--;
}
else
{
board[x][y] = '*';
}
}
}
int get_mine_count(char board[ROW][COL], int x, int y)
{
int count = , i, j;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (i >= && i < ROW && j >= && j < COL && board[i][j] == '*')
{
count++;
}
}
}
return count;
}
阅读全文