2013-04-30 26 views
0

標題種說這一切。我不確定爲什麼井字遊戲程序沒有檢測到一條領帶。我附加了主要功能,附加功能等。我不知道我做錯了什麼。任何幫助是極大的讚賞。C++井字趾 - 檢測領帶的問題

#include <string> 
#include <iostream> 
#include <cstdlib> 
using namespace std; 

string displayBoard(string board[9]); // displays tic tac toe board 
bool isGameOver(string board[9]);  // checks if game is over 
void displayGameWelcome();    // displays welcome message 

int main() 
{ 
string board[9];   // tic tac toe, top row 0 thru 2, middle row 3 thru 5, bottom row 6 thru 8 
int position = 0;   // player's position 
bool gameOver = false; // flag variable to mark end of the game 
bool validMove = false; // determines if move is valid or not 

displayGameWelcome(); 

// initializing board with blank spaces 
for (int i = 0; i < 9; i++) 
{ 
    board[i] = " "; 
} 

while (!gameOver) 
{ 
    // player #1's turn ************************** 
    validMove = false; 


    while (!validMove) 
    { 
     cout << "Enter your position: "; 
     cin >> position; 

     if (position >= 0 && position <= 8 && board[position] == " ") 
     { 
      validMove = true; 
      board[position] = "x";  // placing x in desired board location 
     } 
     else 
     { 
      cout << "That position is already occupied." << endl; 
      cout << displayBoard(board) << endl; 
     } 

    } 

    cout << displayBoard(board) << endl; 

    if (isGameOver(board)) 
    { 
     gameOver = true; 
    } 

    // player #2's turn ********************************** 
    validMove = false; 

    while (!validMove) 
    { 
     cout << "Enter your position: "; 
     cin >> position; 

     if (position >= 0 && position <= 8 && board[position] == " ") 
     { 
      validMove = true; 
      board[position] = "o";  // placing o in desired board position 
     } 
     else 
     { 
      cout << "That position is already occupied." << endl; 
      cout << displayBoard(board) << endl; 
     } 

    } 

    cout << displayBoard(board) << endl; 

    if (isGameOver(board)) 
    { 
     gameOver = true; 
    } 

}// end of validMove while loop 

system("pause"); 
return 0; 
}// end of main 

// ************************** functions ************************* 

void displayGameWelcome() 
{ 
    cout << "Welcome to Tic Tac Toe v3.2" << endl;\ 
    cout << "by Brett Singley & Nick Canarelli" << endl; 
    cout << "****************************\n\n\n"; 
}// end of displayGameWelcome 

