내 바카라 게임에 대한 나의 방법 중 하나는 은행가가 플레이어의 손가에 따라 세 번째 카드를 뽑을 수 있는지 확인하는 것입니다.자바 checkstyle에 따라 메서드 내에서 return 문이 너무 많습니다. 최대 반환 수 2를 준수하도록 코드를 어떻게 단순화 할 수 있습니까?
게임 자체에는 은행가가 인출 할 수있는 조건이 많아 많은 조건문이있는 메소드를 만들 수 있습니다. 반환 문이 너무 많아서 2로 제한하는 방법을 모르겠습니다.
/**
* Determines if banker draws third card.
* @param playerCard Card object
* @param bankerHand Hand object
* @return boolean if banker gets third card.
*/
public static boolean mustHit(Card playerCard, Hand bankerHand) {
int playerValue = valueOf(playerCard);
int bankerValue = valueOf(bankerHand);
if (playerValue == 2 || playerValue == 3) { //player has 2 or 3
return bankerValue >= 0 && bankerValue <= 4; //banker must have 0-4
} else if (playerValue == 4 || playerValue == 5) { //player has 4 or 5
return bankerValue >= 0 && bankerValue <= 5; //banker must have 0-5
} else if (playerValue == 6 || playerValue == 7) { //player has 6 or 7
return bankerValue >= 0 && bankerValue <= 8; //banker must have 0-8
} else if (playerValue == 8) { //player has 8
return bankerValue >= 0 && bankerValue <= 2; //banker must have 0-2
} else {
return bankerValue >= 0 && bankerValue <= 3;
//if none of the above, banker must have 0-3
}
}
어떻게 간단하게 할 수 있습니까?
편집 :
수정 됨, 나는 그것을 극복하기가 가장 어려웠습니다. 생성 된 부울 변수를 각 부울을 반환하는 대신 할당했습니다.
/**
* Determines if banker gets third card.
* @param playerCard Card object
* @param bankerHand Hand object
* @return boolean if banker gets third card.
*/
public static boolean mustHit(Card playerCard, Hand bankerHand) {
int playerValue = valueOf(playerCard);
int bankerValue = valueOf(bankerHand);
boolean bankerDraw = false;
if (playerValue == 2 || playerValue == 3) {
bankerDraw = bankerValue >= 0 && bankerValue <= 4;
} else if (playerValue == 4 || playerValue == 5) {
bankerDraw = bankerValue >= 0 && bankerValue <= 5;
} else if (playerValue == 6 || playerValue == 7) {
bankerDraw = bankerValue >= 0 && bankerValue <= 8;
} else if (playerValue == 8) {
bankerDraw = bankerValue >= 0 && bankerValue <= 2;
} else {
bankerDraw = bankerValue >= 0 && bankerValue <= 3;
}
return bankerDraw;
}
이 더 적합합니다. –
당신은 당신의 개인적인 판단 이상으로 자동 판매기를 신뢰해서는 안됩니다. –