C ++控制台TicTacToe:检查Win条件

时间:2016-12-12 17:01:44

标签: c++ console tic-tac-toe

游戏板存储为2D字符数组。播放器使用小键盘在棋盘上移动光标,并使用输入键选择 - 光标的当前位置以两个整数存储。

每次移动后,使用以下方法评估棋盘获胜。

void checkwin()
{
    //look along lines from current position
    int x = cursorPosX;
    int y = cursorPosY;
    int c = playerTurn ? 1 : 2; //which mark to look for

    for (int xAxis = 0; xAxis <= 2; xAxis++) //look along x axis
    {
        x = WrapValue(0, sizeof(squares[0]), x + 1);
        if (CheckPos(x, y) != c) //if we don't find the same mark, must not be a horizontal line, otherwise, break out.
        {
            x = cursorPosX; //reset x
            for (int yAxis = 0; yAxis <= 2; yAxis++) //look along y axis
            {
                y = WrapValue(0, sizeof(squares[0]), y + 1);
                if (CheckPos(x, y) != c) 
                {
                    y = cursorPosY;
                    //look for diagonal
                    for (int i = 0; i <= 2; i++ )
                    {
                        x = WrapValue(0, sizeof(squares[0]), x + 1);
                        y = WrapValue(0, sizeof(squares[0]), y + 1);
                        if (CheckPos(x, y) != c)
                        {
                            //failed everything, return
                            winConditions = -1;
                            return;
                        }
                    }
                    break;
                }
            }
            break;
        }
    }
    //if we make it out of the loops, we have a winner.
    winConditions = playerTurn ? 0 : 1;
}

我得到错误的结果 - 在不合适时返回平局或胜利。我几乎可以肯定x和y在某些时候会得到错误的值并开始检查错误的位置。

Visual Studio在进入yAxis循环后停止在x和y上更新监视 - 我不确定原因,但它阻止我跟踪这些值。我是否违反了关于确定范围的规定?这是我使用x和y作为变量名的唯一地方。

下面的相关包装方法。我的目标是始终能够通过添加来检查其他2个空格,无论我在哪个板上

int WrapValue(int min, int max, int value)
{
    auto range = max - min;

    while (value >= max)
    {
        value -= range;
    }
    while (value < min)
    {
        value += range;
    }

    return value;
}

我很感激训练有素的眼睛告诉我这里做错了什么。非常感谢你的时间。

1 个答案:

答案 0 :(得分:1)

嵌套for循环是个糟糕的主意。我通过将代码重构为多个单独的循环来解决问题,每个循环都做一件事,而不是相互陷入更深层次的地狱。

for (int xAxis = 0; xAxis <= 2; xAxis++) //look along x axis
{
    x = WrapValue(0, sizeof(squares[0]), x + 1);
    if (CheckPos(x, y) != c) //if we don't find the same mark, must not be a horizontal line, otherwise, break out.
    {
        x = cursorPosX; //reset x
        break;
    }
    else if (xAxis == 2)
    {
        winConditions = playerTurn ? 0 : 1;
        return;
    }
}

for (int yAxis = 0; yAxis <= 2; yAxis++) //look along y axis
{
    y = WrapValue(0, sizeof(squares[0]), y + 1);
    if (CheckPos(x, y) != c)
    {
        y = cursorPosY;
        break;
    }
    else if (yAxis == 2)
    {
        winConditions = playerTurn ? 0 : 1;
        return;
    }
}
...ect

这违反了DRY,但它确实按照预期的方式工作,我确信我以后可以简化它。

虽然我不完全确定为什么以前的方式不起作用,但我确实意识到这只是糟糕的设计。