在这种情况下我可以使用goto吗

May I use goto in such situation?

本文关键字:goto 可以使 这种情况下 我可以      更新时间:2023-10-16

我想知道,在这种情况下使用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。成员rowcolumn要明确得多比CCD_ 8和CCD_

通常,如果你被goto(甚至continuebreak),这是在单个函数中投入过多的症状。

两个循环,没有常量条件表达式,只有一个对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;
        }
    }
}