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

yeonjy java-baseball 구현 #1

Open
wants to merge 10 commits 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
6 changes: 5 additions & 1 deletion src/main/java/baseball/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package baseball;

import baseball.controller.InputController;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
InputController inputController = new InputController();
System.out.println("숫자 야구 게임을 시작합니다.");
inputController.requireAnswer();
}
}
54 changes: 54 additions & 0 deletions src/main/java/baseball/controller/InputController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package baseball.controller;

import baseball.repository.AnswerRepository;
import baseball.service.BaseballService;
import camp.nextstep.edu.missionutils.Console;

import java.util.List;
import java.util.Map;

public class InputController {

private static final String NOTHING = "낫싱";


private BaseballService baseballService = new BaseballService();
private AnswerRepository answerRepository = new AnswerRepository();

private static final String STRIKE = "스트라이크";

public void continueGame() {
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요");
int continueGame = Integer.parseInt(Console.readLine());
if (continueGame == 1) {
requireAnswer();
}
if (continueGame == 2) {
return;
}
}

public void requireAnswer() {
baseballService.makeAnswer();
recieveUserAnswer();
}

public void recieveUserAnswer() {
System.out.print("숫자를 입력해주세요 : ");
String userAnswer = Console.readLine();
baseballService.makeUserAnswer(userAnswer);
checkCorrect();
}


public void checkCorrect() {
Map<String, Integer> result = baseballService.makeReply(answerRepository.getAnswer(), answerRepository.getUserAnswer());
System.out.println(baseballService.makeStringReply(result));
if (result.containsKey(STRIKE) && result.get(STRIKE).equals(3)) {
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
continueGame();
return;
}
recieveUserAnswer();
}
}
43 changes: 43 additions & 0 deletions src/main/java/baseball/repository/AnswerRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package baseball.repository;

import camp.nextstep.edu.missionutils.Randoms;

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

public class AnswerRepository {

private static List<Integer> answer;
private static List<Integer> userAnswer;

public List<Integer> getAnswer() {
return answer;
}

public List<Integer> getUserAnswer() {
return userAnswer;
}

public List<Integer> makeAnswer() {
List<Integer> answer = new ArrayList<>();
while (answer.size() < 3) {
int randomNumber = Randoms.pickNumberInRange(1, 9);
if (!answer.contains(randomNumber)) {
answer.add(randomNumber);
}
}
this.answer = answer;
return answer;
}

public List<Integer> setUerAnswer(String answer) {
// List<Integer> ints = Arrays.asList(Stream.of(answer.split("")).mapToInt(Integer::parseInt).toArray());
List<Integer> userAnswer = new ArrayList<Integer>();
String[] splitAnswer = answer.split("");
for (String tmp : splitAnswer) {
userAnswer.add(Integer.parseInt(tmp));
}
this.userAnswer = userAnswer;
return userAnswer;
}
}
93 changes: 93 additions & 0 deletions src/main/java/baseball/service/BaseballService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package baseball.service;

import baseball.repository.AnswerRepository;

import java.util.List;
import java.util.Map;


public class BaseballService {

private final AnswerRepository answerRepository = new AnswerRepository();

private static final String NOTHING = "낫싱";
private static final String STRIKE = "스트라이크";
private static final String BALL = "볼";


public List<Integer> makeUserAnswer(String userAnswer) {
isCorrectUserAnswer(userAnswer);
return answerRepository.setUerAnswer(userAnswer);
}

private void isCorrectUserAnswer(String userAnswer) {
lengthOfUserAnswer(userAnswer);
isInteger(userAnswer);

}

private void lengthOfUserAnswer(String userAnswer) {
if (userAnswer.length() != 3) {
throw new IllegalArgumentException("숫자는 3개만 입력해주세요.");
}
}

private void isInteger(String userAnswer) {
try {
Integer.parseInt(userAnswer);
} catch (NumberFormatException e) {
throw new IllegalArgumentException();
}
}

public List<Integer> makeAnswer() {
return answerRepository.makeAnswer();
}

public Map<String, Integer> makeReply(List<Integer> answer, List<Integer> userAnswer) {
return makeMapReply(countStrikes(answer, userAnswer), countBalls(answer, userAnswer));
}

private int countStrikes(List<Integer> answer, List<Integer> userAnswer) {
int cnt = 0;
for (int i = 0; i < answer.size(); i++) {

if (answer.get(i).equals(userAnswer.get(i))) {
cnt++;
}
}
return cnt;
}

private int countBalls(List<Integer> answer, List<Integer> userAnswer) {
int cnt = 0;
for (int userAns : userAnswer) {
if (answer.contains(userAns)) {
cnt++;
}
}
return cnt;
}

public String makeStringReply(Map<String, Integer> mapReply) {
StringBuilder sb = new StringBuilder();
if (mapReply.containsKey(NOTHING)) {
return NOTHING;
}
if (mapReply.get(BALL) != 0) {
sb.append(mapReply.get(BALL).toString()).append(BALL).append(" ");
}
if (mapReply.get(STRIKE) != 0) {
sb.append(mapReply.get(STRIKE).toString()).append(STRIKE);
}
return sb.toString();
}

private Map<String, Integer> makeMapReply(int strikeCnt, int ballCnt) {
if (ballCnt == 0) {
return Map.of(NOTHING, 0);
}
ballCnt -= strikeCnt;
return Map.of(BALL, ballCnt, STRIKE, strikeCnt);
}
}
38 changes: 38 additions & 0 deletions src/test/java/baseball/repository/AnswerRepositoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package baseball.repository;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

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

import static org.assertj.core.api.Assertions.assertThat;


class AnswerRepositoryTest {

private static AnswerRepository repository;

@BeforeEach
void setUp() {
repository = new AnswerRepository();
}

@Test
void correctAnswer() {
List<Integer> answer = repository.makeAnswer();
for (int ans : answer) {
assertThat(ans > 0 && ans < 10);
}
}

@Test
void stringToListUserAnswer() {
String userTry = "926";
List<Integer> answer = repository.setUerAnswer(userTry);
List<Integer> expected = Arrays.asList(9, 2, 6);
assertThat(answer.equals(expected));
}


}
35 changes: 35 additions & 0 deletions src/test/java/baseball/service/BaseballServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package baseball.service;

import baseball.repository.AnswerRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

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

import static org.assertj.core.api.Assertions.assertThat;

class BaseballServiceTest {

static AnswerRepository repository;
static BaseballService service;


@BeforeEach
void setUp() {
service = new BaseballService();
}

@Test
void testStringReply() {
List<Integer> answer = Arrays.asList(7, 1, 3);
List<Integer> userTryList = Arrays.asList(1, 2, 3);
String expected = "1볼 1스트라이크";
Map<String, Integer> result = service.makeReply(answer, userTryList);
assertThat(result.equals(Map.of("볼", 1, "스트라이크", 1)));

}


}