当前位置:网站首页>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;
	}
}



原网站

版权声明
本文为[fentiaoOvO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207222155566938.html