让我们在terminal下愉快的...扫雷

昨天跟奇葩霖聊起“雷区”这个敏感词汇,然后非常荣幸的。。。

应该轰炸不到我。。

后来百无聊赖的去玩了把扫雷。然后发现我之前都是乱扫的,没有不论什么技巧。百科之后才发现,扫雷是有技巧的,接着玩了一把。咦挺有意思的。。。大概感受了一下,今天又要考数电,昨晚写了个框架。就到两点半了。。。睡。

。。

今天中午回来。第一件事就是接着写。。。简直是爽。。。

这家伙。原来昨天之前。。。我一直不会玩。。

。假设还是跟我一样不会玩的。。

我也懒得介绍规则了。。。自行google吧。。

this blog的目的不是推广扫雷。

。。

/******************************************************************
Code writer : EOF
code file : mine_sweep.c
code date : 2014.06.23
e-mail: jasonleaster@gmail.com Code purpose :
This code is just a simulation of mine sweeping...
If there are some where wrong with my code, just touch me by e-mail.
Thank you. Have fun! ********************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <string.h> #define MATRIX_SIZE 4 //demention of square matrix
#define BUFFSIZE 1024 //buffer size #define NUM_MINE 4 //number of mine #define IS_BOMB 1 //MACRO for determining current location has a bomb
#define NOT_BOMB 0 #define GAMEOVER 0
#define GAMING 1 struct battle
{
/*
char current_battlefiled is used for descripting current state of battlefield
*/
char current_battlefiled[MATRIX_SIZE][MATRIX_SIZE]; /*
char answer is used for determining that location has a bomb or not.
*/
int answer[MATRIX_SIZE][MATRIX_SIZE]; /*
char num_bomb is used for determing that how many bombs around the current location.
*/
int num_bomb[MATRIX_SIZE][MATRIX_SIZE];
}; void print_current_battle(struct battle *p_battlefiled); struct battle initialize_battle(void); void do_open(struct battle *p_battlefiled,int row,int rank);
void do_mine(struct battle *p_battlefiled,int row,int rank);
void do_unknow(struct battle *p_battlefiled,int row,int rank); void do_operation(struct battle *p_battlefiled,int row, int rank); void count_bomb_around(struct battle *p_battlefiled); int Game = GAMING;
//In fact, I try my best to avoid to use global varible in my code....
//but I think it is ok here.You know I can't accept that long long parameter list.
int Open_safely_times = 0; int main()
{ int row = 0;
int rank = 0; struct battle new_york; new_york = initialize_battle(); print_current_battle(&new_york);//Firstly, show player the initial battlefiled. printf("Hey guys, please input two numbers that represent \nthe number of row and rank of the location in the battlefiled\n"); while(!scanf("%d",&row) ||row >= MATRIX_SIZE)
{
printf("input row error\nplease input again\n");
while(getchar() != '\n')
{ }
} while(!scanf("%d",&rank) || rank >= MATRIX_SIZE)
{
printf("input rank error\nplease input again\n");
while(getchar() != '\n')
{ }
} printf("Remember that there are only three operation could be done.\n");
printf("If you want to open the current location, just input command \"open\"\n");
printf("If you want to sign the current location as mine, just input command \"mine\"\n");
printf("If you want to sign the current location as question mark, just input command \"unknow\"\n"); printf("Never forget that!\n"); while(Game == GAMING && Open_safely_times < MATRIX_SIZE*MATRIX_SIZE-NUM_MINE)
{ do_operation(&new_york,row,rank); if(Game == GAMEOVER)
{
break;
} printf("Again! Input two numbers that represent \nthe number of row and rank of the location in the battle\n"); while(!scanf("%d",&row) ||row >= MATRIX_SIZE)
{
printf("input row error\nplease input again\n");
while(getchar() != '\n')
{ }
} while(!scanf("%d",&rank) || rank >= MATRIX_SIZE )
{
printf("input rank error\nplease input again\n");
while(getchar() != '\n')
{ }
}
} return 0;
} void count_bomb_around(struct battle *p_battlefiled)
{
//This function used in "struct battle initialize_battle(void)" for counting how many bombs around.
int row = 0;
int rank = 0; for(row = 0; row < MATRIX_SIZE; row++)
{
for(rank = 0;rank < MATRIX_SIZE;rank++)
{
if(row-1>=0 && rank -1 >= 0 && p_battlefiled->answer[row-1][rank-1] == IS_BOMB)
{
(p_battlefiled->num_bomb[row][rank])++;
} if(row-1>=0 && rank >= 0 && p_battlefiled->answer[row-1][rank] == IS_BOMB)
{
(p_battlefiled->num_bomb[row][rank])++;
} if(row >=0 && rank -1 >= 0 && p_battlefiled->answer[row][rank-1] == IS_BOMB)
{
(p_battlefiled->num_bomb[row][rank])++;
} if(row+1 <= MATRIX_SIZE-1 && rank +1 <= MATRIX_SIZE-1 && p_battlefiled->answer[row+1][rank+1] == IS_BOMB)
{
(p_battlefiled->num_bomb[row][rank])++;
} if(row+1 <= MATRIX_SIZE-1 && rank <= MATRIX_SIZE-1 && p_battlefiled->answer[row+1][rank] == IS_BOMB)
{
(p_battlefiled->num_bomb[row][rank])++;
} if(row <= MATRIX_SIZE-1 && rank +1 <= MATRIX_SIZE-1 && p_battlefiled->answer[row][rank+1] == IS_BOMB)
{
(p_battlefiled->num_bomb[row][rank])++;
} if(row+1 <= MATRIX_SIZE-1 && rank -1 >= 0 && p_battlefiled->answer[row+1][rank-1] == IS_BOMB)
{
(p_battlefiled->num_bomb[row][rank])++;
} if(row-1 >= 0 && rank+1 <= MATRIX_SIZE && p_battlefiled->answer[row-1][rank+1] == IS_BOMB)
{
(p_battlefiled->num_bomb[row][rank])++;
}
}
}
} void do_open(struct battle *p_battlefiled,int row,int rank)
{ if(p_battlefiled == NULL)
{
printf("parameter error in do_open\n");
return ;
} if(p_battlefiled->answer[row][rank] == IS_BOMB)
{
printf("BOMB~~!!!\n");
for(row = 0; row < MATRIX_SIZE; row++)
{
for(rank = 0;rank < MATRIX_SIZE;rank++)
{
if(p_battlefiled->answer[row][rank] == IS_BOMB)
{
p_battlefiled->current_battlefiled[row][rank] = '@';
}
}
} //game over...
Game = GAMEOVER;
}
else if(p_battlefiled->answer[row][rank] == NOT_BOMB)
{
p_battlefiled->current_battlefiled[row][rank] = ((char)p_battlefiled->num_bomb[row][rank]) + '0'; //Lucky, not bomb
Open_safely_times++;
}
} void do_mine(struct battle *p_battlefiled,int row,int rank)
{
if(p_battlefiled == NULL)
{
printf("parameter error in do_open\n");
return ;
} p_battlefiled->current_battlefiled[row][rank] = '!';
} void do_unknow(struct battle *p_battlefiled,int row,int rank)
{ if(p_battlefiled == NULL)
{
printf("parameter error in do_open\n");
return ;
} p_battlefiled->current_battlefiled[row][rank] = '?';
} void do_operation(struct battle *p_battlefiled,int row, int rank)
{
char operation[BUFFSIZE] = {0}; printf("Which operation would you want to do?\n"); read_operation:
scanf("%s",operation);//Obevious,It's unsafe that there may be a overflow,Just a game never mind it... if(strcmp(operation,"open") == 0)
{
do_open(p_battlefiled,row,rank); print_current_battle(p_battlefiled);
}
else if(strcmp(operation,"mine") == 0)
{
do_mine(p_battlefiled,row,rank); print_current_battle(p_battlefiled);
}
else if(strcmp(operation,"unknow") == 0)
{
do_unknow(p_battlefiled,row,rank); print_current_battle(p_battlefiled);
}
else
{
printf("operation error\nAttention!...Just input one of \"open\" \"mine\" or \"unknow\"\n"); goto read_operation;
} } struct battle initialize_battle(void)
{
struct battle s_battle; int row = 0;
int rank = 0;
int temp = 0; for(rank = 0; rank < MATRIX_SIZE; rank++)
{
for(row = 0;row < MATRIX_SIZE;row++)
{
s_battle.current_battlefiled[row][rank] = '*';
s_battle.answer[row][rank] = NOT_BOMB;
s_battle.num_bomb[row][rank] = 0;
}
} //Maybe, it is time consuming...But it do work correctly.
for(temp = 0; temp < NUM_MINE; temp++)
{
row = (rand() % MATRIX_SIZE);
rank = (rand() % MATRIX_SIZE); if(s_battle.answer[row][rank] == NOT_BOMB)
{
s_battle.answer[row][rank] = IS_BOMB;
}
else
{
temp--;
}
} count_bomb_around(&s_battle); return s_battle;
} void print_current_battle(struct battle *p_battlefiled)
{
int row = 0;
int rank = 0;
for(row = 0; row < MATRIX_SIZE; row++)
{
for(rank = 0;rank < MATRIX_SIZE;rank++)
{
printf("%c ",p_battlefiled->current_battlefiled[row][rank]);
}
printf("\n");
}
}

