/*
* Guess number game
* Program: David Zhang ( Feb - 03 - 2008 )
* E-mail: Crispgm@gmail.com
* WARNING:
* This source file is distributed under GNU General Public Licence
* PLEASE READ IT CAREFULLY
*/
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
void Generate( int *n );
bool IsExist ( int x, int *n );
void Compare ( int x, int *n, int index );
int main()
{
/* FOUR digit array generate by program */
int n[4] = { 0 };
/* player input this */
int in;
/* program use compare to compare with input */
int compare;
enum status{ WON, LOST, CONTINUE };
status gameStatus = CONTINUE;
cout << "Guess number V0.2\n";
Generate( n );
compare = n[0] * 1000 + n[1] * 100 + n[2] * 10 + n[3];
for( int i = 0; i < 8; i++ )
{
cout << endl << "Input what you guess: ";
cin >> in;
if( compare == in )
{
gameStatus = WON;
break;
}
Compare( in, n, i );
}
if( gameStatus == WON )
cout << "You won!" << endl;
else
cout << "You lost!" << endl;
return 0;
}
void Generate( int *n )
/* generate an array n */
{
int x, i = 0;
srand(time(NULL));
while( i < 4 )
{
x = rand() % 9 + 1;
if( !IsExist( x, n ) )
n[ i++ ] = x;
}
}
bool IsExist( int x, int *n )
/* This function is test where x is exist in array of n */
{
for( int i = 0; i < 4; i++ )
if( x == n[i] )
return true;
return false;
}
void Compare( int x, int *n, int index )
{
int A = 0, B = 0;
int c[4], temp;
c[0] = (int)( x/1000 );
temp = x - c[0] * 1000;
c[1] = (int)( temp/100 );
temp = temp - c[1] * 100;
c[2] = (int)( temp/10 );
c[3] = temp - c[2] * 10;
for( int i = 0; i < 4; i++ )
{
for( int j = 0; j < 4; j++ )
{
if( n[i] == c[j] ){
if( i == j )
A++;
else
B++;
}
}
}
cout << index + 1 << " "
<< c[0] << c[1] << c[2] << c[3]
<< " " << A << "A"
<< B << "B"
<< endl;
return;
}
分享到:
相关推荐
FLOW-3D-v11-1-user-manual_难得的FLOW-3D原版手册_flow_flow3dv11user_flow
很难得的信息标准化工作的重要参考文献,我找了很长时间
投资担保培训史上---非常难得.ppt
投资担保培训(史上最全---非常难得).pptx
多难得好东西 JK-XU多路温度巡检仪软件
这是一份MIPI C-PHY介绍,可帮助大家快速认识C-PHY,与D-PHY对比来看更能明白。十分难得的资料。
机器人聊天代码-亲测可以-研究机器人聊天难得的代码
无论你是个整天黏在肥皂剧前的宅人还是坐电脑面前的白领,一个澳大利亚的研究表明每坐一个小时经过短暂休息可以减轻上面提到的危害。短暂休息不是指去健身,虽然健身有其好处,但是对于抵消久坐危害却没有多少帮助。...
经典国外教材《数字通信-基础与应用》 习题解答 非常难得
电脑拥有免费无线雷达-Easy V1.05 ‖汉化绿色版‖难得的一款精品软件
这是一个压缩包,里面是东南大学2009年数模一等奖获奖论文,真实性, 可以在获奖名单中查询。对于准备数学建模美国赛的同学绝对有用,资源 难得,要的同学抓紧速下! -------------------------------------------...
这是一个压缩包,里面是东南大学2009年数模一等奖获奖论文,真实性, 可以在获奖名单中查询。对于准备数学建模美国赛的同学绝对有用,资源 难得,要的同学抓紧速下! -------------------------------------------...
这是一个压缩包,里面是东南大学2009年数模一等奖获奖论文,真实性, 可以在获奖名单中查询。对于准备数学建模美国赛的同学绝对有用,资源 难得,要的同学抓紧速下! -------------------------------------------...
这是一个压缩包,里面是东南大学2009年数模二等奖获奖论文,真实性, 可以在获奖名单中查询。对于准备数学建模美国赛的同学绝对有用,资源 难得,要的同学抓紧速下! -------------------------------------------...
在一个区域内有一个#else ,但是没有一个#if, #ifdef 或#ifndef。 -4-太多的#if 嵌套层次 检测出一个内部的限制,有关#if的嵌套层次 (包括 #ifdef和#ifndef)。 -5-太多的#endif 出现一个#endif ,但不是#if 或 ...
传统的模拟器玩微信太麻烦了,现在推荐这个的原理其实是 里面封装了一个浏览器进去,登录认证就是web版微信,但是可以有消息提示,且不用担心安全问题。 分享给大家,经常在电脑旁的话还是比手机打字方便许多的。
矩阵取数游戏 (game.pas/c/cpp) 【问题描述】 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数。游戏规则 ... https://vijos.org/discuss/5109fde44e4112501400074b ...