欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

扫雷游戏

程序员文章站 2024-03-16 22:30:34
...

头文件game.h
#define _CRT_SECURE_NO_WARNINGS
#ifndef GAME_H
#define GAME_H
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>

#define ROW 9
#define COL 9

#define ROWS ROW+2
#define COLS COL+2

#define EASY_COUNT 10
void Initboard(char arr[ROWS][COLS], int rows, int cols, char set);
void Display(char arr[ROWS][COLS], int row, int col);
void Setmine(char arr[ROWS][COLS], int row, int col);
void Findmine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col);
int GetmineCount(char mine[ROWS][COLS], int x, int y);
#endif //GAME_H

实现函数game.c
#include “game.h”
void Initboard(char arr[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++)
{
arr[i][j] = set;
}
}
}
void Display(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i <=col; i++)
{
printf("%d “, i);
}
printf(”\n");
for (i = 1; i <= row; i++)
{
printf("%d “, i);
for (j = 1; j <= col; j++)
{
printf(”%c “, arr[i][j]);
}
printf(”\n");
}
}
void Setmine(char arr[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (arr[x][y] == ‘0’)
{
arr[x][y] = ‘1’;
count–;
}

}

}
void Findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while ( win<rowcol-EASY_COUNT)
{
printf(“请输入坐标:〉”);
scanf("%d%d", &x, &y);
if (x >= 1 && x<=row && y>=1 && y <= col)
{
if (mine[x][y] == ‘1’)
{
printf(“很遗憾,你被炸死了!\n”);
Display(mine, row, col);
break;
}
else
{
int ret = GetmineCount(mine, x, y);
show[x][y] = ret + ‘0’;
Display(show, row, col);
win++;
}
}
else
{
printf(“输入错误,请重新输入!\n”);
}
}
if (win == row
col - EASY_COUNT)
{
printf(“恭喜你排雷成功!\n”);
}

}
int GetmineCount(char mine[ROWS][COLS], int x, int y)
{
int count = mine[x][y - 1] +
mine[x - 1][y - 1] +
mine[x - 1][y] +
mine[x][y - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] +
mine[x + 1][y] +
mine[x + 1][y - 1]-8*‘0’;
return count;
}

主函数test.c
#include “game.h”
void game()
{
//创建数组
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
//初始化mine show
Initboard(mine, ROWS, COLS,‘0’);
//Display(mine, ROW, COL);
Initboard(show, ROWS, COLS,’*’);
Display(show, ROW, COL);
//打印Display
//布置雷
Setmine(mine, ROW, COL);
//Display(mine, ROW, COL);
//找雷
Findmine(mine,show, ROW, COL);

}
void menu()
{
printf("******************************\n");
printf("****** 1.play \n");
printf("
0.exit ******\n");
printf("******************************\n");
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf(“请输入:〉”);
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf(“退出游戏\n”);
break;
default:
printf(“输入错误,请重新输入!\n”);
break;
}
} while (input);
return 0;
}

相关标签: 扫雷游戏