C language 模拟 win的经典游戏——扫雷的更多相关文章

  1. C++复现经典游戏——扫雷

    国庆小长假,当大家都去看人山人海的时候,我独自一人狂码代码.这两天想要实现的内容是Windows上的一个经典游戏——扫雷.相信90后和一些上班族对此并不陌生.然而,从win8开始,扫雷就不再是Wind ...

  2. JY游戏之毁经典《扫雷》

    JY游戏之毁经典<扫雷> 这是一个经典的pc端游戏,一定的运气加一点数学常识,讲的是一个速度,这次,我利用js JY库重做了这款游戏,加了三次生命,过关难度,也兼容了移动端的触摸事件. 它 ...

  3. Python之模拟职场人生游戏

    题目:模拟人生 要求:1.至少有两个角色 2.玩的过程中,有冲突 3.根据不同的交互,产生不同的行为. 4.一定要用到面向对象语法和思想 1.解题思路 创建一个类,赋予角色不同的方法,使用面向对象思想 ...

  4. 常用Java API之Ramdom--用代码模拟猜数小游戏

    常用Java API之Ramdom Ramdom类用来生成随机数字.使用起来也是三个步骤: 1.导包 import java.util.Random; 2.创建 Random r = new Rand ...

  5. Javascript版经典游戏之《扫雷》

    翻出年初写的游戏贴上来,扫雷相信大家都玩过,先上图: 源码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...

  6. 用Javascript模拟微信飞机大战游戏

    最近微信的飞机大战非常流行,下载量非常高. 利用JS进行模拟制作了一个简单的飞机大战[此源码有很多地方可以进行重构和优化] [此游戏中没有使用HTML5 任何浏览器都可以运行]. 效果图: 原理:利用 ...

  7. FC经典游戏还原之:松鼠大作战2

    版权声明:本文原创发布于博客园"优梦创客"的博客空间(id:raymondking123) 原帖地址:http://www.cnblogs.com/raymondking123/p ...

  8. Unity2017 经典游戏开发教程 算法分析与实现 (张帆 著)

    https://meta.box.lenovo.com/link/view/82c451b41ce34e81a4b34cb46747d3d5 第1章 熟悉Unity软件的操作 第2章 打地鼠 (已看) ...

  9. cocos2d-x《农场模拟经营养成》游戏完整源代码

    cocos2d-x农场模拟经营养成游戏完整源代码,cocos2d-x引擎开发,使用JSON交互,支持IOS与 Android,解压后1016MB. 非常强大的游戏源代码         完整游戏源代码 ...

