用c语言写扫雷游戏倒计时代码,课内资源 - 基于VS2013实现的扫雷小游戏
时间: 2023-06-24 21:06:17 浏览: 98
以下是一个基于 C 语言的扫雷游戏倒计时代码,可以在 VS2013 中运行。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#define ROW 10
#define COL 10
#define MINE_NUM 10
void drawMap(char map[ROW][COL], int row, int col);
void initMap(char map[ROW][COL], int row, int col, int mineNum);
int getMineCount(char map[ROW][COL], int row, int col, int x, int y);
void expand(char map[ROW][COL], int row, int col, int x, int y);
int checkWin(char map[ROW][COL], int row, int col, int mineNum);
int main()
{
char map[ROW][COL];
int row = ROW;
int col = COL;
int mineNum = MINE_NUM;
initMap(map, row, col, mineNum);
int start = time(NULL);
int elapsed = 0;
while (1) {
system("cls");
drawMap(map, row, col);
if (checkWin(map, row, col, mineNum)) {
printf("\nYou win!\n");
break;
}
if (elapsed >= 60) {
printf("\nTime is up! You lose!\n");
break;
}
printf("\nElapsed time: %d seconds\n", elapsed);
printf("Remaining mines: %d\n", mineNum);
int x, y;
printf("Enter the coordinates (row, col): ");
scanf_s("%d %d", &x, &y);
if (x < 0 || x >= row || y < 0 || y >= col) {
printf("Invalid coordinates!\n");
continue;
}
if (map[x][y] == '*') {
printf("You hit a mine! You lose!\n");
break;
}
if (map[x][y] >= '0' && map[x][y] <= '9') {
printf("The cell has been revealed!\n");
continue;
}
if (getMineCount(map, ROW, COL, x, y) == 0) {
expand(map, ROW, COL, x, y);
}
else {
map[x][y] = getMineCount(map, ROW, COL, x, y) + '0';
}
elapsed = time(NULL) - start;
}
drawMap(map, ROW, COL);
printf("\nPress any key to exit...");
_getch();
return 0;
}
void drawMap(char map[ROW][COL], int row, int col) {
printf(" ");
for (int j = 0; j < col; j++) {
printf("%d ", j);
}
printf("\n");
for (int i = 0; i < row; i++) {
printf("%d ", i);
for (int j = 0; j < col; j++) {
printf("%c ", map[i][j]);
}
printf("\n");
}
}
void initMap(char map[ROW][COL], int row, int col, int mineNum) {
srand(time(NULL));
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
map[i][j] = '-';
}
}
int count = 0;
while (count < mineNum) {
int x = rand() % row;
int y = rand() % col;
if (map[x][y] != '*') {
map[x][y] = '*';
count++;
}
}
}
int getMineCount(char map[ROW][COL], int row, int col, int x, int y) {
int count = 0;
if (x > 0 && map[x - 1][y] == '*') {
count++;
}
if (x < row - 1 && map[x + 1][y] == '*') {
count++;
}
if (y > 0 && map[x][y - 1] == '*') {
count++;
}
if (y < col - 1 && map[x][y + 1] == '*') {
count++;
}
if (x > 0 && y > 0 && map[x - 1][y - 1] == '*') {
count++;
}
if (x > 0 && y < col - 1 && map[x - 1][y + 1] == '*') {
count++;
}
if (x < row - 1 && y > 0 && map[x + 1][y - 1] == '*') {
count++;
}
if (x < row - 1 && y < col - 1 && map[x + 1][y + 1] == '*') {
count++;
}
return count;
}
void expand(char map[ROW][COL], int row, int col, int x, int y) {
if (x < 0 || x >= row || y < 0 || y >= col) {
return;
}
if (map[x][y] != '-') {
return;
}
int count = getMineCount(map, ROW, COL, x, y);
if (count > 0) {
map[x][y] = count + '0';
return;
}
map[x][y] = ' ';
expand(map, ROW, COL, x - 1, y);
expand(map, ROW, COL, x + 1, y);
expand(map, ROW, COL, x, y - 1);
expand(map, ROW, COL, x, y + 1);
}
int checkWin(char map[ROW][COL], int row, int col, int mineNum) {
int count = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (map[i][j] == '-') {
count++;
}
}
}
if (count == mineNum) {
return 1;
}
return 0;
}
```
这个代码实现了一个简单的扫雷游戏,并且有倒计时功能。在游戏开始时,程序会记录当前时间,每次循环计算已经过去的时间,并在屏幕上显示出来。当时间超过 60 秒时,游戏结束,玩家失败。
阅读全文