Skip to content

Commit

Permalink
[refactor] 외부에서 필드 값 변경으로 인한 사이드 이팩트 발생 가능성 고려
Browse files Browse the repository at this point in the history
- 외부에서 클래스 내의 필드 값 변경하지 않도록 수정
- 필드를 생성하지 않고 단순히 strike와 ball 계산 후 값 return
  • Loading branch information
mia2583 committed Jan 3, 2023
1 parent 9acd60f commit 03e9195
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
9 changes: 3 additions & 6 deletions baseball_game/src/main/java/Game/Answer.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ public Boolean checkAnswer(int guess) {
int tens = (answerNumber/10)%10;
int hundreds = answerNumber/100;

// strike 수 세기
Evaluate.countStrike(guess, hundreds, tens, units);
Evaluate.countBall(guess, hundreds, tens, units);

int strike = Evaluate.getStrike();
int ball = Evaluate.getBall();
// strike, ball 수 세기
int strike = Evaluate.countStrike(guess, hundreds, tens, units);
int ball = Evaluate.countBall(guess, hundreds, tens, units);

if(strike==0 && ball==0) {
System.out.print("낫싱\n");
Expand Down
23 changes: 5 additions & 18 deletions baseball_game/src/main/java/Game/Evaluate.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
package Game;

public class Evaluate {
private int inputNumber;
private static int strike;
private static int ball;
public Evaluate(int guess, int answer) {}

public Evaluate(int guess, int answer) {

}

public static int getStrike() {
return strike;
}
public static int getBall() {
return ball;
}

public static void countStrike(int guess, int hundreds, int tens, int units) {
public static int countStrike(int guess, int hundreds, int tens, int units) {
int strikeN = 0;
if(guess%10 == units) strikeN++;
if((guess/10)%10 == tens) strikeN++;
if(guess/100 == hundreds) strikeN++;
strike = strikeN;
return strikeN;
}

public static void countBall(int guess, int hundreds, int tens, int units) {
public static int countBall(int guess, int hundreds, int tens, int units) {
int ballN = 0;
if((guess%10==hundreds) || (guess%10==tens)) ballN++;
if(((guess/10)%10==hundreds) || ((guess/10)%10==units)) ballN++;
if((guess/100==tens) || (guess/100==units)) ballN++;
ball = ballN;
return ballN;
}

}
Expand Down

0 comments on commit 03e9195

Please sign in to comment.