随机推荐

  1. visual studio 2013使用github获取代码

    如图点击"视图""团队资源管理器".   点击主页上方的那个插头形状按钮"连接到团队项目".点击"克隆"   然后在克隆 ...

  2. jQuery插件实现瀑布留布局masonry + infinitescroll 图片高度处理

    jQuery插件实现瀑布留布局masonry + infinitescroll . 使用官方的示例代码实际测试发现,当上传到服务器的时候,由于图片下载速度问题,导致图片高度不能被正确识别,从而造成层的 ...

  3. IDEA搭建SSMM框架(详细过程)

    IDEA搭建SSMM框架(详细过程) 相关环境 Intellij IDEA Ultimate Tomcat JDK MySql 5.6(win32/win64) Maven (可使用Intellij ...

  4. selenium 之 ActionChains (二)

    今天,小编为大家介绍的是标题中的三个新方法,以及一个老方法 以下方法都需要操作一个名为Keys的包,先来简单认识下 ALT = u'\ue00a' CONTROL = u'\ue009' ENTER ...

  5. 页面第一次加载,JS没有效果,刷新一下就好了

    问题详述:页面跳转的时候,第一个第二个页面都没有问题,跳到第三个页面,JS脚本没有起作用,刷新一下就好了. 1.猜测:第一个页面和第二个页面的JS,会对第三个页面产生影响,(因为之前没有这个问题,只改 ...

  6. C#自动实现Dll(OCX)控件注册的两种方法

    尽管MS为我们提供了丰富的.net framework库,我们的程序C#开发带来了极大的便利,但是有时候,一些特定功能的控件库还是需要由第三方提供或是自己编写.当需要用到Dll引用的时候,我们通常会通 ...

  7. 【转】MYSQL 使用SQLyog导入遇到问题解决

    原文地址:http://blog.163.com/o5655@126/blog/static/1667428342010910112510738/  昨天公司想要将一个数据库的数据导出再导入到另外一个 ...

  8. 从零一起学Spring Boot之LayIM项目长成记(五)websocket

    前言 距离上一篇已经比较久的时间了,项目也是开了个头.并且,由于网上的关于Spring Boot的websocket讲解也比较多.于是我采用了另外的一个通讯框架 t-io 来实现LayIM中的通讯功能 ...

  9. javaweb-2-Tomcat初步学习与使用

    一.Tomcat服务器简介(此点网上官方有详尽的解释,故此不赘述,以学习使用为主) Apache Jakarta的开源项目 JSP/Servlet容器 二.Tomcat的目录结构 三.启动和停止Tom ...

  10. webstorm激活破解码+++使用技巧

    Webstorm激活破解码 2017-06-15更新 之前都是使用2017.2.27的方法,版本是2017.1.1,还没提示过期,但是根据评论说这个链接已经失效了,评论也给出了个新地址:http:// ...