C语言程序设计——扫雷
作者:互联网
涉及知识:
时间戳,数组,随机数的生成,函数,宏
Mine sweeper.h
#ifndef game_h
#define game_h
#define ROW 9
#define COL 9
#define ROWS ROW + 2
#define COLS COL + 2
#define EASY 10
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void InitBoard(char arr[ROWS][COLS],int row,int col,char ch);
void PrintBoard(char arr[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);
void MineCount(char mine[ROWS][COLS],char show[ROWS][COLS],int x,int y);
#endif /* game_h */
Mine sweeper.c
#include "game.h"
void InitBoard(char arr[ROWS][COLS],int rows,int cols,char ch)
{
int i = 0,j = 0;
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
arr[i][j] = ch;
}
}
}
void PrintBoard(char arr[ROWS][COLS],int row,int col)
{
int i = 1,j = 0;
printf("******** Mine sweeper ********\n");
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 ",arr[i][j]);
}
printf("\n");
}
printf("******** Mine sweeper ********\n");
}
void SetMine(char mine[ROWS][COLS],int row,int col)
{
int count = EASY;
int x = 0;
int y = 0;
while(count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if(mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
void MineCount(char mine[ROWS][COLS],char show[ROWS][COLS],int x,int y)
{
int count = 0;
count = mine[x - 1][y - 1] + mine[x - 1][y + 1] + mine[x - 1][y] +
mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] +
mine[x][y - 1] + mine[x][y + 1] - 8 * mine[x][y];
show[x][y] = count + '0';
}
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col)
{
int x = 0,y = 0,win = 0;
while(win < ROW * COL - EASY)
{
scanf("%d %d",&x,&y);
if(x >= 1 && x <= ROW && y >= 1 && y <= COL)
{
if(mine[x][y] == '1')
{
printf("Wasted!!\n");
PrintBoard(mine, ROW, COL);
break;
}
else
{
MineCount(mine,show,x,y);
PrintBoard(show, ROW, COL);
win++;
}
}
else
{
printf("Error input\n");
}
}
if(win == ROW * COL - EASY)
{
printf("Congratulations!!! You win!!!\n");
PrintBoard(mine, ROW, COL);
}
}
test.c
#include <stdio.h>
#include "game.h"
void mune(void)
{
printf("**************************\n");
printf("******* 1.Play *******\n");
printf("******* 0.Exit *******\n");
printf("**************************\n");
}
void game(void)
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
InitBoard(mine,ROWS,COLS,'0');
InitBoard(show,ROWS,COLS,'*');
//PrintBoard(mine,ROW,COL);
PrintBoard(show,ROW,COL);
SetMine(mine,ROW,COL);
PrintBoard(mine,ROW,COL);
FindMine(mine,show,ROW,COL);
}
void test(void)
{
int input;
do
{
scanf("%d",&input);
switch(input)
{
case 1:
game();
break;
case 0:
printf("End of game\n");
break;
default:
printf("Error input\n");
}
}while(input);
}
int main(int argc, const char * argv[]) {
// insert code here...
srand((unsigned int)time(NULL));
mune();
test();
return 0;
}
标签:ROWS,int,void,COLS,C语言,char,扫雷,mine,程序设计 来源: https://blog.csdn.net/m0_60867520/article/details/123072264