-
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
레이싱게임 #19
base: master
Are you sure you want to change the base?
레이싱게임 #19
Changes from all commits
7afd339
35e83a2
d0c3e97
ba9ca4f
5292efd
7d68bf8
64d45c7
a840fdf
e91463b
cf226c1
96a38b5
ff9bc3b
7f26694
40cb0f6
272855b
d9ef760
af9c07f
07f4a17
241b395
cce2667
d57877e
a0a4359
d08eff9
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.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package racing; | ||
|
||
import racing.domain.RacingGame; | ||
import racing.dto.RacingGameInfo; | ||
import racing.support.DefaultEngine; | ||
import racing.view.InputView; | ||
import racing.view.OutputView; | ||
|
||
public class RacingGameApplication { | ||
|
||
public static void main(String[] args) { | ||
RacingGameInfo racingGameInfo = new RacingGameInfo(InputView.askNameOfCar(), InputView.askCountOfMovement()); | ||
RacingGame racingGame = new RacingGame(racingGameInfo); | ||
|
||
racingGame.raceWith(new DefaultEngine()); | ||
|
||
OutputView.printResult(racingGame.getRacingGameResult()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package racing.domain; | ||
|
||
import racing.support.Engine; | ||
|
||
public class Car { | ||
|
||
private final String name; | ||
private int location; | ||
|
||
public Car(String name) { | ||
this.name = name; | ||
} | ||
|
||
public void tryMoveWith(Engine engine) { | ||
if (engine.enough()) { | ||
location++; | ||
} | ||
} | ||
|
||
public int getLocation() { | ||
return location; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package racing.domain; | ||
|
||
import racing.dto.RacingGameInfo; | ||
import racing.support.Engine; | ||
import racing.vo.CarSnapshot; | ||
import racing.vo.RacingCarsSnapshot; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class RacingCars { | ||
|
||
private final List<Car> cars; | ||
|
||
public RacingCars(final RacingGameInfo racingGameInfo) { | ||
this.cars = createCar(racingGameInfo.getCarNameGroup()); | ||
} | ||
|
||
private List<Car> createCar(final List<String> carGroup) { | ||
List<Car> cars = new ArrayList<>(carGroup.size()); | ||
for (String carName : carGroup) { | ||
cars.add(new Car(carName)); | ||
} | ||
return cars; | ||
} | ||
|
||
public RacingCarsSnapshot runWith(final int round, final Engine engine) { | ||
List<CarSnapshot> carSnapshots = new ArrayList<>(cars.size()); | ||
for (Car car : cars) { | ||
car.tryMoveWith(engine); | ||
carSnapshots.add(new CarSnapshot(car.getName(), car.getLocation())); | ||
} | ||
return new RacingCarsSnapshot(round, carSnapshots); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package racing.domain; | ||
|
||
import racing.dto.RacingGameInfo; | ||
import racing.dto.RacingGameResult; | ||
import racing.support.Engine; | ||
import racing.vo.RacingCarsSnapshot; | ||
|
||
public class RacingGame { | ||
|
||
private final RacingCars racingCars; | ||
private final RacingGameInfo racingGameInfo; | ||
private final RacingGameResult racingGameResult; | ||
|
||
public RacingGame(RacingGameInfo racingGameInfo) { | ||
this(new RacingCars(racingGameInfo), racingGameInfo, new RacingGameResult(racingGameInfo)); | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. 아 이거 디디디 강의때 모든 생성을 주생성자에게 위임하는방식을 봐서 한번 적용해봤던거얌 |
||
} | ||
|
||
private RacingGame(RacingCars racingCars, RacingGameInfo racingGameInfo, RacingGameResult racingGameResult) { | ||
this.racingCars = racingCars; | ||
this.racingGameInfo = racingGameInfo; | ||
this.racingGameResult = racingGameResult; | ||
} | ||
|
||
public void raceWith(Engine engine) { | ||
for (int currentRound = 1; currentRound < racingGameInfo.getCountOfMovement() + 1; currentRound++) { | ||
RacingCarsSnapshot snapshot = racingCars.runWith(currentRound, engine); | ||
recordRacingGameSnapshot(snapshot); | ||
} | ||
} | ||
|
||
private void recordRacingGameSnapshot(RacingCarsSnapshot racingCars) { | ||
racingGameResult.record(racingCars); | ||
} | ||
|
||
public RacingGameResult getRacingGameResult() { | ||
return racingGameResult; | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package racing.dto; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class RacingGameInfo { | ||
private static final String NAME_SPLITTER = ","; | ||
|
||
private final List<String> carNameGroup; | ||
private final int countOfMovement; | ||
|
||
public RacingGameInfo(String nameOfCars, String countOfMovement) { | ||
try { | ||
List<String> carGroup = toCarGroup(nameOfCars.trim()); | ||
int inputCountOfMovement = Integer.parseInt(countOfMovement); | ||
|
||
validation(carGroup, inputCountOfMovement); | ||
|
||
this.carNameGroup = carGroup; | ||
this.countOfMovement = inputCountOfMovement; | ||
} catch (NumberFormatException ne) { | ||
throw new IllegalArgumentException("Integer 값만 들어올 수 있습니다"); | ||
} | ||
} | ||
|
||
private List<String> toCarGroup(String carGroup) { | ||
return Arrays.asList(carGroup.split(NAME_SPLITTER)); | ||
} | ||
|
||
private void validation(final List<String> inputNumberOfCar, final int inputCountOfMovement) { | ||
if (inputNumberOfCar.size() <= 1 || inputCountOfMovement <= 0) { | ||
throw new IllegalArgumentException("올바른 입력값이 아닙니다"); | ||
} | ||
} | ||
|
||
public List<String> getCarNameGroup() { | ||
return carNameGroup; | ||
} | ||
|
||
public int getCountOfMovement() { | ||
return countOfMovement; | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package racing.dto; | ||
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. 지금 나눈 dto와 vo의 차이가 뭐야? 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. dto 는 입력, 출력할 때 사용되는 객체고 vo 는 도메인(경기가 끝난 레이싱카)을 그대로 반환하는 것을 막기 위해 Snapshot 클래스를 만든건데 이상한가 ,,? Dto를 도메인이 물고있는 구조가 이상한건 동의해! 이건 더 생각해서 고쳐볼겝 ! 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. 경기 결과를 받는 클라이언트한테 도메인을 노출시키고 싶지 않은데 vo를 도메인 영역으로 표현하면 그게 안될거같거든 ㅠ^ㅠ 머르게따 ,, |
||
|
||
import racing.vo.RacingCarsSnapshot; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class RacingGameResult { | ||
private final int totalGameRound; | ||
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 final List<RacingCarsSnapshot> racingCarsSnapshots = new ArrayList<>(); | ||
|
||
public RacingGameResult(final RacingGameInfo racingGameInfo) { | ||
this.totalGameRound = racingGameInfo.getCountOfMovement(); | ||
} | ||
|
||
public void record(RacingCarsSnapshot racingCarsSnapshot) { | ||
if (disableRecord()) { | ||
throw new IllegalArgumentException(String.format("%d경기 이상 기록할 수 없습니다.", totalGameRound)); | ||
} | ||
racingCarsSnapshots.add(racingCarsSnapshot); | ||
} | ||
|
||
private boolean disableRecord() { | ||
return racingCarsSnapshots.size() >= totalGameRound; | ||
} | ||
|
||
public List<RacingCarsSnapshot> getRacingCarsSnapshots() { | ||
return racingCarsSnapshots; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package racing.support; | ||
|
||
import racing.util.RandomGenerator; | ||
|
||
public class DefaultEngine implements Engine { | ||
|
||
private static final int MOVE_CONDITION = 4; | ||
|
||
@Override | ||
public boolean enough() { | ||
return isMovable(RandomGenerator.pickRandomNumber()); | ||
} | ||
|
||
private boolean isMovable(final int power) { | ||
return power >= MOVE_CONDITION; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package racing.support; | ||
|
||
public interface Engine { | ||
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. Engine과 관련된 클래스들은 다 도메인 영역으로 옮기는게 좋아보여 |
||
boolean enough(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package racing.util; | ||
|
||
import java.util.Random; | ||
|
||
public class RandomGenerator { | ||
|
||
private static final int LIMIT = 10; | ||
private static final Random RANDOM = new Random(); | ||
|
||
public static int pickRandomNumber() { | ||
return RANDOM.nextInt(LIMIT); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package racing.view; | ||
|
||
import java.util.Scanner; | ||
|
||
public class InputView { | ||
|
||
public static String askNameOfCar() { | ||
System.out.println("경주할 자동차 이름을 입력하세요. 이름은 쉼표를 기준으로 구분."); | ||
return new Scanner(System.in).nextLine(); | ||
} | ||
|
||
public static String askCountOfMovement() { | ||
System.out.println("이동횟수를 입력하세요"); | ||
return new Scanner(System.in).next(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package racing.view; | ||
|
||
import racing.dto.RacingGameResult; | ||
import racing.vo.CarSnapshot; | ||
import racing.vo.RacingCarsSnapshot; | ||
|
||
import java.util.List; | ||
|
||
public class OutputView { | ||
|
||
private static final String TRACK_SIGNATURE = "-"; | ||
|
||
public static void printResult(RacingGameResult racingGameResult) { | ||
List<RacingCarsSnapshot> racingCarsPerRound = racingGameResult.getRacingCarsSnapshots(); | ||
for (RacingCarsSnapshot racingCarsSnapshot : racingCarsPerRound) { | ||
printNameAndTrack(racingCarsSnapshot); | ||
System.out.println(); | ||
} | ||
|
||
} | ||
|
||
private static void printNameAndTrack(RacingCarsSnapshot racingCarsSnapshot) { | ||
for (CarSnapshot carSnapshot : racingCarsSnapshot.getCarSnapshots()) { | ||
System.out.printf("%s: ", carSnapshot.getName()); | ||
drawTrack(carSnapshot); | ||
} | ||
} | ||
|
||
private static void drawTrack(CarSnapshot car) { | ||
StringBuilder track = new StringBuilder(); | ||
for (int i = 0; i < car.getLocation(); i++) { | ||
track.append(TRACK_SIGNATURE); | ||
} | ||
System.out.print(track.toString()); | ||
System.out.println(); | ||
} | ||
|
||
} |
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.
이 RacingGameResult라는 객체는 record 라는 메서드 때문에 가변일 수 밖에 없는 객체인데, RacingGameResult를 멤버 필드로 강한 참조를 해버려서 RacingGame도 가변객체가 된다는 느낌이 있네..!
나라면 raceWith를 void가 아닌 RacingGameResult로 낮추어서 조금 관계를 약하게 만들 것 같아