Tic Tac Toe 程序输出语句和函数
Tic Tac Toe program output statements and functions
对于这个程序,我应该创建一个 tic tac toe 游戏,到目前为止我已经有了这个,但我不确定如何写谁赢以及如何让玩家不能如果 row/col 已经被占用,则覆盖彼此的移动。
#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";
}
}
你知道,如果一个板块有任何 char
除了 -
那么玩家不能写入那个板块,因为肯定有人已经拿走了它。您还可以编写一个函数,通过遍历每一行、每一列和对角线来查看是否有三个 X
或 O
连续出现,从而测试任一玩家是否已经获胜。
这将是一种蛮力方法,但由于我们讨论的是 3x3 矩阵,因此它是否低效也无关紧要。
无需过多更改您的程序,您就可以使用一个函数来检查获胜者和一个数组来检查已占用的位置:
// 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;
}
}
}
对于这个程序,我应该创建一个 tic tac toe 游戏,到目前为止我已经有了这个,但我不确定如何写谁赢以及如何让玩家不能如果 row/col 已经被占用,则覆盖彼此的移动。
#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";
}
}
你知道,如果一个板块有任何 char
除了 -
那么玩家不能写入那个板块,因为肯定有人已经拿走了它。您还可以编写一个函数,通过遍历每一行、每一列和对角线来查看是否有三个 X
或 O
连续出现,从而测试任一玩家是否已经获胜。
这将是一种蛮力方法,但由于我们讨论的是 3x3 矩阵,因此它是否低效也无关紧要。
无需过多更改您的程序,您就可以使用一个函数来检查获胜者和一个数组来检查已占用的位置:
// 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;
}
}
}