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