2014-08-28 67 views
-1

我現在正在開發一個Tic Tac Toe項目。我遇到的問題是,無論何時輸入控制檯的縱座標,例如[6] [0](程序會將標記'X'或'O'放入該位置),大小爲[15] [15],它會自動將標記'X'或'O'保存到另一個不在數組範圍內的位置(在我的例子中是[5] [15]),這是我的程序(P/S:我是越南這樣就忽略了在越南的部分):C++ Tic Tac Toe項目

int size = 15; 
int inputAmount; 
int inputX = 0; 
int inputY = 0; 
char board[15][15]; 
bool checkWin = false; 
char mark = 'X'; 

// Interdisciplinary examination of scale loss 
bool checkWinLose() { 
    int max; 
    int x,y; 
    x = inputX; 
    y = inputY; 

    // Looking horizontally under investigation 
    for (; max < 3; max++) 
    { 
     if ((board[x][y] == 'X' || board[x][y] == 'O') && (board[x][y] == board[x - 1][y] && board[x - 1][y] == board[x - 2][y])) 
     { 
      cout << "Game over ngang!" << endl; 
      return 1; 
     } 
     x++; 
    } 

    x = inputX; 
    y = inputY; 
    max = 0; 
    // Interdisciplinary examination of vertical 
    for (; max < 3; max++) 
    { 
     if ((board[x][y] == 'X' || board[x][y] == 'O') && (board[x][y] == board[x][y - 1] && board[x][y - 1] == board[x][y - 2])) 
     { 
      cout << "Game over doc!" << endl; 
      return 1; 
     } 
     y++; 
    } 

    x = inputX; 
    y = inputY; 
    max = 0; 
    // Interdisciplinary examination of under the cliff cave from left to right 
    for (; max < 3; max++) 
    { 
     if ((board[x][y] == 'X' || board[x][y] == 'O') && (board[x][y] == board[x - 1][y - 1] && board[x - 1][y - 1] == board[x - 2][y - 2])) 
     { 
      cout << "Game over trai sang phai!" << endl; 
      return 1; 
     } 
     x++; 
     y++; 
    } 

    x = inputX; 
    y = inputY; 
    max = 0; 
    // Interdisciplinary examination of under the cliff cave from right to left 
    for (; max < 3; max++) 
    { 
     if ((board[x][y] == 'X' || board[x][y] == 'O') && (board[x][y] == board[x + 1][y - 1] && board[x + 1][y - 1] == board[x + 2][y - 2])) 
     { 
      cout << "Game over phai sang trai!" << endl; 
      return 1; 
     } 
     x--; 
     y++; 
    } 

    // Flower test case 
    if (inputAmount == 225) 
    { 
     cout << "Game over hoa!" << endl; 
     return 1; 
    } 
} 

// Lay-coordinate of the muon practice player list 
void takeInput() { 
    do { 
     // Lay gia tri toa do x 
     do { 
      cout << "Please choose the horizontal (rightward) number (smaller than " << size + 1 << "): "; 
      cin >> inputX; 
     } while ((inputX > size) || (inputX <= 0)); 
     // Lay y coordinate values 
     do { 
      cout << "Please choose the vertical (downward) number (smaller than " << size + 1 << "): "; 
      cin >> inputY; 
     } while ((inputY > size) || (inputY <= 0)); 
     inputX--; 
     inputY--; 
     if (board[inputX][inputY] != '.') 
      cout << "Already chosen!" << endl ; 
    } while (board[inputX][inputY] != '.'); 
    board[inputX][inputY] = mark; 
    if (mark == 'X') 
     mark = 'O'; 
    else 
     mark = 'X'; 
    cout << endl << endl << endl; 
} 

// Hien game board on the screen 
void loadGameboard() { 
    int x,y; 

    ////TODO: check win or lose 
    while (!checkWin) { 
     for (; y < size ; y++) 
     { 
      for (; x < size ; x++) 
      { 
       cout << board[x][y] << " "; 
      } 
      cout << endl; 
      x = 0; 
     } 
     checkWin = checkWinLose(); 
     if (checkWin == true) 
      return; 
     x,y = 0; 
     takeInput(); 
     inputAmount++; 
    } 
} 

// At first preparation game board 
void prepareGameboard() { 
    int x,y; 

    for (; y < size ; y++) 
    { 
     for (; x < size ; x++) 
     { 
      board[x][y] = '.' ; 
     } 
     x = 0; 
    } 
} 

