在这种情况下我可以使用goto吗
May I use goto in such situation?
我想知道,在这种情况下使用goto是否可以?你能提出更好的解决方案吗?我看到了唯一一个在cicle时获得第二名的,但之后需要调用"makeMove"两次。
void BoardView::startGame()
{
int currStep=0;
int x,y;
while (board_->isWin()==none)
{
currStep++;
show();
wrong:
std::cout << " Player " << (currStep%2==0 ? 1 : 2) << ": ";
std::cin >> x;
y=x%10;
x/=10;
if (!board_->makeMove(x,y,(currStep%2==0 ? cross : zero)))
{
std::cout << "Wrong move! Try again.n";
goto wrong;
}
}
}
不要使用goto
。当你成功移动时,使用while (true)
循环和break
循环。
while (true) {
std::cout << " Player " << (currStep%2==0 ? 1 : 2) << ": ";
std::cin >> x;
y=x%10;
x/=10;
if (board_->makeMove(x,y,(currStep%2==0 ? cross : zero)))
break;
std::cout << "Wrong move! Try again.n";
}
可能:
void BoardView::startGame()
{
int currStep=1;
int x,y;
show();
while (board_->isWin()==none)
{
std::cout << " Player " << (currStep%2==0 ? 1 : 2) << ": ";
std::cin >> x;
y=x%10;
x/=10;
if (!board_->makeMove(x,y,(currStep%2==0 ? cross : zero)))
{
std::cout << "Wrong move! Try again.n";
continue;
}
++currStep;
show();
}
}
这并不完全相同,但它不使用goto。
是的,您可以进行这样的跳转,尽管通常最好避免goto
。你可以这样重写它,例如:
void BoardView::startGame()
{
int currStep=1;
int x,y;
show();
while (board_->isWin()==none)
{
std::cout << " Player " << (currStep%2==0 ? 1 : 2) << ": ";
std::cin >> x;
y=x%10;
x/=10;
if (board_->makeMove(x,y,(currStep%2==0 ? cross : zero)))
{
currStep++;
show();
}
else
{
std::cout << "Wrong move! Try again.n";
}
}
}
一般建议是避免GOTO语句,但是,请参阅修改后的代码
void BoardView::startGame()
{
int currStep=0;
int x,y;
while (board_->isWin()==none) {
currStep++;
show();
int retry = 0; /* So that 'retry' is visible to do while loop */
do {
retry = 0;
std::cout << " Player " << (currStep%2==0 ? 1 : 2) << ": ";
std::cin >> x;
y=x%10;
x/=10;
if (!board_->makeMove(x,y,(currStep%2==0 ? cross : zero))) {
std::cout << "Wrong move! Try again.n";
retry = 1
}
} while (retry);
}
}
只有在大型嵌套程序中使用时,才应该尽可能避免goto。否则,goto的使用会使程序变得不可靠、不可读并且难以调试。goto的另一个大问题是,当我们使用它们时,我们永远无法确定我们是如何在代码中达到某一点的。它们模糊了控制流。所以要避开它们。
我建议用两个while循环。。。。它会更好。。。
有什么问题
std::pair<int, int> BoardView::getNextMove()
{
std::cout << " Player " << (currStep & 2 == 0 ? 1 : 2) << ": ";
int tmp;
std::cin >> temp;
return std::pair<int, int>( tmp / 10, tmp % 10 );
}
void BoardView::startGame()
{
int currentStep = 0;
while ( myBoard->isWin() == none ) {
std::pair<int, int> move = getNextMove();
while ( ! myBoard->makeMove( move, (currentStep % 2 == 0 ? cross : zero) ) {
std::cout << "Wrong move! Try again" << std::endl;
move = getNextMove();
}
}
}
(尽管我更喜欢Move类型的显式类,而不是仅CCD_ 5。成员row
和column
要明确得多比CCD_ 8和CCD_
通常,如果你被goto
(甚至continue
或break
),这是在单个函数中投入过多的症状。
两个循环,没有常量条件表达式,只有一个对makeMove:的调用
void BoardView::startGameLoop()
{
int currStep = 0;
int x,y;
while (none == board_->isWin())
{
++currStep;
show();
for (;;)
{
std::cout << " Player " << ((currStep & 1) + 1) << ": ";
std::cin >> x;
y = x % 10;
x /= 10;
if (!board_->makeMove(x, y, (currStep & 1) ? zero : cross))
{
std::cout << "Wrong move! Try again.n";
continue;
}
break;
}
}
}
相关文章:
- 是否可以使C++类成为Objc类的委托
- 是否可以使一个类成为两个不同层次结构的子类?
- 是否有编译器标志可以使较新的 gcc 版本像旧版本一样构建
- C 可以使destuructor不称为班级成员和基类攻击方的灾难
- 使用 goto 可以创建编译器无法在C++生成的优化吗?
- 可以使未命名的结构静态
- 是否可以使整数仅收到一个单个数字而不是两个接收输入
- C++:有哪些常规方法可以使代码更有效地用于大数字
- 是否可以使 std 容器使用默认运算符为新?
- LD_BIND_NOW可以使可执行文件运行得更慢?
- 是否可以使头文件使文本居中?- 在控制台中
- 有什么方法可以使核心忙碌等待
- 有没有一种技术可以使虚函数在所有派生类中强制重写?
- 有没有一种方法可以使全局函数/静态成员函数一次可呼出
- 是否有一种方法可以使此C 14递归模板在C 17中短
- 编写一个可以使二维数组平坦的函数
- 有哪些优化技巧可以使我的代码运行得更快
- 有没有更好的方法可以使此代码线程安全?线程局部静态似乎是一个生硬的工具
- 如果必须在同一向量上写入线程,是否可以使用线程
- 对于具有两个模板化变量的模板化类,是否可以使一个 var 引用另一个 var