Tic-tac-toe 程序:通过检查每一行来寻找获胜者时遇到麻烦。我的 while 循环有什么问题?
Tic-tac-toe program :Having trouble with looking for the winner by checking each row. What's the problem with my while loop?
程序只要求玩家输入行和列五次,然后停止。但是程序还在运行ning,只是卡住了。我知道我的 while 循环有一些问题,但不确定究竟是什么导致程序 运行 出错。
(我知道调试器会有帮助,但还没有弄清楚如何在 Visual Code 中设置调试器。如果您知道设置 C++ 调试器的任何重要指南,请告诉我。)
这是我的代码
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
const int ROWS = 3;
const int COLS = 3;
const char *values[ROWS][COLS] = { {"*A", "*A", "*A"} , {"*A", "*A", "*A"}, {"*A", "*A", "*A"}};;
int player_iteration;
int rows;
int cols;
int counter_x;
int counter_y;
//ask the choose of player 1 and player 2 back and forth for a total of 9 times
for (player_iteration = 0; player_iteration < 9; player_iteration++)
{
//player 1
if (player_iteration % 2 == 0)
{
cout << "Please enter Player 1's choose: row colum" << endl;
cout << "row: " ;
cin >> rows;
cout << "colum: ";
cin >> cols;
//check for input validation
while (rows < 0 || rows > 2 || cols < 0 || cols > 2)
{
cout << "Your input is invalid, try again!" << endl;
cout << "row: " ;
cin >> rows;
cout << "colum: ";
cin >> cols;
}
//Check if any duplication occurs
for ( int i = 0 ; i < 1; i++)
{
if ( strcmp(values[rows][cols], "*O") == 0 || strcmp(values[rows][cols], "*X") == 0)
{
cout << "Your input is duplicated, try again!" << endl;
cout << "row: " ;
cin >> rows;
cout << "colum: ";
cin >> cols;
break;
}
else
{
continue;
}
}
values[rows][cols] = "*O";
}
//player 2
else
{
cout << "Please enter Player 2's choose: " << endl;
cout << "row: " ;
cin >> rows;
cout << "colum: ";
cin >> cols;
while (rows < 0 || rows > 2 || cols < 0 || cols > 2)
{
cout << "Your input is invalid, try again!" << endl;
cout << "row: " ;
cin >> rows;
cout << "colum: ";
cin >> cols;
}
//Check if any duplication occurs
for ( int i = 0 ; i < 1; i++)
{
if ( strcmp(values[rows][cols], "*O") == 0 || strcmp(values[rows][cols], "*X") == 0)
{
cout << "Your input is duplicated, try again!" << endl;
cout << "row: " ;
cin >> rows;
cout << "colum: ";
cin >> cols;
break;
}
else
{
continue;
}
}
values[rows][cols] = "*X";
}
//check if the winner exists after the second round
while ( player_iteration > 3)
{ //check by row
for (counter_x = 0; counter_x < 3; counter_x++)
{
if(strcmp(values[counter_x][0], "*O") == 0)
{
if (strcmp(values[counter_x][0], values[counter_x][1] ) == 0)
{
if (strcmp(values[counter_x][1], values[counter_x][2] ) == 0)
{
cout << "Player 1 won" << endl;
return 0;
}
}
}
else if(strcmp(values[counter_x][0], "*X") == 0)
{
if (strcmp(values[counter_x][0], values[counter_x][1] ) == 0)
{
if (strcmp(values[counter_x][1], values[counter_x][2] ) == 0)
{
cout << "Player 2 won" << endl;
return 0;
}
}
}
else
{
continue;
}
}
}
//check by column
}
}
输出就在这里
PS C:\Users\xueyi\Desktop\Sac_City\C C++> cd "c:\Users\xueyi\Desktop\Sac_City\C C++\" ; if ($?) { g++ tempCodeRunnerFile.cpp -o tempCodeRunnerFile } ; if ($?) { .\tempCodeRunnerFile }
Please enter Player 1's choose: row colum
row: 0
colum: 0
Please enter Player 2's choose:
row: 1
colum: 0
Please enter Player 1's choose: row colum
row: 2
colum: 0
Please enter Player 2's choose:
row: 1
colum: 1
Please enter Player 1's choose: row colum
row: 2
colum: 2
您处于无限循环中:您的
while (player_iteration > 3)
永不退出。
程序只要求玩家输入行和列五次,然后停止。但是程序还在运行ning,只是卡住了。我知道我的 while 循环有一些问题,但不确定究竟是什么导致程序 运行 出错。
(我知道调试器会有帮助,但还没有弄清楚如何在 Visual Code 中设置调试器。如果您知道设置 C++ 调试器的任何重要指南,请告诉我。)
这是我的代码
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
const int ROWS = 3;
const int COLS = 3;
const char *values[ROWS][COLS] = { {"*A", "*A", "*A"} , {"*A", "*A", "*A"}, {"*A", "*A", "*A"}};;
int player_iteration;
int rows;
int cols;
int counter_x;
int counter_y;
//ask the choose of player 1 and player 2 back and forth for a total of 9 times
for (player_iteration = 0; player_iteration < 9; player_iteration++)
{
//player 1
if (player_iteration % 2 == 0)
{
cout << "Please enter Player 1's choose: row colum" << endl;
cout << "row: " ;
cin >> rows;
cout << "colum: ";
cin >> cols;
//check for input validation
while (rows < 0 || rows > 2 || cols < 0 || cols > 2)
{
cout << "Your input is invalid, try again!" << endl;
cout << "row: " ;
cin >> rows;
cout << "colum: ";
cin >> cols;
}
//Check if any duplication occurs
for ( int i = 0 ; i < 1; i++)
{
if ( strcmp(values[rows][cols], "*O") == 0 || strcmp(values[rows][cols], "*X") == 0)
{
cout << "Your input is duplicated, try again!" << endl;
cout << "row: " ;
cin >> rows;
cout << "colum: ";
cin >> cols;
break;
}
else
{
continue;
}
}
values[rows][cols] = "*O";
}
//player 2
else
{
cout << "Please enter Player 2's choose: " << endl;
cout << "row: " ;
cin >> rows;
cout << "colum: ";
cin >> cols;
while (rows < 0 || rows > 2 || cols < 0 || cols > 2)
{
cout << "Your input is invalid, try again!" << endl;
cout << "row: " ;
cin >> rows;
cout << "colum: ";
cin >> cols;
}
//Check if any duplication occurs
for ( int i = 0 ; i < 1; i++)
{
if ( strcmp(values[rows][cols], "*O") == 0 || strcmp(values[rows][cols], "*X") == 0)
{
cout << "Your input is duplicated, try again!" << endl;
cout << "row: " ;
cin >> rows;
cout << "colum: ";
cin >> cols;
break;
}
else
{
continue;
}
}
values[rows][cols] = "*X";
}
//check if the winner exists after the second round
while ( player_iteration > 3)
{ //check by row
for (counter_x = 0; counter_x < 3; counter_x++)
{
if(strcmp(values[counter_x][0], "*O") == 0)
{
if (strcmp(values[counter_x][0], values[counter_x][1] ) == 0)
{
if (strcmp(values[counter_x][1], values[counter_x][2] ) == 0)
{
cout << "Player 1 won" << endl;
return 0;
}
}
}
else if(strcmp(values[counter_x][0], "*X") == 0)
{
if (strcmp(values[counter_x][0], values[counter_x][1] ) == 0)
{
if (strcmp(values[counter_x][1], values[counter_x][2] ) == 0)
{
cout << "Player 2 won" << endl;
return 0;
}
}
}
else
{
continue;
}
}
}
//check by column
}
}
输出就在这里
PS C:\Users\xueyi\Desktop\Sac_City\C C++> cd "c:\Users\xueyi\Desktop\Sac_City\C C++\" ; if ($?) { g++ tempCodeRunnerFile.cpp -o tempCodeRunnerFile } ; if ($?) { .\tempCodeRunnerFile }
Please enter Player 1's choose: row colum
row: 0
colum: 0
Please enter Player 2's choose:
row: 1
colum: 0
Please enter Player 1's choose: row colum
row: 2
colum: 0
Please enter Player 2's choose:
row: 1
colum: 1
Please enter Player 1's choose: row colum
row: 2
colum: 2
您处于无限循环中:您的
while (player_iteration > 3)
永不退出。