Skip to content

Commit 03e9195

Browse files
committed
[refactor] 외부에서 필드 값 변경으로 인한 사이드 이팩트 발생 가능성 고려
- 외부에서 클래스 내의 필드 값 변경하지 않도록 수정 - 필드를 생성하지 않고 단순히 strike와 ball 계산 후 값 return
1 parent 9acd60f commit 03e9195

File tree

2 files changed

+8
-24
lines changed

2 files changed

+8
-24
lines changed

baseball_game/src/main/java/Game/Answer.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,9 @@ public Boolean checkAnswer(int guess) {
3333
int tens = (answerNumber/10)%10;
3434
int hundreds = answerNumber/100;
3535

36-
// strike 수 세기
37-
Evaluate.countStrike(guess, hundreds, tens, units);
38-
Evaluate.countBall(guess, hundreds, tens, units);
39-
40-
int strike = Evaluate.getStrike();
41-
int ball = Evaluate.getBall();
36+
// strike, ball 수 세기
37+
int strike = Evaluate.countStrike(guess, hundreds, tens, units);
38+
int ball = Evaluate.countBall(guess, hundreds, tens, units);
4239

4340
if(strike==0 && ball==0) {
4441
System.out.print("낫싱\n");

baseball_game/src/main/java/Game/Evaluate.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,22 @@
11
package Game;
22

33
public class Evaluate {
4-
private int inputNumber;
5-
private static int strike;
6-
private static int ball;
4+
public Evaluate(int guess, int answer) {}
75

8-
public Evaluate(int guess, int answer) {
9-
10-
}
11-
12-
public static int getStrike() {
13-
return strike;
14-
}
15-
public static int getBall() {
16-
return ball;
17-
}
18-
19-
public static void countStrike(int guess, int hundreds, int tens, int units) {
6+
public static int countStrike(int guess, int hundreds, int tens, int units) {
207
int strikeN = 0;
218
if(guess%10 == units) strikeN++;
229
if((guess/10)%10 == tens) strikeN++;
2310
if(guess/100 == hundreds) strikeN++;
24-
strike = strikeN;
11+
return strikeN;
2512
}
2613

27-
public static void countBall(int guess, int hundreds, int tens, int units) {
14+
public static int countBall(int guess, int hundreds, int tens, int units) {
2815
int ballN = 0;
2916
if((guess%10==hundreds) || (guess%10==tens)) ballN++;
3017
if(((guess/10)%10==hundreds) || ((guess/10)%10==units)) ballN++;
3118
if((guess/100==tens) || (guess/100==units)) ballN++;
32-
ball = ballN;
19+
return ballN;
3320
}
3421

3522
}

0 commit comments

Comments
 (0)