我已經完成了前兩種模式的成功,這是我正在研究的數學遊戲。我掙扎與十六進制到十進制部分雖然。隨機並不像我希望的那樣隨機。我希望十六進制代碼能夠在不同位置使用更多隨機字母:而不是同一個地方只有一個字母。此外,我想保持字符輸出的數量至少爲7.C++數學遊戲十六進制到十進制問題
有人可以向我解釋我的代碼中的缺陷,給我一個工作的例子,或在正確的方向一些指導?
請幫助我,我的唯一的希望!
下面是遊戲的圖象爲止,
以下是其中我在與一個問題的代碼的部分。它不喜歡類或任何東西,只是一個簡單的函數原型,因爲我對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'一次,而不是在生成隨機數的函數內調用'srand'。 –