-
Notifications
You must be signed in to change notification settings - Fork 5
자동차 경주 게임! #15
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
base: master
Are you sure you want to change the base?
자동차 경주 게임! #15
Changes from 3 commits
ececbca
34ee1a3
e47e617
8e3228a
deefa90
36c7945
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package racing; | ||
|
|
||
| import racing.domain.CarFactory; | ||
| import racing.domain.RacingResult; | ||
| import racing.domain.Cars; | ||
| import racing.util.RandomNumberGenerator; | ||
| import racing.view.InputView; | ||
| import racing.view.ResultView; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class RacingGame { | ||
| private final InputView inputView = new InputView(new Scanner(System.in)); | ||
|
|
||
| public void run() { | ||
| int carCount = inputView.inputCarCount(); | ||
| int tryCount = inputView.inputTryCount(); | ||
|
|
||
| Cars cars = new Cars(CarFactory.createCar(carCount)); | ||
| RacingResult racingResult = cars.operate(tryCount, new RandomNumberGenerator()); | ||
| ResultView resultView = new ResultView(); | ||
| resultView.printResult(racingResult, carCount); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package racing; | ||
|
|
||
| public class RacingGameApplication { | ||
| public static void main(String[] args) { | ||
| RacingGame racingGame = new RacingGame(); | ||
| racingGame.run(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package racing.domain; | ||
|
|
||
| public class Car { | ||
| private int distance; | ||
|
|
||
| public Car(int distance) { | ||
| this.distance = distance; | ||
| } | ||
|
|
||
| public int getDistance() { | ||
| return distance; | ||
| } | ||
|
|
||
| public void move(int distance) { | ||
| this.distance += distance; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package racing.domain; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class CarFactory { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 굿 좋습니다b 기존코드와의 변경사항이 적어서 좋네요 |
||
| public static List<Car> createCar(int carCount) { | ||
| List<Car> cars = new ArrayList<>(); | ||
| for (int carIdx = 0; carIdx < carCount; carIdx++) { | ||
| cars.add(new Car(0)); | ||
| } | ||
| return cars; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package racing.domain; | ||
|
|
||
| import racing.util.NumberGenerator; | ||
| import racing.util.RandomUtil; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Cars { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 일급 컬렉션 굿b |
||
| private final List<Car> cars; | ||
|
|
||
| public Cars(List<Car> cars) { | ||
| this.cars = cars; | ||
| } | ||
|
|
||
| public RacingResult operate(int tryCount, NumberGenerator numberGenerator) { | ||
| List<Integer> racingResult = new ArrayList<>(); | ||
| for (int curTryCount = 0; curTryCount < tryCount; curTryCount++) { | ||
| moveCars(racingResult, numberGenerator); | ||
| } | ||
|
|
||
| return new RacingResult(racingResult); | ||
| } | ||
|
|
||
| private Car get(int idx) { | ||
| return cars.get(idx); | ||
| } | ||
|
|
||
| private int size() { | ||
| return cars.size(); | ||
| } | ||
|
|
||
| private void moveCars(List<Integer> racingResult, NumberGenerator numberGenerator) { | ||
| for (int carIdx = 0; carIdx < size(); carIdx++) { | ||
| Car car = get(carIdx); | ||
| car.move(RandomUtil.getRandomDistance(numberGenerator)); | ||
| racingResult.add(car.getDistance()); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package racing.domain; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class RacingResult { | ||
| private final List<Integer> racingResult; | ||
|
|
||
| public RacingResult(List<Integer> racingResult) { | ||
| this.racingResult = racingResult; | ||
| } | ||
|
|
||
| public int size() { | ||
| return racingResult.size(); | ||
| } | ||
|
|
||
| public int get(int idx) { | ||
| return racingResult.get(idx); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package racing.util; | ||
|
|
||
| public interface NumberGenerator { | ||
| int generateNumber(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package racing.util; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| public class RandomNumberGenerator implements NumberGenerator { | ||
| private final int MAX = 10; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 상수는 |
||
|
|
||
| @Override | ||
| public int generateNumber() { | ||
| return new Random().nextInt(MAX); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package racing.util; | ||
|
|
||
| public class RandomUtil { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사용되는 메서드의 이름이 |
||
| private static final int MOVE = 1; | ||
| private static final int STOP = 0; | ||
|
|
||
| public static int getRandomDistance(NumberGenerator numberGenerator) { | ||
| if (numberGenerator.generateNumber() >= 4) { | ||
| return MOVE; | ||
| } | ||
| return STOP; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package racing.view; | ||
|
|
||
| public class InputCount { | ||
| private int count; | ||
|
|
||
| public InputCount(int count) { | ||
| if (count <= 0) { | ||
| throw new IllegalArgumentException("입력하신 값이 양수가 아닙니다."); | ||
| } | ||
| this.count = count; | ||
| } | ||
|
|
||
| public int getCount() { | ||
| return count; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package racing.view; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class InputView { | ||
| private final String FIRST_QUESTION = "자동차 대수는 몇 대 인가요?"; | ||
| private final String SECOND_QUESTION = "시도할 횟수는 몇 회 인가요?"; | ||
|
|
||
| private final Scanner sc; | ||
|
|
||
| public InputView(Scanner sc) { | ||
|
malibinYun marked this conversation as resolved.
|
||
| this.sc = sc; | ||
| } | ||
|
|
||
| public int inputCarCount() { | ||
| System.out.println(FIRST_QUESTION); | ||
| return new InputCount(sc.nextInt()).getCount(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 단순히 검증을 거쳤다 바깥으로 반환하는 용도로 사용하고 있는 객체인데 |
||
| } | ||
|
|
||
| public int inputTryCount() { | ||
| System.out.println(SECOND_QUESTION); | ||
| return new InputCount(sc.nextInt()).getCount(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package racing.view; | ||
|
|
||
| import racing.domain.RacingResult; | ||
|
|
||
| public class ResultView { | ||
| private final String OPERATE_RESULT = "실행 결과"; | ||
| private final String DISTANCE = "-"; | ||
| private final String ENTER = "\n"; | ||
| private final String BORDER = "라운드 종료!!"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 매직넘버와 매직리터럴은 |
||
|
|
||
| public ResultView() { | ||
| printEmpty(); | ||
| System.out.println(OPERATE_RESULT); | ||
| } | ||
|
|
||
| public void printResult(RacingResult racingResults, int carCount) { | ||
| for (int racingIdx = 0; racingIdx < racingResults.size(); racingIdx++) { | ||
| printDistance(racingResults.get(racingIdx)); | ||
| checkEachRound(racingIdx, carCount); | ||
| } | ||
| printEmpty(); | ||
| } | ||
|
|
||
| private void printDistance(int distance) { | ||
| for (int length = 0; length < distance; length++) { | ||
| System.out.print(DISTANCE); | ||
| } | ||
| printEmpty(); | ||
| } | ||
|
|
||
| private void checkEachRound(int racingIdx, int carCount) { | ||
| if (isLastCar(racingIdx, carCount)) { | ||
| System.out.println(BORDER); | ||
| } | ||
| } | ||
|
|
||
| private boolean isLastCar(int racingIdx, int carCount) { | ||
| return (racingIdx + 1) % carCount == 0; | ||
| } | ||
|
|
||
| private void printEmpty() { | ||
| System.out.print(ENTER); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package racing.domain; | ||
|
|
||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| public class CarFactoryTest { | ||
| @DisplayName("자동차의 갯수가 입력한 값과 같은지 확인한다.") | ||
| @ParameterizedTest | ||
| @ValueSource(ints = {1, 3, 5, 6}) | ||
| public void createCar(int carCount){ | ||
| List<Car> cars = CarFactory.createCar(carCount); | ||
| assertEquals(carCount, cars.size()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package racing.domain; | ||
|
|
||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
| import racing.util.FixedNumberGenerator; | ||
| import racing.util.RandomUtil; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| public class CarTest { | ||
|
|
||
| @DisplayName("자동차가 랜덤으로 들어온 값이 4이상일 때 전진한다.") | ||
| @ParameterizedTest | ||
| @ValueSource(ints = {4, 9}) | ||
| void move1(int distance) { | ||
| Car car = new Car(distance); | ||
| car.move(RandomUtil.getRandomDistance(new FixedNumberGenerator(distance))); | ||
| assertEquals(distance + 1, car.getDistance()); | ||
| } | ||
|
|
||
| @DisplayName("자동차가 랜덤으로 들어온 값이 4 보다 작을 때 움직이지 않는다.") | ||
| @ParameterizedTest | ||
| @ValueSource(ints = {1, 2}) | ||
| void move2(int distance) { | ||
| Car car = new Car(distance); | ||
| car.move(RandomUtil.getRandomDistance(new FixedNumberGenerator(distance))); | ||
| assertEquals(distance, car.getDistance()); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 두개의 테스트 코드를 하나로 합쳐보자! |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package racing.domain; | ||
|
|
||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
| import racing.util.FixedNumberGenerator; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| class CarsTest { | ||
|
|
||
| @DisplayName("자동차들이 랜덤한 값으로 들어온 값만큼 움직였는지 확인한다.") | ||
| @ParameterizedTest | ||
| @ValueSource(ints = {3, 5}) | ||
| void moveCars(int tryCount) { | ||
| int carCount = 3; | ||
| Cars cars = new Cars(CarFactory.createCar(carCount)); | ||
| RacingResult racingResult = cars.operate(tryCount, new FixedNumberGenerator(10)); | ||
| assertEquals(tryCount, racingResult.get(racingResult.size() - 1)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package racing.util; | ||
|
|
||
| public class FixedNumberGenerator implements NumberGenerator { | ||
| private int distance; | ||
|
|
||
| public FixedNumberGenerator(int distance) { | ||
| this.distance = distance; | ||
| } | ||
|
|
||
| @Override | ||
| public int generateNumber() { | ||
| return this.distance; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package racing.util; | ||
|
|
||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| public class RandomUtilTest { | ||
|
|
||
| @DisplayName("랜덤으로 생성된 값이 4보다 작을경우 0을 리턴한다.") | ||
| @ParameterizedTest | ||
| @ValueSource(ints = {1, 3}) | ||
| public void getRandomDistance1(int distance) { | ||
| int actual = RandomUtil.getRandomDistance(new FixedNumberGenerator(distance)); | ||
| assertEquals(0, actual); | ||
| } | ||
|
|
||
| @DisplayName("랜덤으로 생성된 값이 4 이상을 경우 1을 리턴한다.") | ||
| @ParameterizedTest | ||
| @ValueSource(ints = {4, 5}) | ||
| public void getRandomDistance2(int distance) { | ||
| int actual = RandomUtil.getRandomDistance(new FixedNumberGenerator(distance)); | ||
| assertEquals(1, actual); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이것 또한 하나로 합칠수 있을것 같아! |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
엇근데 random으로 받은 distance 값을 바탕으로 outputview를 작성하는데,
이렇게 하면 주어진 요구사항에 맞지 않을 것 같습니다.
레이싱 게임 요구사항을 확인해주세요.