【发布时间】:2012-03-28 19:43:23
【问题描述】:
我想知道,如果在这种情况下使用 goto ok?你能提出更好的解决方案吗?我看到只有一个在 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;
}
}
}
【问题讨论】:
-
一个循环或一个单独的函数。
-
@PlasmaHH 一个循环和一个单独的函数。输入确实需要分解成一个单独的函数。
-
@JamesKanze:我没有使用异或:P
-
@karlphillip:该示例适用于 C。首选 RAII 用于在 C++ 中进行清理。
-
@Fippo 如果是为了自学,更重要的是做对了。