2014-03-31 112 views
0

我已經完成了前兩種模式的成功,這是我正在研究的數學遊戲。我掙扎與十六進制到十進制部分雖然。隨機並不像我希望的那樣隨機。我希望十六進制代碼能夠在不同位置使用更多隨機字母:而不是同一個地方只有一個字母。此外,我想保持字符輸出的數量至少爲7.C++數學遊戲十六進制到十進制問題

有人可以向我解釋我的代碼中的缺陷,給我一個工作的例子,或在正確的方向一些指導?

請幫助我,我的唯一的希望!

下面是遊戲的圖象爲止,

enter image description here

以下是其中我在與一個問題的代碼的部分。它不喜歡類或任何東西,只是一個簡單的函數原型,因爲我對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'; 

    } 


} 
+2

您通常應該在程序開始時調用'srand'一次,而不是在生成隨機數的函數內調用'srand'。 –

回答

0

如果這是我的問題是有一個一個陣列充分的所有可用字符,然後有一個功能而產生與最大長度,像這樣的字符串,我會做。

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在hex中不是一個有效的字符,原因很簡單,它不能從1,5中區分出來。這也是爲了什麼。我不願意幫你欺騙一項任務,雖然這還沒有經過測試,並且可能顯然被剽竊,你只是使用它的整個銷售我不太關心。

+0

我正在學習任何機構之外的C++。我寫了整個程序,這只是我正在努力的一部分。 – AEGIS