Упрощение итеративного логического цикла, оценка легального шахматного хода в идиоматической Java - PullRequest
0 голосов
/ 04 февраля 2019

Позвольте мне сначала заявить, что я хорошо знаю, что то, что вы собираетесь увидеть, шокирует и беспокоит, однако, оно работает, и поэтому я его использую, но мне действительно нужно его почистить.Дело в том, что я пока не смог его зафиксировать.

Вот оно:

private boolean threatSpaceQueenPlusPlus() {
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {

            if (proposedCoordinates[0] + 1 <= 7 && proposedCoordinates[0] + 1 >= 0 && proposedCoordinates[1] - 1 <= 7 && proposedCoordinates[1] - 1 >= 0) {
                if (inputPieceMatrix[proposedCoordinates[0] + 1][proposedCoordinates[1] - 1] instanceof WhiteQueen) {
                    return true;
                }
            }
            /* * */
            if (proposedCoordinates[0] + 2 <= 7 && proposedCoordinates[0] + 2 >= 0 && proposedCoordinates[1] - 2 <= 7 && proposedCoordinates[1] - 2 >= 0) {
                if (inputPieceMatrix[proposedCoordinates[0] + 2][proposedCoordinates[1] - 2] instanceof WhiteQueen) {
                    if ((inputPieceMatrix[proposedCoordinates[0] + 1][proposedCoordinates[1] - 1] == null) ||
                        (inputPieceMatrix[proposedCoordinates[0] + 1][proposedCoordinates[1] - 1] instanceof LegalMove)) {
                        return true;
                    }
                }
            }
            /* * */
            if (proposedCoordinates[0] + 3 <= 7 && proposedCoordinates[0] + 3 >= 0 && proposedCoordinates[1] - 3 <= 7 && proposedCoordinates[1] - 3 >= 0) {
                if (inputPieceMatrix[proposedCoordinates[0] + 3][proposedCoordinates[1] - 3] instanceof WhiteQueen) {
                    if ((inputPieceMatrix[proposedCoordinates[0] + 2][proposedCoordinates[1] - 2] == null) ||
                        (inputPieceMatrix[proposedCoordinates[0] + 2][proposedCoordinates[1] - 2] instanceof LegalMove)) {
                        if ((inputPieceMatrix[proposedCoordinates[0] + 1][proposedCoordinates[1] - 1] == null) ||
                            (inputPieceMatrix[proposedCoordinates[0] + 1][proposedCoordinates[1] - 1] instanceof LegalMove)) {
                            return true;
                        }
                    }
                }
            }
            /* * */
            if (proposedCoordinates[0] + 4 <= 7 && proposedCoordinates[0] + 4 >= 0 && proposedCoordinates[1] - 4 <= 7 && proposedCoordinates[1] - 4 >= 0) {
                if (inputPieceMatrix[proposedCoordinates[0] + 4][proposedCoordinates[1] - 4] instanceof WhiteQueen) {
                    if ((inputPieceMatrix[proposedCoordinates[0] + 3][proposedCoordinates[1] - 3] == null) ||
                        (inputPieceMatrix[proposedCoordinates[0] + 3][proposedCoordinates[1] - 3] instanceof LegalMove)) {
                        if ((inputPieceMatrix[proposedCoordinates[0] + 2][proposedCoordinates[1] - 2] == null) ||
                            (inputPieceMatrix[proposedCoordinates[0] + 2][proposedCoordinates[1] - 2] instanceof LegalMove)) {
                            if ((inputPieceMatrix[proposedCoordinates[0] + 1][proposedCoordinates[1] - 1] == null) ||
                                (inputPieceMatrix[proposedCoordinates[0] + 1][proposedCoordinates[1] - 1] instanceof LegalMove)) {
                                return true;
                            }
                        }
                    }
                }
            }
            /* * */
            if (proposedCoordinates[0] + 5 <= 7 && proposedCoordinates[0] + 5 >= 0 && proposedCoordinates[1] - 5 <= 7 && proposedCoordinates[1] - 5 >= 0) {
                if (inputPieceMatrix[proposedCoordinates[0] + 5][proposedCoordinates[1] - 5] instanceof WhiteQueen) {
                    if ((inputPieceMatrix[proposedCoordinates[0] + 4][proposedCoordinates[1] - 4] == null) ||
                        (inputPieceMatrix[proposedCoordinates[0] + 4][proposedCoordinates[1] - 4] instanceof LegalMove)) {
                        if ((inputPieceMatrix[proposedCoordinates[0] + 3][proposedCoordinates[1] - 3] == null) ||
                            (inputPieceMatrix[proposedCoordinates[0] + 3][proposedCoordinates[1] - 3] instanceof LegalMove)) {
                            if ((inputPieceMatrix[proposedCoordinates[0] + 2][proposedCoordinates[1] - 2] == null) ||
                                (inputPieceMatrix[proposedCoordinates[0] + 2][proposedCoordinates[1] - 2] instanceof LegalMove)) {
                                if ((inputPieceMatrix[proposedCoordinates[0] + 1][proposedCoordinates[1] - 1] == null) ||
                                    (inputPieceMatrix[proposedCoordinates[0] + 1][proposedCoordinates[1] - 1] instanceof LegalMove)) {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
            /* * */
            if (proposedCoordinates[0] + 6 <= 7 && proposedCoordinates[0] + 6 >= 0 && proposedCoordinates[1] - 6 <= 7 && proposedCoordinates[1] - 6 >= 0) {
                if (inputPieceMatrix[proposedCoordinates[0] + 6][proposedCoordinates[1] - 6] instanceof WhiteQueen) {
                    if ((inputPieceMatrix[proposedCoordinates[0] + 5][proposedCoordinates[1] - 5] == null) ||
                        (inputPieceMatrix[proposedCoordinates[0] + 5][proposedCoordinates[1] - 5] instanceof LegalMove)) {
                        if ((inputPieceMatrix[proposedCoordinates[0] + 4][proposedCoordinates[1] - 4] == null) ||
                            (inputPieceMatrix[proposedCoordinates[0] + 4][proposedCoordinates[1] - 4] instanceof LegalMove)) {
                            if ((inputPieceMatrix[proposedCoordinates[0] + 3][proposedCoordinates[1] - 3] == null) ||
                                (inputPieceMatrix[proposedCoordinates[0] + 3][proposedCoordinates[1] - 3] instanceof LegalMove)) {
                                if ((inputPieceMatrix[proposedCoordinates[0] + 2][proposedCoordinates[1] - 2] == null) ||
                                    (inputPieceMatrix[proposedCoordinates[0] + 2][proposedCoordinates[1] - 2] instanceof LegalMove)) {
                                    if ((inputPieceMatrix[proposedCoordinates[0] + 1][proposedCoordinates[1] - 1] == null) ||
                                        (inputPieceMatrix[proposedCoordinates[0] + 1][proposedCoordinates[1] - 1] instanceof LegalMove)) {
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            /* * */
            if (proposedCoordinates[0] + 7 <= 7 && proposedCoordinates[0] + 7 >= 0 && proposedCoordinates[1] - 7 <= 7 && proposedCoordinates[1] - 7 >= 0) {
                if (inputPieceMatrix[proposedCoordinates[0] + 7][proposedCoordinates[1] - 7] instanceof WhiteQueen) {
                    if ((inputPieceMatrix[proposedCoordinates[0] + 6][proposedCoordinates[1] - 6] == null) ||
                        (inputPieceMatrix[proposedCoordinates[0] + 6][proposedCoordinates[1] - 6] instanceof LegalMove)) {
                        if ((inputPieceMatrix[proposedCoordinates[0] + 5][proposedCoordinates[1] - 5] == null) ||
                            (inputPieceMatrix[proposedCoordinates[0] + 5][proposedCoordinates[1] - 5] instanceof LegalMove)) {
                            if ((inputPieceMatrix[proposedCoordinates[0] + 4][proposedCoordinates[1] - 4] == null) ||
                                (inputPieceMatrix[proposedCoordinates[0] + 4][proposedCoordinates[1] - 4] instanceof LegalMove)) {
                                if ((inputPieceMatrix[proposedCoordinates[0] + 3][proposedCoordinates[1] - 3] == null) ||
                                    (inputPieceMatrix[proposedCoordinates[0] + 3][proposedCoordinates[1] - 3] instanceof LegalMove)) {
                                    if ((inputPieceMatrix[proposedCoordinates[0] + 2][proposedCoordinates[1] - 2] == null) ||
                                        (inputPieceMatrix[proposedCoordinates[0] + 2][proposedCoordinates[1] - 2] instanceof LegalMove)) {
                                        if ((inputPieceMatrix[proposedCoordinates[0] + 1][proposedCoordinates[1] - 1] == null) ||
                                            (inputPieceMatrix[proposedCoordinates[0] + 1][proposedCoordinates[1] - 1] instanceof LegalMove)) {
                                            return true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            /* *** */
        }
    }
    return false;
}

Это проверяет, что пространство, в которое вы собираетесь двигаться, какКороль в шахматах, не находится под угрозой от королевы или епископа.

Как я могу сделать это с помощью цикла?Это помогло бы мне обобщить это на случай, когда мы не просто проверяем направление положительной разности обеих координат.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...