/**********************************************************/ //Function : main,投骰子游戏 //parm : //comment : //return : void //Author : //date : /**********************************************************/ #include <iostream.h> #include <stdlib.h> #include <time.h> int rollDice(void); void main() { enum Status{CONTINUE,WON,LOST}; //Here use the enumeration type,please to study yourself int sum,myPoint,num = 1; Status gameStatus; srand(time(NULL)); //set rand seed using time of system sum = rollDice(); //first roll of the dice switch(sum) { case 7: case 11: gameStatus = LOST;//win on first roll break; case 2: case 3: case 12: gameStatus = WON; //lose on first roll break; default: gameStatus = CONTINUE; myPoint = sum; //remember point cout<<"Point is "<<myPoint<<endl; break; //optional } while(gameStatus == CONTINUE) //keeping rolling { num++; if(num <= 7) { sum = rollDice(); if(sum == myPoint) //win by making point gameStatus = WON; } else gameStatus = LOST; } if(gameStatus == WON) cout<<"Player wins!"<<endl; else cout<<"Player Loses!"<<endl; return; } int rollDice() //get sum of two random numbers between 1 and 6 { int die1,die2,workSum; die1 = 1 + rand() % 6; //first number die2 = 1 + rand() % 6; //second number workSum = die1 + die2; cout<<"Player rolled "<<die1<<" + "<<die2<<" = "<<workSum<<endl; return workSum; }
第二次作业参考代码
最新推荐文章于 2019-07-02 22:36:21 发布