标签:
#include <iostream.h> #include <stdlib.h> #include <time.h> int rollDice(void); void main() { enum Status{CONTINUE,WON,LOST}; int sum,myPoint,num = 1; Status gameStatus; srand(time(NULL)); //init only one time sum = rollDice(); //first roll of the dice switch(sum) { case 7: case 11: gameStatus = WON;//win on first roll break; case 2: case 3: case 12: gameStatus = LOST; //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) //from 2 to 7,6 times loop { sum = rollDice(); if(sum == myPoint) //win by making point { gameStatus = WON; break; } } 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 7 { 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; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/laoduan_78/article/details/49225987