掷骰子游戏代码
骰子游戏源代码

7.1 程序的源代码★Main()文件: // 文件路径名:dice_game\main.cpp#include "utility.h" // 实用程序软件包#include "dice_game.h" // 骰子游戏int main(void) // 主函数main(void){DiceGame objGame; // 骰子游戏对象objGame.Game(); // 运行游戏system("PAUSE"); // 调用库函数system()return 0; // 返回值0, 返回操作系统}★dice_game.h文件: // 文件路径名: dice_game\dice_game.h #ifndef __DICE_GAME_H__ // 如果没有定义__DICE_GAME_H__#define __DICE_GAME_H__ // 那么定义__DICE_GAME_H__typedef enum{WIN, LOSE, TIE} GameStatus; // WIN:赢,LOSE:输,TIE:平局// 骰子游戏类DiceGame声明class DiceGame{private:// 数据成员:int numOfWin; // 胜利数次int numOfLose; // 失败数次int numOfTie; // 平局数次// 辅助函数int RollDice(); // 模拟投掷一次骰子void Help(); // 获得帮助void Show(); // 显示统计结果public:// 方法声明:DiceGame(); // 无参数的构造函数virtual ~DiceGame(){} // 析构函数void Game(); // 模拟游戏};// 骰子游戏类DiceGame的实现部分DiceGame::DiceGame() // 初始化骰子游戏{numOfWin = 0; // 胜利数次numOfLose = 0; // 失败数次numOfTie = 0; // 平局数次cout << "游戏开始" << endl;}int DiceGame::RollDice() // 模拟投掷一次骰子,返回值为所投掷的两颗骰子向上一面的点数之和{int numOfDice1; // 所投掷的第1颗骰子向上一面的点数int numOfDice2; // 所投掷的第2颗骰子向上一面的点数int sum; // 所投掷的两颗骰子向上一面的点数之和numOfDice1 = 1 + Rand::GetRand(6); // 模拟所投掷骰子1的点数numOfDice2 = 1 + Rand::GetRand(6); // 模拟所投掷骰子2的点数sum = numOfDice1 + numOfDice2; // 计算所投掷两颗骰子向上一面的点数之和cout << "选手掷骰子:" << numOfDice1 << "+" << numOfDice2 << "=" << sum << endl;return sum; // 返回所掷骰子向上一面点数之和}void DiceGame::Help() // 获得帮助{cout << " 游戏者每次投掷两颗骰子,每个骰子是一个正方体,有" << endl; cout << "6面上面分别标有1、2、3、4、5、6个圆点,当骰子停止时" << endl; cout << ",将每个骰子朝上的点的数相加,在第一次投掷骰时, 如果" << endl;cout << "所得到的和为7或11,那么游戏者为赢得胜利; 如果所得到" << endl;cout << "的和为2、3或12,那么游戏者为输掉了;如果和为4、5、6" << endl; cout << "、8、9或10,那么为游戏者的点数;如要想赢得胜利,必" << endl; cout << "须继续投掷骰子,直到取得自已的点数为止, 但是如果投" << endl;cout << "掷出的和为7或投掷6次仍未赚到该点数,则游戏者为输了." << endl << endl;}void DiceGame::Show() // 显示统计结果{cout << "选手游戏统计:" << endl;cout << "获胜" << numOfWin << "次" << endl;cout << "失败" << numOfLose << "次" << endl;cout << "平局" << numOfTie << "次" << endl;}void DiceGame::Game() // 模拟游戏int select = 1;int sum, myPoint;GameStatus status; // 游戏状态Rand::SetRandSeed(); // 设置当前时间为随机数种子Help(); // 获得帮助while(select!=3){cout << endl << endl << "请选择:" << endl;cout << "1. 获得帮助" << endl;cout << "2. 玩一手游戏" << endl;cout << "3. 退出游戏" << endl;cin >> select; // 输入选择if(select!=1&&select!=2&&select!=3) //若输入不是 1,2,3 重新输入{cout<<"请用1,2,3回答"<<endl;continue;}if(1){while(select==1) //输入1,帮助{Help();break;}if(select==2) //输入2{sum=RollDice(); //模拟掷骰子switch(sum){case 7: //掷得7或11胜利case 11:status=WIN;numOfWin++;break;case 2: //投掷得2、3、12,输了 case 3:case 12:status=LOSE;numOfLose++;break;default: //投得其他数值,处于平局 status=TIE;myPoint=sum;numOfTie++;cout<<"点数"<<myPoint<<endl;}while(1){if(status==WIN){cout<<"恭喜你,赢得游戏!"<<endl;break;}if(status==LOSE){cout<<"很遗憾,你输了!"<<endl;break;}while(status==TIE){cout<<"你现在是平局,是否要继续游戏";if(UserSaysYes()){int again;again=RollDice(); //处于平局再次掷骰子if(myPoint==again) //掷得自己的点数,赢得胜利{status=WIN;numOfWin++;break;}else if(again==7) //掷得7,输了{status=LOSE;numOfLose++;break;}else //平局{numOfTie++;if(numOfTie == 6) //平局6次,输了{status=LOSE;cout<<"你已平局6次,";break;}}}}}}else if(select==3) //输入为3{if(UserSaysYes()) //退出游戏break;elseselect=1; //返回游戏}}}Show(); // 显示统计结果}#endif★Utility.h文件:#ifndef __UTILITY_H__ // 如果没有定义__UTILITY_H__ #define __UTILITY_H__ // 那么定义__UTILITY_H__// 实用程序工具包#ifdef _MSC_VER // 表示是Visual C++#if _MSC_VER == 1200 // 表示Visual C++6.0// 标准库头文件#include <string.h> // 标准串和操作#include <iostream.h> // 标准流操作#include <limits.h> // 极限#include <math.h> // 数据函数#include <fstream.h> // 文件输入输出#include <ctype.h> // 字符处理#include <time.h> // 日期和时间函数#include <stdlib.h> // 标准库#include <stdio.h> // 标准输入输出#include <iomanip.h> // 输入输出流格式设置#include <stdarg.h> // 支持变长函数参数#include <assert.h> // 支持断言#else // 其它版本的Visual C++// ANSI C++标准库头文件#include <string> // 标准串和操作#include <iostream> // 标准流操作#include <limits> // 极限#include <cmath> // 数据函数#include <fstream> // 文件输入输出#include <cctype> // 字符处理#include <ctime> // 日期和时间函数#include <cstdlib> // 标准库#include <cstdio> // 标准输入输出#include <iomanip> // 输入输出流格式设置#include <cstdarg> // 支持变长函数参数#include <cassert> // 支持断言using namespace std; // 标准库包含在命名空间std中#endif // _MSC_VER == 1200#else // 非Visual C++// ANSI C++标准库头文件#include <string> // 标准串操作#include <iostream> // 标准流操作#include <limits> // 极限#include <cmath> // 数据函数#include <fstream> // 文件输入输出#include <cctype> // 字符处理#include <ctime> // 日期和时间函数#include <cstdlib> // 标准库#include <cstdio> // 标准输入输出#include <iomanip> // 输入输出流格式设置#include <cstdarg> // 支持变长函数参数#include <cassert> // 支持断言using namespace std; // 标准库包含在命名空间std中#endif // _MSC_VER// 实用函数char GetChar(istream &inStream = cin); // 从输入流inStream中跳过空格及制表符获取一字符bool UserSaysYes(); // 当用户肯定回答(yes)时, 返回true,用户否定回答(no)时,返回false// 函数模板template <class ElemType >void Swap(ElemType &e1, ElemType &e2); // 交换e1, e2之值template<class ElemType>void Display(ElemType elem[], int n); // 显示数组elem的各数据元素值// 实用类class Timer; // 计时器类Timerclass Error; // 通用异常类class Rand; // 随机数类Randchar GetChar(istream &in) // 从输入流in中跳过空格及制表一字符{char ch; // 临时变量while ((ch = in.peek()) != EOF // 文件结束符(peek()函数从输入流中接受1// 字符,流的当前位置不变) && ((ch = in.get()) == ' ' // 空格(get()函数从输入流中接受1字符,流// 的当前位置向后移1个位置) || ch == '\t')); // 制表符return ch; // 返回字符}bool UserSaysYes() // 当用户肯定回答(yes)时, 返回true, 用户否定回答(no)时,返回false{char ch; // 用户回答字符bool initialResponse = true; // 初始回答do{ // 循环直到用户输入恰当的回答为止if (initialResponse) cout << "(y, n)?"; // 初始回答else cout << "用y或n回答:"; // 非初始回答while ((ch = GetChar()) == '\n'); // 跳过空格,制表符及换行符获取一字符initialResponse = false; // 非初始回答} while (ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N');while (GetChar() != '\n'); // 跳过当前行后面的字符if (ch == 'y' || ch == 'Y') return true; // 肯定回答返回trueelse return false; // 否定回答返回false }template <class ElemType >void Swap(ElemType &e1, ElemType &e2) // 交换e1, e2之值{ElemType temp; // 临时变量temp = e1; e1 = e2; e2 = temp; // 循环赋值实现交换e1, e2}template<class ElemType>void Show(ElemType elem[], int n) // 显示数组elem的各数据元素值{for (int i = 0; i < n; i++){ // 显示数组elemcout << elem[i] << " "; // 显示elem[i]}cout << endl; // 换行}// 计时器类Timerclass Timer{private:// 数据成员clock_t startTime;public:// 方法声明Timer(){ startTime = clock(); } // 构造函数, 由当前时间作为开始时间构造对象double ElapsedTime() const // 返回已过的时间{clock_t endTime = clock(); // 结束时间return (double)(endTime - startTime) / (double)CLK_TCK;// 计算已过时间}void Reset(){ startTime = clock(); } // 重置开始时间};// 通用异常类Error #define MAX_ERROR_MESSAGE_LEN 100class Error{private:// 数据成员char message[MAX_ERROR_MESSAGE_LEN]; // 异常信息public:// 方法声明Error(char mes[] = "一般性异常!"){ strcpy(message, mes); }// 构造函数void Show() const{ cout << message << endl; } // 显示异常信息}; // 随机数类Randclass Rand{public:// 方法声明static void SetRandSeed(){ srand((unsigned)time(NULL)); }// 设置当前时间为随机数种子static int GetRand(int n){ return rand() % n; }// 生成0 ~ n-1之间的随机数static int GetRand(){ return rand(); } // 生成0 ~ n-1之间的随机数};#endif。
c++掷骰子

