-
Notifications
You must be signed in to change notification settings - Fork 5
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
base: master
Are you sure you want to change the base?
자동차 경주 게임! #15
Changes from all 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.
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,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); | ||
} | ||
} |
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,23 @@ | ||
package racing.domain; | ||
|
||
public class Car { | ||
private String name; | ||
private int distance; | ||
|
||
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; | ||
} | ||
} |
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 { | ||
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> createCars(List<String> carNames) { | ||
List<Car> cars = new ArrayList<>(); | ||
for (String carName : carNames) { | ||
cars.add(new Car(carName, 0)); | ||
} | ||
return cars; | ||
} | ||
} |
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 { | ||
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 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())); | ||
} | ||
} | ||
} |
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(); | ||
} | ||
} |
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; | ||
} | ||
} |
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(); | ||
} | ||
} |
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,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; | ||
} | ||
} |
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); | ||
} | ||
} |
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(); | ||
} | ||
} |
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("최소 한번 이상의 게임을 진행해야 합니다."); | ||
} | ||
} | ||
} |
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를 작성하는데,
이렇게 하면 주어진 요구사항에 맞지 않을 것 같습니다.
레이싱 게임 요구사항을 확인해주세요.