【发布时间】:2014-05-10 07:01:14
【问题描述】:
我已经成功完成了我正在开发的这款数学游戏的前两种模式。我虽然在十六进制到小数部分的问题上苦苦挣扎。随机并不像我希望的那样随机。我希望十六进制代码可以在不同的位置找到更多随机字母:而不是在同一个地方只有一个字母。此外,我希望将字符输出量保持在 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';
}
}
【问题讨论】:
-
您通常应该在程序开始时调用一次
srand,而不是在生成随机数的函数内部。
标签: c++ math random hex decimal