{ c;c','b','a','d'};
char c;
int ques = 0, numques = 5, numcorrect = 0;
cout << "Enter the " << numques << " question tests:" << endl;
if(mydicegame.getstatus()==1)
cout<<"player wins\n";
else
cout<<"player loses\n";
return 0;
}
#include <iostream>
using namespace std;
int getstatus(){return gamestatus;}
int getmypoint(){return mypoint;}
int getsum(){return sum;}
void setstatus(int gs){ gamestatus=gs;}
void setmypoint(int mp){ mypoint=mp;}
dicegame()
{
cout<<"按回车开始第一轮投骰子 ";
getchar();
}
void rolldice();
void nextroll();
else
{ cout<< " Score "<<float(numcorrect)/numques*100<< "%";
CC++实现投骰子游戏

CC++实现投骰⼦游戏我们将要模拟⼀个⾮常流⾏的游戏——掷骰⼦。
骰⼦的形式多种多样,最普遍的是使⽤两个6⾯骰⼦。
在⼀些冒险游戏中,会使⽤5种骰⼦:4⾯、6 ⾯、8⾯、12⾯和20⾯。
聪明的古希腊⼈证明了只有5种正多⾯体,它们的所有⾯都具有相同的形状和⼤⼩。
各种不同类型的骰⼦就是根据这些正多⾯体发展⽽来。
也可以做成其他⾯数的,但是其所有的⾯不会都相等,因此各个⾯朝上的⼏率就不同。
计算机计算不⽤考虑⼏何的限制,所以可以设计任意⾯数的电⼦骰⼦。
我们先从6⾯开始。
我们想获得1~6的随机数。
然⽽,rand()⽣成的随机数在0~ RAND_MAX之间。
RAND_MAX被定义在stdlib.h中,其值通常是 INT_MAX。
因此,需要进⾏⼀些调整,⽅法如下。
1.把随机数求模6,获得的整数在0~5之间。
2.结果加1,新值在1~6之间。
3.为⽅便以后扩展,把第1步中的数字6替换成骰⼦⾯数。
下⾯的代码实现了这3个步骤:#include <stdlib.h> /* 提供rand()的原型 */int rollem(int sides){int roll;roll = rand() % sides + 1;return roll;}我们还想⽤⼀个函数提⽰⽤户选择任意⾯数的骰⼦,并返回点数总和。
/* diceroll.c -- 掷骰⼦模拟程序 *//* 与 mandydice.c ⼀起编译 */#include "diceroll.h"#include <stdio.h>#include <stdlib.h> /* 提供库函数 rand()的原型 */int roll_count = 0; /* 外部链接 */static int rollem(int sides) /* 该函数属于该⽂件私有 */{int roll;roll = rand() % sides + 1;++roll_count; /* 计算函数调⽤次数 */return roll;}int roll_n_dice(int dice, int sides){int d;int total = 0;if (sides < 2){printf("Need at least 2 sides.\n");return -2;}if (dice < 1){printf("Need at least 1 die.\n");return -1;}for (d = 0; d < dice; d++)total += rollem(sides);return total;}该⽂件加⼊了新元素。
投色子游戏

colorConsole.h#include <windows.h>#include <iostream.h>HANDLE initiate();BOOL textout(HANDLE hOutput,int x,int y,WORD wColors[],int nColors,LPTSTR lpszString);colorConsole.cpp#include "colorConsole.h"HANDLE initiate(){HANDLE hOutput;hOutput = GetStdHandle(STD_OUTPUT_HANDLE);return hOutput;}BOOL textout(HANDLE hOutput,int x,int y,WORD wColors[],int nColors,LPTSTR lpszString) {DWORD cWritten;BOOL fSuccess;COORD coord;coord.X = x; // start at first cellcoord.Y = y; // of first rowfSuccess = WriteConsoleOutputCharacter(hOutput, // screen buffer handlelpszString, // pointer to source stringlstrlen(lpszString), // length of stringcoord, // first cell to write to&cWritten); // actual number writtenif (! fSuccess)cout<<"error:WriteConsoleOutputCharacter"<<endl;for (;fSuccess && coord.X < lstrlen(lpszString)+x; coord.X += nColors){fSuccess = WriteConsoleOutputAttribute(hOutput, // screen buffer handlewColors, // pointer to source stringnColors, // length of stringcoord, // first cell to write to&cWritten); // actual number written}if (! fSuccess)cout<<"error:WriteConsoleOutputAttribute"<<endl;return 0;}Main.cpp#include <conio.h>#include <stdlib.h>#include <time.h>#include "colorConsole.h"//投筛子void rolldice(HANDLE hOutput,int n,int col,int row,WORD wColors[]);void main(void){HANDLE handle;WORD wColors[1];int row,col;//初始化handle = initiate();//生成6个不同骰子wColors[0]=FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTENSITY;row=col=2;for (int i=0;i<6;i++)rolldice(handle,i+1,col,row+6*i,wColors);//打印屏幕底部菜单WORD wMenuColors[1];wMenuColors[0]=FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY;textout(handle,1,24,wMenuColors,1, "游戏规则:");textout(handle,11,24,wMenuColors,1,"投掷开始/结束=ENTER;");textout(handle,34,24,wMenuColors,1,"更换游戏者=空格;");textout(handle,53,24,wMenuColors,1,"退出=q.");bool flag=false;int count=1;int sum=0;//随机数的种子srand( (unsigned)time( NULL ));col=15;row=8;//游戏开始while(1){if (_kbhit()){int ch=_getch();if (ch==13){flag=!flag;if (!flag){wColors[0]=FOREGROUND_RED|FOREGROUND_INTENSITY;rolldice(handle,i+1,row,col,wColors);//记录游戏者和点数char buf[20];itoa(count,buf,10);textout(handle,1,13+2*count,wMenuColors,1,buf);textout(handle,3,13+2*count,wMenuColors,1,"点数:");sum+=i+1;itoa(sum,buf,10);textout(handle,9,13+2*count,wMenuColors,1,buf);}}else if (ch==32)//更换游戏者{sum=0;count++;}else if (ch=='q' || ch=='Q')break;}if (flag)//随机投筛子{i=rand() % 6;wColors[0]=FOREGROUND_RED|FOREGROUND_INTENSITY;rolldice(handle,i+1,row,col,wColors);Sleep(100);wColors[0]=0;rolldice(handle,i+1,row,col,wColors);}}}void rolldice(HANDLE hOutput,int n,int col ,int row,WORD wColors[]){switch(n){case 1: textout(hOutput,row+1,col+1,wColors,1,"●");break;case 2: textout(hOutput,row+1,col, wColors,1,"●");textout(hOutput,row+1,col+2,wColors,1,"●");break;case 3: textout(hOutput,row, col+2,wColors,1,"●");textout(hOutput,row+1,col+1,wColors,1,"●");textout(hOutput,row+2,col, wColors,1,"●");break;case 4: textout(hOutput,row, col, wColors,1,"●");textout(hOutput,row, col+2,wColors,1,"●");textout(hOutput,row+2,col, wColors,1,"●");textout(hOutput,row+2,col+2,wColors,1,"●");break;;case 5: textout(hOutput,row, col, wColors,1,"●");textout(hOutput,row, col+2,wColors,1,"●");textout(hOutput,row+1,col+1,wColors,1,"●");textout(hOutput,row+2,col, wColors,1,"●");textout(hOutput,row+2,col+2,wColors,1,"●");break;case 6: textout(hOutput,row, col, wColors,1,"●");textout(hOutput,row, col+1,wColors,1,"●");textout(hOutput,row, col+2,wColors,1,"●");textout(hOutput,row+2,col, wColors,1,"●");textout(hOutput,row+2,col+1,wColors,1,"●");textout(hOutput,row+2,col+2,wColors,1,"●");break;default:cout<<"投掷骰子失败!"<<endl;}}。
C语言的一个掷骰子游戏

{
if(a==b && a==c)
{
printf("豹子%d\n",a);
return 3;
}
else
{
if(a+b+c > 11)
{
printf("大\n");
return 1;
if(x==y)
{
if(y==1 || y==2)
{
rmb = 2*RMB;
PlayerMoney=PlayerMoney + rmb;
BankerMoney=BankerMoney - rmb;
printf("玩儿家赢,玩儿家赢取%d个元宝,闲家当前元宝数%d个,庄家当前元宝数%d个\n\n",rmb,PlayerMoney,BankerMoney);
BankerMoney=BankerMoney + rmb;
printf("玩儿家输,玩儿家输取%d个元宝,闲家当前元宝数%d个,庄家当前元宝数%d个\n\n",rmb,PlayerMoney,BankerMoney);
}
}
{
printf("庄家输光所有的钱,并欠玩儿家%d个元宝,请尽快偿还,否则后果自负!\n",abs(BankerMoney));
}
break;
}
getch(); //解决kbhit函数在内存地址保存的字符影响下面程序的问题
while(1) //询问继续游戏与否
{
if(PlayerMoney<10 && PlayerMoney>=0)
基于C++实现掷双骰游戏的示例代码

基于C++实现掷双骰游戏的⽰例代码在最流⾏的博彩游戏中有⼀种名为“掷双骰”(craps)的骰⼦游戏,这种游戏在世界各地的娱乐场所和⼤街⼩巷⾮常受欢迎。
游戏的规则很简单:玩家掷两个骰⼦。
每个骰⼦有六⾯,分别含有1、2、3、4、5和6个点。
掷完骰⼦后,计算两个朝上的⾯的点数之和。
1、如果⾸次投掷的点数总和是7或者11,那么玩家赢;2、如果⾸次投掷的点数只和事2、3或者12(称为"craps"),那么玩家输(即庄家赢);3、如果⾸次投掷的点数只和事4、5、6、7、8、9或者10,那么这个和就成为玩家的“⽬标点数”。
要想赢的话,玩家必须连续的掷骰⼦直到点数与这个⽬标点数相同为⽌,即“得到了点数”。
但在得到点数前,如果掷到的是7,就会输掉。
#include <iostream>#include <cstdlib>#include <ctime>using namespace std;unsigned int rollDice();int main() {enum Status {CONTINUE, WON, LOST}; //这个就是⾃定义⼀个变量类型,就类似于int,double这种,即这⾥的说⽩了就是设置⼀个枚举类变量类//⽽这个类的关键字就是Status,以这个类型定义的变量只能取枚举内的⼏个值,⽽这⼏个值⼜对应了数字。
srand(static_cast<unsigned int>(time(0)));unsigned int myPoint = 0;Status gameStatus = CONTINUE;unsigned int sumOfDice = rollDice();switch (sumOfDice) {case 7:case 11:gameStatus = WON;break;case 2:case 3:case 12:gameStatus = LOST;break;default:gameStatus = CONTINUE;myPoint = sumOfDice;cout << "Point is " << myPoint << endl;break;};while (gameStatus == CONTINUE){sumOfDice = rollDice();if (sumOfDice == myPoint)gameStatus = WON;else if (sumOfDice == 7)gameStatus = LOST;}if (gameStatus == WON)cout << "Player wins" << endl;elsecout << "Player lose" << endl;}unsigned int rollDice(){unsigned int die1 = 1 + rand() % 6;unsigned int die2 = 1 + rand() % 6;unsigned int sum = die1 + die2;cout << "Player rolled: " << die1 << " + " << die2<< " = " << sum << endl;return sum;}这个是我拿来当作笔记的,主要是为了记住这么个问题,当我要想循环的实现博彩游戏并且统计输赢的时候,会⾃然⽽然的想到在外部套⼀个for循环去执⾏,但是这样就会涉及到⼀个问题,即随机数的⽣成,按照我最开始的理解是随着循环的进⾏,给srand提供的seed不同(也就是实参time(0))就会在每次循环都产⽣不同的随机序列。
c语言掷骰子课程设计

c语言掷骰子课程设计一、课程目标知识目标:1. 学生能够理解并掌握C语言中随机数生成的概念和原理。
2. 学生能够运用C语言的基本语法,编写实现掷骰子功能的程序代码。
3. 学生能够了解并解释掷骰子程序中的变量、循环和条件判断语句的作用。
技能目标:1. 学生能够运用所学知识,独立设计并实现一个模拟掷骰子的C语言程序。
2. 学生能够通过调试程序,解决代码中出现的常见错误,提高问题解决能力。
3. 学生能够运用合作学习的方法,与同伴共同分析、讨论并优化程序设计。
情感态度价值观目标:1. 学生能够养成积极思考、勇于探索的学习态度,对编程产生兴趣。
2. 学生能够认识到编程在实际生活中的应用,增强学以致用的意识。
3. 学生能够在团队协作中,学会尊重他人意见,培养良好的沟通能力和团队精神。
本课程针对高年级学生,结合C语言基础知识,以掷骰子为案例,培养学生的编程兴趣和实际操作能力。
课程目标旨在使学生掌握随机数生成和基本语法运用,提高问题解决能力,同时注重培养学生的合作精神和团队意识。
通过本课程的学习,为学生后续深入学习C语言打下坚实基础。
二、教学内容1. C语言基础知识回顾:变量定义、数据类型、运算符、输入输出函数。
2. 随机数生成:介绍rand()函数和srand()函数的用法,理解伪随机数的概念。
3. 循环结构:复习for循环和while循环的使用,重点讲解循环控制流程。
4. 条件判断:复习if-else语句,讲解其在程序中的应用。
5. 掷骰子程序设计:a. 设计思路分析:明确掷骰子功能的实现需求,分析程序结构。
b. 编写代码:根据分析,编写掷骰子程序,包括主函数、生成随机数的函数等。
c. 调试与优化:介绍调试方法,引导学生发现并解决程序中的问题。
6. 案例分析与拓展:分析教材中相关案例,拓展学生思维,提高编程技能。
教学内容依据课程目标,按照教材章节顺序进行安排。
在教学过程中,注重理论与实践相结合,使学生能够扎实掌握C语言基础知识,并能运用所学设计简单的程序。
骰子游戏源代码

7.1 程序的源代码★Main()文件: // 文件路径名:dice_game\main.cpp#include "utility.h" // 实用程序软件包#include "dice_game.h" // 骰子游戏int main(void) // 主函数main(void){DiceGame objGame; // 骰子游戏对象objGame.Game(); // 运行游戏system("PAUSE"); // 调用库函数system()return 0; // 返回值0, 返回操作系统}★dice_game.h文件: // 文件路径名: dice_game\dice_game.h #ifndef __DICE_GAME_H__ // 如果没有定义__DICE_GAME_H__#define __DICE_GAME_H__ // 那么定义__DICE_GAME_H__typedef enum{WIN, LOSE, TIE} GameStatus; // WIN:赢,LOSE:输,TIE:平局// 骰子游戏类DiceGame声明class DiceGame{private:// 数据成员:int numOfWin; // 胜利数次int numOfLose; // 失败数次int numOfTie; // 平局数次// 辅助函数int RollDice(); // 模拟投掷一次骰子void Help(); // 获得帮助void Show(); // 显示统计结果public:// 方法声明:DiceGame(); // 无参数的构造函数virtual ~DiceGame(){} // 析构函数void Game(); // 模拟游戏};// 骰子游戏类DiceGame的实现部分DiceGame::DiceGame() // 初始化骰子游戏{numOfWin = 0; // 胜利数次numOfLose = 0; // 失败数次numOfTie = 0; // 平局数次cout << "游戏开始" << endl;}int DiceGame::RollDice() // 模拟投掷一次骰子,返回值为所投掷的两颗骰子向上一面的点数之和{int numOfDice1; // 所投掷的第1颗骰子向上一面的点数int numOfDice2; // 所投掷的第2颗骰子向上一面的点数int sum; // 所投掷的两颗骰子向上一面的点数之和numOfDice1 = 1 + Rand::GetRand(6); // 模拟所投掷骰子1的点数numOfDice2 = 1 + Rand::GetRand(6); // 模拟所投掷骰子2的点数sum = numOfDice1 + numOfDice2; // 计算所投掷两颗骰子向上一面的点数之和cout << "选手掷骰子:" << numOfDice1 << "+" << numOfDice2 << "=" << sum << endl;return sum; // 返回所掷骰子向上一面点数之和}void DiceGame::Help() // 获得帮助{cout << " 游戏者每次投掷两颗骰子,每个骰子是一个正方体,有" << endl; cout << "6面上面分别标有1、2、3、4、5、6个圆点,当骰子停止时" << endl; cout << ",将每个骰子朝上的点的数相加,在第一次投掷骰时, 如果" << endl;cout << "所得到的和为7或11,那么游戏者为赢得胜利; 如果所得到" << endl;cout << "的和为2、3或12,那么游戏者为输掉了;如果和为4、5、6" << endl; cout << "、8、9或10,那么为游戏者的点数;如要想赢得胜利,必" << endl; cout << "须继续投掷骰子,直到取得自已的点数为止, 但是如果投" << endl;cout << "掷出的和为7或投掷6次仍未赚到该点数,则游戏者为输了." << endl << endl;}void DiceGame::Show() // 显示统计结果{cout << "选手游戏统计:" << endl;cout << "获胜" << numOfWin << "次" << endl;cout << "失败" << numOfLose << "次" << endl;cout << "平局" << numOfTie << "次" << endl;}void DiceGame::Game() // 模拟游戏int select = 1;int sum, myPoint;GameStatus status; // 游戏状态Rand::SetRandSeed(); // 设置当前时间为随机数种子Help(); // 获得帮助while(select!=3){cout << endl << endl << "请选择:" << endl;cout << "1. 获得帮助" << endl;cout << "2. 玩一手游戏" << endl;cout << "3. 退出游戏" << endl;cin >> select; // 输入选择if(select!=1&&select!=2&&select!=3) //若输入不是 1,2,3 重新输入{cout<<"请用1,2,3回答"<<endl;continue;}if(1){while(select==1) //输入1,帮助{Help();break;}if(select==2) //输入2{sum=RollDice(); //模拟掷骰子switch(sum){case 7: //掷得7或11胜利case 11:status=WIN;numOfWin++;break;case 2: //投掷得2、3、12,输了 case 3:case 12:status=LOSE;numOfLose++;break;default: //投得其他数值,处于平局 status=TIE;myPoint=sum;numOfTie++;cout<<"点数"<<myPoint<<endl;}while(1){if(status==WIN){cout<<"恭喜你,赢得游戏!"<<endl;break;}if(status==LOSE){cout<<"很遗憾,你输了!"<<endl;break;}while(status==TIE){cout<<"你现在是平局,是否要继续游戏";if(UserSaysYes()){int again;again=RollDice(); //处于平局再次掷骰子if(myPoint==again) //掷得自己的点数,赢得胜利{status=WIN;numOfWin++;break;}else if(again==7) //掷得7,输了{status=LOSE;numOfLose++;break;}else //平局{numOfTie++;if(numOfTie == 6) //平局6次,输了{status=LOSE;cout<<"你已平局6次,";break;}}}}}}else if(select==3) //输入为3{if(UserSaysYes()) //退出游戏break;elseselect=1; //返回游戏}}}Show(); // 显示统计结果}#endif★Utility.h文件:#ifndef __UTILITY_H__ // 如果没有定义__UTILITY_H__ #define __UTILITY_H__ // 那么定义__UTILITY_H__// 实用程序工具包#ifdef _MSC_VER // 表示是Visual C++#if _MSC_VER == 1200 // 表示Visual C++6.0// 标准库头文件#include <string.h> // 标准串和操作#include <iostream.h> // 标准流操作#include <limits.h> // 极限#include <math.h> // 数据函数#include <fstream.h> // 文件输入输出#include <ctype.h> // 字符处理#include <time.h> // 日期和时间函数#include <stdlib.h> // 标准库#include <stdio.h> // 标准输入输出#include <iomanip.h> // 输入输出流格式设置#include <stdarg.h> // 支持变长函数参数#include <assert.h> // 支持断言#else // 其它版本的Visual C++// ANSI C++标准库头文件#include <string> // 标准串和操作#include <iostream> // 标准流操作#include <limits> // 极限#include <cmath> // 数据函数#include <fstream> // 文件输入输出#include <cctype> // 字符处理#include <ctime> // 日期和时间函数#include <cstdlib> // 标准库#include <cstdio> // 标准输入输出#include <iomanip> // 输入输出流格式设置#include <cstdarg> // 支持变长函数参数#include <cassert> // 支持断言using namespace std; // 标准库包含在命名空间std中#endif // _MSC_VER == 1200#else // 非Visual C++// ANSI C++标准库头文件#include <string> // 标准串操作#include <iostream> // 标准流操作#include <limits> // 极限#include <cmath> // 数据函数#include <fstream> // 文件输入输出#include <cctype> // 字符处理#include <ctime> // 日期和时间函数#include <cstdlib> // 标准库#include <cstdio> // 标准输入输出#include <iomanip> // 输入输出流格式设置#include <cstdarg> // 支持变长函数参数#include <cassert> // 支持断言using namespace std; // 标准库包含在命名空间std中#endif // _MSC_VER// 实用函数char GetChar(istream &inStream = cin); // 从输入流inStream中跳过空格及制表符获取一字符bool UserSaysYes(); // 当用户肯定回答(yes)时, 返回true,用户否定回答(no)时,返回false// 函数模板template <class ElemType >void Swap(ElemType &e1, ElemType &e2); // 交换e1, e2之值template<class ElemType>void Display(ElemType elem[], int n); // 显示数组elem的各数据元素值// 实用类class Timer; // 计时器类Timerclass Error; // 通用异常类class Rand; // 随机数类Randchar GetChar(istream &in) // 从输入流in中跳过空格及制表一字符{char ch; // 临时变量while ((ch = in.peek()) != EOF // 文件结束符(peek()函数从输入流中接受1// 字符,流的当前位置不变) && ((ch = in.get()) == ' ' // 空格(get()函数从输入流中接受1字符,流// 的当前位置向后移1个位置) || ch == '\t')); // 制表符return ch; // 返回字符}bool UserSaysYes() // 当用户肯定回答(yes)时, 返回true, 用户否定回答(no)时,返回false{char ch; // 用户回答字符bool initialResponse = true; // 初始回答do{ // 循环直到用户输入恰当的回答为止if (initialResponse) cout << "(y, n)?"; // 初始回答else cout << "用y或n回答:"; // 非初始回答while ((ch = GetChar()) == '\n'); // 跳过空格,制表符及换行符获取一字符initialResponse = false; // 非初始回答} while (ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N');while (GetChar() != '\n'); // 跳过当前行后面的字符if (ch == 'y' || ch == 'Y') return true; // 肯定回答返回trueelse return false; // 否定回答返回false }template <class ElemType >void Swap(ElemType &e1, ElemType &e2) // 交换e1, e2之值{ElemType temp; // 临时变量temp = e1; e1 = e2; e2 = temp; // 循环赋值实现交换e1, e2}template<class ElemType>void Show(ElemType elem[], int n) // 显示数组elem的各数据元素值{for (int i = 0; i < n; i++){ // 显示数组elemcout << elem[i] << " "; // 显示elem[i]}cout << endl; // 换行}// 计时器类Timerclass Timer{private:// 数据成员clock_t startTime;public:// 方法声明Timer(){ startTime = clock(); } // 构造函数, 由当前时间作为开始时间构造对象double ElapsedTime() const // 返回已过的时间{clock_t endTime = clock(); // 结束时间return (double)(endTime - startTime) / (double)CLK_TCK;// 计算已过时间}void Reset(){ startTime = clock(); } // 重置开始时间};// 通用异常类Error #define MAX_ERROR_MESSAGE_LEN 100class Error{private:// 数据成员char message[MAX_ERROR_MESSAGE_LEN]; // 异常信息public:// 方法声明Error(char mes[] = "一般性异常!"){ strcpy(message, mes); }// 构造函数void Show() const{ cout << message << endl; } // 显示异常信息}; // 随机数类Randclass Rand{public:// 方法声明static void SetRandSeed(){ srand((unsigned)time(NULL)); }// 设置当前时间为随机数种子static int GetRand(int n){ return rand() % n; }// 生成0 ~ n-1之间的随机数static int GetRand(){ return rand(); } // 生成0 ~ n-1之间的随机数};#endif。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
void delayms(unsigned char a) //豪秒延时子程序 { while(--a); //采用while(--a) 不要采用while(a--); 各位可编译一 下看看汇编结果就知道了! } void main() { unsigned char p,m; //m为频率常数变量 unsigned char i=0; TMOD&=0x0f; TMOD|=0x01; TH0=0xd8;TL0=0xef; IE=0x82; play: while(1) { a: p=music_tab[i]; if(p==0x00) { i=0, delayms(10000); goto play;} //如果碰到结束 符,延时1秒,回到开始再来一遍 else if(p==0xff) { i=i+1;delayms(10),TR0=0; goto a;} //若碰到休止符, 延时100ms,继续取下一音符 else {m=music_tab[i++], n=music_tab[i++];} //取频率常数 和 节拍常数 TR0=1; //开定时器1 while(n!=0) Beep=~Beep,delay(m); //等待节拍完成, 通过P1口输出音频(可多声道哦!) TR0=0; //关定时器1 } }