Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ repositories {
dependencies {
testImplementation "org.junit.jupiter:junit-jupiter:5.7.2"
testImplementation "org.assertj:assertj-core:3.19.0"
testImplementation 'org.mockito:mockito-core:4.11.0'

}

test {
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/BaseballApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import study.Baseball;
import study.BaseballGame;
import study.BaseballScanner;

public class BaseballApplication {
public static void main(String[] args){
BaseballScanner scanner = new BaseballScanner();
Baseball baseball = new Baseball();
BaseballGame game = new BaseballGame(scanner, baseball);

do {
String result = game.playGame();
System.out.println(result);

} while(game.isReplay());

System.out.println(game.endGame());
}
}
48 changes: 48 additions & 0 deletions src/main/java/study/Baseball.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package study;

import java.util.*;

public class Baseball {
private final List<Integer> answerList;

public Baseball(){
answerList = getAnswer();
}

public Baseball(List<Integer> answerList) {
this.answerList = answerList;
}

public BaseballScore play(List<Integer> userInput){
BaseballScore score = new BaseballScore();

for (int i=0; i<userInput.size(); i++){
int index = answerList.indexOf(userInput.get(i));
score.addScore(index, i);
}

return score;
}

public List<Integer> getAnswer() {
List<Integer> answers = new ArrayList<>();
Random random = new Random();

while(answers.size() < 3){
int num = random.nextInt(9)+1;
answers = addUniqueNumber(num, answers);
}

return answers;

}

public List<Integer> addUniqueNumber(int num, List<Integer> answers){
if (!answers.contains(num)){
answers.add(num);
}

return answers;
}

}
39 changes: 39 additions & 0 deletions src/main/java/study/BaseballGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package study;

import study.messages.SuccessMessages;

import java.util.List;

public class BaseballGame {
private final BaseballScanner scanner;
private final Baseball baseball;

public BaseballGame(BaseballScanner scanner, Baseball baseball){
this.scanner = scanner;
this.baseball = baseball;
}

public String playGame(){
boolean collect = false;

while(!collect){
List<Integer> userInput = scanner.askNumbers();
BaseballScore score = baseball.play(userInput);

System.out.println(score.printScore());
collect = score.isThreeStrike();
}

return SuccessMessages.CORRECT_GAME;
}

public boolean isReplay(){
int input = scanner.askReplay();
return input != 2;
}

public String endGame(){
return SuccessMessages.END_GAME;
}

}
88 changes: 88 additions & 0 deletions src/main/java/study/BaseballScanner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package study;

import study.messages.ErrorMessages;

import java.util.*;

public class BaseballScanner {
private final Scanner scanner;

public BaseballScanner(){
this.scanner = new Scanner(System.in);
}

public List<Integer> askNumbers() {
System.out.println("숫자를 입력해 주세요 : ");
String input;

try{
input = checkLength(scanner.next());
} catch (NoSuchElementException ex){
throw new NoSuchElementException(ErrorMessages.INVALID_INPUT);
}

return convertList(input);
}

public int askReplay(){
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
int input;

try{
input = validateOneOrTwo(scanner.nextInt());
} catch (NoSuchElementException ex){
throw new NoSuchElementException(ErrorMessages.INVALID_INPUT_REPLAY);
}

return input;

}

public String checkLength(String input){
input = input.replace(" ", "");

if (input.length() != 3){
throw new NoSuchElementException(ErrorMessages.INVALID_INPUT);
}
return input;
}

public int parseStringToInt(String str){
int result;

try{
result = Integer.parseInt(str);
} catch (NumberFormatException ex){
throw new NumberFormatException(ErrorMessages.INVALID_INPUT_NUMBER);
}

return result;
}


public List<Integer> convertList(String input){
List<Integer> numbers = new ArrayList<>();
String[] inputArr = input.split("");

for (String str: inputArr){
int number = parseStringToInt(str);
numbers.add(number);
}

return numbers;
}


public int validateOneOrTwo(int input){
if (input == 1 || input == 2){
return input;
}

throw new NoSuchElementException(ErrorMessages.INVALID_INPUT_REPLAY);
}


public void close(){
scanner.close();
}
}
61 changes: 61 additions & 0 deletions src/main/java/study/BaseballScore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package study;

public class BaseballScore {
private int strikeCnt;
private int ballCnt;
private int nothingCnt;


public BaseballScore() {
this.strikeCnt = 0;
this.ballCnt = 0;
this.nothingCnt = 0;
}

public BaseballScore(int strikeCnt, int ballCnt, int nothingCnt) {
this.strikeCnt = strikeCnt;
this.ballCnt = ballCnt;
this.nothingCnt = nothingCnt;
}


public void addScore(int index, int i){
if (index == -1){
nothingCnt++;
} else if (index == i){
strikeCnt++;
} else if(index != i){
ballCnt++;
}
}

public boolean isThreeStrike(){
return strikeCnt == 3;
}

public String printScore(){
if(nothingCnt == 3) {
return "낫싱";
} else if (ballCnt != 0 && strikeCnt != 0){
return ballCnt + "볼 " + strikeCnt + "스트라이크";
} else if (ballCnt == 0 && strikeCnt != 0){
return strikeCnt + "스트라이크";
} else if(ballCnt != 0){
return ballCnt + "볼";
}

return "";
}

public int getStrikeCnt() {
return strikeCnt;
}

public int getBallCnt() {
return ballCnt;
}

public int getNothingCnt() {
return nothingCnt;
}
}
7 changes: 7 additions & 0 deletions src/main/java/study/messages/ErrorMessages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package study.messages;

public class ErrorMessages {
public static final String INVALID_INPUT = "3자리 숫자를 입력해주세요.";
public static final String INVALID_INPUT_NUMBER = "숫자만 입력 가능합니다.";
public static final String INVALID_INPUT_REPLAY = "1 또는 2 숫자만 입력 가능합니다.";
}
6 changes: 6 additions & 0 deletions src/main/java/study/messages/SuccessMessages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package study.messages;

public class SuccessMessages {
public static final String CORRECT_GAME = "3개의 숫자를 모두 맞히셨습니다! 게임 종료";
public static final String END_GAME = "게임이 종료되었습니다.";
}
73 changes: 73 additions & 0 deletions src/test/java/study/BaseballGameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package study;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import study.messages.SuccessMessages;

import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class BaseballGameTest {

@Test
@DisplayName("playGame(): 3 스트라이크")
void playGame3Strike(){
// given
List<Integer> userInput = Arrays.asList(1, 2, 3);

BaseballScanner scanner = mock(BaseballScanner.class);
Baseball baseball = mock(Baseball.class);
BaseballGame game = new BaseballGame(scanner, baseball);

when(scanner.askNumbers()).thenReturn(userInput);
when(baseball.play(userInput)).thenReturn(new BaseballScore(3, 0, 0));


// when
String response = game.playGame();

// then
assertEquals(response, SuccessMessages.CORRECT_GAME);
}

@Test
@DisplayName("isReplayScanner(): 재게임 여부 1 입력으로 true 반환")
void isReplayInput1(){
// given
BaseballScanner scanner = mock(BaseballScanner.class);
Baseball baseball = mock(Baseball.class);
BaseballGame game = new BaseballGame(scanner, baseball);

when(scanner.askReplay()).thenReturn(1);

// when
boolean response = game.isReplay();

// then
assertTrue(response);
}

@Test
@DisplayName("isReplayScanner(): 재게임 여부 2 입력으로 false 반환")
void isReplayInput2(){
// given
BaseballScanner scanner = mock(BaseballScanner.class);
Baseball baseball = mock(Baseball.class);
BaseballGame game = new BaseballGame(scanner, baseball);

when(scanner.askReplay()).thenReturn(2);

// when
boolean response = game.isReplay();

// then
assertFalse(response);
}



}
Loading