2017-08-20 34 views
-3

我試圖在C++中製作一個基本的Tic Tac Toe遊戲。雖然這段代碼編譯並運行,但我仍然在棋盤遊戲之上得到這個錯誤代碼:0x6fefec84。我對C++非常陌生。這個錯誤是段錯誤嗎?或者代碼中是否存在某種UB?任何幫助將不勝感激。謝謝!一個基於文本的C++遊戲,帶有代碼錯誤中的謎題

#include <iostream> 


using namespace std; 



    char square[10] = {'0','1','2','3','4','5','6','7','8','9'}; 

    int checkwin(); 
    int board(); 


    int main() { 


     int player = 1,i,choice; 

     char mark; 

     do { 


     board(); 
     player=(player%2)?1:2; 


     cout << "Player" << player << ", enter a number: "; 
     cin >> choice; 


     mark=(player == 1) ? 'X' : 'O'; 

     if (choice == 1 && square [1] == '1') 


      square [1] = mark; 
     else if (choice == 2 && square [2] == '2') 

      square [2] = mark; 
     else if(choice == 3 && square [3] == '3') 

      square [3] = mark; 
     else if(choice == 4 && square [4] == '4') 

      square [4] = mark; 
     else if(choice == 5 && square [5] == '5') 

      square [5] = mark; 
     else if(choice == 6 && square [6] == '6') 

      square [6] = mark; 
     else if(choice == 7 && square [7] == '7') 

      square [7] = mark; 
     else if(choice == 8 && square [8] == '8') 

      square [8] = mark; 
     else if(choice == 9 && square [9] == '9') 


      square [9] = mark; 

     else { 

      cout << "Invalid move"; 

     player--; 
     cin.ignore(); 
     cin.get(); 

     } 
     i=checkwin(); 

     player++; 
    }while(i==-1); 

    board(); 
    if(i==1) 



     cout<<"==>\aPlayer "<<--player<<" win "; 

    else 

     cout<<"==>\aGame draw"; 


    cin.ignore(); 
    cin.get(); 
    return 0; 

} 



    int checkwin() { 


    if(square[1] == square[2] && square [2] == square[3]) 

       return 1; 
     else if(square[4] == square[5] && square [5] == square[6]) 

       return 1; 
     else if(square[7] == square[8] && square[8] == square[9]) 

       return 1; 
     else if(square[1] == square[4] && square[4] == square[7]) 

       return 1; 
     else if(square[2] == square[5] && square[5] == square[8]) 

       return 1; 
     else if(square[3] == square[6] && square[6] == square[9]) 

       return 1; 
     else if(square[1] == square[5] && square[5] == square[9]) 

       return 1; 
     else if(square[3] == square[5] && square[5] == square[7]) 

       return 1; 
     else if(square[1] != '1' && square[2] != '2' && square [3] != '3' && square [4] != '4' 
      && square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8' && square [9] != '9') 

      return 0; 

     else 

      return -1; 
    } 


     int board() { 


     cout << "\n\n\tTic Tac Toe\n\n"; 

     cout << "Player 1 (X) - Player 2 (O)" << endl << endl<< 
     cout << endl; 

     cout << "  | | " << endl; 
     cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl; 

     cout << "----|----|----" << endl; 
     cout << "----|----|----" << endl; 


     cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl; 


     cout << "----|----|----" << endl; 
     cout << "----|----|----" << endl; 


     cout << " " << square[7] << " | " << square[8] << " | " << square[9] <<endl; 

     cout << "  | | " << endl << endl; 

    return 0; 

} 

回答

3

您在「播放器」輸出行末尾缺少分號。

cout << "Player 1 (X) - Player 2 (O)" << endl << endl; 

隨着<<末,你輸出的cout的地址,這是你所看到的「0x6fefec84」值。

+0

非常感謝你幫助我。我做了更改,我沒有看到那個錯誤代碼了。非常感謝! – Gus

3
cout << "Player 1 (X) - Player 2 (O)" << endl << endl<< 
cout << endl; 

您正在打印cout本身,它將打印該十六進制代碼。您需要分號而不是第一行的最後一個< <。

+0

非常感謝你的先生。非常感謝!我只是用新的更正重新編譯了代碼,現在它沒有錯誤地工作。再次感謝您的幫助先生! – Gus