通过C语言简单来实现一个小游戏-扫雷片段
接下来是将代码分成三块;
分别是test.c 和game.c的源文件以及game.h的头文件
首先是test.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void menu()
{
printf("************************************\n");
printf("******* 1.play **********\n");
printf("******* 0.exit **********\n");
printf("************************************\n");
}
void game()
{
char mine[ROWS][COLS] = { '0' };
char show[ROWS][COLS] = { '*' };
//棋盘-写一个 函数
initboard(mine, ROWS, COLS,'0');
initboard(show, ROWS, COLS,'*');
//布置雷
displayboard(show, ROW, COL);
setmine(mine, ROW, COL);
//displayboard(mine, ROW, COL);
// 扫雷
findmine(mine, show,ROW, COL);
// displayboard(show,ROW, COL);
}
void test()
{
int i = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:>");
scanf_s("%d", &i);
switch (i)
{
case 1:
{
printf("进入扫雷游戏\n");
game();
break;
}
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
}
} while (i);
}
int main()
{
test();
return 0;
}
接下来是game.h
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<string.h>
#include <time.h>
#include<stdlib.h>
#define mine_count 10
#define ROW 9
#define COL 9
#define COLS COL+2
#define ROWS ROW+2
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 );
void findmine(char board[ROWS][COLS],char show[ROWS][COLS],int row,int col);
最后是game.c
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
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;
//printf("%c ", board[i][j]);
}//printf("\n");
}
}
void displayboard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i <= row; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}printf("\n");
}
}
void setmine(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int count=mine_count;
while (count)
{
int x = rand() % 9 + 1;
int y = rand() % 9 + 1;
if (board[x][y] = '0')
{
board[x][y] = '1';
count--;
}
}
}
int judge(char board[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
return board[x][y - 1] + board[x - 1][y - 1] + board[x][y + 1]
+ board[x + 1][y + 1] + board[x + 1][y - 1] + board[x + 1][y]
+ board[x - 1][y + 1]+board[x-1][y] -8 *'0';
}
void findmine(char board[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win<=row*col-mine_count)
{
printf("请输入坐标,进行扫雷:");
scanf_s("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x][y] == '1')
{
printf("很遗憾,扫雷失败,踩到雷了\n");
displayboard(board, row, col);
break;
}
else
{
int ret=judge(board, show, x, y);
printf("ret=%d\n", ret);
show[x][y] = ret+'0';
displayboard(show, row, col);
win++;
}
}
else
{
printf("输入错误,请重新输入\n");
}
if (win == row*col - mine_count)
{
printf("恭喜你,扫雷成功!");
}
}
}
用简单的两百行不到的代码来实现一个小游戏,其中也包含了许多源文件与头文件之间的引用等等。
以上就是扫雷片段的实现。
?
|