2016-03-05 28 views
0

對於這個程序,我應該創建一個tic tac腳趾遊戲,到目前爲止我已經得到了這個,但我不知道如何寫誰贏,以及如何使它如果行/列已被佔用,則玩家不能覆蓋彼此的移動。Tic Tac Toe程序輸出語句和函數

#include <iostream> 
using namespace std; 

void Locations(int &, int &); 
void Tables(char [][3], int); 

int main() 
{ 
    const int cRow = 3; 
    const int cCol = 3; 
    char table[cRow][cCol] = { '-', '-', '-', 
     '-', '-', '-', 
     '-', '-', '-'}; 
    int nRow, nCol; 


    Tables(table, cRow); 



    for(int count = 0; count < 5; count++) 
    { 
     if (count < 5) { 
      cout << "\n Player X"; 
      Locations(nRow, nCol); 
      table[nRow][nCol] = 'X'; 
      Tables(table, cRow); 
     } 

     if (count < 4) { 
      cout << "\n Player O"; 
      Locations(nRow, nCol); 
      table[nRow][nCol] = 'O'; 
      Tables(table, cRow); 
     } 
    } 


    return 0; 
} 

void Locations(int &nRow, int &nCol) { 
    cout << " please enter row (0 to 2): "; 
    cin >> nRow; 
    while(nRow < 0 || nRow > 2) 
    { 
     cout << "Invalid entry\n"; 
     cout << " please enter row (0 to 2): "; 
     cin >> nRow; 
    } 
    cout << " please enter col (0 to 2): "; 
    cin >> nCol; 
    while(nCol < 0 || nCol > 2) 
    { 
     cout << "Invalid entry\n"; 
     cout << " please enter col (0 to 2): "; 
     cin >> nCol; 
    } 
} 

void Tables(char table[][3], int nRow) { 
    for(int iRow = 0; iRow < nRow; iRow++) 
    { 
     for(int iCol = 0; iCol < 3; iCol++) 
     { 
      cout << " " << table[iRow][iCol]; 
     } 
     cout << "\n"; 
    } 
} 
+0

你需要當用戶輸入一列檢查,如果一個正方形是空的和行(你可以檢查矩陣的值是O還是X(我會推薦)或者爲每個矩陣元素創建一個標誌變量)。也許創建一個簡單的函數來檢查X,O或 - ,然後在您的位置函數中調用它。就像元素包含「 - 」一樣,返回true可以工作。爲了宣佈獲勝者,你可以創建另一個簡單的函數來檢查獲勝模式,並在每個玩家佔據一個方塊後調用它。 – jeffkempf

回答

0

不改變太多的程序,你可以有一個函數,檢查是否有贏家和一個數組來檢查佔用位置:

// Prototypes 
void check_for_winner(char [][3], bool&, bool&); 

bool taken[3][3]; // to mark positions as taken 

在主可以有兩個bool的決定贏家:

bool x_wins = false; 
bool o_wins = false; 

而在你for loop,你可以在每次移動後,檢查贏家:

for(int count = 0; count < 5; count++) 
{ 
    if (count < 5) { 
     cout << "\n Player X"; 
     Locations(nRow, nCol); 
     table[nRow][nCol] = 'X'; 
     Tables(table, cRow); 
     check_for_winner(table, x_wins, o_wins); // Check for winner 
     if (x_wins || o_wins) break;    // break if there is one 
    } 

    if (count < 4) { 
     cout << "\n Player O"; 
     Locations(nRow, nCol); 
     table[nRow][nCol] = 'O'; 
     Tables(table, cRow); 
     check_for_winner(table, x_wins, o_wins); // Check for winner 
     if (x_wins || o_wins) break;    // break if there is one 
    } 
} 

// Output winner/draw 
if (x_wins) cout << "\nX Wins!\n"; 
else if (o_wins) cout << "\nO Wins!\n"; 
else cout << "\nIt's a draw!\n"; 

在你Locations功能:

void Locations(int &nRow, int &nCol) { 
    bool stop = false; 
    while (!stop) { 
     cout << " please enter row (0 to 2): "; 
     cin >> nRow; 
     cout << " please enter col (0 to 2): "; 
     cin >> nCol; 

     // If position is invalid 
     if (nRow < 0 || nRow > 2 || nCol < 0 || nCol > 2) { 
     cout << "Invalid position, try again\n"; 
     continue; 
     } 
     else if (taken[nRow][nCol]) { 
     cout << "Position taken, try again\n"; 
     } 
     else { // success 
     stop = true; 
     taken[nRow][nCol] = true; // Mark position as taken 
     } 
    } 
} 

,對於一個勝利者檢查水平檢查,垂直和對角線功能:

void check_for_winner(char table[][3], bool& x_wins, bool& o_wins) 
{ 
    // check horizontally 
    for (int x = 0; x != 3; ++x) { 
     for (int y = 0, occur_x = 0, occur_o = 0; y != 3; ++y) { 
     if (table[x][y] == 'X') ++occur_x; 
     else if (table[x][y] == 'O') ++occur_o; 

     if (occur_x == 3) { 
      x_wins = true; 
      return; 
     } 
     if (occur_o == 3) { 
      o_wins = true; 
      return; 
     } 
     } 
    } 

    // Check vertically 
    for (int x = 0; x != 3; ++x) { 
     for (int y = 0, occur_x = 0, occur_o = 0; y != 3; ++y) { 
     if (table[y][x] == 'X') ++occur_x; 
     else if (table[y][x] == 'O') ++occur_o; 

     if (occur_x == 3) { 
      x_wins = true; 
      return; 
     } 
     if (occur_o == 3) { 
      o_wins = true; 
      return; 
     } 
     } 
    } 

    // Check diagonally top left->bottom right 
    for (int x = 0, y = 0, occur_x = 0, occur_o = 0; x != 3; ++x, ++y) { 
     if (table[x][y] == 'X') ++occur_x; 
     else if (table[x][y] == 'O') ++occur_o; 

     if (occur_x == 3) { 
     x_wins = true; 
     return; 
     } 
     if (occur_o == 3) { 
     o_wins = true; 
     return; 
     } 
    } 

    // Check diagonally top right->bottoml left 
    for (int x = 2, y = 0, occur_x = 0, occur_o = 0; x != -1; --x, ++y) { 
     if (table[x][y] == 'X') ++occur_x; 
     else if (table[x][y] == 'O') ++occur_o; 

     if (occur_x == 3) { 
     x_wins = true; 
     return; 
     } 
     if (occur_o == 3) { 
     o_wins = true; 
     return; 
     } 
    } 
} 
0

你知道,如果瓷磚有其他任何char-那麼玩家不能寫入的瓷磚,因爲必須有人已經採取它。您還可以編寫一個函數,通過循環遍歷每行,每列和對角線來測試兩個玩家是否贏了,以查看連續有三個XO

這將是一個蠻力的方法,但由於我們正在討論一個3x3矩陣,如果它效率低下並不重要。