java - Java 中的 ConnectFour 程序问题

标签 java arrays if-statement for-loop matrix

我正在从头开始创建一个 Connect Four 程序以进行良好的实践,但我在使用 checkAlignment() 方法时遇到了问题,或者获胜条件是什么。它适用于某些行,但不是所有行,并且不适用于任何其他方向(垂直、对角向前、对角向后)。

public char checkAlignment(int row, int column) {
char color = board[row][column];
char[][] current = getBoard();

// Horizontal Left-to-Right Check - - - - - - - - - -
if (column + 4 <= columns) {
    for (int i = 1; i < 4; i++) {
        if (current[row][column + i] != color) {
            return NONE;
        }
    }
    return color;
}

// Horizontal Right-To-Left Check - - - - - - - -
if (column - 4 > -1) {
    for (int i = 1; i < 4; i++) {
        if (current[row][column - i] != color) {
            return NONE;
        }
    }
    return color;
}

//  Vertical Top-To-Bottom Check - - - - - - -
if (row + 4 <= rows) {
    for (int i = 1; i < 4; i++) {
        if (current[row + i][column] != color) {
            return NONE;
        }
    }
    return color;
}

// Vertical Bottom-To-Top Check - - - - - - - -
if (row - 4 > -1) {
    for (int i = 1; i < 4; i++) {
        if (current[row - i][column] != color) {
            return NONE;
        }
    }
    return color;
}

// Main Diagonal Backwards Check - - - - - - - - - -
if (column - 4 > -1 && row - 4 > -1) {
    for (int i = 1; i < 4; i++) {
        for (int j = 1; j < 4; j++) {
            if (current[row - i][column - j] != color) {
                return NONE;
            }
        }
    }
    return color;
}

// Main Diagonal Forwards Check - - - - - - - - - -
if (column + 4 <= columns && row + 4 <= rows) {
    for (int i = 1; i < 4; i++) {
        for (int j = 1; j < 4; j++) {
            if (current[row + i][column + j] != color) {
                return NONE;
            }
        }
    }
    return color;
}

// Secondary Diagonal Backwards Check - - - - - - - - -
if (column - 4 > -1 && row + 4 <= rows) {
    for (int i = 1; i < 4; i++) {
        for (int j = 1; j < 4; j++) {
            if (current[row + i][column - j] != color) {
                return NONE;
            }
        }
    }
    return color;
}
// Secondary Diagonal Forwards Check - - - - - - - - - -
if (column + 4 <= columns && row - 4 > -1) {
    for (int i = 1; i < 4; i++) {
        for (int j = 1; j < 4; j++) {
            if (current[row - i][column + j] != color) {
                return NONE;
            }
        }
    }
    return color;
}
return NONE;

}

谁能帮帮我吗?

编辑/调整:

public char checkAlignment(int row, int column) {
    char color = board[row][column];
    char[][] current = getBoard();

    // Horizontal Left-to-Right Check
    if (column + 4 <= NUM_COLS) {
        for (int i = 0; i < 4; i++) {
            if (current[row][column + i] != color) {
                return NONE;
            }
        }
        return color;
    }

    // Horizontal Right-To-Left Check
    if (column - 4 > -1) {
        for (int i = 0; i < 4; i++) {
            if (current[row][column - i] != color) {
                return NONE;
            }
        }
        return color;
    }

    //  Vertical Top-To-Bottom Check
    if (row + 4 <= NUM_ROWS) {
        for (int i = 0; i < 4; i++) {
            if (current[row + i][column] != color) {
                return NONE;
            }
        }
        return color;
    }

    // Vertical Bottom-To-Top Check
    if (row - 4 > -1) {
        for (int i = 0; i < 4; i++) {
            if (current[row - i][column] != color) {
                return NONE;
            }
        }
        return color;
    }

    // Main Diagonal Backwards Check
    if (column - 4 > -1 && row - 4 > -1) {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (current[row - i][column - j] != color) {
                    return NONE;
                }
            }
        }
        return color;
    }

    // Main Diagonal Forwards Check - - - - - - - - - -
    if (column + 4 <= NUM_COLS && row + 4 <= NUM_ROWS) {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (current[row + i][column + j] != color) {
                    return NONE;
                }
            }
        }
        return color;
    }

    // Secondary Diagonal Backwards Check - - - - - - - - -
    if (column - 4 > -1 && row + 4 <= NUM_ROWS) {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (current[row + i][column - j] != color) {
                    return NONE;
                }
            }
        }
        return color;
    }
    // Secondary Diagonal Forwards Check - - - - - - - - - -
    if (column + 4 <= NUM_COLS && row - 4 > -1) {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (current[row - i][column + j] != color) {
                    return NONE;
                }
            }
        }
        return color;
    }
    return NONE;
}

最佳答案

从外观上看,每个循环都使用 i=1,然后最多使用 4 个循环,每个循环运行四次。但是,您的行和列始终会被检查为距当前值 +/- 4。如果我没看错的话,在某些情况下,你会比需要的多检查一项。因此,要么使 i=0 要么达到 3(因为您已经用当前检查了自己的方 block )。

编辑:我再看了一下

我上面的第一句话并不是真正的问题。我认为真正的问题是,对于每一项,您只在远离“当前”一项的单一方向上检查 4 项。这并不总是正确的,如以下示例所示,其中“y”是最后放入网格中的一 block 。

x x y x

因此,在这种情况下,您可以尝试以下代码来检查“当前”所选 token 左右两侧的总数。如果两个方向上有 4 个或更多连接,则为 connect-4:

//Horizantal check
//Right side
int connected = 0;
int i = 1;
while (column + i <= columns) {
    if (current[row + i][column] != color) {
        break;
    }else{
        connected++;
    }
    i++;
}   
i = 1;
//left side
while (column - i <= columns) {
    if (current[row - i][column] != color) {
        break;
    }else{
        connected++;
    }
    i++;
}
if(connected >= 4){//total connected
    return color;
}else{
    return NONE;
}

经审查,您的垂直平纹支票看起来不错。回想一下,在 connect-4 中,您将 token 沿着网格放下,因此您永远无法将新 token 放入其上方有另一个 token 的插槽中(在您放下它的回合中),这意味着不需要向上进行垂直检查。

//Vertical plain check - The one dropped must always be on top
if (row - 4 > -1) {
    for(int i=1; i<4;i++){
        if (current[row - i][column] != color) {
            return NONE;
        }
    }
    return color;
}

希望这对正确理解这两个方向有所帮助。对于对角线,你需要像我对水平平面所做的那样。

关于java - Java 中的 ConnectFour 程序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32857403/

相关文章:

php - 如何在不使用 PHP 循环的情况下计算多维数组的元素数?

python - 如何在python中随机采样矩阵?

java - Java 中仅出现句子的第一个单词

java - GetResourceAsStream 返回 null,文件存在

java - 投影矩阵 OpenGL/GLSL 问题

java - 从bluemix上的Liberty服务器调用javamail api

java - 如何在xHtml中的IF-Else语句中插入HTML代码?

java - 将 wicket 1.4 迁移到 1.5 - PageParameters.getAsEnum()

php - 如何将数组键复制到另一个数组,并用空数组替换值?

java if long 不起作用