判断棋盘上的两个方块是否颜色相同 - Codefights

Determine if two squares on a chessboard are the same color - Codefights

我一直在努力弄清楚为什么我的代码在 CodeFights chessBoardCellColor 街机挑战中没有返回正确的结果。

Given two cells on the standard chess board, determine whether they have the same color or not.

输入为两位数字字符串,由一个大写字母 A-H 后跟一个数字 1-8 组成。

    boolean chessBoardCellColor(String cell1, String cell2) {    
        return isWhite(cell1)==isWhite(cell2);
    }

    boolean isWhite(String digits){
        boolean state1=false; 
        boolean state2=false;
        boolean white = false;
        char[] digs = digits.toCharArray();
            switch(digs[0]){
                case 'A': state1=true;
                case 'C': state1=true;
                case 'E': state1=true;
                case 'G': state1=true;
                default : state1=false;}
            switch(digs[1]){
                case '1': state2=true;
                case '3': state2=true;
                case '5': state2=true;
                case '7': state2=true;
                default : state2=false;}
        if(state1==state2){white=false;}
        else{white=true;}
        return white;
        }

我已经研究了几个小时了,但我看不出哪里出了问题。如果我遗漏了一个非常明显的问题,请原谅我。

您应该在每个 case xx 之后添加 break,例如:

case '1': state2=true; return;

否则程序会一直持续到最后defaultstate1&state2永远是false

我认为逻辑比需要的更复杂。我会先检查列,然后检查行。像这样:

public final class Chess {
    public boolean isBlack(String field) {
        char[] digs = field.toCharArray();
        return (isOddColumn(digs[0]) && isOddRow(digs[1])) || (isEvenColumn(digs[0]) && isEvenRow(digs[1]));
    }

    public boolean isWhite(String field) {
        return !isBlack(field);
    }

    private boolean isOddColumn(char col) {
        return col == 'A' || col == 'C' || col == 'E' || col == 'G';
    }

    private boolean isEvenColumn(char col) {
        return col == 'B' || col == 'D' || col == 'F' || col == 'H';
    }

    private boolean isOddRow(char row) {
        return row == '1' || row == '3' || row == '5' || row == '7';
    }

    private boolean isEvenRow(char row) {
        return row == '2' || row == '4' || row == '6' || row == '8';
    }

    public static void main(String[] args) {
        Chess app = new Chess();
        System.out.println(app.isBlack("A1"));
        System.out.println(app.isBlack("B2"));
        System.out.println(app.isBlack("C3"));
    }
}

你可以这样做更清楚:

private static final String ROW = "12345678";
private static final String COL = "ABCDEFGH";

boolean myIsWhite(String loc) {
    // A row's leftmost square is white every other row starting at 1.
    boolean rowStartsWhite = (ROW.indexOf(loc.charAt(1)) % 2) == 0;
    // They then alternate
    boolean cellIsWhiteIfRowStartsWhite = (COL.indexOf(loc.charAt(0)) % 2) == 0;
    return rowStartsWhite ? cellIsWhiteIfRowStartsWhite : !cellIsWhiteIfRowStartsWhite;
}

public void test(String[] args) {
    for(int r = 0; r < ROW.length(); r++) {
        for(int c = 0; c < COL.length(); c++) {
            String loc = ""+COL.charAt(c)+ROW.charAt(r);
            boolean isWhite = myIsWhite(loc);
            System.out.println("Loc = "+loc+" is "+(isWhite?"White":"Black"));
            if(isWhite != isWhite(loc)) {
                System.out.println("You were wrong with this one.");
            }
        }
    }
}

...determine whether they have the same color or not.

boolean chessBoardCellColor(String cell1, String cell2)

你不需要知道颜色,你需要知道它们是否相同:

boolean isSameColor(String square0, String square1) {
    return
    (Character.codePointAt(square0, 0) + Character.codePointAt(square0, 1)) % 2 ==
    (Character.codePointAt(square1, 0) + Character.codePointAt(square1, 1)) % 2;
}

/**
 * @function chessBoardCellColor
 * @param {String} cell1
 * @param {String} cell2
 * @return {Boolean}
 */

function chessBoardCellColor(cell1: string, cell2: string): boolean {
  return Math.abs((cell1.charCodeAt(0) - cell2.charCodeAt(0))) % 2
    === Math.abs((cell1.charCodeAt(1) - cell2.charCodeAt(1))) % 2;
}