C\C++ 1A2B小游戏源码
学了一段时间,心血来潮写了一个1A2B小游戏,很多人应该玩过,是一个挺有意思的益智小游戏,之前用易语言写过,现在又用C++重写了一下。
编译运行无错,整体程序设计思路为:进入循环,初始化游戏,读入一个数,判断是否合法,判断是否符合规则,判断是否正确,再给出答案提示。各部分都用函数封转方便管理和维护。不过有一点确实还需要改进,就是在输入输出语句的使用上,显得有些许混乱,用一个单独的函数来二次封装也许会更好,这样也能方便控制程序在任何时候都能退出游戏和做出最外层的响应。
1A2B游戏规则介绍:
// name:1A2B.cpp
// author:Frank
// descraption: 1A2B Game, in the beginning, the program will generate a random four-digits number
// in which the digits is not equal to each other, you need to guess it, and as you
// enter your answer, there would be a tips likes: xAyB. x means the count of right
// numbers those are in the right sites, y means the count of right numbers those
// are not in the right sites. 4A0B means you have got the right number.
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdbool.h> void InitializeGame(void);
void GetInput(char * input);
bool CheckAnswer(char * input);
bool GiveTips(char * input);
void GetRandom(char * random);
using namespace std; char answer[] = "";
char input[] = "";
int times = ; int main(int argc, char** argv) {
char c;
while (true){
cout << "Enter 'S' to start the game, 'Q' to quit." << endl;
c = toupper(getchar());
while(getchar() != '\n');
if (c == 'Q')
break;
else if(c == 'S'){
cout << "Game Start! Enter your answer:" << endl;
times = ;
InitializeGame();//初始化游戏
// cout << "The answer is: " << answer << endl;
GetInput(input); //输入猜测值
//检查猜测是否正确 不正确则给出提示
while(GiveTips(input) == false){
times++;
GetInput(input);
}
times++;
cout << "Congratulations! You have got it after " << times << " times." << endl;
}else
cout << "Only 'S' and 'Q' are received." << endl;
} return ;
} /******************************************************************************
*函数名称:void InitializeGame(void)
*函数功能:初始化游戏,生成随机数
*入口参数:无
*返 回 值:无
*******************************************************************************/
void InitializeGame(void){
static bool init_rand = false;
if (init_rand == false){
srand ((unsigned) time(NULL)); //如果未初始化则初始化随机数种子
init_rand = true;
}
GetRandom(answer);//生成随机数
// cout << answer << endl;
} /******************************************************************************
*函数名称:void GetInput(char * input)
*函数功能:读取一个字符串
*入口参数:返回读取的字符串
*返 回 值:无
*******************************************************************************/
void GetInput(char * input){
gets(input);
while(true){
if(strlen(input) != ){
cout << "Please input a 4-digits number!" << endl;
gets(input);
continue;
}
if(CheckAnswer(input) == false){
cout << "There couldn't be two same character in your answer!" << endl;
gets(input);//不合法则重新输入
continue;
}
break;
}
} /******************************************************************************
*函数名称:bool checkanswer(char * input)
*函数功能:判断答案是否合法,即是否存在重复数字
*入口参数:input为待判断的答案
*返 回 值:正确则返回真,否则返回假
*******************************************************************************/
bool CheckAnswer(char * input){
char temp[];
strcpy (temp, input);
for(int i = ; i < ; i++){
for(int j = i + ; j < ; j++)
if(temp[i] == input[j])
return false;
}
return true;
} /******************************************************************************
*函数名称:void GiveTips(char * input)
*函数功能:根据输入的答案来给出提示
*入口参数:待判断的答案
*返 回 值:无
*******************************************************************************/
bool GiveTips(char * input){
// cout << "I'm checking." << endl;
int a = , b = ;
for(int i = ; i < ; i++){
for(int j = ; j < ; j++){
// cout << "i:" << i << "j:" << j << endl;
if (input[i] == answer[j]){
if(i == j)
a++;
else
b++;
continue;
}
}
}
cout << "Tips:" << a << "A" << b << "B\n" << endl;
if (a == )
return true;
cout << "Enter another answer:";
return false;
} /******************************************************************************
*函数名称:void GetRandom(char * random)
*函数功能:产生一个各位数不相等的四位随机数
*入口参数:random为返回的随机数
*返 回 值:无
*备 注:先生成一个0-9的整数数组,再随机从中取四个数,每取一个将该位置为-1
*******************************************************************************/
void GetRandom(char * random){
int i, j[], k;
for (i = ; i < ; i++){
j[i] = i;
}
for(i = ; i < ; i++){
//生成第i个随机数
k = (int)rand() % ;//k为下标
while (j[k] == -){
k = (k + ) % ;
}
random[i] = '' + j[k];
j[k] = -;
}
}
代码格式相对工整,每个函数都比较简短,便于阅读和理解。当然,如果有更好的建议,还望不啬赐教。
C\C++ 1A2B小游戏源码的更多相关文章
- HTML5小游戏源码收藏
html5魅族创意的贪食蛇游戏源码下载 html5网页版打砖块小游戏源码下载 html5 3D立体魔方小游戏源码下载 html5网页版飞机躲避游戏源码下载 html5三国人物连连看游戏源码下载 js ...
- Creator仿超级玛丽小游戏源码分享
Creator仿超级玛丽小游戏源码分享 之前用Cocos Creator 做的一款仿超级玛丽的游戏,使用的版本为14.2 ,可以直接打包为APK,现在毕设已经完成,游戏分享出来,大家一起学习进步.特别 ...
- flappy pig小游戏源码分析(1)——主程序初探
闲逛github发现一个javascript原生实现的小游戏,源码写的很清晰,适合想提高水平的同学观摩学习.读通源码后,我决定写一系列的博客来分析源码,从整体架构到具体实现细节来帮助一些想提高水平的朋 ...
- h5小球走迷宫小游戏源码
无意中找到的一个挺有意思的小游戏,关键是用h5写的,下面就分享给大家源码 还是先来看小游戏的截图 可以用键盘的三个键去控制它,然后通关 下面是源代码 <!doctype html> < ...
- flappy pig小游戏源码分析(4)——核心pig模块(未完待续)
热身之后,我们要动点真格的了,游戏叫flappy pig,我们的pig终于要出场了. 老规矩,看看目录结构,读者对着目录结构好好回想我们已经讲解的几个模块: 其中game.js是游戏主程序,optio ...
- flappy pig小游戏源码分析(3)——解剖util
这一节我们继续高歌猛进,如果对源码中有无论无何都理解不通的问题,欢迎和我交流,让我也学习一下,我的qq是372402487. 还是按照惯例看看我们的目录结构. 我们在前两节中已经分析了game.js, ...
- flappy pig小游戏源码分析(2)——解剖option
今天继续分析flappy bird的源码.重温一下源码的目录结构. 在本系列第一篇中我们分析了game.js文件,也就是整个程序的架构.这一篇我们来看看option.js文件,这个文件的内容很简单,主 ...
- 2d命令行小游戏源码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- xss小游戏源码分析
配置 下载地址:https://files.cnblogs.com/files/Lmg66/xssgame-master.zip 使用:下载解压,放到www目录下(phpstudy),http服务下都 ...
随机推荐
- Linux-/proc目录简介
Linux /proc目录简介 1.简单了解 以文件系统的方式为访问系统内核数据的操作提供接口 由linux内核提供:通过/proc文件系统,在运行时访问内核内部数据结构.改变内核设置的一种机制 pr ...
- 解决java.lang.NoClassDefFoundError: org/objectweb/asm/util/TraceClassVisitor
方案一: <dependency> <groupId>asm</groupId> <artifactId>asm-all</artifactId& ...
- Struts2中Date日期转换的问题
今天跑程序的时候莫名其妙的出现了下面的一个异常: java.lang.NoSuchMethodException:com.ca.agent.model.mybatis.ApprovalInforC ...
- HTTP访问错误大全
400 - 错误的请求. ·401 - 访问被拒绝.IIS 定义了许多不同的 401 错误,它们指明更为具体的错误原因.这些具体的错误代码在浏览器中显示,但不在 IIS 日志中显示: ·401.1 - ...
- sqlserver Between And的问题
Id Name RegisterDate 1 澎澎 2007/1/5 00:00:00 2 丁丁 2007/1/6 04:37:00 3 亞亞 2007/1/7 00:00:00 数据库的数据如上.若 ...
- 使用 jsoup 对 HTML 文档进行解析和操作
jsoup 简介 Java 程序在解析 HTML 文档时,相信大家都接触过 htmlparser 这个开源项目,我曾经在 IBM DW 上发表过两篇关于 htmlparser 的文章,分别是:从 HT ...
- nodejs持续学习--必须关注4网站
1.官网:https://nodejs.org/en/ 2.模块社区:www.npmjs.com(FQ) 3.github.com 4.全球技术问题讨论社区 http://stackoverflow. ...
- VS2012调试时很慢的解决方案
1.转自http://guooge.com/archives/408.html VS2010调试极慢获取出现死机,因为启动了IntelliTrace Visual Studio 2010 Ulti ...
- datatable 加序号列
最近使用datatable时,发现没有像jqgrid那样生成序号列,在国外网站搜罗了一下还是很简单的,就要在aoColumns中添加一空列占位就行,然后再用fnRowCallback添加序号 示例如下 ...
- 转:看看 Delphi XE2 为 VCL 提供的 14 种样式
http://www.linuxso.com/linuxbiancheng/8889.html 其实只提供了 个 vsf 样式文件, 还有默认的 Windows 样式, 共 种. 在空白窗体上添加 L ...