• C++*游戏


    #include <ctime>
    #include <cstdlib>
    #include<stdio.h>
    int TimeRand()//根据系统时间随机取1-6中的数字
    {
    int t; //seed
    srand((unsigned)time(NULL));
    t = rand() % 6+ 1; // random number 1-10
    return t;
    }
    int game()
    {
    srand((unsigned)time(NULL));
    int *a=new int;
    int *b=new int;
    int *sum=new int;
    printf("请掷第1次个骰子");
    a[0]=rand()% 6+ 1;
    b[0]=rand()% 6+ 1;
    sum[0]=a[0]+b[0];
    printf("结果为:%d和%d ",a[0],b[0]);
    if(sum[0]==7||sum[0]==11)
    return 1;
    else if(sum[0]==2||sum[0]==3||sum[0]==12)
    return 0;
    else
    {
    int count=1;
    bool IsEnd=false;
    while(!IsEnd)
    {
    printf("请掷第%d次个骰子",count+1);
    a[count]=rand()% 6+ 1;
    b[count]=rand()% 6+ 1;
    sum[count]=a[count]+b[count];
    printf("结果为:%d和%d ",a[count],b[count]);
    if(sum[count]==sum[0])
    {
    return 1;
    }
    else if(sum[count]==7)
    {
    return 0;
    }
    else
    {
    count=count+1;
    }
    }
    }
    }
    void main()
    {
    int a=game();
    if(a==1)
    printf("Yes,congratulation! ");
    else
    {
    printf("sorry,you are fail ");
    }
    }

    这里值得自己注意的是:

    随机数的产生。

    一开始想用函数产生随机数的,但是原来函数是这么写的

    int TimeRand()//根据系统时间随机取1-6中的数字
    {
    int t; //seed
    srand((unsigned)time(NULL));
    t = rand() % 6+ 1; // random number 1-10
    return t;
    }

    发现得到的两个随机数是一样的。

    所以现在直接在game函数里写随机数

    srand((unsigned)time(NULL)); 

    a[0]=rand()% 6+ 1;
    b[0]=rand()% 6+ 1;

    这样就可以产生两个不同的随机数啦

  • 相关阅读:
    【HAOI2014】贴海报
    【HAOI2016】食物链
    【NOI2003】银河英雄传
    【HAOI2013】花卉节
    【BZOJ1702】[usaco2007margold]队列平衡
    【网络流24】餐巾
    洛谷 [P1265] 公路修建
    全排列与 康托展开
    洛谷 [P1403] 约数研究
    高精度模板
  • 原文地址:https://www.cnblogs.com/yunerlalala/p/5471304.html
Copyright © 2020-2023  润新知