// 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++程序性能2
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
猜数字小程序源码安卓
-
C++小练习:猜数游戏
编写猜数小游戏。要去玩家从数列中猜一个数,每猜对一个加一分,并(使用文件)记录玩
#include ios 斐波那契数列 -
C语言——用简单语句制作一个猜数小游戏
基于C语言随机数的猜数游戏。
C语言随机数 rand() srand() C语言随机数游戏 C语言随机数猜数游戏 -
c语言猜数游戏
任务描述本关任务:猜数游戏。相关知识为了完成本关任务,你需要掌握:C 语言基础。猜数游戏
c语言 #include 语言基础 -
一个猜数字游戏
猜字游戏
猜字游戏