mardi 29 septembre 2015

ConnectFour Program Issues in Java

I'm creating a Connect Four program from scratch for good practice and I am having trouble with my checkAlignment() method, or what would be the win condition. It works in some rows but not all, and it doesn't work for any other direction (vertically, diagonally forwards, diagonally backwards).

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;

}

Can anyone help me out?

Aucun commentaire:

Enregistrer un commentaire