当前位置:网站首页>C language minesweeping
C language minesweeping
2022-07-23 08:07:00 【fentiaoOvO】
Real time update time
File archiving
Record the game time to rank
#include "game.h"
// Data operation
void CheckCapacity(Num* pc)
{
if (pc->size == pc->capacity)
{
Rank* tmp = (Rank*)realloc(pc->data, (pc->capacity * 2) * sizeof(Rank));
if (tmp != NULL)
{
pc->data = tmp;
}
else
{
perror("CheckCapacity::realloc");
exit(-1);
}
pc->capacity *= 2;
printf(" Successful expansion \n");
}
}
void AddRank(Num* pc, double time)
{
assert(pc);
CheckCapacity(pc);
char name[20];
printf(" Please enter a name \n");
scanf("%s", pc->data[pc->size].name);
pc->data[pc->size].time = time;
pc->size++;
printf(" Successful listing \n");
}
// Read the data of the file
void LoadRank(Num* pc)
{
// Open file
FILE* pf = fopen("rank.dat", "rb");
if (pf == NULL)
{
perror("LoadRank::fopen");
return 1;
}
// Reading documents
Rank tmp = { 0 };
while (fread(&tmp, sizeof(Rank), 1, pf))
{
CheckCapacity(pc);
pc->data[pc->size] = tmp;
pc->size++;
}
// Close file
fclose(pf);
pf = NULL;
}
// File initialization
void InitRank(Num* pc)
{
assert(pc);
pc->capacity = 3;
pc->size = 0;
pc->data = (Rank*)malloc(pc->capacity * sizeof(Rank));
if (pc->data == NULL)
{
perror("InitRank;;malloc");
return;
}
memset(pc->data, 0, sizeof(Rank) * pc->capacity);
// Load information
LoadRank(pc);
}
// Save data to file
void SaveRank(const Num* pc)
{
FILE* pf = fopen("rank.dat", "wb");
if (pf == NULL)
{
perror("SaveRank::open");
return 1;
}
int i = 0;
for (i = 0; i < pc->size; i++)
{
fwrite(pc->data + i, sizeof(Rank), 1, pf);
}
// Close file
fclose(pf);
pf = NULL;
}
// Destruction of documents , Free memory ;
void RankDestory(Num* pc)
{
assert(pc);
free(pc->data);
pc->data = NULL;
pc->capacity = 0;
pc->size = 0;
printf(" Destroy succeeded \n");
}
// Sort
void SortRank(Num* pc)
{
int i = 0;
int j = 0;
Rank tmp;
for (i = 0; i < pc->size - 1; i++)
{
for (j = 0; j < pc->size - 1 - i; j++)
{
if (pc->data[j].time > pc->data[j + 1].time)
{
tmp = pc->data[j];
pc->data[j] = pc->data[j + 1];
pc->data[j + 1] = tmp;
}
}
}
}
void FinRank(const Num* pc)
{
printf(" Need to find your grades \n");
printf("1. lookup \n");
printf("0. Unwanted \n");
int wheather = 0;
int i = 0;
int k = 0; // Determine if it is found
char name[20];
scanf("%d", &wheather);
if (wheather == 1)
{
printf(" Please enter your name \n");
scanf("%s", name);
for (i = 0; i < pc->size; i++)
{
if (0 == strcmp(name, pc->data[i].name))
{
printf("%s The best ranking of is :\n", name);
printf(" ranking -- name ---- Time \n");
printf("%5d\t%-5s\t%-20lf\n", i + 1, pc->data[i].name, pc->data[i].time);
k = 1;
break;
}
}
if (k == 0)
{
printf("%s There is no ranking for the time being \n", name);
}
}
printf(" Exit find \n");
}
void PrintfRank(const Num* pc)// Print
{
system("cls");
assert(pc);
SortRank(pc);
int i = 0;
// Determine whether it is null
if (pc->size == 0)
{
printf(" No ranking at the moment \n");
return;
}
printf(" ranking -- name ---- Time \n");
for (i = 0; i < pc->size; i++)
{
printf("%5d\t%-10s\t%-10.2lf second \n", i + 1, pc->data[i].name, pc->data[i].time);
}
// Find my ranking
FinRank(pc);
}
// game
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
printf("-------------------------\n");
printf(" ");
for (int i = 1; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (int i = 1; i <= row; i++)
{
printf("%d ", i);
for (int j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] != '1')
{
board[x][y] = '1';
count--;
}
}
}
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
int ret = 0;
for (int i = x - 1; i <= x + 1; i++)
{
for (int j = y - 1; j <= y + 1; j++)
{
if (mine[i][j] == '1')
{
ret++;
}
}
}
return ret;
}
void OpenOwn(char show[ROWS][COLS], char mine[ROWS][COLS],
int row, int col, int x, int y, int state[][COLS])
{
if (state[x][y] == 0)
{
state[x][y] = 1;
int count = GetMineCount(mine, x, y);
show[x][y] = '0' + count;
}
}
// an
void OpenNeighbor(char show[ROWS][COLS], char mine[ROWS][COLS],
int row, int col, int x, int y, int state[][COLS])
{
if (state[x][y] == 0)
{
state[x][y] = 1;
int count1 = GetMineCount(mine, x, y);
show[x][y] = '0' + count1;
for (int i = x - 1; i <= x + 1; i++)
{
for (int j = y - 1; j <= y + 1; j++)
{
if (i >= 1 && i <= ROW && j >= 1 && j <= COL)
{
if (state[i][j] == 0)
{
int count = GetMineCount(mine, i, j);
if (count != 0)
OpenOwn(show, mine, row, col, i, j, state);
else
OpenNeighbor(show, mine, row, col, i, j, state);
}
}
}
}
}
else
{
return;
}
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, Num* pc)
{
int x, y;
double start, end, cost;
int wheather;
start = clock();
int win = 0;
int state[ROWS][COLS] = { 0 };
// Altogether COL*ROW Lattice , When we lined up COL*ROW-EASY_COUNT When it's a grid , Mine clearance is successful
while (win < COL * ROW - EASY_COUNT)
{
//char whethermark[10] = { 0 };
int whethermark = 0;
//DisplayBoard(mine, ROW, COL); ///test
printf(" Please enter the coordinates of minesweeping ->");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= ROW && y >= 1 && y <= COL)
{
if (mine[x][y] == '1')
{
printf(" Unfortunately , You're killed in the blast \n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
int count = GetMineCount(mine, x, y);
if (count != 0)
{
win += 1;
OpenOwn(show, mine, ROW, COL, x, y, state);
system("cls");
DisplayBoard(show, ROW, COL);
//Sleep(2000);
}
else
{
OpenNeighbor(show, mine, row, col, x, y, state);
int nums = 0;
for (int i = 1; i <= ROW; i++)
{
for (int j = 1; j <= COL; j++)
{
if (state[i][j] == 1)
{
nums++;
}
}
}
win = nums;
system("cls");
DisplayBoard(show, ROW, COL);
//Sleep(2000);
}
while (1)
{
end = clock();
printf(" Used time %2.lf second \n", (end - start) / 1000);
printf(" Whether to mark ray ?\n");
printf("1. Mark \n");
printf(" Continue at will \n");
scanf("%d", &whethermark);
if (whethermark == 1)
{
Mark(show, ROW, COL);
}
else
{
break;
}
}
}
}
else
{
printf(" Input error, please input again \n");
}
}
if (win == COL * ROW - EASY_COUNT )// When Lei is eighty bug
{
system("cls");
end = clock(); // Recording time
printf(" Congratulations on your successful mine clearance !\n");
DisplayBoard(mine, ROW, COL);
printf(" Your total time %2.lf second \n", (end - start) / 1000);
// Record ranking
printf(" Whether the score is recorded to the ranking \n");
printf("1. Jin Bang Gao Xuan's surname is Zi Zhen , It's clearly broken into a spring \n");
printf("2. When it's time to brush your clothes , Deep in knowledge and fame \n");
scanf("%d", &wheather);
if (wheather == 1)
{
system("cls");
printf(" Start adding \n");
AddRank(pc, (end - start) / 1000.0);
PrintfRank(pc);
}
printf(" One more ?\n");
}
}
void Mark(char show[ROWS][COLS], int row, int col)
{
int x, y;
again:
printf(" Please enter the coordinates to be marked as mine ->");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
system("cls");
printf(" Mark successful \n");
show[x][y] = '#';
DisplayBoard(show, ROW, COL);
return;
}
else
{
printf(" Input error Please re-enter \n");
goto again;
}
}
边栏推荐
- VMware虚拟机更改静态IP和主机名,使用Xshell进行连接
- Worthington:来自酵母的己糖激酶的特性及其它参数说明
- Simulate not all endpoints registered exceptions and Solutions
- 轮毂电机主动减振系统及其垂向性能优化
- 项目升级遇到的坑
- Worthington溶菌酶技术说明及文献参考
- 学会这些Sketchup技巧,工作效率提高一半
- 大咖訪談 | 開源社區裏各種奇怪的現狀——夜天之書陳梓立tison
- How to use selenium.chrome to realize the extended function of intercepting or forwarding requests
- ASP. Net core creates MVC projects and uploads multiple files (streaming)
猜你喜欢

