zoukankan      html  css  js  c++  java
  • 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;

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

  • 相关阅读:
    添加一个用户到指定用户组: gpasswd –a 用户名 组名 usermod –G 组名 用户名
    MySQL创建用户和授权
    shell
    启动脚本
    TODO
    mysql表分区
    mysql导入千万级数据实操
    mysql快速保存插入大量数据一些方法总结(转)
    MySQL存储过程插入数据过慢处理方法(转)
    mysql备份删库建库导入库
  • 原文地址:https://www.cnblogs.com/yunerlalala/p/5471304.html
Copyright © 2011-2022 走看看