确定棋盘上的两个方格是否是相同的颜色 - Codefights

时间:2018-04-26 15:48:52

标签: java switch-statement boolean boolean-logic chess

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

  

在标准棋盘上给出两个单元格,确定它们是否具有相同的颜色。

输入为两位数字符串,由一个大写字母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;
        }

我已经过了几个小时,我看不出哪里出错了。请原谅我,如果它是一个我完全不知道的问题。

5 个答案:

答案 0 :(得分:2)

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

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"));
    }
}

答案 1 :(得分:1)

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

case '1': state2=true; return;

否则该程序将继续到最后defaultstate1& state2始终为false

答案 2 :(得分:0)

你可以更清楚地做到这一点:

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.");
            }
        }
    }
}

答案 3 :(得分:0)

  
    

...确定它们是否具有相同的颜色。

  
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;
}

答案 4 :(得分:-1)

/**
 * @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;
}