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

자동차 경주 게임! #15

Open
wants to merge 6 commits into
base: master
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: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/modules/racing-game.main.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions .idea/modules/racing-game.test.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/racing-game.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions src/main/java/racing/RacingGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package racing;

import racing.domain.*;
import racing.util.RandomNumberGenerator;
import racing.view.InputView;
import racing.view.ResultView;

import java.util.List;
import java.util.Scanner;

public class RacingGame {
private final InputView inputView = new InputView(new Scanner(System.in));

public void run() {
List<String> carNames = inputView.inputCarNames();
int tryCount = inputView.inputTryCount();

Cars cars = new Cars(CarFactory.createCars(carNames));

EachRoundResult eachRoundResult = new EachRoundResult();
cars.operate(tryCount, new RandomNumberGenerator(), eachRoundResult);

Winners winners = new Winners(eachRoundResult.getWinners());

ResultView resultView = new ResultView();
resultView.printResult(eachRoundResult, winners);
}
}
8 changes: 8 additions & 0 deletions src/main/java/racing/RacingGameApplication.java
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();
}
}
23 changes: 23 additions & 0 deletions src/main/java/racing/domain/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package racing.domain;

public class Car {
private String name;
private int distance;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엇근데 random으로 받은 distance 값을 바탕으로 outputview를 작성하는데,
이렇게 하면 주어진 요구사항에 맞지 않을 것 같습니다.
레이싱 게임 요구사항을 확인해주세요.


public Car(String name, int distance) {
this.name = name;
this.distance = distance;
}

public String getName() {
return name;
}

public int getDistance() {
return distance;
}

public void move(int moveDistance) {
this.distance += moveDistance;
}
}
14 changes: 14 additions & 0 deletions src/main/java/racing/domain/CarFactory.java
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

굿 좋습니다b 기존코드와의 변경사항이 적어서 좋네요

public static List<Car> createCars(List<String> carNames) {
List<Car> cars = new ArrayList<>();
for (String carName : carNames) {
cars.add(new Car(carName, 0));
}
return cars;
}
}
31 changes: 31 additions & 0 deletions src/main/java/racing/domain/Cars.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package racing.domain;

import racing.util.NumberGenerator;
import racing.util.RandomDistanceUtil;

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

public class Cars {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 void operate(int tryCount, NumberGenerator numberGenerator, EachRoundResult eachRoundResult) {
for (int roundIdx = 1; roundIdx <= tryCount; roundIdx++) {
List<ResultCarInfo> roundResult = new ArrayList<>();
moveCars(roundResult, numberGenerator);
eachRoundResult.addRoundResult(roundIdx, roundResult);
}
}

private void moveCars(List<ResultCarInfo> roundResult, NumberGenerator numberGenerator) {
for (int carIdx = 0; carIdx < cars.size(); carIdx++) {
Car car = cars.get(carIdx);
car.move(RandomDistanceUtil.getRandomDistance(numberGenerator));
roundResult.add(new ResultCarInfo(car.getName(), car.getDistance()));
}
}
}
41 changes: 41 additions & 0 deletions src/main/java/racing/domain/EachRoundResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package racing.domain;

import java.util.*;
import java.util.stream.Collectors;

public class EachRoundResult {
private final Map<Integer, List<ResultCarInfo>> eachRoundResult = new HashMap<>();

public void addRoundResult(int roundIdx, List<ResultCarInfo> resultCarInfos) {
eachRoundResult.put(roundIdx, resultCarInfos);
}

public List<ResultCarInfo> getRoundResult(int roundIdx) {
return eachRoundResult.get(roundIdx);
}

public List<String> getWinners() {
List<ResultCarInfo> lastRound = getLastRound();
int maxDistance = getMaxDistance(lastRound);
return lastRound.stream()
.filter(carDataInfo -> carDataInfo.getDistance() == maxDistance)
.map(ResultCarInfo::getName)
.collect(Collectors.toList());
}

private List<ResultCarInfo> getLastRound() {
return eachRoundResult.get(eachRoundResult.size());
}

private int getMaxDistance(List<ResultCarInfo> lastRound) {
ResultCarInfo maxByDistance = lastRound.stream()
.max(Comparator.comparing(ResultCarInfo::getDistance))
.orElseThrow((NoSuchElementException::new));

return maxByDistance.getDistance();
}

public int size() {
return eachRoundResult.size();
}
}
19 changes: 19 additions & 0 deletions src/main/java/racing/domain/ResultCarInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package racing.domain;

public final class ResultCarInfo {
private final String name;
private final int distance;

public ResultCarInfo(String name, int distance) {
this.name = name;
this.distance = distance;
}

public String getName() {
return name;
}

public int getDistance() {
return distance;
}
}
19 changes: 19 additions & 0 deletions src/main/java/racing/domain/Winners.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package racing.domain;

import java.util.List;

public class Winners {
private final List<String> names;

public Winners(List<String> names) {
this.names = names;
}

public String getName(int idx) {
return names.get(idx);
}

public int size() {
return names.size();
}
}
Empty file.
5 changes: 5 additions & 0 deletions src/main/java/racing/util/NumberGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package racing.util;

public interface NumberGenerator {
int generateNumber();
}
13 changes: 13 additions & 0 deletions src/main/java/racing/util/RandomDistanceUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package racing.util;

public class RandomDistanceUtil {
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;
}
}
12 changes: 12 additions & 0 deletions src/main/java/racing/util/RandomNumberGenerator.java
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 static final int MAX = 10;

@Override
public int generateNumber() {
return new Random().nextInt(MAX);
}
}
41 changes: 41 additions & 0 deletions src/main/java/racing/util/SplitUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package racing.util;

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

public class SplitUtil {
public static List<String> split(String carNames) {
checkEmpty(carNames);
List<String> splitValue = Arrays.asList(carNames.split(","));

checkCount(splitValue.size());
checkDuplicate(splitValue);

return splitValue;
}

private static void checkEmpty(String carNames) {
if (carNames.isEmpty()) {
throw new NullPointerException("경주할 자동차 이름을 입력하지 않았습니다.");
}
}

private static void checkCount(int carCount) {
if (carCount < 1) {
throw new IllegalArgumentException("게임을 진행하려면 한대 이상의 자동차가 필요합니다.");
}
}

private static void checkDuplicate(List<String> splitValue) {
if (isDuplicate(splitValue)) {
throw new IllegalArgumentException("자동차의 이름은 모두 달라야 합니다.");
}
}

private static boolean isDuplicate(List<String> splitValue) {
return splitValue
.stream()
.distinct()
.count() != splitValue.size();
}
}
35 changes: 35 additions & 0 deletions src/main/java/racing/view/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package racing.view;

import racing.util.SplitUtil;

import java.util.List;
import java.util.Scanner;

public class InputView {
private static final String FIRST_QUESTION = "경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분).";
private static final String SECOND_QUESTION = "시도할 횟수는 몇 회 인가요?";

private final Scanner sc;

public InputView(Scanner sc) {
malibinYun marked this conversation as resolved.
Show resolved Hide resolved
this.sc = sc;
}

public List<String> inputCarNames() {
System.out.println(FIRST_QUESTION);
return SplitUtil.split(sc.nextLine());
}

public int inputTryCount() {
System.out.println(SECOND_QUESTION);
int tryCount = sc.nextInt();
checkPositiveNumber(tryCount);
return tryCount;
}

private void checkPositiveNumber(int tryCount) {
if (tryCount <= 0) {
throw new IllegalArgumentException("최소 한번 이상의 게임을 진행해야 합니다.");
}
}
}
Loading