-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
feature(step4): 자동차 경주(우승자) 기능 구현 #6056
Merged
testrace
merged 14 commits into
next-step:leejin0527
from
LeeJin0527:feature-racinggame
Mar 26, 2025
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3755dff
refactor(step3): 코드리뷰 반영
LeeJin0527 66a7abf
refactor(step3): 코드리뷰 반영
LeeJin0527 897b852
feat(step4): 자동차 이름 입력 값 검증 추가
LeeJin0527 de6c4b8
feat(step4): 자동차 객체 생성 시 이름 로직 추가
LeeJin0527 d0bacbd
feat(step4): 경주 진행 시 매 회차 자동차들의 위치 출력 기능 구현
LeeJin0527 c535ddf
feat(step4): 최종 우승자 출력기능 구현
LeeJin0527 5b22668
docs(step4): 기능 요구사항 md 완료 항목 체크
LeeJin0527 06dbad0
refactor(step4) : final 추가
LeeJin0527 75f3c15
refactor(step4) : 자동차명 원시 값 포장 & 테스트코드 작성
LeeJin0527 df07b2f
refactor(step4) : 부 생성자를 주 생성자를 호출하도록 수정
LeeJin0527 b5e3e93
refactor(step4) : 출력과 관련된 상수 OutputView로 이동
LeeJin0527 d8def67
test(step4) : 객체 값 비교 테스트 추가
LeeJin0527 7c551d1
test(step4) : PositionTest 위치 기반으로 수정
LeeJin0527 722e61c
test(step4) : 우승자 출력 테스트 추가
LeeJin0527 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 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 was deleted.
Oops, something went wrong.
This file contains 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,47 @@ | ||
package racingcar; | ||
|
||
import racingcar.model.Car; | ||
import racingcar.model.RacingGame; | ||
import racingcar.util.InputValidator; | ||
import racingcar.view.InputView; | ||
import racingcar.view.OutputView; | ||
|
||
import java.util.List; | ||
|
||
import static racingcar.message.ErrorMessage.INVALID_NAME; | ||
import static racingcar.message.Message.*; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
|
||
final InputView inputView = new InputView(); | ||
|
||
final List<String> names = getValidCarNames(inputView); | ||
final int attemptCount = getValidAttemptCount(inputView); | ||
|
||
final RacingGame racingGame = RacingGame.createRacingGame(names); | ||
|
||
final RacingGame finalResult = racingGame.race(attemptCount); | ||
|
||
final List<Car> winners = finalResult.getWinners(); | ||
OutputView.printWinners(winners); | ||
} | ||
|
||
private static List<String> getValidCarNames(InputView inputView) { | ||
while (true) { | ||
try { | ||
inputView.printMessage(CAR_NAME_MESSAGE); | ||
return InputValidator.validateNames(inputView.readInput()); | ||
|
||
} catch (IllegalArgumentException e) { | ||
OutputView.print(INVALID_NAME); | ||
} | ||
} | ||
} | ||
|
||
private static int getValidAttemptCount(InputView inputView) { | ||
inputView.printMessage(ATTEMPT_COUNT_MESSAGE); | ||
|
||
return InputValidator.validatePositiveNumber(inputView.readInput()); | ||
} | ||
} |
This file contains 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 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 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 |
---|---|---|
@@ -1,34 +1,48 @@ | ||
package racingcar.model; | ||
|
||
import static racingcar.view.OutputView.NAME_POSITION_SEPERATOR; | ||
|
||
public class Car { | ||
public final class Car { | ||
|
||
private final CarName name; | ||
private final Position position; | ||
|
||
private static final int MOVE_CONDITION_NUMBER = 4; | ||
|
||
public Car() { | ||
this.position = new Position(); | ||
public Car(CarName name) { | ||
this(name, new Position(0)); | ||
} | ||
|
||
public Car(int initialPosition) { | ||
this.position = new Position(initialPosition); | ||
public Car(CarName name, Position initialPosition) { | ||
this.name = name; | ||
this.position = initialPosition; | ||
} | ||
|
||
public void move(int randomValue) { | ||
public Car move(int randomValue) { | ||
if (canMove(randomValue)) { | ||
position.incrementPosition(); | ||
return new Car(this.name, this.position.incrementPosition()); | ||
} | ||
return this; | ||
} | ||
|
||
private static boolean canMove(int randomValue) { | ||
private boolean canMove(int randomValue) { | ||
return randomValue >= MOVE_CONDITION_NUMBER; | ||
} | ||
|
||
public boolean isAtPosition(Position other) { | ||
return position.isSameAs(other); | ||
} | ||
|
||
public void printPosition() { | ||
System.out.println(position); | ||
public CarName getName() { | ||
return name; | ||
} | ||
|
||
public int getPosition() { | ||
return position.getPosition(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return name + NAME_POSITION_SEPERATOR + position; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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,48 @@ | ||
package racingcar.model; | ||
|
||
import static racingcar.message.ErrorMessage.INVALID_CAR_NAME_LENGTH; | ||
import static racingcar.message.ErrorMessage.INVALID_NAME; | ||
|
||
public class CarName { | ||
|
||
private final String name; | ||
Comment on lines
+6
to
+8
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 MAXIMUM_NAME_LENGTH = 5; | ||
|
||
public CarName(String value) { | ||
validate(value); | ||
this.name = value; | ||
} | ||
|
||
private void validate(String value) { | ||
if (value == null || value.isBlank()) { | ||
throw new IllegalArgumentException(INVALID_NAME); | ||
} | ||
|
||
if (value.length() > MAXIMUM_NAME_LENGTH) { | ||
throw new IllegalArgumentException(INVALID_CAR_NAME_LENGTH); | ||
} | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (!(o instanceof CarName)) return false; | ||
CarName carName = (CarName) o; | ||
return name.equals(carName.name); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return name.hashCode(); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,28 +1,33 @@ | ||
package racingcar.model; | ||
|
||
import static racingcar.view.OutputView.MOVE; | ||
|
||
public class Position { | ||
|
||
private int position; | ||
private static final String DELIMITER = "-"; | ||
private final int position; | ||
|
||
public Position() { | ||
this.position = 0; | ||
this(0); | ||
} | ||
|
||
public Position(int initialPosition) { | ||
this.position = initialPosition; | ||
} | ||
|
||
public void incrementPosition() { | ||
position += 1; | ||
public Position incrementPosition() { | ||
return new Position(this.position + 1); | ||
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 boolean isSameAs(Position other) { | ||
return this.position == other.position; | ||
} | ||
|
||
public int getPosition() { | ||
return position; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return DELIMITER.repeat(position); | ||
return MOVE.repeat(position); | ||
} | ||
} |
This file contains 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
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.
MVC 패턴을 의식하셔서 model, view 패키지를 나누셨을거라 생각되는데요
이미 OutputView에서 Car 모델을 참조하고 있기에 model 패키지와 view 패키지는 순환참조하게 되었네요.
다음 단계의 주제이니 의존성 방향을 정리해 보시면 좋을 것 같아요.