2017-09-15 101 views
-2

我是C++新手,在分配時遇到問題。我無法弄清楚我在計算雙白釘的問題。有人可以幫忙嗎?我可以理解,我有重複計數的問題,因爲我只使用「或」語句,但除此之外,我認爲我需要從白釘中扣除黑釘,但我不知道該怎麼做。C++ Mastermind Double White Peg問題

#include <iostream> 
#include <cstdlib> 
#include <ctime> 

using namespace std; 

int main() { 

    char colors[4]; 

    srand(time(0)); 
    int randomint = (rand() % 5) + 1; 

    for (int i = 0;i<4;i++) { 
     randomint = (rand() % 5) + 1; 

     switch (randomint) { 
     case 1: 
      colors[i] = 'R'; 
      break; 
     case 2: 
      colors[i] = 'B'; 
      break; 
     case 3: 
      colors[i] = 'Y'; 
      break; 
     case 4: 
      colors[i] = 'P'; 
      break; 
     case 5: 
      colors[i] = 'G'; 
      break; 
     case 6: 
      colors[i] = 'Bl'; 
      break; 
     case 7: 
      colors[i] = 'R'; 
      break; 
     case 8: 
      colors[i] = 'O'; 
      break; 
     case 9: 
      colors[i] = 'T'; 
      break; 
     } 
    } 


    char usercolors[4]; 

    cout << "We have our colors!" << endl; 
    cout << endl << endl; 
    int turncounter = 0; 
    while (turncounter != 12) { 
     turncounter++; 

     cout << "Current try: " << turncounter << endl; 

     for (int i = 0;i<4;i++) { 
      cout << "Color " << i << ": "; 
      cin >> usercolors[i]; 
      cout << endl; 
     } 

     for (int i = 0;i<4;i++) { 
      if (usercolors[i] == colors[i]) 
       cout << "Black Peg" << " "; 
     } 

     if (usercolors[0] == colors[1] || 
      usercolors[0] == colors[2] || 
      usercolors[0] == colors[3]) { 
      cout << "White Peg" << " "; 
     } 
     if (usercolors[1] == colors[0] || 
      usercolors[1] == colors[2] || 
      usercolors[1] == colors[3]) { 
      cout << "White Peg" << " "; 
     } 
     if (usercolors[2] == colors[0] || 
      usercolors[2] == colors[1] || 
      usercolors[2] == colors[3]) { 
      cout << "White Peg" << " "; 
     } 
     if (usercolors[3] == colors[0] || 
      usercolors[3] == colors[1] || 
      usercolors[3] == colors[2]) 
     { 
      cout << "White Peg" << " "; 
     } 

     cout << endl << endl; 

     if (usercolors[0] == colors[0] && 
      usercolors[1] == colors[1] && 
      usercolors[2] == colors[2] && 
      usercolors[3] == colors[3]) 
     { 
      cout << "You win! Number of tries: " << turncounter << endl; 
      turncounter = 12; 
     } 
     else { 
      cout << "Try Again!" << endl << endl; 
     } 

    } 
    if (turncounter == 12) { 
     cout << "Sorry, you are incorrect!" << endl; 
     cout << "Answer: "; 
     cout << "Color 1: " << colors[0] << "\t" << "Color 2: " << colors[1] << "\t" << "Color 3: " << colors[2] << "\t" << "Color 4: " << colors[3] << "\t" << endl; 
    } 

    cin.get(); 
    cin.get(); 
    return 0; 
} 
+0

'(RAND()%5)+ 1'絕不會產生6,7,8或9 – rustyx

+0

這不應該工作顏色[I] = 'BL';顏色是字符,你把兩個字符在單引號。用大寫字母B和小寫字母b代替?爲黑色和藍色。或者使用枚舉類型。 – Rob

+3

用一個調試器逐步完成,看看第一個會發生什麼。 – John

回答

0

當您計算白色掛釘時,必須忽略所有檢測到黑色掛釘的位置。因此,在檢測到黑釘後,您試圖通過檢測來檢測白釘:

if (usercolors[0] == colors[1] || 
    usercolors[0] == colors[2] || 
    usercolors[0] == colors[3]) { 
    cout << "White Peg" << " "; 
} 