// checks if game is over 
bool isGameOver(string board[9]) 
{ 

if (board[0] == "x" && board[1] == "x" && board[2] == "x") 
{ 
    cout << endl << "The game is over - x wins" << endl; 
    return true; 
} 
else if (board[0] == "o" && board[1] == "o" && board[2] == "o") 
{ 
    cout << endl << "The game is over - o wins" << endl; 
    return true; 
} 
else if (board[3]=="x" && board[4]=="x" && board[5]=="x") 
{ 
    cout <<endl << "The game is over - X wins" <<endl; 
} 
else if (board[3]=="o" && board[4]=="o" && board[5]=="o") 
{ 
    cout <<endl << "The game is over - o wins" <<endl; 
} 
else if (board[0]=="x" && board[3]=="x" && board[6]=="x") 
{ 
    cout <<endl << "The game is over - X wins" <<endl; 

} 
else if (board[0]=="o" && board[3]=="o" && board[6]=="o") 
{ 
    cout <<endl << "The game is over - o wins" <<endl; 
} 
else if (board[6]=="x" && board[7]=="x" && board[8]=="x") 
{ 
    cout <<endl << "The game is over - X wins" <<endl; 
} 
else if (board[6]=="o" && board[7]=="o" && board[8]=="o") 
{ 
    cout <<endl << "The game is over - O wins" <<endl; 
} 
else if (board[1]=="x" && board[4]=="x" && board[7]=="x") 
{ 
    cout <<endl << "The game is over - X wins" <<endl; 

} 
else if (board[1]=="o" && board[4]=="o" && board[7]=="o") 
{ 
    cout <<endl << "The game is over - O wins" <<endl; 
} 
else if (board[2]=="x" && board[5]=="x" && board[8]=="x") 
{ 
    cout <<endl << "The game is over - X wins" <<endl; 
} 
else if (board[2]=="o" && board[5]=="o" && board[8]=="o") 
{ 
    cout <<endl << "The game is over - o wins" <<endl; 
} 
else if (board[0]=="x" && board[4]=="x" && board[8]=="x") 
{ 
    cout <<endl << "The game is over - X wins" << endl; 
} 
else if (board[0]=="o" && board[4]=="o" && board[8]=="o") 
{ 
    cout <<endl << "The game is over - o wins" <<endl; 
} 
else if (board[2]=="x" && board[4]=="x" && board[6]=="x") 
{ 
    cout <<endl << "The game is over - X wins" <<endl; 
} 
else if (board[2]=="o" && board[4]=="o" && board[6]=="o") 
{ 
    cout <<endl << "The game is over - o wins" <<endl; 
} 
else 
{ 
    cout << "WOAH!!!!! Tie Game" <<endl; 
} 


// more to do here (don't forget to check for draw) 

return false; 
}// end of isGameOver 

// displays tic tac toe board in the format 
// |0 1 2| 
// |3 4 5| 
// |6 7 8| 
string displayBoard(string board[9]) 
{ 
string str = ""; 

for (int i = 0; i < 9; i++) 
{ 

    if (i == 0) 
    { 
     str = str + "|" + board[i]; 
    } 
    else if (i == 2 || i == 5) 
    { 
     str = str + board[i] + "|\n|"; 
    } 
    else if (i == 8) 
    { 
     str = str + board[i] + "|\n"; 
    } 
    else 
    { 
     str = str + board[i]; 
    } 

} 

return str; 
}// end of displayBoard 
+0

好吧,您可能需要先完成isGameOver函數或發佈最新的代碼。返回缺少 – 2013-04-30 19:09:23

+0

你會介意在正確的方向引導我嗎?我不知道什麼是缺少的 – nackolous 2013-04-30 19:12:31

+2

當遊戲結束時,你不會返回任何情況...... – DGomez 2013-04-30 19:14:50

回答

0

可以簡化使用for循環和實現,在三個空間的價值是相等的(比初始值以外的東西),檢查贏家。此外,由於有3行3列,行和列可以在同一個for循環內進行檢查:

const char initial_value = ' '; 
const unsigned int max_rows_or_columns = 3; 
bool Is_Game_Over(int board[9], char& winner) 
{ 
    for (unsigned int i = 0; i < max_rows_or_columns; ++i) 
    { 
     // Check the row 
     if ( (board[(i * 3) + 0] == board[(i * 3) + 1]) 
      && (board[(i * 3) + 1) == board[(i * 3) + 2])) 
     { 
      // By transitive property, board[(i * 3) + 0] == board[(i * 3) + 2] 
      winner = board[(i * 3) + 0]; 
      if (winner != initial_value) 
      { 
       break; 
      } 
     } 
     else // Check the columns 
     { 
      if ( (board[(0 * 3) + i] == board[(1 * 3) + i]) 
       && (board[(1 * 3) + i] == board[(2 * 3) + i])) 
      { 
       winner = board[(0 * 3) + i]; 
       if (winner != initial_value) 
       { 
        break; 
       } 
      } 
     } 
    bool winner_found = true; 
    if (i >= max_rows_or_columns) 
    { 
     winner_found = false; 
    } 
    return winner_found; 
} 

對角線的檢查就留給讀者自己練習。