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服务下都 ...
随机推荐
- SQL转换时间的时分
SELECT WorkerNo, DutyTime, DATENAME(weekday, DutyTime) AS WeekDay, CycleType, CycleNumber, YnOnDuty, ...
- SQL Server删除重复行的6个方法
SQL Server删除重复行是我们最常见的操作之一,下面就为您介绍六种适合不同情况的SQL Server删除重复行的方法,供您参考. 1.如果有ID字段,就是具有唯一性的字段 delect ta ...
- SVN本地代码未提交而被覆盖
本地代码未提交而不小心被覆盖了,肿么办... 到回收站找到你的文件 xxx.mine,代码就可以找回来了.如果回收站没有了,那就没办法了. ---- 失而复得的感觉真好!
- 把 excel 和 mysq l数据库相互转换
不用代码轻松搞定,参考http://jingyan.baidu.com/article/fc07f9891cb56412ffe5199a.html 1.excel 转 mysql a.首先确认你的数据 ...
- iOS10 推送必看(基础篇)
虽然这篇文章比较长,也不好理解,但是还是建议大家收藏,以后用到的时候,可以看看,有耐心的还是读一读. 这篇文章开始,我会跟大家好好讲讲,苹果新发布的iOS10的所有通知类. 一.创建本地通知事例详解: ...
- MYSQL数据库相关知识合集
1 MYSQL取得某一范围随机数: 关键词:RAND() [产生0~1之间的随机数] mysql> SELECT RAND( ), RAND( ), RAND( ); +----------- ...
- SQLServer
# 将检索到的数据插入到一张新表 SELECT * INTO <NEW_TABLE_NAME> FROM <OLD_TABLE_NAME>
- 【leetcode】Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...
- eclipse按照svn插件
在线安装: (1).点击 Help --> Install New Software... (2).在弹出的窗口中点击add按钮,输入Name(任意)和Location(插件的URL),点击OK ...
- Java 之 集合框架(JCF)
1.集合框架 a.框架:为了实现某一目的或功能,而预先提供的一系列封装好的.具有继承或实现关系的类与集合 b.集合:①定义:Java中对一些数据结构和算法进行封装,即封装(集合也是一种对象) ②特点: ...