c语言写一篇扫雷游戏代码
时间: 2024-01-19 18:02:39 浏览: 78
以下是一个简单的C语言扫雷游戏代码,实现了创建棋盘、初始化棋盘、布置雷和排查雷等功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 9
#define COLS 9
#define MINE_NUM 10
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char board[ROWS][COLS], int row, int col, int num);
int main()
{
char board[ROWS][COLS];
srand((unsigned int)time(NULL)); // 用系统时间初始化随机数种子
InitBoard(board, ROWS, COLS, ' ');
DisplayBoard(board, ROWS, COLS);
SetMine(board, ROWS, COLS, MINE_NUM);
DisplayBoard(board, ROWS, COLS);
return 0;
}
// 初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
// 打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf(" ");
for (i = 0; i < col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 0; i < row; i++)
{
printf("%d ", i);
for (j = 0; j < col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
// 布置雷
void SetMine(char board[ROWS][COLS], int row, int col, int num)
{
int count = 0;
while (count < num)
{
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '*';
count++;
}
}
}
```
阅读全文