我目前正在为大学做一个零零碎碎的课程。我已经完成了这项任务的基本内容,但是我在创建获胜条件以结束游戏时遇到了一些麻烦。以下是我到目前为止使用的所有代码:
#include <iostream>
#include <string>
using namespace std;
class Player
{
private:
char NorX;
public:
char Choose(char InitialValue)
{
NorX = InitialValue;
return InitialValue;
}
char GetNorX()
{
return NorX;
}
};
int main()
{
Player Player1;
Player Player2;
Player1.Choose('O');
Player2.Choose('X');
cout << "The board is being drawn please wait..." << endl;
const int Rows = 4;
const int Columns = 4;
char Board[Rows][Columns] = { {' ', ' ', ' ', ' ' },
{' ', '_', '_', '_' },
{' ', '_', '_', '_' },
{' ', '_', '_', '_' } };
for (int i = 0; i < Rows; ++i)
{
for (int j = 0; j < Columns; ++j)
cout << Board [i][j];
cout << endl;
}
cout << endl << endl;
int row;
int column;
do
{
do
{
cout << "Please enter the value of the row you would like to take ";
cin >> row;
}while (row != 0 && row != 1 && row != 2 && row != 3);
do
{
cout << "Please enter the value of the column you would like to take ";
cin >> column;
}while (column != 0 && column != 1 && column != 2 && column != 3);
Board [row][column] = Player1.GetNorX();
for (int i = 0; i < Rows; ++i)
{
for (int j = 0; j < Columns; ++j)
cout << Board [i][j];
cout << endl;
}
/*int row;*/
do
{
cout << "Please enter the value of the row you would like to take ";
cin >> row;
}while (row != 0 && row != 1 && row != 2 && row != 3);
/*int column;*/
do
{
cout << "Please enter the value of the column you would like to take ";
cin >> column;
}while (column != 0 && column != 1 && column != 2 && column != 3);
Board [row][column] = Player2.GetNorX();
for (int i = 0; i < Rows; ++i)
{
for (int j = 0; j < Columns; ++j)
cout << Board [i][j];
cout << endl;
}
if (Board[1][1] == Board[1][2] == Board[1][3] == 'O')
{
cout << endl << "Well done you win";
}
}while (column != 4 && row != 4);
system("pause");
}
问题出现在 if 语句中,因为它似乎对程序的运行没有任何影响。