博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C/C++ rand()产生随机数 模拟 掷骰子 小游戏代码
阅读量:6856 次
发布时间:2019-06-26

本文共 2235 字,大约阅读时间需要 7 分钟。

1、源代码如下:

/*! @file  ********************************************************************************  模块名       :  文件名       : tossGame.cpp  相关文件     :  文件实现功能 : 模拟掷骰子的随即过程,同时掷出2个骰子,如果结果和为7或者11则玩家获胜,                如果结果为2则玩家失败;其他结果可以按回车继续,或者输入q结束程序。  作者         : QuNengrong (Qunero)  公司         :  版本         : 1.0  新版         :  --------------------------------------------------------------------------------  多线程安全性 :  异常时安全性 :  --------------------------------------------------------------------------------  备注         :  --------------------------------------------------------------------------------  修改记录     :  日 期          版本        修改人      修改内容  10/08/2011         1.0     QuNengrong      创建  ********************************************************************************  * 版权所有(c) 2011, 保留所有权利  *******************************************************************************/ #include    
#include
#include
#include
int main(){
using namespace std; cout << "Note: press RETURN to play, if you get 7 or 11 you win, if you get 2 you lose\n" " Or you can input q to quit the game!" << endl; char ch; int a,b; srand(time(0)); ch=getchar(); while(ch != 'q'){
a = random() % 6; if( a==0 ) a=6; b = random() % 6; if(b == 0) b=6; switch(a+b){
case 7: case 11: cout << "You get " << a+b; cout << ", and You Win!\n"; return 0; case 2: cout << "You get " << 2; cout << ", You lose!\n"; return 0; default: cout << "You get " << a+b; cout << " Press return to go on\n"; } ch=getchar(); } }

2、运行效果如下:

$ ./tossGame Note: press RETURN to play, if you get 7 or 11 you win, if you get 2 you lose       Or you can input q to quit the game! You get 5 Press return to go on You get 6 Press return to go on You get 8 Press return to go on You get 9 Press return to go on You get 4 Press return to go on You get 6 Press return to go on You get 7, and You Win!

3、原文参考:

 

可惜网易blog对代码支持的不好啊,代码全乱了~~

转载于:https://www.cnblogs.com/QuLory/archive/2011/10/26/linux_C_CPP_funny_random_toss_game.html

你可能感兴趣的文章
目前的.NET(C#)世界里,主流的ORM框架
查看>>
Java 基础知识点
查看>>
Linux--忘记MySQL密码的解决方法和输入mysqld_safe --skip-grant-tables &后无法进入MySQL的解决方法...
查看>>
vimperator
查看>>
(原創) 如何使用boost::array? (C/C++) (template) (boost)
查看>>
Oracle for Windows 相关下载地址
查看>>
电子书下载:Microsoft Silverlight 4 Business Application Development: Beginners Guide
查看>>
arm 裸奔经验
查看>>
.Net下RabbitMQ的使用(2) -- 发送接收消息
查看>>
2009年云数据库的开发和应用前景(转载)
查看>>
Some key terms of Data Mining
查看>>
咏南中间件更新日志
查看>>
9-1让我想起了学生时代~~
查看>>
谷歌用户体验设计准则
查看>>
LaTeX中的数学公式
查看>>
计算二重定积分
查看>>
asp.net Web项目中c#读取域用户名的方法
查看>>
重绘TabControl
查看>>
Python Decorator
查看>>
delphi debug release区别是什么?
查看>>