当使用 goto 语句时,代码不会在循环内想要的地方输出
When using the goto statement code does not output at wanted inside of a loop
我正在尝试制作一个简单的井字游戏。现在我正在努力使计算机无法放置在二维数组中的某些地方。计算机设置为随机放置,我正在使用循环和转到使其随机,直到找到合适的位置。
#include <iostream>
#include <string>
#include <ctime>
#include <Windows.h>
using namespace std;
string team;
string a = " ";
const int rows = 5;
const int elements = 5;
string Board[rows][elements] = { a, "| ", a, "| ", a,
"- ", "+ ", "- ", "+ ", "- ",
a, "| ", a, "| ", a,
"- ", "+ ", "- ", "+ ", "- ",
a, "| ", a, "| ", a };
void showBoard()
{
for (int i = 0; i < rows; i++) {
for (int j = 0; j < elements; j++) {
cout << Board[i][j];
}
cout << endl;
}
}
int main()
{
int nonFilled = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < elements; j++) {
if (Board[i][j] == a && a == " ")
nonFilled++;
}
}
int circleFilled = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < elements; j++) {
if (Board[i][j] == a && a == "0 ")
circleFilled++;
}
}
int crossFilled = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < elements; j++) {
if (Board[i][j] == a && a == "X ")
crossFilled++;
}
}
cout << "Welcome to tic-tac-toe game." << endl;
cout << endl;
showBoard();
cout << "Please select team if circle or cross: (0/X)" << endl;
cin >> team;
if (team == "0") {
int ifCircle = 1;
cout << "You have selected circle." << endl;
while (nonFilled > circleFilled + crossFilled) {
srand(time(NULL));
int x, y = 0;
int xx, xy = 0;
cout << "Select square: " << endl;
cout << "Y cord (0-4): ";
cin >> x;
cout << endl;
cout << "X cord (0-4): ";
cin >> y;
if (x == 0 || y == 0)
return 0;
/*if (Board[x - 1][y - 1] = "| ", "+ ", "- ")
{
cout << "You cannot place one there. " << endl;
nonFilled = 100;
}*/
int b = 1;
if (Board[x - 1][y - 1] == a)
b = 1;
else
b = 2;
switch (b) {
case 1:
Board[x - 1][y - 1] = "0 ";
break;
case 2:
cout << "You cannot place one there. " << endl;
continue;
}
/* if (Board[x - 1][y - 1] == a)
{
Board[x - 1][y - 1] = "0 ";
}
else
{
cout << "You cannot place one there." << endl;
}*/
cout << endl;
showBoard();
cout << "The opponent will now pick a square:" << endl;
system("pause");
xx = rand() % rows;
xy = rand() % elements;
int c = 1;
if (Board[xx][xy] == a)
c = 1;
else if (Board[xx][xy] == "| ", "+ ", "- ")
c = 2;
switch (c) {
case 1:
Board[xx][xy] = "X ";
break;
case 2: {
LOOP: // here is my label for the goto statement
while (true) // this loop
{
xx = rand() % rows;
xy = rand() % elements;
if (Board[xx][xy] == "| ", "+ ", "- ") {
goto LOOP; // goto statement
}
else {
Board[xx][xy] = "X ";
}
}
}
}
cout << endl;
cout << "The opponent has picked:" << endl;
showBoard();
}
}
else if (team == "X") {
int ifCircle = 0;
}
system("pause");
return 0;
}
有问题的循环在最底部,我不确定是我放置标签的方式有问题还是我使用语句的方式有问题,还是代码的不同部分有问题.
我查看了大量关于 while 循环内 goto 语句的问题,但我找不到任何问题。
眼前的问题既不在于 LOOP:
标签的位置也不在于 goto
语句的使用(我 不是 参与关于该关键字是否应该 ever 在 C++ 程序中使用的争论。
问题出在以下行:
if (Board[xx][xy] == "| ", "+ ", "- ") {
(以及前面几行类似的 if else...
语句)。
这不是做你想做的事!事实上,它将 always return 一个 true
值,因为表达式的净结果很简单, if ("- ")
- 这将永远是一个non-null(即 non-zero)字符串文字的地址。
您需要的(如果您希望将索引 Board[][]
字符串与三个文字中的任何一个相匹配)如下:
if (Board[xx][xy] == "| " || Board[xx][xy] == "+ " || Board[xx][xy] == "- ") {
//...
您的代码使用 comma operator 计算每个 comma-separated 表达式(按 left-to-right 顺序),丢弃除最后一个 (right-most) 之外的每个表达式价值;总体结果就是 right-most 表达式的结果。
编辑:应用我上面建议的修复后,我注意到另一个问题:你的 while
循环,就目前而言,将 never 退出(它会 goto
到 LOOP:
或保持 运行)。这是修复循环的一种(快速)方法,它也消除了对 goto
语句的需要:
case 2:
{
bool done = false;
while (!done)
{
xx = rand() % rows;
xy = rand() % elements;
if (Board[xx][xy] == "| " || Board[xx][xy] == "+ " || Board[xx][xy] == "- ") {
continue;
}
else {
Board[xx][xy] = "X ";
done = true;
}
}
}
有更多 'elegant' 方法可以达到相同的结果,但希望您至少能够理解(并理解)我对您的代码所做的相当小的更改。请随时要求任何进一步的说明 and/or 解释。
我正在尝试制作一个简单的井字游戏。现在我正在努力使计算机无法放置在二维数组中的某些地方。计算机设置为随机放置,我正在使用循环和转到使其随机,直到找到合适的位置。
#include <iostream>
#include <string>
#include <ctime>
#include <Windows.h>
using namespace std;
string team;
string a = " ";
const int rows = 5;
const int elements = 5;
string Board[rows][elements] = { a, "| ", a, "| ", a,
"- ", "+ ", "- ", "+ ", "- ",
a, "| ", a, "| ", a,
"- ", "+ ", "- ", "+ ", "- ",
a, "| ", a, "| ", a };
void showBoard()
{
for (int i = 0; i < rows; i++) {
for (int j = 0; j < elements; j++) {
cout << Board[i][j];
}
cout << endl;
}
}
int main()
{
int nonFilled = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < elements; j++) {
if (Board[i][j] == a && a == " ")
nonFilled++;
}
}
int circleFilled = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < elements; j++) {
if (Board[i][j] == a && a == "0 ")
circleFilled++;
}
}
int crossFilled = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < elements; j++) {
if (Board[i][j] == a && a == "X ")
crossFilled++;
}
}
cout << "Welcome to tic-tac-toe game." << endl;
cout << endl;
showBoard();
cout << "Please select team if circle or cross: (0/X)" << endl;
cin >> team;
if (team == "0") {
int ifCircle = 1;
cout << "You have selected circle." << endl;
while (nonFilled > circleFilled + crossFilled) {
srand(time(NULL));
int x, y = 0;
int xx, xy = 0;
cout << "Select square: " << endl;
cout << "Y cord (0-4): ";
cin >> x;
cout << endl;
cout << "X cord (0-4): ";
cin >> y;
if (x == 0 || y == 0)
return 0;
/*if (Board[x - 1][y - 1] = "| ", "+ ", "- ")
{
cout << "You cannot place one there. " << endl;
nonFilled = 100;
}*/
int b = 1;
if (Board[x - 1][y - 1] == a)
b = 1;
else
b = 2;
switch (b) {
case 1:
Board[x - 1][y - 1] = "0 ";
break;
case 2:
cout << "You cannot place one there. " << endl;
continue;
}
/* if (Board[x - 1][y - 1] == a)
{
Board[x - 1][y - 1] = "0 ";
}
else
{
cout << "You cannot place one there." << endl;
}*/
cout << endl;
showBoard();
cout << "The opponent will now pick a square:" << endl;
system("pause");
xx = rand() % rows;
xy = rand() % elements;
int c = 1;
if (Board[xx][xy] == a)
c = 1;
else if (Board[xx][xy] == "| ", "+ ", "- ")
c = 2;
switch (c) {
case 1:
Board[xx][xy] = "X ";
break;
case 2: {
LOOP: // here is my label for the goto statement
while (true) // this loop
{
xx = rand() % rows;
xy = rand() % elements;
if (Board[xx][xy] == "| ", "+ ", "- ") {
goto LOOP; // goto statement
}
else {
Board[xx][xy] = "X ";
}
}
}
}
cout << endl;
cout << "The opponent has picked:" << endl;
showBoard();
}
}
else if (team == "X") {
int ifCircle = 0;
}
system("pause");
return 0;
}
有问题的循环在最底部,我不确定是我放置标签的方式有问题还是我使用语句的方式有问题,还是代码的不同部分有问题.
我查看了大量关于 while 循环内 goto 语句的问题,但我找不到任何问题。
眼前的问题既不在于 LOOP:
标签的位置也不在于 goto
语句的使用(我 不是 参与关于该关键字是否应该 ever 在 C++ 程序中使用的争论。
问题出在以下行:
if (Board[xx][xy] == "| ", "+ ", "- ") {
(以及前面几行类似的 if else...
语句)。
这不是做你想做的事!事实上,它将 always return 一个 true
值,因为表达式的净结果很简单, if ("- ")
- 这将永远是一个non-null(即 non-zero)字符串文字的地址。
您需要的(如果您希望将索引 Board[][]
字符串与三个文字中的任何一个相匹配)如下:
if (Board[xx][xy] == "| " || Board[xx][xy] == "+ " || Board[xx][xy] == "- ") {
//...
您的代码使用 comma operator 计算每个 comma-separated 表达式(按 left-to-right 顺序),丢弃除最后一个 (right-most) 之外的每个表达式价值;总体结果就是 right-most 表达式的结果。
编辑:应用我上面建议的修复后,我注意到另一个问题:你的 while
循环,就目前而言,将 never 退出(它会 goto
到 LOOP:
或保持 运行)。这是修复循环的一种(快速)方法,它也消除了对 goto
语句的需要:
case 2:
{
bool done = false;
while (!done)
{
xx = rand() % rows;
xy = rand() % elements;
if (Board[xx][xy] == "| " || Board[xx][xy] == "+ " || Board[xx][xy] == "- ") {
continue;
}
else {
Board[xx][xy] = "X ";
done = true;
}
}
}
有更多 'elegant' 方法可以达到相同的结果,但希望您至少能够理解(并理解)我对您的代码所做的相当小的更改。请随时要求任何进一步的说明 and/or 解释。