forked from next-step/java-racingcar-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Sdrs6921 #1
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
Open
sdrs6921
wants to merge
35
commits into
main
Choose a base branch
from
sdrs6921
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Sdrs6921 #1
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
53bf7fc
feat : 이름 VO 테스트 및 구현
sdrs6921 4ec32f2
feat : 위치 VO 테스트 및 구현
sdrs6921 84505b7
feat : Car 객체 테스트 및 구현
sdrs6921 8686549
feat : Movement 전략 테스트 및 구현
sdrs6921 259d5c4
feat : Car 테스트 및 구현
sdrs6921 2cdce39
feat : Name equals, hashCode 테스트 및 구현
sdrs6921 417ad45
feat : equals, hashCode, isLargerThan 테스트 및 기능 구현
sdrs6921 1a2eccf
feat : Car equals(), hashCode() 테스트 및 기능 구현
sdrs6921 484aea8
chore : java, Junit, assertj 버전 변경
sdrs6921 63ed9d3
refactor : Position 불필요한 메서드 삭제
sdrs6921 79d6bd9
feat : Cars 테스트 및 기능 구현
sdrs6921 a93edc3
feat : 시도 횟수 VO 테스트 및 기능 구현
sdrs6921 d03d3ae
feat : InputView, InputConsoleView 기능 구현
sdrs6921 218ce76
refactor : Cars 불변 객체로 변경
sdrs6921 6435528
refactor : 테스트 코드 given, when, then 구조로 변경
sdrs6921 f51293e
refactor : Dto 객체 생성 적용 및 기능 테스트 및 구현
sdrs6921 e8432ac
feat : Names 일급 컬렉션 테스트 및 기능 구현
sdrs6921 4af857c
test : given, when, then 명시적으로 추가
sdrs6921 bb66fba
feat : 시도 횟수 차감 기능 구현
sdrs6921 42d8126
refactor : 메서드 이름 변경
sdrs6921 e7be73d
feat : 라운드 테스트작성 및 기능 구현
sdrs6921 9f1a305
feat : 이름 일급 컬렉션 반환 메서드 테스트 및 기능 구현
sdrs6921 aa7a36d
refactor : 부 생성자 추가
sdrs6921 4d348c6
refactor : 이름 일급컬렉션으로 생성할 수 있도록 변경
sdrs6921 b4bfd50
refactor : Dto 에서 엔티티를 반환하도록 변경
sdrs6921 a1e0b40
feat : Dto 및 Output View 기능 구현
sdrs6921 60bc1a6
feat : Round 테스트 수정
sdrs6921 caf496a
feat : GameController 기능 구현
sdrs6921 3a149be
refactor : 패키지 구조 수정
sdrs6921 6a19f77
refactor : 메서드 이름 수정
sdrs6921 cdf7a23
refactor : 메인 함수 기능 구현
sdrs6921 fcb9e4f
refactor : 이름 중복 로직 기능 구현
sdrs6921 1b9f35b
refactor : 이름 공백 제거 기능 추가
sdrs6921 30e257d
style : 코드 통일성 추가
sdrs6921 135c27b
style : final 키워드 추가
sdrs6921 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package racing; | ||
|
|
||
| import racing.controller.GameController; | ||
| import racing.strategy.MovementStrategy; | ||
| import racing.strategy.RandomMovementStrategyStrategy; | ||
| import racing.view.InputConsoleView; | ||
| import racing.view.InputView; | ||
| import racing.view.OutputConsoleView; | ||
| import racing.view.OutputView; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class RacingCarApplication { | ||
| public static void main(String[] args) { | ||
| Scanner scanner = new Scanner(System.in); | ||
| InputView inputView = new InputConsoleView(scanner); | ||
| OutputView outputView = new OutputConsoleView(); | ||
| MovementStrategy movementStrategy = new RandomMovementStrategyStrategy(); | ||
| GameController gameController = new GameController(inputView, outputView, movementStrategy); | ||
| gameController.run(); | ||
| scanner.close(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package racing.controller; | ||
|
|
||
| import racing.domain.Car; | ||
| import racing.domain.Cars; | ||
| import racing.domain.Names; | ||
| import racing.domain.Round; | ||
| import racing.domain.vo.NumberOfAttempt; | ||
| import racing.dto.InputCarNameDto; | ||
| import racing.dto.InputNumberOfAttemptDto; | ||
| import racing.dto.OutputRoundResultDto; | ||
| import racing.dto.OutputWinnerDto; | ||
| import racing.strategy.MovementStrategy; | ||
| import racing.view.InputView; | ||
| import racing.view.OutputView; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class GameController { | ||
| private final InputView inputView; | ||
| private final OutputView outputView; | ||
| private final MovementStrategy movementStrategy; | ||
|
|
||
| public GameController(final InputView inputView, final OutputView outputView, final MovementStrategy movementStrategy) { | ||
| this.inputView = inputView; | ||
| this.outputView = outputView; | ||
| this.movementStrategy = movementStrategy; | ||
| } | ||
|
|
||
| public void run() { | ||
| Round round = initRound(); | ||
| outputView.printRoundResultMessage(); | ||
| round = race(round); | ||
| printWinner(round); | ||
| } | ||
|
|
||
| private Round race(final Round initialRound) { | ||
| Round round = initialRound; | ||
|
|
||
| while (round.isPlaying()) { | ||
| round = round.play(movementStrategy); | ||
| Cars nextRoundCars = round.cars(); | ||
| OutputRoundResultDto outputRoundResultDto = new OutputRoundResultDto(nextRoundCars); | ||
| outputView.printRoundResult(outputRoundResultDto); | ||
| } | ||
|
|
||
| return round; | ||
| } | ||
|
|
||
| private Round initRound() { | ||
| InputCarNameDto inputCarNameDto = inputView.inputCarNames(); | ||
| InputNumberOfAttemptDto inputNumberOfAttemptDto = inputView.inputNumberOfAttempt(); | ||
| Names names = inputCarNameDto.toNames(); | ||
| NumberOfAttempt numberOfAttempt = inputNumberOfAttemptDto.toNumberOfAttempt(); | ||
| return new Round(names, numberOfAttempt); | ||
| } | ||
|
|
||
| private void printWinner(final Round round) { | ||
| List<Car> winners = round.winners(); | ||
| OutputWinnerDto outputWinnerDto = new OutputWinnerDto(winners); | ||
| outputView.printWinner(outputWinnerDto); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package racing.domain; | ||
|
|
||
| import racing.domain.vo.Name; | ||
| import racing.domain.vo.Position; | ||
| import racing.strategy.MovementStrategy; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class Car { | ||
|
|
||
| private static final int INITIAL_LOCATION = 0; | ||
| private static final int DISTANCE_MOVED_AT_ONCE = 1; | ||
|
|
||
| private final Name name; | ||
| private final Position position; | ||
|
|
||
| public Car(final Name name, final Position position) { | ||
| this.name = name; | ||
| this.position = position; | ||
| } | ||
|
|
||
| public Car(final String name, final int position) { | ||
| this(new Name(name), new Position(position)); | ||
| } | ||
|
|
||
| public Car(final String name) { | ||
| this(name, INITIAL_LOCATION); | ||
| } | ||
|
|
||
| public Car(final Name name) { | ||
| this(name, new Position(INITIAL_LOCATION)); | ||
| } | ||
|
|
||
| public String name() { | ||
| return name.value(); | ||
| } | ||
|
|
||
| public int position() { | ||
| return position.value(); | ||
| } | ||
|
|
||
| public Car move(MovementStrategy movementStrategy) { | ||
| if (movementStrategy.canMove()) { | ||
| return new Car(name, position.increaseBy(DISTANCE_MOVED_AT_ONCE)); | ||
| } | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(final Object other) { | ||
| if (this == other) return true; | ||
| if (!(other instanceof Car)) return false; | ||
| final Car car = (Car) other; | ||
| return name.equals(car.name); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(name); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package racing.domain; | ||
|
|
||
| import racing.strategy.MovementStrategy; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Cars { | ||
|
|
||
| private final List<Car> elements; | ||
|
|
||
| public Cars(final List<Car> elements) { | ||
| this.elements = elements; | ||
| } | ||
|
|
||
| public Cars(final Names carNames) { | ||
| this(convertFrom(carNames)); | ||
| } | ||
|
|
||
| private static List<Car> convertFrom(final Names carNames) { | ||
| return carNames.elements() | ||
| .stream() | ||
| .map(Car::new) | ||
| .collect(Collectors.toUnmodifiableList()); | ||
|
|
||
| } | ||
|
|
||
| public List<Car> elements() { | ||
| return elements; | ||
| } | ||
|
|
||
| public Cars driveAll(final MovementStrategy movementStrategy) { | ||
| List<Car> droveCar = elements.stream() | ||
| .map(car -> car.move(movementStrategy)) | ||
| .collect(Collectors.toUnmodifiableList()); | ||
| return new Cars(droveCar); | ||
| } | ||
|
|
||
| public List<Car> findMaxPositionCars() { | ||
| int max = findMaxPosition(); | ||
| return elements.stream() | ||
| .filter(car -> car.position() == max) | ||
| .collect(Collectors.toUnmodifiableList()); | ||
| } | ||
|
|
||
| private int findMaxPosition() { | ||
| return elements.stream() | ||
| .map(Car::position) | ||
| .max(Integer::compareTo) | ||
| .orElseThrow(() -> new IllegalArgumentException("최대값을 찾을 수 없습니다")); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package racing.domain; | ||
|
|
||
| import racing.domain.vo.Name; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Names { | ||
|
|
||
| private final List<Name> elements; | ||
|
|
||
| public Names(final List<Name> elements) { | ||
| this.elements = elements; | ||
| } | ||
|
|
||
| public List<Name> elements() { | ||
| return elements; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package racing.domain; | ||
|
|
||
| import racing.domain.vo.NumberOfAttempt; | ||
| import racing.strategy.MovementStrategy; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Round { | ||
|
|
||
| private final Cars cars; | ||
| private final NumberOfAttempt numberOfAttempt; | ||
|
|
||
| public Round(final Cars cars, final NumberOfAttempt numberOfAttempt) { | ||
| this.cars = cars; | ||
| this.numberOfAttempt = numberOfAttempt; | ||
| } | ||
|
|
||
| public Round(final Names names, final NumberOfAttempt numberOfAttempt) { | ||
| this(new Cars(names), numberOfAttempt); | ||
| } | ||
|
|
||
| public Round play(final MovementStrategy movementStrategy) { | ||
| NumberOfAttempt numberOfAttemptAfterTrying = numberOfAttempt.decrease(); | ||
| Cars carsAfterDriveAll = this.cars.driveAll(movementStrategy); | ||
| return new Round(carsAfterDriveAll, numberOfAttemptAfterTrying); | ||
| } | ||
|
|
||
| public Cars cars() { | ||
| return cars; | ||
| } | ||
|
|
||
| public List<Car> winners() { | ||
| return cars.findMaxPositionCars(); | ||
| } | ||
|
|
||
| public boolean isPlaying() { | ||
| return !numberOfAttempt.isOver(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package racing.domain.vo; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class Name { | ||
|
|
||
| private static final int MIN_NAME_LENGTH = 1; | ||
| private static final int MAX_NAME_LENGTH = 5; | ||
|
|
||
| private final String value; | ||
|
|
||
| public Name(final String value) { | ||
| validate(value); | ||
| this.value = value; | ||
| } | ||
|
|
||
| private void validate(final String value) { | ||
| if (value.length() < MIN_NAME_LENGTH || value.length() > MAX_NAME_LENGTH) { | ||
| throw new IllegalArgumentException("이름이 길이는 1이상 5이하여야 합니다"); | ||
| } | ||
| } | ||
|
|
||
| public String value() { | ||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(final Object other) { | ||
| if (this == other) return true; | ||
| if (!(other instanceof Name)) return false; | ||
| final Name name = (Name) other; | ||
| return value.equals(name.value); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(value); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package racing.domain.vo; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class NumberOfAttempt { | ||
|
|
||
| private static final int ZERO = 0; | ||
| private static final int SUBTRACTED_VALUE = 1; | ||
|
|
||
| private final int value; | ||
|
|
||
| public NumberOfAttempt(final int value) { | ||
| if (value < ZERO) { | ||
| throw new IllegalArgumentException("시도 횟수는 0 이상의 수 입니다"); | ||
| } | ||
|
|
||
| this.value = value; | ||
| } | ||
|
|
||
| public int value() { | ||
| return value; | ||
| } | ||
|
|
||
| public NumberOfAttempt decrease() { | ||
| return new NumberOfAttempt(value - SUBTRACTED_VALUE); | ||
| } | ||
|
|
||
| public boolean isOver() { | ||
| return value == ZERO; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(final Object o) { | ||
| if (this == o) return true; | ||
| if (!(o instanceof NumberOfAttempt)) return false; | ||
| final NumberOfAttempt that = (NumberOfAttempt) o; | ||
| return value == that.value; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(value); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package racing.domain.vo; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class Position { | ||
|
|
||
| private final int value; | ||
|
|
||
| public Position(final int value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public int value() { | ||
| return value; | ||
| } | ||
|
|
||
| public Position increaseBy(final int distanceMovedAtOnce) { | ||
| return new Position(value + distanceMovedAtOnce); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(final Object o) { | ||
| if (this == o) return true; | ||
| if (!(o instanceof Position)) return false; | ||
| final Position position = (Position) o; | ||
| return value == position.value; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(value); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
버전 변경한 이유가 무엇인가요?