2017-04-03 96 views
-5

我對C++來說很新穎。C++井字遊戲程序優化

我剛完成我的第一個「真實」程序。

這是一個井字遊戲。

是否有人願意閱讀槽我的代碼告訴我在哪裏我可以優化一些東西?

#include <iostream> 

using namespace std; 

    /**< Board variables */ 
    string a1 = "_"; string a2 = "_"; string a3 = "_"; 
    string b1 = "_"; string b2 = "_"; string b3 = "_"; 
    string c1 = "_"; string c2 = "_"; string c3 = "_"; 

    string showboard(){ 
     cout << '\t' << ""<< '\t' << "1" << '\t' << "2" << '\t' << "3" << "\n"; 
     cout << '\t' << "a"<< '\t' << a1 << '\t' << a2 << '\t' << a3 << "\n"; 
     cout << '\t' << "b"<< '\t' << b1 << '\t' << b2 << '\t' << b3 << "\n"; 
     cout << '\t' << "c"<< '\t' << c1 << '\t' << c2 << '\t' << c3 << "\n\n"; 
     return ""; 
    }; 

    /**< Turn */ 
    int turn; 
    /**< useless variable */ 
    int x =1; 
    /**< "tiles" for example "X" or "O" */ 
    string boardvalue; 

    /**< Checks if Turn is valid/legal */ 
    string turnvalidation (string boardvalue){ 
      if(turn==1 && a1!="_"){ 
       cout << "Invalid option. Choose again! \n\n"; 
       x=1; 
      } else if(turn==2 && a2!="_"){ 
       cout << "Invalid option. Choose again! \n\n"; 
       x=1; 
      } else if(turn==3 && a3!="_"){ 
       cout << "Invalid option. Choose again! \n\n"; 
       x=1; 
      } else if(turn==4 && b1!="_"){ 
       cout << "Invalid option. Choose again! \n\n"; 
       x=1; 
      } else if(turn==5 && b2!="_"){ 
       cout << "Invalid option. Choose again! \n\n"; 
       x=1; 
      } else if(turn==6 && b3!="_"){ 
       cout << "Invalid option. Choose again! \n\n"; 
       x=1; 
      } else if(turn==7 && c1!="_"){ 
       cout << "Invalid option. Choose again! \n\n"; 
       x=1; 
      } else if(turn==8 && c2!="_"){ 
       cout << "Invalid option. Choose again! \n\n"; 
       x=1; 
      } else if(turn==9 && c3!="_"){ 
       cout << "Invalid option. Choose again! \n\n"; 
       x=1; 
      } else if(turn==10){ 
       cout << "Invalid option. Choose again! \n\n"; 
       x=1; 
      } else { 

       x=0; 
       switch (turn){ 

        case 1: a1=boardvalue; break; 
        case 2: a2=boardvalue; break; 
        case 3: a3=boardvalue; break; 
        case 4: b1=boardvalue; break; 
        case 5: b2=boardvalue; break; 
        case 6: b3=boardvalue; break; 
        case 7: c1=boardvalue; break; 
        case 8: c2=boardvalue; break; 
        case 9: c3=boardvalue; break; 
       } 
      } 
      return""; 
    }; 

    /**< Checks if the game is won */ 
    int wincheck(string boardvalue){ 
    // Rows 
    if(a1==boardvalue && a2==boardvalue && a3==boardvalue){return 0;}else 
    if(b1==boardvalue && b2==boardvalue && b3==boardvalue){return 0;}else 
    if(c1==boardvalue && c2==boardvalue && c3==boardvalue){return 0;}else 

    // Columns 
    if(a1==boardvalue && b1==boardvalue && c1==boardvalue){return 0;}else 
    if(a2==boardvalue && b2==boardvalue && c2==boardvalue){return 0;}else 
    if(a3==boardvalue && b3==boardvalue && c3==boardvalue){return 0;}else 

    // Diagonals 
    if(c1==boardvalue && b2==boardvalue && a3==boardvalue){return 0;}else 
    if(a1==boardvalue && b2==boardvalue && c3==boardvalue){return 0;}else 
    {return 1;}; 
    } 

int main() 
{ 
    string player1; 
    string player2; 

    cout << "Player 1 enter your name \n"; 
    cin >> player1; 

    cout << "\nPlayer 2 enter your name \n"; 
    cin >> player2; 
    cout << endl; 

    cout << "Input example: a1 [enter]\n\n"; 

    string turni; /**< Converts Input in switch value! */ 

    while(1){ /**< Play again */ 
      string a1 = "_"; string a2 = "_"; string a3 = "_"; 
      string b1 = "_"; string b2 = "_"; string b3 = "_"; 
      string c1 = "_"; string c2 = "_"; string c3 = "_"; 
     while(1){ 

      /**< Player 1s turn */ 
      x=1; 
      while(1){ 

       cout << player1 << " place your X \n\n"; 
       showboard(); 
       cin >> turni; 

       if(turni=="a1"){turn=1;} else if(turni=="a2"){turn=2;} else if(turni=="a3"){turn=3;} else if(turni=="b1"){turn=4;} else if(turni=="b2"){turn=5;} else if(turni=="b3"){turn=6;} else if(turni=="c1"){turn=7;} else if(turni=="c2"){turn=8;} else if(turni=="c3"){turn=9;} else{turn=10;}; 

       /**< Checks if Turn is valid/legal */ 

       turnvalidation("X"); 
       break; 
      } 

      /**< Win Check */ 

      if(wincheck("X") == 0){ 

       showboard(); 
       cout << "Congratulations " << player1 << " you won!"; 
       break; 
      }; 

      /**< Player 1s turn is over */ 


      /**< Player 2s turn */ 
      x=1; 
      while(1){ 

       cout << player2 << " place your 0 \n\n"; 
       showboard(); 
       cin >> turni; 

       if(turni=="a1"){turn=1;} else if(turni=="a2"){turn=2;} else if(turni=="a3"){turn=3;} else if(turni=="b1"){turn=4;} else if(turni=="b2"){turn=5;} else if(turni=="b3"){turn=6;} else if(turni=="c1"){turn=7;} else if(turni=="c2"){turn=8;} else if(turni=="c3"){turn=9;} else{turn=10;}; 

       /**< Checks if Turn is valid/legal */ 

       turnvalidation("O"); 
       break; 
      } 

      /**< Win Check */ 

      if(wincheck("O")==0){ 

       showboard(); 
       cout << "Congratulations " << player2 << " you won!"; 
       break; 
      }; 

      /**< Player 1s turn is over */ 
     } 

      /**< Play again and clear board */ 
     cout << "\n\nIf you want to play again type ""1""! \n"; 
     cin >> x; 

     if(x==1){ 
     a1 = "_";  a2 = "_";  a3 = "_"; 
     b1 = "_";  b2 = "_";  b3 = "_"; 
     c1 = "_";  c2 = "_";  c3 = "_"; 
     } else {break;} 
    } 
} 

