扫雷小游戏
Posted 流浪孤儿
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了扫雷小游戏相关的知识,希望对你有一定的参考价值。
首先先创建一个工程,再创建三个文件如图
game.h中
#define _CRT_SECURE_NO_WARNINGS 1
//包含的头文件
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
//标识符常量
#define ROW 6//地雷的行数
#define COL 6//地雷的列数
#define COUNT 20//地雷数
#define ROWS ROW+2//防止越边界于是加2
#define COLS COL+2
//函数声明
void menu();//游戏菜单
void game();//游戏主体
void setmine(char mine[ROWS][COLS], int row, int col);//设置地雷
void setbattle(char battle[ROWS][COLS], int row, int col,char sym);//设置战场
void InitBattle(char mine[ROWS][COLS], int row, int col);//输出战场
int testmine(char mine[ROW][COL], int x, int y);//检测所排的地标是否有地雷,并将是否有地雷的情况返回
//返回1说明踩到了地雷游戏失败
//返回0说明未踩到地雷
char testcount(char mine[ROWS][COLS], int rows, int cols, int x, int y);//检测所排的地标四周有多少雷
int testwin(char battle[ROWS][COLS], int row, int col);//检测是否胜利
text.c中
#include"game.h"
int main()
{
srand((unsigned int)time(NULL));
int OPTION = -1;
do
{
int tmp;
menu();
printf("输入1玩游戏输入0退出游戏:\\n");
error:
tmp = 0;//清空输入函数的缓冲区
scanf("%d", &OPTION);
while (tmp != '\\n')
{
tmp = getchar();
}
switch (OPTION)
{
case 1:
game();
break;
case 0:
break;
default:
printf("输入错误,请重新输入:");
goto error;
}
} while (OPTION);
return 0;
}
void menu()
{
printf("-----------------扫 雷 游 戏--------------------\\n");
printf("------------------1 : PLAY----------------------\\n");
printf("------------------0 : EXIT ---------------------\\n");
printf("------------------------------------------------\\n");
}
game.c中
#include"game.h"
void game()
{
printf("赢了有奖励哦!\\n");
char tmp = 0;
int z = 0;
int x = -1;//要排的地雷的地标
int y = -1;
char mine[ROWS][COLS] = { 0 };//地雷
char board[ROWS][COLS] = { 0 };//战场
setbattle(mine, ROWS, COLS,'0');
setbattle(board, ROWS, COLS,'*');
setmine(mine, ROW, COL);
InitBattle(board, ROW, COL);
//printf("-------------------------\\n");
//InitBattle(mine, ROW, COL);
do
{
printf("请输入要排的地标(如输入行列Enter):\\n");
error1:
scanf("%d%", &z);
x = z / 10;
y = z % 10;
tmp = getchar();
while (tmp != '\\n')//清除scanf的缓冲区
{
tmp = getchar();
}
if (x < 1 || x>9 || y < 1 || y>9)
{
printf("输入错误请重新输入:\\n");
goto error1;
}
if (0 == testmine(mine, x, y))//为0则游戏继续
{
board[x][y] = testcount(mine, ROWS, COLS, x, y);
/*InitBattle(mine, ROW, COL);
printf("--------------------------------\\n");*/
InitBattle(board, ROW, COL);
if (testwin(board, ROW, COL) == (ROW*COL - COUNT))
{
printf("恭喜你赢了!!!\\n");
printf("美女畅聊电话110\\n");
break;
}
}
else
{
board[x][y] = 'X';
printf("游戏失败!!!\\n");
InitBattle(board, ROW, COL);
break;
}
} while (1);
}
void setmine(char mine[ROWS][COLS], int row, int col)
{
int x = -1;
int y = -1;
int count = COUNT;
while (count)
{
x = rand() % 10;
y = rand() % 10;
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
count--;
mine[x][y]= '1';
}
}
}
void InitBattle(char battle[ROWS][COLS], int row, int col)
{
int i = 1;
int j = 1;
for (i = 0; i < row + 1; i++)
{
printf("%-3d", i);
}
printf("\\n");
for (i = 0; i < 3*row+1 ; i++)
{
printf("%c", '-');
}
printf("\\n");
for (i = 1; i <= row; i++)
{
printf("%-2d|", i);
for (j = 1; j <= col; j++)
{
printf("%-3c", battle[i][j]);
}
printf("\\n");
}
}
void setbattle(char battle[ROWS][COLS], int rows, int cols,char sym)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
battle[i][j]= sym;
}
}
}
int testmine(char mine[ROWS][COLS], int x, int y)
{
if (mine[x][y] == '1')
return 1;
else
return 0;
}
char testcount(char mine[ROWS][COLS], int rows, int cols, int x, int y)
{
char count = '0';
if (mine[x][y+1] == '1')
count++;
if (mine[x][y-1] == '1')
count++;
if (mine[x+1][y] == '1')
count++;
if (mine[x+1][y-1] == '1')
count++;
if (mine[x+1][y+1] == '1')
count++;
if (mine[x-1][y] == '1')
count++;
if (mine[x-1][y+1] == '1')
count++;
if (mine[x-1][y-1] == '1')
count++;
return count;
}
int testwin(char battle[ROWS][COLS], int row, int col)
{
int count = 0;
int i = -1;
int j = -1;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (battle[i][j] != '*')
count++;
}
}
return count;
}
以上是关于扫雷小游戏的主要内容,如果未能解决你的问题,请参考以下文章