C语言实现小游戏
前言
实践是最好的锻炼,扫雷小游戏学完二维数组就能做。源码贴在下面啦~感兴趣的小伙伴可以尝试去做一做哦
源代码
头文件game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"head.h"
void menu()
{
printf("****************\n");
printf("*****1.PLAY*****\n");
printf("*****0.EXIT*****\n");
printf("****************\n");
}
void Init_Board(char board[ROWS][COLS], int rows, int cols, char ch)
{
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = ch;
}
}
}
void Display_board(char board[ROWS][COLS], int row, int col)
{
int i, j;
printf("-------扫雷—————————\n");
printf("0 ");
for (i = 1; i <= 9; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <=row; i++)
{
printf("%d ", i);
for (j = 1; j <=col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("-------扫雷—————————\n");
}
void Set_Mine(char board[ROWS][COLS],int row,int col)
{
int x, y, cnt;
cnt = SMINE;
while (cnt)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
cnt--;
}
}
}
int get_mine_count(char Mine[ROWS][COLS], int x, int y)
{
return((Mine[x - 1][y] + Mine[x-1][y-1] + Mine[x-1][y+1] + Mine[x][y-1] + Mine[x-1][y+1]
+ Mine[x+1][y+1] + Mine[x+1][y] + Mine[x+1][y-1]) - 8 * '0');
}
void Around_mine_count(char Mine[ROWS][COLS],char Show[ROWS][COLS], int x, int y)
{
if (Show[x][y] != '*'||Mine[x][y]=='1') return;
int ret = get_mine_count(Mine, x, y);
if (ret == 0)
{
Show[x][y] = ' ';
Around_mine_count(Mine,Show, x-1, y);
Around_mine_count(Mine, Show, x - 1, y-1);
Around_mine_count(Mine, Show, x - 1, y+1);
Around_mine_count(Mine, Show, x , y-1);
Around_mine_count(Mine, Show, x , y+1);
Around_mine_count(Mine, Show, x + 1, y-1);
Around_mine_count(Mine, Show, x + 1, y);
Around_mine_count(Mine, Show, x + 1, y+1);
}
else
{
Show[x][y] = ret + '0';
return;
}
}
int Judge(char Show[ROW][COL], int row, int col)
{
int cnt = 0;
int i, j;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (Show[i][j] != '*')
cnt++;
}
}
return cnt;
}
void Find_Mine(char Mine[ROWS][COLS],char Show[ROWS][COLS], int row, int col)
{
int x=0, y=0;
int count=0;
while (1)
{
count = Judge(Show, ROW, COL);
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (Show[x][y] != '*')
{
printf("该坐标已经被排查过了\n");
continue;
}
if (Mine[x][y] == '1')
{
printf("很遗憾你被炸死了\n");
Display_board(Mine, ROW, COL);
break;
}
else
{
Around_mine_count(Mine, Show, x, y);
Display_board(Show, ROW, COL);
}
}
else
{
printf("输入坐标错误,请重新输入\n");
}
if (count == (row * col - SMINE))
{
printf("恭喜你,排雷成功\n");
Display_board(Mine, ROW, COL);
break;
}
}
}
void game()
{
char Mine[ROWS][COLS], Show[ROWS][COLS];
Init_Board(Mine,ROWS,COLS, '0');
Init_Board(Show, ROWS,COLS,'*');
Set_Mine(Mine, ROW,COL);
Display_board(Mine, ROW, COL);
Display_board(Show, ROW, COL);
Find_Mine(Mine, Show, ROW, COL);
}
head.c
#pragma
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROWS 11
#define COLS 11
#define ROW 9
#define COL 9
#define SMINE 10
void menu();
void game();
void Init_Board(char board[ROWS][COLS], int rows, int cols, char ch);
void Set_Mine(char board[ROWS][COLS],int row,int col);
void Find_Mine(char Mine[ROWS][COLS], char Show[ROWS][COLS], int row, int col);
void Around_mine_count(char Mine[ROWS][COLS], char Show[ROWS][COLS], int x, int y);
int Judge(char Show[ROW][COL], int row, int col);
test.c
#define _CRT_SECURE_NO_WARNINGS
#include "head.h"
int count;
int main()
{
int input;
do
{
menu();
printf("请选择》》\n");
scanf("%d", &input);
srand((unsigned int)time(NULL));
switch (input)
{
case 1: game(); break;
case 0: printf("游戏结束\n"); break;
default: printf("输入错误请重新输入\n"); break;
}
} while (input);
return 0;
}