繁体   English   中英

C ++数学游戏十六进制转换为小数

[英]c++ math game hex to decimal issue

我已经成功完成了此数学游戏的前两种模式。 在用十六进制到小数部分作斗争 随机性不如我希望的那样随机。 我希望十六进制代码可以在不同位置引用更多随机字母:而不是在同一位置仅包含一个字母。 另外,我想将字符输出的数量保持在最少7个。

有人可以向我解释我的代码中的缺陷,给我一个有效的例子,或在正确的方向上提供一些指导吗?

请帮助我stackoverflow,

您是我唯一的希望!

这是到目前为止的游戏图片,

在此处输入图片说明

以下是我遇到问题的部分代码。 它不喜欢类或任何东西,只是一个简单的函数原型,因为我对C ++和编程还很陌生。

void gamethree()
{

    float secs;
    secs = 33;
    clock_t delay = secs * CLOCKS_PER_SEC;              
    clock_t start = clock();
    int correct = 0;                                    

while (clock() - start < delay )
{
    srand(time(NULL));

    int hexdigits[15] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
    char hexletters[7] = {'a', 'b', 'c', 'd', 'e', 'f'};
    //
    //enum letters{

    //  hexa = 10, 
    //  hexb = 11, 
    //  hexc = 12, 
    //  hexd = 13, 
    //  hexe = 14, 
    //  hexf = 15
    //};
    //hexa == hexletters[0];
    //hexb == hexletters[1];
    //hexc == hexletters[2];
    //hexd == hexletters[3];
    //hexe == hexletters[4];
    //hexf == hexletters[5];

    int randindex = rand() % 14;
    int randindexx = rand() % 6;
    cout << hexdigits[randindex];
    cout << hexletters[randindexx];


    int hexf[4] = {
        hexdigits[randindexx], 
        hexdigits[randindex], 
        hexletters[randindexx], 
        hexdigits[randindex],
    };


    random_shuffle(begin(hexf), end(hexf));

    for(int hi = 0; hi < 4; ++hi)
        cout << hexf[hi];
        char hexy = 0;
    cin >> hexy;

    //int hexy = 0;
    //cin >> hexy;



    ////int c = (a * b);
    //int d = 0;
    //char choice = 0;

    //cout <<"What does " << a << " * " << b << " equal?" << endl << endl << endl;
    //cout << "\n";

    //do
    //{
    //  cout << "Please enter a number: ";
    //  cin >> d;       
    //  if(d == c)
    //       ++correct;
    //}while(d != c);

    //cout << "\n\nCorrect! " << (a * b) << " is the answer!" << endl << endl;  
}
    cout << "Your score is: " << correct << "\n\n" <<endl;
cout << "Would you like to play again (Y) or (N)?\n\n\n";
}

void hextodec()
{
        float secs;
        secs = 1;
        clock_t delay = secs * CLOCKS_PER_SEC;              
        clock_t start = clock();
        while (clock() - start < delay )
            ;


        char choice = 'z';
        gamethree();
        while(choice != 'n')
        {

        cin >> choice;

        if (choice == 'y')
        {
            cout << "\n\n";
            game();
        }
        else
            choice = 'n';

    }


}

如果这是我的问题,我将要做的就是让一个数组充满所有可用字符,然后具有一个从中生成最大长度的字符串的函数,类似这样。

std::string rand_string(const char* possible, int options, int max_length){
    std::string result; //we're using strings just so we don't have to pass a char*
    int length = rand()%max_length+1; //You may want to try to biase this towards bigger values or just set it at some value

    for (int i=0; i<length; ++i) {
        result[i]=possible[rand()%options]; //simple really
    }

    return result;
}

基本上只是将所有字符放在一个数组中并循环遍历。

几个旁注。 为什么您的hexdigits数组最多可以达到15。15不能作为十六进制中的有效字符,原因很简单,无法将其与1,5区分开。 这也是为了什么。 我不希望能帮助您作弊,尽管这尚未经过测试,而且很显然是窃,如果您只用它整个销售,我就不会太在意。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM