Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

과제 수행 #2795

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
9 changes: 9 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
컴퓨터가 가지는 난수 리스트를 생성한다.

사용자의 입력을 받는 서비스를 만든다.

사용자의 입력값과 난수를 비교하여 결과를 뱉는다

출력을 하는 서비스를 만든다.

이걸 감싸는 루프도는 친구를 만든다.
26 changes: 26 additions & 0 deletions src/main/java/baseball/AppConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package baseball;

import baseball.Computer.ComputerService;
import baseball.Computer.ComputerServiceImpl;
import baseball.Parse.ParseService;
import baseball.Parse.ParseServiceImpl;
import baseball.check.CheckService;
import baseball.check.CheckServiceImpl;
import baseball.input.InputService;
import baseball.input.InputServiceImpl;
import baseball.print.PrintService;
import baseball.print.PrintServiceImpl;

public class AppConfig {

public PrintService printService() {return new PrintServiceImpl();}

public ComputerService computerService() {return new ComputerServiceImpl();}

public InputService inputService() {return new InputServiceImpl();}

public ParseService parseService() {return new ParseServiceImpl();}

public CheckService checkService() {return new CheckServiceImpl();}

}
36 changes: 35 additions & 1 deletion src/main/java/baseball/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
package baseball;

import baseball.Computer.ComputerService;
import baseball.Parse.ParseService;
import baseball.check.CheckService;
import baseball.check.Hint;
import baseball.input.InputService;
import baseball.print.PrintService;

import java.util.List;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
AppConfig appConfig = new AppConfig();

PrintService printService = appConfig.printService();
ComputerService computerService = appConfig.computerService();
InputService inputService = appConfig.inputService();
ParseService parseService = appConfig.parseService();
CheckService checkService = appConfig.checkService();

List<Integer> computer = computerService.settingComputer();
printService.gameStartMessage();

while(true){
printService.inputMessage();
List<Integer> numbers = parseService.stringParseToNumbers(inputService.readLine());
Hint hint = checkService.checkNumber(computer, numbers);
printService.resultMessage(hint.getStrike(), hint.getBall());
if(checkService.checkGameOver(hint)){
printService.gameEndMessage();
printService.restartMessage();
if(parseService.isRestart(inputService.readLine())){
computer = computerService.settingComputer();
continue;
}
break;
}
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/baseball/Computer/ComputerService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package baseball.Computer;

import java.util.List;

public interface ComputerService {
List<Integer> settingComputer();
}
24 changes: 24 additions & 0 deletions src/main/java/baseball/Computer/ComputerServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package baseball.Computer;



import static camp.nextstep.edu.missionutils.Randoms.*;
import static baseball.Constant.*;

import java.util.ArrayList;
import java.util.List;

public class ComputerServiceImpl implements ComputerService{

@Override
public List<Integer> settingComputer() {
List<Integer> computer = new ArrayList<>();
while (computer.size() < SIZE) {
int randomNumber = pickNumberInRange(COMPUTER_MIN, COMPUTER_MAX);
if (!computer.contains(randomNumber)) {
computer.add(randomNumber);
}
}
return computer;
}
}
10 changes: 10 additions & 0 deletions src/main/java/baseball/Constant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package baseball;

public class Constant {
public final static int COMPUTER_MIN = 1;
public final static int COMPUTER_MAX = 9;
public final static int SIZE = 3;

public final static String RESTART_FLAG = "1";
public final static String END_FLAG = "2";
}
8 changes: 8 additions & 0 deletions src/main/java/baseball/Parse/ParseService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package baseball.Parse;

import java.util.List;

public interface ParseService {
List<Integer> stringParseToNumbers(String StringNumbers);
boolean isRestart(String flag);
}
29 changes: 29 additions & 0 deletions src/main/java/baseball/Parse/ParseServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package baseball.Parse;

import java.util.ArrayList;
import java.util.List;

import static baseball.Constant.*;

public class ParseServiceImpl implements ParseService{
@Override
public List<Integer> stringParseToNumbers(String StringNumbers) {
if(StringNumbers.length() != SIZE)
throw new IllegalArgumentException();

List<Integer> numbers = new ArrayList<>();
for(String string : StringNumbers.split("")){
numbers.add(Integer.parseInt(string));
}
return numbers;
}

@Override
public boolean isRestart(String flag) {
if (flag.equals(RESTART_FLAG))
return true;
if (flag.equals(END_FLAG))
return false;
throw new IllegalArgumentException();
}
}
9 changes: 9 additions & 0 deletions src/main/java/baseball/check/CheckService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package baseball.check;

import java.util.List;

public interface CheckService {
Hint checkNumber(List<Integer> computer, List<Integer> numbers);

boolean checkGameOver(Hint hint);
}
27 changes: 27 additions & 0 deletions src/main/java/baseball/check/CheckServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package baseball.check;

import java.util.ArrayList;
import java.util.List;

public class CheckServiceImpl implements CheckService {

@Override
public Hint checkNumber(List<Integer> computer, List<Integer> numbers) {
Hint hint = new Hint();
for(int i = 0; i < computer.size(); i++){
if(computer.get(i).equals(numbers.get(i))){
hint.strikeCounting();
continue;
}
if(computer.contains(numbers.get(i))){
hint.ballCounting();
}
}
return hint;
}

@Override
public boolean checkGameOver(Hint hint) {
return hint.getStrike() == 3;
}
}
35 changes: 35 additions & 0 deletions src/main/java/baseball/check/Hint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package baseball.check;

public class Hint {
private int strike;
private int ball;

public Hint( ) {
this.strike = 0;
this.ball = 0;
}

public int getBall() {
return ball;
}

public void setBall(int ball) {
this.ball = ball;
}

public int getStrike() {
return strike;
}

public void setStrike(int strike) {
this.strike = strike;
}

public void strikeCounting(){
this.strike++;
}

public void ballCounting(){
this.ball++;
}
}
7 changes: 7 additions & 0 deletions src/main/java/baseball/input/InputService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package baseball.input;

import java.util.List;

public interface InputService {
String readLine();
}
11 changes: 11 additions & 0 deletions src/main/java/baseball/input/InputServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package baseball.input;

import camp.nextstep.edu.missionutils.Console;

public class InputServiceImpl implements InputService{

public String readLine(){
String line = Console.readLine();
return line;
}
}
19 changes: 19 additions & 0 deletions src/main/java/baseball/print/PrintService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package baseball.print;

import baseball.check.Hint;

import java.util.List;

public interface PrintService {

void gameStartMessage();

void inputMessage();

void gameEndMessage();

void restartMessage();

void resultMessage(int strikeCount, int ballCount);

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

import static baseball.Constant.*;

public class PrintServiceImpl implements PrintService{
@Override
public void gameStartMessage() {
System.out.println("숫자 야구 게임을 시작합니다.");
}

@Override
public void inputMessage() {
System.out.print("숫자를 입력해주세요 : ");
}

@Override
public void gameEndMessage() {
System.out.println(String.format("%d개의 숫자를 모두 맞히셨습니다! 게임 종료", SIZE));
}

@Override
public void restartMessage() {
System.out.println(String.format("게임을 새로 시작하려면 %s, 종료하려면 %s를 입력하세요.", RESTART_FLAG, END_FLAG));
}

@Override
public void resultMessage(int strikeCount, int ballCount) {
if(strikeCount == 0 && ballCount ==0)
System.out.print("낫싱");

if(ballCount > 0)
System.out.print(String.format("%d볼 ", ballCount));
if(strikeCount > 0)
System.out.print(String.format("%d스트라이크 ", strikeCount));

System.out.println();
}

}
3 changes: 3 additions & 0 deletions src/test/java/baseball/ApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class ApplicationTest extends NsTest {



@Test
void 게임종료_후_재시작() {
assertRandomNumberInRangeTest(
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/baseball/Computer/ComputerServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package baseball.Computer;

import baseball.AppConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

import static baseball.Constant.*;

class ComputerServiceTest {

private ComputerService computerService;

@BeforeEach
void beforeEach(){
AppConfig appConfig = new AppConfig();
computerService = appConfig.computerService();
}

@Test
@DisplayName("computer에 값이 올바르게 할당되는가 ")
void checkSettingComputer() {
// given
List<Integer> computer = computerService.settingComputer();
// when, then
assertTrue(computer.size() == SIZE);
assertTrue(computer.size() == computer.stream().distinct().count());
}

}
Loading