正如您已經注意到的,這種方法會導致雙重計數。舉一個例子:祕密圖案在第一個位置包含一個綠色釘。現在你輸入你的猜測:第一個位置是綠色,第四個是綠色。你的代碼會在第一個位置檢測到一個黑釘,並且(至少)兩個額外的白釘,這不是它應該工作的方式。在計算白色掛釘和所有已檢測到的白色掛釘時,必須忽略黑色掛釘位置。

所以你必須修改你的代碼。通過將檢測到的黑色掛鉤位置寫入地圖(稱爲match_map),並在第一次白色掛鉤匹配(也必須寫入列表)後留下內部循環,可以避免雙白色掛鉤計數。請注意,您的顏色列表中有兩次「R」。 'Bt'作爲字符不能工作,我用'b'替換它。你可以通過使用顏色數組來完成沒有開關()的操作,然後一個循環就足以處理祕密模式。

避免使用名稱空間標準; - 請看here的解釋 - ,因爲它被認爲是不好的實踐,並可能導致更多的問題。

#include <iostream> 
#include <cstdlib> 
#include <ctime> 

enum { 
    PEG_NO_MATCH, 
    PEG_BLACK, 
    PEG_WHITE, 
}; 

int main() { 
    char colors[4], usercolors[4], match_map[4]; 
    const char rand_colors[8] = { 'R', 'B', 'Y', 'P', 'G', 'b', 'O', 'T' }; // B -> Black, b -> Blue 
    int turncounter = 0, black_cnt, white_cnt; 

    std::srand(time(0)); 

    for (int i = 0; i < 4; i++) colors[i] = rand_colors[std::rand() % 8]; 

    std::cout << "We have our colors!" << std::endl << std::endl << std::endl; 

    while (turncounter != 12) { 
     turncounter++; 

     std::cout << "Current try: " << turncounter << std::endl; 

     black_cnt = 0; 
     white_cnt = 0; 

     // Get input and count black pegs 
     for (int i = 0; i < 4; i++) { 
      std::cout << "Color " << i << ": "; 
      std::cin >> usercolors[i]; 
      std::cout << std::endl; 

      if (usercolors[i] == colors[i]){ 
       black_cnt++; 
       match_map[i] = PEG_BLACK; 
      }else{ 
       match_map[i] = PEG_NO_MATCH; 
      } 
     } 

     // Count white pegs 
     for (int i = 0; i < 4; i++) { 
      if (match_map[i] != PEG_BLACK){ 
       for (int k = 0; k < 4; k++) { 
        if ((i != k) && (match_map[k] == PEG_NO_MATCH) && (usercolors[i] == colors[k])){ 
         match_map[k] = PEG_WHITE; 
         white_cnt++; 
         break; 
        } 
       } 
      } 
     } 

     std::cout << std::endl << std::endl; 

     // Display result 
     std::cout << "Black Pegs : " << black_cnt << std::endl; 
     std::cout << "White Pegs : " << white_cnt << std::endl; 

     // Do all pegs match? 
     if (black_cnt == 4) 
     { 
      std::cout << "You win! Number of tries: " << turncounter << std::endl; 
      break; 
     } 
     else { 
      std::cout << "Try Again!" << std::endl << std::endl; 
     } 
    } 

    if (turncounter == 12) { 
     std::cout << "Sorry, you are incorrect!" << std::endl; 
     std::cout << "Answer: " << "Color 1: " << colors[0] << "\t" << "Color 2: " << colors[1] << "\t" << "Color 3: " << colors[2] << "\t" << "Color 4: " << colors[3] << "\t" << std::endl; 
    } 

    // Wait for input 
    std::cin.get(); 
    std::cin.get(); 

    return 0; 
} 
+0

歡迎來到Stack Overflow!這個問題是尋找*解釋*,而不僅僅是工作代碼。您的回答對提問者提供的信息很少,可能會被刪除。請[編輯]解釋導致觀察到的症狀的原因,以及您的代碼是如何從原始文件中獲得的。你可能還想提一下爲什麼我們也避免使用'namespace std;'。 –

+0

@Toby:我會補充說明。 – CheviN

+0

@ sp2danny:你能舉一個例子說明它匹配多次嗎? – CheviN