-
Notifications
You must be signed in to change notification settings - Fork 104
[자동차 경주 미션 1,2단계] 김하은 미션 제출합니다. #170
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
base: haeun92e0
Are you sure you want to change the base?
Changes from all commits
1bf94e9
d93de67
788a72e
4b0049c
7b5354b
86f8c67
79f756b
57e94f2
6228d17
301a791
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # 🚗 자동차 경주 게임 1,2단계 (Racing Car Game) | ||
|
|
||
| 주어진 횟수 동안 n대의 자동차가 무작위 값에 따라 전진 또는 멈추며, 최종적으로 가장 멀리 이동한 우승자를 가려내는 게임입니다. | ||
|
|
||
| ## 🎯 핵심 요구사항 | ||
| - 주어진 횟수 동안 n대의 자동차는 전진 또는 멈출 수 있다. | ||
| - 각 자동차는 `0에서 9 사이에서 무작위 값`을 구한 후, 그 값이 **4 이상일 경우 전진**하고 **3 이하의 값이면 멈춘다.** | ||
| - 자동차 경주 게임을 완료한 후 누가 우승했는지를 알려준다. (우승자는 한 명 이상일 수 있다.) | ||
|
|
||
| --- | ||
|
|
||
| ## 🛠️ 기능 구현 목록 | ||
|
|
||
| ### 1. 자동차 (Car) | ||
| - [x] 자동차는 이름과 현재 위치 상태를 가진다. | ||
| - [x] 무작위 값이 4 이상일 경우 위치를 1칸 전진한다. | ||
| - [x] 무작위 값이 3 이하일 경우 현재 위치를 유지한다. | ||
|
|
||
| ### 2. 레이싱 게임 진행 (RacingGame) | ||
| - [x] 출전하는 자동차 명단을 관리한다. | ||
| - [x] `java.util.Random`을 활용하여 0~9 사이의 무작위 값을 생성한다. | ||
| - [x] 입력받은 시도 횟수만큼 각 자동차에게 무작위 값을 전달하여 경주를 진행한다. | ||
|
|
||
| ### 3. 우승자 판별 | ||
| - [x] 경주가 끝난 후, 가장 멀리 이동한 자동차의 최대 위치(maxPosition)를 구한다. | ||
| - [x] 최대 위치와 동일한 위치에 있는 자동차(우승자)를 찾아 반환한다. | ||
| - [x] 우승자가 여러 명일 경우 공동 우승자로 모두 반환한다. | ||
|
|
||
| --- | ||
|
|
||
| ## ✅ 테스트 구현 목록 | ||
|
|
||
| **`CarTest` (개별 자동차 책임 테스트)** | ||
| - [x] 무작위 값이 4 이상일 때 자동차가 정상적으로 전진하는가? | ||
| - [x] 무작위 값이 3 이하일 때 자동차가 전진하지 않고 멈추는가? | ||
|
|
||
| **`RacingTest` (우승자 판별 로직 테스트)** | ||
| - [x] 경주 종료 후 우승자가 여러 명일 경우 공동 우승자를 모두 정확히 반환하는가? | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| public class Car { | ||
| private final String name; | ||
| private int position; | ||
|
Comment on lines
+1
to
+3
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. 여기 name을 final 로 선언한 것은 매우 좋은것 같네요! final 로 선언을 했을때 무슨 이점이 있을까요??
Author
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. 미션에서 name은 한번 지정되면 바뀌지 않는다고 했으므로 final로 선언했습니다! final이 이름이 바뀌지 않는다는 것을 보장해줘 코드를 안전하게..? 만들어주는 이점이 있을 것 같습니다!!! 또한 다른 사람이 코드를 봤을 때 final이라는 단어 하나로 자동차 객체의 불변성을 이해할 수 있을 것 같습니다. 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. final은 객체가 한 번 생성된 이후 상태가 바뀌지 않기 때문에 상태를 예측하기 쉬워진다는 장점이 있습니다 저는 개인적으로 가장 큰 장점은 말씀해주신 것처럼, 그 덕분에 코드를 처음 읽는 사람도 |
||
|
|
||
| private static final int MIN_FORWARD_NUMBER = 4; | ||
|
|
||
| public Car(String name){ | ||
| this.name = name; | ||
| this.position = 0; | ||
| } | ||
|
|
||
| public void move(int randomValue){ | ||
| if(randomValue >= MIN_FORWARD_NUMBER){ | ||
| this.position++; | ||
| } | ||
| } | ||
|
Comment on lines
+12
to
+16
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. 이동 조건 값 4는 상수로 분리하는건 어떨까요? 현재 예를 들면 단순한 숫자 4보다, |
||
|
|
||
| public boolean isAtPosition(int maxPosition){ | ||
| return this.position== maxPosition; | ||
| } | ||
|
|
||
| public String getName(){ | ||
| return this.name; | ||
| } | ||
|
|
||
| public int getPosition(){ | ||
| return this.position; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Random; | ||
|
|
||
| public class RacingGame { | ||
| private final List<Car> 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. 여기도 final 키워드를 붙여 주셨네요! 그렇다면 퀴즈 입니다 |
||
|
|
||
| public RacingGame(List<Car> cars){ | ||
| this.cars = cars; | ||
| } | ||
|
|
||
| public void race(int tryCount){ | ||
| for(int i=0; i< tryCount; i++){ | ||
| moveCars(); | ||
| } | ||
| } | ||
|
Comment on lines
+12
to
+16
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. 빠졌던 요구사항을 잘 구현해 주셨군요!! 이전에는 자동차를 모두 이동시킨 뒤에 RacingGame이 우승자만 판단했는데, 좀 더 명확하게 질문을 드리면 |
||
|
|
||
| public void moveCars(){ | ||
| Random random = new Random(); | ||
| for (Car car : cars){ | ||
| int randomValue = random.nextInt(10); | ||
| car.move(randomValue); | ||
| } | ||
|
|
||
| } | ||
|
Comment on lines
+18
to
+25
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 List<Car> getWinners(){ | ||
| int maxPosition = getMaxPosition(); | ||
| return findCarsAt(maxPosition); | ||
| } | ||
|
|
||
| private int getMaxPosition(){ | ||
| int max=0; | ||
|
Comment on lines
+32
to
+33
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. 현재 코드가 인텔리제이에서는 코드 자동 정렬 기능이 있다는 것을 알고 계시나요? mac : cmd + shift + L 코드를 제출하기 전에 한 번씩 정렬을 하고 커밋을 하면 더욱 깔끔해 집니다 |
||
| for (Car car : cars){ | ||
| max = updateMax(max, car.getPosition()); | ||
| } | ||
| return max; | ||
| } | ||
|
|
||
| private int updateMax(int currentMax, int carPosition){ | ||
| if (carPosition > currentMax){ | ||
| return carPosition; | ||
| } | ||
| return currentMax; | ||
| } | ||
|
Comment on lines
+40
to
+45
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. 이 Max 값을 찾기 위한 메서드는 자바 표준 라이브러리(Math)를 이용해서 없애 볼 수 있을 것 같아요 |
||
|
|
||
| private List<Car> findCarsAt(int maxPosition){ | ||
| List<Car> winners = new ArrayList<>(); | ||
| for(Car car : cars){ | ||
| addIfWinner(winners, car, maxPosition); | ||
| } | ||
| return winners; | ||
| } | ||
|
|
||
| private void addIfWinner(List<Car> winners, Car car, int maxPosition){ | ||
| if (car.isAtPosition(maxPosition)){ | ||
| winners.add(car); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import org.junit.jupiter.api.Test; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| class CarTest { | ||
|
|
||
| @Test | ||
| void 무작위_값이_4_이상이면_전진한다(){ | ||
| Car car = new Car("pobi"); | ||
|
|
||
| car.move(4); | ||
|
|
||
| assertThat(car.getPosition()).isEqualTo(1); | ||
| } | ||
|
Comment on lines
+7
to
+14
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. 테스트 네이밍이 훨씬 명확해 졌네요! 다만 네이밍에서 현재 테스트는 실제로 무작위 값을 생성해서 검증하는 것이 아니라, 또한 Car 객체 자체는 무작위라는 개념을 알지 않고, 그래서 테스트 이름도 |
||
|
|
||
| @Test | ||
| void 무작위_값이_3_이하이면_멈춘다(){ | ||
| Car car = new Car("rabbit"); | ||
|
|
||
| car.move(3); | ||
|
|
||
| assertThat(car.getPosition()).isEqualTo(0); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import org.junit.jupiter.api.Test; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| public class RacingTest { | ||
|
|
||
|
Comment on lines
+5
to
+7
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. 우리가 이 클래스에서 테스트 하고자 하는 대상이 |
||
| @Test | ||
| void 우승자가_여러명일_경우_공동_우승자를_모두_반환한다(){ | ||
|
Comment on lines
+8
to
+9
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.
현재 테스트가 공동 우승자 상황에만 집중되어 있는데, 예를 들어
특히 테스트에서는 경계 상황에 대해 내가 의도한 동작을 하는지를 검증해 보는게 특히나 중요한 것 같아요! |
||
| Car pobi = new Car("pobi"); | ||
| Car crong = new Car("crong"); | ||
| Car rabbit = new Car("rabbit"); | ||
|
|
||
| pobi.move(4); | ||
| crong.move(5); | ||
| rabbit.move(2); | ||
|
|
||
| RacingGame game = new RacingGame(Arrays.asList(pobi, crong, rabbit)); | ||
| List<Car> winners = game.getWinners(); | ||
|
|
||
| assertThat(winners).containsExactly(pobi, crong); | ||
|
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 comment
The reason will be displayed to describe this comment to others. Learn more.
👏👏👏👏