遊戲本身的作品。

在本節是一個錯誤:

 cout << "\n\nIf you want to play again type ""1""! \n"; 
     cin >> x; 

     if(x==1){ 
     a1 = "_";  a2 = "_";  a3 = "_"; 
     b1 = "_";  b2 = "_";  b3 = "_"; 
     c1 = "_";  c2 = "_";  c3 = "_"; 
     } else {break;} 

你贏得進入此節。

如果您輸入「1」,遊戲將重新啓動並應清除棋盤。

每隔一個輸入將關閉該程序。

遊戲將重新啓動,但棋盤不會清除。

+1

如果您的代碼正常工作,而您只是在尋找同行評審,則應該在[codereview.se]上發帖。這個網站是關於您遇到的問題的問題,而不是*您能否爲我看看這個問題?*問題。 –

+2

您聲明瞭9個變量而不是某種數組或結構,並且強制您的代碼通過它進行大量重複。當你的代碼開始看起來像是所有的東西都被複制/粘貼時,這表明你做錯了什麼。 –

+0

你很幸運井字棋只有9個位置。學習使用數組。 – PaulMcKenzie

回答

1

你的錯誤是在這裏

while(1){ /**< Play again */ 
      string a1 = "_"; string a2 = "_"; string a3 = "_"; 
      string b1 = "_"; string b2 = "_"; string b3 = "_"; 
      string c1 = "_"; string c2 = "_"; string c3 = "_"; 

當你已經聲明他們作爲全球您不應該再次聲明他們。

您應該將此部分更改爲此。所以你的變量值將隨時更新。

while (1){ /**< Play again */ 
     a1 = "_"; a2 = "_";  a3 = "_"; 
     b1 = "_"; b2 = "_";  b3 = "_"; 
     c1 = "_";  c2 = "_"; c3 = "_"; 
0

Mohammad Tayyab的回答對修正錯誤是正確的。給他勝利,但如果下面的內容也有幫助的話,upvote會很好。您在遊戲開始時設置了棋盤,您無需在其他時間設置棋盤。改變原來的語句:再次

/* Board variables */ 
string a1, a2, a3, b1, b2, b3, c1, c2, c3; 

和播放代碼:

/**< Play again and clear board */ 
cout << "\n\nIf you want to play again type ""1""! \n"; 
cin >> x; 

if (x != 1) 
{ 
    break; 
} 

此外,你會用向量/陣列您的主板如更好在一個簡單的程序中一起擺脫a1-c3並使用:

int board[9]; // a1 now = board[0], a2 now = board[1] and so on 

現在可以大大簡化「turnvalidation」功能。

而且你是在雙方球員的代碼重複這樣的代碼:

if (turni == "a1") { turn = 1; } 
else if (turni == "a2") { turn = 2; } 
else if (turni == "a3") { turn = 3; } 
else if (turni == "b1") { turn = 4; } 
else if (turni == "b2") { turn = 5; } 
else if (turni == "b3") { turn = 6; } 
else if (turni == "c1") { turn = 7; } 
else if (turni == "c2") { turn = 8; } 
else if (turni == "c3") { turn = 9; } 
else { turn = 10; }; 

隨着板[9]數組來代替,而這些代碼合併到驗證功能(也將名稱更改爲「movevalidation」)你可以這樣做:

bool movevalidation(string move_string, string player_symbol) 
{ 
    string moves[9] = { "a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3" }; 
    int move_int = -1; // -1 means invalid 
    for (int i = 0; i < 9; ++i) 
    { 
     if (move_string == moves[i]) 
      move_int = i; 
    } 

    if (moves < 0 || board[move_int] != "_") 
    { 
     cout << "Invalid option. Choose again! \n\n"; 
     return false; 
    } 

    board[move_int] = player_symbol; 
    return true; 
} 

而且玩家選擇,此舉循環變成這樣:

 while (1) { 

      cout << player1 << " place your X \n\n"; 
      showboard(); 
      cin >> turni; 

      if(movevalidation(turni)) 
       break; 
     } 

適用於兩位球員。對於數組,你還需要更改明確板代碼是一個循環:

while (1) { /**< Play again */ 
    for(int i = 0; i < 9 ++i) 
     board[i] = "_"; 

你必須改變你的wincheck考慮更換名稱與陣列名稱。您可以簡單地用板[0]替換a1,用板[1]替換a2等等。

+0

這真是不可思議。詳細的描述非常感謝。我自學了一本關於數組的書籍和天堂,所以我使用了我所知道的工具。如果我能夠達到15的聲望,你會看到我的喜歡。 – Yannic