广告联盟全自动赚钱系统惠州短视频seo
扫雷游戏的源代码比较简单,不设计比较复杂的代码,主要是多个函数的组合,每个函数执行自己的功能,最终支持游戏的完成。
1.菜单
我们需要一个提醒信息来让用户进行选择。
void menu()
{printf("***********************\n");printf("******1. play *****\n");printf("******0.exit *****\n");printf("***********************\n");
}
void test()
{int input = 0;do{menu();printf("请选择:>\n");scanf("%d", &input);switch (input){case 1:printf("扫雷\n");break;case 0:printf("退出游戏\n");break;default:printf("选择错误,重新选择\n");break;}} while (input);
}
2.游戏代码的实现
运用二维数组来制定表格,当是雷的时候,我们填补空格为*,当不是雷的时候,我们填补空格为0,再创建一个相同的表格,将我们在玩游戏过程中排查到的雷的情况显示出来。在实际创建表格的过程中,行和列均比所需要的多一行(例如:如果棋盘的大小是9*9,则数组的大小就为11*11)。这样有利于我们对于雷的排查(雷的排查需要以排查的对象为中心,知道所有周围雷的情况)。
游戏代码我们用两个文件来实现,game.c和game.h
在game.h 中主要包含的是所需要函数的声明,在game.c中是对各个函数的实现。
首先,我们需要对两个表格进行初始化。
2.1 表格初始化
函数的声明为:
//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
函数的代码实现:
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{int i = 0;for (i = 0; i < rows; i++){int j = 0;for (j = 0; j < cols; j++){board[i][j] = set;}}
}
2.2 打印棋盘
为了方便后续对于表盘中位置的确定,在打印棋盘的时候,我们将坐标也标了出来。
DisplayBoard(char board[ROWS][COLS], int row, int col)
{printf("--------扫雷--------\n");int i = 0;for (i = 0; i <= col; i++){printf("%d ", i);}printf("\n");for (i = 1; i <= row; i++){printf("%d ", i);int j = 0;for (j = 1; j <= col; j++){printf("%c ", board[i][j]);}printf("\n");}
}
2.3 布置雷
在布置雷的时候,我们要确定雷的个数,除此之外,我们还要确保雷位置的随机性。
void SetMine(char mine[ROWS][COLS], int row, int col)
{int count = EASY_COUNT;int x = 0;int y = 0;while (count){x = rand()%row+1;y = rand()%col+1;if (mine[x][y] != '1'){mine[x][y] = '1';count--;}}
}
2.4 排查雷
在排查雷时,当我们所确定的位置不是雷,那么就要计算其周围的雷的个数,这里就需要一个函数来进行实现。
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{int i = 0;int j = 0;int sum = 0;for (i = x - 1; i <= x + 1; i++){for (j = y - 1; j <= y + 1; j++){sum += (mine[i][j] - '0');}}return sum;
}
由于只有八个数相加,因此,除了这个办法之外,我们还可以直接让它们进行相加即可。
接下来,我们就进行了排查雷的工作,要注意的是,当经历每个循环时,我们要判断用户是否把每个雷都排查了出来,所以,我们还要有一个判断的步骤。
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 < col*row - EASY_COUNT){printf("请输入要排查的坐标:\n");scanf("%d %d", &x, &y);if ((x >= 1 && x <= row) && (y >= 1 && y <= col)){if (mine[x][y] == '1'){printf("很遗憾,踩雷,游戏结束!\n");DisplayBoard(mine, ROW, COL);break;}else//输入的位置不是雷{int count = GetMineCount(mine, x, y);show[x][y] = count + '0';DisplayBoard(show, ROW, COL);win++;}}else{printf("输入的坐标有误,请重新输入!\n");}}if (win == row * col - EASY_COUNT){printf("恭喜你,排雷成功!\n");DisplayBoard(mine, ROW, COL);}
}
3.源代码展示
3.1 test.c 中的代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{printf("***********************\n");printf("******1. play *****\n");printf("******0.exit *****\n");printf("***********************\n");
}void game()
{char mine[ROWS][COLS];//存放雷的信息char show[ROWS][COLS];//存放排查出雷的信息//初始化棋盘InitBoard(mine, ROWS, COLS, '0');InitBoard(show, ROWS, COLS, '*');//打印棋盘DisplayBoard(show, ROW, COL);printf("\n");//布置雷SetMine(mine, ROW, COL);//DisplayBoard(mine, ROW, COL);//排查雷FindMine(mine, show, ROW, COL);
}void test()
{srand((unsigned int)time(NULL));int input = 0;do{menu();printf("请选择:>\n");scanf("%d", &input);switch (input){case 1:printf("扫雷\n");game();break;case 0:printf("退出游戏\n");break;default:printf("选择错误,重新选择\n");break;}} while (input);
}int main()
{test();return 0;
}
3.2 game.h 中的代码
#pragma once#include<stdio.h>
#include<stdlib.h>
#include<time.h>#define ROW 9
#define COL 9#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
//打印棋盘
DisplayBoard(char board[ROWS][COLS], int row, int col);
//布置雷
void SetMine(char mine[ROWS][COLS], int row, int col);
//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
3.3 game.c 中的代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{int i = 0;for (i = 0; i < rows; i++){int j = 0;for (j = 0; j < cols; j++){board[i][j] = set;}}
}DisplayBoard(char board[ROWS][COLS], int row, int col)
{printf("--------扫雷--------\n");int i = 0;for (i = 0; i <= col; i++){printf("%d ", i);}printf("\n");for (i = 1; i <= row; i++){printf("%d ", i);int j = 0;for (j = 1; j <= col; j++){printf("%c ", board[i][j]);}printf("\n");}
}//在布置雷时,要注意随机
void SetMine(char mine[ROWS][COLS], int row, int col)
{int count = EASY_COUNT;int x = 0;int y = 0;while (count){x = rand()%row+1;y = rand()%col+1;if (mine[x][y] != '1'){mine[x][y] = '1';count--;}}
}int GetMineCount(char mine[ROWS][COLS], int x, int y)
{int i = 0;int j = 0;int sum = 0;for (i = x - 1; i <= x + 1; i++){for (j = y - 1; j <= y + 1; j++){sum += (mine[i][j] - '0');}}return sum;
}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 < col*row - EASY_COUNT){printf("请输入要排查的坐标:\n");scanf("%d %d", &x, &y);if ((x >= 1 && x <= row) && (y >= 1 && y <= col)){if (mine[x][y] == '1'){printf("很遗憾,踩雷,游戏结束!\n");DisplayBoard(mine, ROW, COL);break;}else//输入的位置不是雷{int count = GetMineCount(mine, x, y);show[x][y] = count + '0';DisplayBoard(show, ROW, COL);win++;}}else{printf("输入的坐标有误,请重新输入!\n");}}if (win == row * col - EASY_COUNT){printf("恭喜你,排雷成功!\n");DisplayBoard(mine, ROW, COL);}
}
总体来说,代码实现简单,思路也比较清晰,今天就到这里,我们下一个知识点见!