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

eddie.kk #24

Open
wants to merge 17 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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,15 @@

## 과제 제출 과정
* [과제 제출 방법](https://github.com/next-step/nextstep-docs/tree/master/ent-precourse)

## 구현할 기능 목록
- 1 ~ 9사이의 임의의 수 생성 기능
- 상대방 수(서로 다른 임의의 수 3개)를 생성하는 기능
- 사용자의 수를 입력 받는 기능
- 상대방의 수와 사용자의 수 비교 결과를 출력하는 기능
- 비교 결과에 따라 낫싱, 스트라이크, 볼 정보를 생성하는 기능
- 수가 같은지, 다른지 판별하는 기능
- 수가 같은 자리에 있는지, 포함하고 있는지 판별하는 기능
- 상대방의 수와 사용자의 수를 비교하는 기능
- 게임 재시작/종료 기능
- 게임 재시작/종료 입력값 유효성 검증 기능
Empty file removed src/main/java/.gitkeep
Empty file.
9 changes: 9 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import controller.BaseballGameController;

public class Main {

public static void main(String[] args) {
new BaseballGameController().execute();
}

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

import domain.BaseballGame;
import domain.Computer;
import domain.GameResult;
import view.Console;

public class BaseballGameController {

private static final String RESTART_FLAG = "1";
private static final String EXIT_FLAG = "2";

private final Computer computer;
private final Console console;

public BaseballGameController() {
computer = new Computer();
console = new Console();
}

public void execute() {
BaseballGame baseballGame = new BaseballGame(computer.generateThreeDifferentNumber());

do {
String userNumbers = console.getUserInputNumber();
GameResult gameResult = baseballGame.compareNumbers(userNumbers);
console.printMessage(gameResult.getDescription());
} while (!baseballGame.isEnd());

handleEndOfGame(console::printEndMessage);
}

public void handleEndOfGame(Runnable runnable) {
runnable.run();
String userInput = console.getUserInput();

if (!validateRestartOrExitInput(userInput)) {
handleEndOfGame(console::printErrorMessage);
return;
}

restartOrExit(userInput);
}

public void restartOrExit(String userInput) {
if (userInput.equals(EXIT_FLAG)) {
return;
}

execute();
}

public boolean validateRestartOrExitInput(String userInput) {
return userInput.equals(RESTART_FLAG) || userInput.equals(EXIT_FLAG);
}

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

public class BaseballGame {

private static final int NUMBER_LENGTH = 3;

private final String computerNumbers;
private GameResult currentGameResult;

public BaseballGame(String computerNumbers) {
this.computerNumbers = computerNumbers;
}

public GameResult compareNumbers(String userNumbers) {
currentGameResult = new GameResult();

for (int i = 0; i < NUMBER_LENGTH; i++) {
compareNumber(userNumbers.charAt(i), computerNumbers.charAt(i));
}

return currentGameResult;
}

private void compareNumber(char userNum, char computerNum) {
if (!computerNumbers.contains(String.valueOf(userNum))) {
return;
}

if (userNum == computerNum) {
currentGameResult.increaseStrikeCount();
return;
}
currentGameResult.increaseBallCount();
}

public boolean isEnd() {
return currentGameResult.getStrikeCount() == NUMBER_LENGTH;
}

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

import java.util.*;

public class Computer {

private static final int MIN = 1;
private static final int MAX = 9;

private final Random random;

public Computer() {
random = new Random();
}

public String generateThreeDifferentNumber() {
Set<Integer> set = new HashSet<>();

while (set.size() < 3) {
set.add(generateRandomNumber());
}

return set.toString()
.replaceAll("[^0-9]", "");
}

public int generateRandomNumber() {
return random.nextInt(MAX) + MIN;
}

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

public class GameResult {

private int strikeCount;
private int ballCount;

public int getStrikeCount() {
return strikeCount;
}

public int getBallCount() {
return ballCount;
}

public String getDescription() {
StringBuilder sb = new StringBuilder();

sb.append(checkIsNothing());
sb.append(checkStrikeCount());
sb.append(checkBallCount());

return sb.toString();
}

public void increaseStrikeCount() {
strikeCount += 1;
}

public void increaseBallCount() {
ballCount += 1;
}

public String checkIsNothing() {
return strikeCount + ballCount == 0 ? "낫싱" : "";
}

public String checkStrikeCount() {
return strikeCount > 0 ? strikeCount + "스트라이크 " : "";
}

public String checkBallCount() {
return ballCount > 0 ? ballCount + "볼" : "";
}

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

import java.util.Scanner;

public class Console {

private static final String INPUT_MESSAGE = "숫자를 입력해주세요 : ";
private static final String END_MESSAGE = "3개의 숫자를 모두 맞히셨습니다! 게임 종료";
private static final String RESTART_OR_EXIT_MESSAGE = "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.";
private static final String ERROR_MESSAGE = "올바른 숫자를 입력해주세요.";

private final Scanner scanner;

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

public String getUserInputNumber() {
System.out.print(INPUT_MESSAGE);
return scanner.nextLine();
}

public String getUserInput() {
return scanner.nextLine();
}

public void printEndMessage() {
printMessage(END_MESSAGE);
printMessage(RESTART_OR_EXIT_MESSAGE);
}

public void printErrorMessage() {
printMessage(ERROR_MESSAGE);
}

public void printMessage(String str) {
System.out.println(str);
}

}
36 changes: 36 additions & 0 deletions src/test/java/domain/BaseballGameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package domain;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

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

class BaseballGameTest {

@DisplayName("사용자의 수와 컴퓨터의 수 비교 테스트")
@ParameterizedTest
@MethodSource("provideComputerNumbersAndUserNumbers")
void compareNumbersTest(String computerNumbers, String userNumbers, String description, int strikeCount, int ballCount) {
BaseballGame baseballGame = new BaseballGame(computerNumbers);
GameResult gameResult = baseballGame.compareNumbers(userNumbers);

assertThat(gameResult.getDescription()).isEqualTo(description);
assertThat(gameResult.getStrikeCount()).isEqualTo(strikeCount);
assertThat(gameResult.getBallCount()).isEqualTo(ballCount);
}

private static Stream<Arguments> provideComputerNumbersAndUserNumbers() {
return Stream.of(
Arguments.of("713", "123", "1스트라이크 1볼", 1, 1),
Arguments.of("713", "145", "1볼", 0, 1),
Arguments.of("713", "256", "낫싱", 0, 0),
Arguments.of("713", "671", "2볼", 0, 2),
Arguments.of("713", "713", "3스트라이크 ", 3, 0)
);
}

}
34 changes: 34 additions & 0 deletions src/test/java/domain/ComputerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package domain;

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

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

class ComputerTest {

@DisplayName("1 ~ 9 사이의 임의의 수 생성 테스트")
@Test
void generateRandomNumberTest() {
Computer computer = new Computer();
int min = 1, max = 9;

for (int i = 0; i < 100; i++) {
int randomNumber = computer.generateRandomNumber();
assertThat(randomNumber).isGreaterThanOrEqualTo(min).isLessThanOrEqualTo(max);
}
}

@DisplayName("서로 다른 임의의 수 3개 생성 테스트")
@Test
void generateThreeDifferentNumberTest() {
Computer computer = new Computer();
char[] nums = computer.generateThreeDifferentNumber()
.toCharArray();

assertThat(nums[0]).isNotEqualTo(nums[1]);
assertThat(nums[0]).isNotEqualTo(nums[2]);
assertThat(nums[1]).isNotEqualTo(nums[2]);
}

}
Loading