当前位置:网站首页>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;
}
}
边栏推荐
- 【第31天】给定一个整数 n ,求出它的每个质因数的底数与指数 | 算术基本定理
- 组蛋白研究丨Worthington小牛胸腺组蛋白的特征及文献参考
- Dispersion tensor analysis open source software DSI studio simplified Chinese version can be downloaded
- MPLS VPN 跨域-optionB
- Storage structure and method of graph (I)
- 1.10 API and string
- 敏捷测试团队组织构成
- Qt+VTK+PCL图片转灰度图且以灰度为Y轴显示
- PNY file to picture
- Live broadcast preview | live broadcast Seminar on open source security governance models and tools
猜你喜欢

Storage structure and method of graph (I)

實驗二 YUV

How to open the file in keil is the real path in the 109th blog of fledgling Xiao Li

目标检测之锚点与锚框

Niuke Xiaobai month race 53

如何用C语言实现简单职工信息管理系统

There are 13 detailed methods for JMeter to view the response of the result tree!

学习总结 | 真实记录 MindSpore 两日集训营能带给你什么(一)!

Spark troubleshooting -precondition eof: no length prefix available

轮毂电机主动减振系统及其垂向性能优化
随机推荐
networkx对图进行可视化
Yolov5 post-processing code of cpu/gpu (CUDA) version
Storage structure and method of graph (I)
Customize flick es source
MySQL消息队列表结构
大咖訪談 | 開源社區裏各種奇怪的現狀——夜天之書陳梓立tison
Experiment III LZW
Talking about performance optimization: analysis and optimization of APP startup process
数据库基础及安装
算法面试高频题解指南【一】
PostgreSQL database master-slave deployment master database suspended restore master database
二叉树(学习日常)
图的存储结构及方法(一)
【读书笔记->统计学】12-01 置信区间的构建-置信区间概念简介
golang--module
这不是真正意义上的元宇宙,元宇宙应当具备自身鲜明的特质和独特的发展逻辑
Wechat applet project practice
多商户系统的直播功能用过吗?用过的朋友扣个 666!
[day 31] given an integer n, find the base and exponent of each prime factor | basic theorem of arithmetic
轮毂电机主动减振系统及其垂向性能优化