一只程序猿的养成日记 第一章 第九节 小游戏 扫雷的实现
来源:互联网 发布:android多线程编程 编辑:程序博客网 时间:2024/06/05 18:35
game.h
#ifndef __GAME_H__
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define cols 11
#define rows 11
#define Count 10
int menu();
void display(char show[rows][cols]);
void set_mine(char mine[rows][cols]);
int get_num(char mine[rows][cols], int x, int y);
int Game(char mine[rows][cols],char show[rows][cols]);
int Sweep(char mine[rows][cols], char show[rows][cols]);
#endif _GAME_H__
game.c
#include"game.h"
int main()
{
int input = 0;
char mine[rows][cols];
char show[rows][cols];
int i = 0;
int j = 0;
for (i = 0; i < rows - 1; i++)
{
for (j = 0; j < cols - 1; j++)
{
mine[i][j] = '0';
show[i][j] = '*';
}
}
menu();
while (1)
{
printf("请选择:");
scanf("%d", &input);
if (input == 1)
{
printf("进入游戏\n");
Game(mine,show);
break;
}
else if (input == 0)
{
printf("退出游戏!\n");
exit(0);
break;
}
else
{
printf("输入错误!\n");
}
}
return 0;
}
test.c
#include"game.h"
//菜单函数
int menu()
{
printf("******************************************\n");
printf("************ 1. 进入游戏 ************\n");
printf("************ 0. 退出游戏 ************\n");
printf("******************************************\n");
return 0;
}
//设置雷的位置
void set_mine(char mine[rows][cols])
{
int count = Count;
int x = 0;
int y = 0;
srand((unsigned)time(NULL));
while (count)
{
x = rand() % 9 + 1;
y = rand() % 9 + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
//打印下棋完了显示的界面
void display(char show[rows][cols])
{
int i = 0;
int j = 0;
printf(" ");
for (i = 1; i < cols - 1; i++)
{
printf(" %d ", i);
}
printf("\n");
for (i = 1; i < rows - 1; i++)
{
printf("%d", i);
for (j = 1; j < cols - 1; j++)
{
printf(" %c ", show[i][j]);
}
printf("\n");
}
}
//计算雷的个数
int get_num(char mine[rows][cols], int x, int y)
{
int count = 0;
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][y - 1] == '1')//上边
{
count++;
}
if (mine[x][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++;
}
return count;
}
//扫雷开始
int Sweep(char mine[rows][cols], char show[rows][cols])
{
int count = 0;
int x = 0;
int y = 0;
while (count!=((rows-2)*(cols-2)-Count))
{
printf("请输入坐标<使用空格隔开>:\n");
scanf("%d%d", &x, &y);
if (mine[x][y] == '1')
{
printf("你被炸死了!\n");
return 0;
}
else
{
int ret = get_num(mine, x, y);
show[x][y] = ret + '0';
display(show);
count++;
}
}
printf("恭喜!你赢了!\n");
display(mine);
return 0;
}
//游戏内容
int Game(char mine[rows][cols],char show[rows][cols])
{
set_mine(mine);
display(show);
Sweep(mine,show);
return 0;
}
- 一只程序猿的养成日记 第一章 第九节 小游戏 扫雷的实现
- 一只程序猿的养成日记 第一章 第七节 三子棋的实现 有错求指导
- 一只程序猿的养成日记 第一章 第十一节 编写一个函数实现n^k,使用递归实现
- 一只程序猿的养成日记 第一章 第十五节 递归和非递归分别实现求n的阶乘
- 一只程序猿的养成日记 第一章 第十六节 递归方式实现打印一个整数的每一位
- 一只程序猿的养成日记 第一章 第十三节 编写一个函数reverse_string(char * string)(递归实现)
- 一只程序猿的养成日记 第一章 第十四节 递归和非递归分别实现strlen
- 一只程序猿的养成日记 第一章 第四节 逻辑运算符的应用
- 一只程序猿的养成日记 第一章 第一节 初接触
- 一只程序猿的养成日记 第一章 第六节 栈帧结构
- 一只程序猿的养成日记 第一章 第八节 栈帧结构补充
- 一只程序猿的养成日记 第一章 第十节 递归和非递归分别实现求第n个斐波那契数
- 一只程序猿的养成日记 第一章 第十二节 输入一个非负整数,返回组成它的数字之和
- 小游戏之扫雷的实现
- 扫雷小游戏的设计与实现
- VB6实现简单的扫雷小游戏
- 用C实现的扫雷小游戏
- 【扫雷】编写一个小游戏--扫雷的c语言实现
- 按键滤波算法
- json字符串中,包含函数的处理
- C#学习笔记之——重载
- jquery限制输入的数字为(8,2)格式
- NERSim v1.09a 1CD+HyperMILL 2018.1
- 一只程序猿的养成日记 第一章 第九节 小游戏 扫雷的实现
- TinyXML2使用方法及示例
- 交叉熵到logistic和softmax
- 我的Ubuntu脚印
- FLASHFTP简体中文破解版
- GOJ 1452(数论+前缀和查询)
- 日期查询:把easyUI的日期格式转成yyyy-MM-dd格式
- HTML5标签构成
- tar包的解压 和压缩