Experiment 4 DPCM

ZABBIX agent creates monitoring items

networkx对图进行可视化

php可不可以拆分数组

文件的一般、特殊、隐藏属性(实例生动画图)

Fault tolerant processing with hystrix

matlab simulink 水能和同步电机发电

Expérience II Yuv

21 -- product of arrays other than itself

Dispersion tensor analysis open source software DSI studio simplified Chinese version can be downloaded
随机推荐
【第31天】给定一个整数 n ,求出它的每个质因数的底数与指数 | 算术基本定理
Celebrity interview | various strange current situations in the open source community -- night sky Book Chen Zili tison
昇思易点通 | 经典卷积神经网络的深度学习解析
VMware虚拟机更改静态IP报错Unit network.service entered failed state解决方案
PNY file to picture
pny 文件转图片
开发者分享|MindSpore Lite 体验,一键实现图像分割
Scala generic generic class details - t
Talking about performance optimization: analysis and optimization of APP startup process
Experiment 4 DPCM
这不是真正意义上的元宇宙,元宇宙应当具备自身鲜明的特质和独特的发展逻辑
Worthington溶菌酶技术说明及文献参考
SQL报错盲注实例分析
ASP. Net core creates MVC projects and uploads multiple files (streaming)
Introduction to JVM monitoring tools jstack, jconsole, Jinfo, jmap, JDB, jstat
怎么使用selenium.chrome实现扩展拦截或转发请求功能
Wechat applet project practice
Web资源共享
Go 语言的 RSA 用秘钥解析JSEncrypt.js 这个库加密的密文失败
如何用C语言实现简单职工信息管理系统