int main(array<System::String ^> ^args) 
{ 
    char reset = 'y'; 
    do { 
     prepareGameboard(); 
     loadGameboard(); 
     checkWin = 0; 
     inputAmount = 0; 
     cout << "Do you want to replay ? (y/n): "; 
     cin >> reset; 
     if ((reset == 'y') || (reset == 'Y')) 
     { 
      system("CLS"); 
     } 
    } while ((reset == 'y') || (reset == 'Y')); 
    return 0; 
} 
+1

我翻譯的註釋成英文與谷歌翻譯。如果不知道它的作用,很難遵循該程序。您可以自行將其翻譯成英文,但這會使程序更易於閱讀。 – john 2014-08-28 13:28:39

+0

這是巨大的努力@john – Vishwanath 2014-08-28 13:32:55

回答

0

我會在你粘貼代碼行123改變x,y = 0x = y = 0。並且還將return 0添加到功能checkWinLose()

0

您尚未初始化xy的其他for的值。

嘗試使用:

void prepareGameboard() { 
    int x,y; 

    for (y = 0; y < size ; y++) 
    { 
     for (x = 0; x < size ; x++) 
     { 
      board[x][y] = '.' ; 
     } 
    } 
} 

和:

void loadGameboard() { 
    int x,y; 

    ////TODO: check win or lose 
    while (!checkWin) { 
     for (y = 0; y < size ; y++) 
     { 
      for (x = 0; x < size ; x++) 
      { 
       cout << board[x][y] << " "; 
      } 
      cout << endl; 
     } 
     checkWin = checkWinLose(); 
     if (checkWin == true) 
      return; 
     takeInput(); 
     inputAmount++; 
    } 
} 

此外,像@Heinz說,和return 0;checkWinLose功能的結束;

看着你的代碼,我相信你假設int總是用0初始化,並且checkWinLose將默認返回0。局部變量具有未定義的初始值,並且當沒有明確返回被調用時,該函數僅返回「垃圾」值。

嘗試始終將返回的值添加到函數並初始化您的變量(特別是計數器)。

更新

這是怎樣的功能checkWinLose應與return 0;

// Interdisciplinary examination of scale loss 
bool checkWinLose() { 
    int max; 
    int x,y; 
    x = inputX; 
    y = inputY; 

    // Looking horizontally under investigation 
    for (; max < 3; max++) 
    { 
     if ((board[x][y] == 'X' || board[x][y] == 'O') && (board[x][y] == board[x - 1][y] && board[x - 1][y] == board[x - 2][y])) 
     { 
      cout << "Game over ngang!" << endl; 
      return 1; 
     } 
     x++; 
    } 

    x = inputX; 
    y = inputY; 
    max = 0; 
    // Interdisciplinary examination of vertical 
    for (; max < 3; max++) 
    { 
     if ((board[x][y] == 'X' || board[x][y] == 'O') && (board[x][y] == board[x][y - 1] && board[x][y - 1] == board[x][y - 2])) 
     { 
      cout << "Game over doc!" << endl; 
      return 1; 
     } 
     y++; 
    } 

    x = inputX; 
    y = inputY; 
    max = 0; 
    // Interdisciplinary examination of under the cliff cave from left to right 
    for (; max < 3; max++) 
    { 
     if ((board[x][y] == 'X' || board[x][y] == 'O') && (board[x][y] == board[x - 1][y - 1] && board[x - 1][y - 1] == board[x - 2][y - 2])) 
     { 
      cout << "Game over trai sang phai!" << endl; 
      return 1; 
     } 
     x++; 
     y++; 
    } 

    x = inputX; 
    y = inputY; 
    max = 0; 
    // Interdisciplinary examination of under the cliff cave from right to left 
    for (; max < 3; max++) 
    { 
     if ((board[x][y] == 'X' || board[x][y] == 'O') && (board[x][y] == board[x + 1][y - 1] && board[x + 1][y - 1] == board[x + 2][y - 2])) 
     { 
      cout << "Game over phai sang trai!" << endl; 
      return 1; 
     } 
     x--; 
     y++; 
    } 

    // Flower test case 
    if (inputAmount == 225) 
    { 
     cout << "Game over hoa!" << endl; 
     return 1; 
    } 

    return 0; //<- Returning false if the all other tests failed 
} 
+0

泰克曼,但這不是真的解決我的問題。你能解決問題嗎? – 2014-08-29 05:50:53

+0

也可以任何人告訴我如何將checkWinLose函數返回到0? – 2014-08-29 10:28:12

+0

進行更改我說,並添加'返回0;'讓你的程序在我的電腦上工作 – braindf 2014-08-29 19:26:19