C++一个猜数游戏程序
2010-12-03 22:29
441 查看
// diceGame.cpp : 定义控制台应用程序的入口点。 //**************************************************************** //diceGame.cpp //功能:一个猜数游戏程序,主要用来演示选择语句和循环语句的使用 //**************************************************************** #include "stdafx.h" #include <iostream> //使用cin,cout,endl #include <cstdlib> //rand() srand(seed) #include <ctime> //seed=time() ,srand(seed) using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int randNum1,randNum2,randNum3; //用于存放3个椭机数 int rsum; int count; bool flag; int n; char c; //game note: cout<<"here will be an exciting game ^_^ "<<endl; cout<<"There are three dice casting in a black box."; cout<<"and each face of one dice shows points from 1 to 6,"; cout<<" please guess the sum of the three dice! "<<endl; cout<<" Note that you have three times to guess at most."<<endl; flag=true; while(flag) { cout<<"Now the game begin!!!"<<endl; rsum=0; srand((unsigned)time(NULL)); //time函数获取系统时间 //srand函数设置道生椭机数的种子 randNum1=rand()%6+1; randNum2=rand()%6+1; randNum3=rand()%6+1; rsum=randNum1+randNum2+randNum3; //gamer 最多有3次机会输入答案 for(count=0;count<3;count++) { cout<<"Please enter the sum you guess:"; cin>>n; if(n<rsum && count<2) { cout<<"Too small number! Enter bigger one!"<<endl; } else if(n>rsum && count<2) { cout<<"Too big number! Enter smaller one!"<<endl; } else if(n==rsum) { cout<<"How smart you are! YOU WIN!!!"<<endl; cout<<"The points one the three dice are:"<<endl; cout<<randNum1<<" "<<randNum2<<" "<<randNum3<<endl; break; } } if(count==3) { cout<<endl; cout<<"Sorry,You lose!"<<endl; cout<<randNum1<<" "<<randNum2<<" "<<randNum3<<endl; cout<<"sum="<<rsum<<endl; } //询问是否再玩一局 cout<<endl<<"Do you want to play the game again?"<<endl; cout<<"Please enter Y or y to play again,enter other letter to exit the game:"<<endl; cin>>c; if(c=='Y' || c=='y') { flag=true; } else { flag=false; } } //暂停操作 char str; std::cin>>str; return 0; }
相关文章推荐
- C++一个猜数游戏程序
- c/c++程序之_猜数游戏
- C实现一个比较简单的猜数游戏
- C++猜数字游戏的程序,用srand()函数才生随机数
- 【C++ in Qt5】一个简单的通讯录程序,支持文件存取
- 解答: 一个简单的c++程序
- 临时起异,要进入C++领域耍一个程序
- 为自己的C++程序写了一个朴素的TDD
- C/C++拾遗录--关于一个C语言小程序的分析
- Linux下,使用C/C++一个简单的消息处理程序
- 有一个数列(1)循环输出数列的值(2)求数列中所有数值的和(3)猜数游戏:从键盘中任意输入一个数据,判断数列中是否包含次数
- C++写的书上一个简单的电话薄程序
- 每日一个C++小程序(六)--基本菜单程序
- C++游戏程序 - 龟兔赛跑Ⅳ
- 如何在一个已有的C++工程中加入cuda程序
- 终端下编译运行一个简单C++程序流程
- 一个强悍的算24点游戏的PHP程序(递归函数的妙用)
- 一个linux下c++程序
- 一个C/C++ 命令行参数处理的程序
- 一个C++多继承带来的游戏开发陷阱