Issue
I have been struggling to figure out why my code isn't returning correct results on the CodeFights chessBoardCellColor arcade challenge.
Given two cells on the standard chess board, determine whether they have the same color or not.
The inputs are given as two digit strings made up of one uppercase letter A-H followed by one number 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;
}
I've been going over this for a few hours and I cannot see where I went wrong. Forgive me if its a totally obvious problem that I'm missing.
Solution
You should add break
after every case xx
, for example:
case '1': state2=true; return;
otherwise the program will continue to the last default
, and state1
& state2
will always be false
.
Answered By - xingbin
Answer Checked By - Clifford M. (JavaFixing Volunteer)