-
Notifications
You must be signed in to change notification settings - Fork 93
[자동차 경주] 최승훈 미션 제출합니다. #60
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
sseung3424
wants to merge
16
commits into
next-step:sseung3424
Choose a base branch
from
sseung3424:main
base: sseung3424
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
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
42161ef
Docs: 기능 요구사항 정리
sseung3424 4b076bd
Feat: 자동차 클래스 추가
sseung3424 71fa8d4
Feat: 자동차 경주 기능 추가
sseung3424 6b51467
Feat: 경주 우승자 찾기 기능 추가
sseung3424 b59021e
Test: 자동차 경주 테스트 추가
sseung3424 e5907b1
Docs: 코드 컨벤션 요구사항 확인
sseung3424 5173d2e
Refactor: camelCase로 통일
sseung3424 b25e0ea
Refactor: 피드백 수정
sseung3424 c2a2a4d
Refactor: 자동차 이름 입력 방식 수정
sseung3424 a83f804
Refactor: WinningIndex 탐색 로직 수정
sseung3424 07f804b
Refactor: findWinner 로직 수정
sseung3424 ac1c758
Test: coWinner 테스트로 수정
sseung3424 b6fd503
Test: 자동차 이동 테스트 추가
sseung3424 4649916
Feat: main에서 자동차 경주 구현
sseung3424 4050ec3
Feat: 자동차 이름 길이 제한 로직 구현
sseung3424 1f79355
Refactor: MVC 패턴 적용
sseung3424 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# 자동차 경주 게임 | ||
|
||
## 기능 요구사항 | ||
- [x] 자동차는 이름을 가지고 있다. | ||
- [x] 자동차는 움직일 수 있다. | ||
- [x] 0에서 9 사이에서 random 값을 구한 후 random 값이 4 이상일 경우 전진하고, 3 이하의 값이면 멈춘다. | ||
- [x] n대의 자동차가 참여할 수 있다. | ||
- [x] 주어진 횟수 동안 n대의 자동차는 전진 또는 멈출 수 있다. | ||
- [x] 자동차 경주 게임을 완료한 후 누가 우승했는지를 구할 수 있다. 우승자는 한 명 이상일 수 있다. | ||
|
||
## 새로운 프로그래밍 요구사항 | ||
- [x] 자동차가 움직이는 기능이 의도대로 동작하는지 테스트한다. | ||
- [x] 우승자를 구하는 기능이 의도대로 동작하는지 테스트한다. | ||
|
||
## 자바 코드 컨벤션(Java Style Guide) | ||
- [x] indent(인덴트, 들여쓰기) depth를 2를 넘지 않도록 구현한다. 1까지만 허용한다. | ||
- 예를 들어 while문 안에 if문이 있으면 들여쓰기는 2이다. | ||
- 힌트: indent(인덴트, 들여쓰기) depth를 줄이는 좋은 방법은 함수(또는 메서드)를 분리하면 된다. | ||
- [x] 3항 연산자를 쓰지 않는다. | ||
- [x] else 예약어를 쓰지 않는다. | ||
- [x] else 예약어를 쓰지 말라고 하니 switch/case로 구현하는 경우가 있는데 switch/case도 허용하지 않는다. | ||
- 힌트: if문에서 값을 반환하는 방식으로 구현하면 else 예약어를 사용하지 않아도 된다. | ||
- [x] 함수(또는 메소드)의 길이가 15라인을 넘어가지 않도록 구현한다. | ||
- [x] 함수(또는 메소드)가 한 가지 일만 잘 하도록 구현한다. |
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,58 @@ | ||
import java.util.ArrayList; | ||
|
||
public class CarRace { | ||
public RacingCar[] cars; | ||
private final int rounds; | ||
sseung3424 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private int winnerIndex; | ||
private final ArrayList<Integer> coWinners = new ArrayList<>(); | ||
sseung3424 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public CarRace(int car_num, int round_num) { | ||
sseung3424 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
rounds = round_num; | ||
cars = new RacingCar[car_num]; | ||
for (int i = 0; i < car_num; i++) { | ||
cars[i] = new RacingCar("자동차" + (i+1)); | ||
} | ||
} | ||
|
||
public void setCarName(int number, String name) { | ||
cars[number].name = name; | ||
} | ||
|
||
public void runRace() { | ||
for(int i = 0; i < rounds; i++) | ||
moveAllCars(); | ||
sseung3424 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private void moveAllCars() { | ||
for(RacingCar car : cars) | ||
car.move(); | ||
} | ||
|
||
private int findWinningDistance() { | ||
winnerIndex = 0; | ||
for(int i = 0; i < cars.length; i++) | ||
sseung3424 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
updateWinner(i); | ||
return cars[winnerIndex].distance; | ||
} | ||
|
||
private void updateWinner(int number) { | ||
if (number < cars.length - 1 && cars[number].distance < cars[number + 1].distance) | ||
winnerIndex = number + 1; | ||
} | ||
|
||
private void findAllWinners(int number, int winning_distance) { | ||
if(cars[number].distance == winning_distance) | ||
coWinners.add(number); | ||
sseung3424 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public void displayAllWinners() { | ||
int winning_distance = findWinningDistance(); | ||
|
||
for(int i = 0; i < cars.length; i++) | ||
findAllWinners(i, winning_distance); | ||
|
||
System.out.println("Winner:"); | ||
for(int coWinnerIndex : coWinners) | ||
System.out.println(cars[coWinnerIndex].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,14 @@ | ||
class RacingCar { | ||
public String name; | ||
sseung3424 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public int distance; | ||
|
||
public RacingCar(String name) { | ||
this.name = name; | ||
this.distance = 0; | ||
} | ||
|
||
public void move() { | ||
sseung3424 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if(( (int) (Math.random() * 10) >= 4)) | ||
distance += 1; | ||
} | ||
} |
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,41 @@ | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class CarRaceTest { | ||
int car_num = 15; | ||
int round_num = 10; | ||
CarRace carRace = new CarRace(car_num, round_num); | ||
|
||
@Test | ||
@DisplayName("자동차_이름_정하기") | ||
public void 자동차_이름_정하기() { | ||
for(int i = 0; i < car_num; i++) { | ||
carRace.setCarName(i, "푸앙이차" + (i+1)); | ||
sseung3424 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
String actual = carRace.cars[i].name; | ||
String expected = "푸앙이차" + (i+1); | ||
|
||
assertThat(actual) | ||
sseung3424 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.isEqualTo(expected); | ||
} | ||
} | ||
|
||
@Test | ||
@DisplayName("자동차_경주_시작하고_우승자_찾기") | ||
public void 자동차_경주_시작하고_우승자_찾기() { | ||
carRace.runRace(); | ||
carRace.displayAllWinners(); | ||
} | ||
|
||
@Test | ||
@DisplayName("자동차_이름_직접_정해서_경주하고_우승자_찾기") | ||
public void 자동차_이름_직접_정해서_경주하고_우승자_찾기() { | ||
for(int i = 0; i < car_num; i++) { | ||
carRace.setCarName(i, "푸앙이차" + (i + 1)); | ||
} | ||
carRace.runRace(); | ||
carRace.displayAllWinners(); | ||
sseung3424 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.