3_15.cpp
上传用户:zipjojo
上传日期:2009-07-20
资源大小:70k
文件大小:1k
源码类别:

文章/文档

开发平台:

C/C++

  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <stdio.h>
  5. int rolldice(void);
  6. int die1,die2,worksum;
  7. int main()
  8. {
  9. enum status{CONTINUE,WON,LOST};
  10. int sum,mypoint;
  11. enum status gamestatus;
  12. sum=rolldice();
  13. switch (sum)
  14. {
  15. case 7:
  16. case 11: gamestatus=WON;
  17.      break;
  18.     case 2: 
  19. case 3:
  20. case 12: gamestatus=LOST;
  21.      break;
  22. default: gamestatus=CONTINUE;
  23.      mypoint=sum;
  24.  cout<<"point is "<<mypoint<<endl;
  25.  break;
  26. }
  27. while (gamestatus==CONTINUE)
  28. {
  29. sum=rolldice();
  30. if (sum==mypoint)
  31. gamestatus=WON;
  32. else{
  33. if(sum==7)
  34. gamestatus=LOST;}
  35. }
  36. if (gamestatus==WON)
  37. cout <<"player wins"<<endl;
  38. else
  39. cout<<"player loses"<<endl;
  40. return 0;
  41. }
  42. int rolldice(void)
  43. {
  44. time_t timenow;
  45. srand((unsigned)time(&timenow));
  46. //srand((unsigned)time(NULL));
  47. die1=1+rand()%6;
  48. die2=1+rand()%6;
  49. worksum=die1+die2;
  50. cout<<"player rolled  "<<die1<<"+"<<die2<<"="<<worksum<<endl;
  51. return worksum;
  52. }