码迷,mamicode.com
首页 > 其他好文 > 详细

面向对象第二次作业参考代码

时间:2015-10-18 11:30:48      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

#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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!