Skip to content

[자동차 경주] 최승훈 미션 제출합니다. #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
wants to merge 16 commits into
base: sseung3424
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
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] 함수(또는 메소드)가 한 가지 일만 잘 하도록 구현한다.
58 changes: 58 additions & 0 deletions src/main/java/CarRace.java
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;
private int winnerIndex;
private final ArrayList<Integer> coWinners = new ArrayList<>();

public CarRace(int car_num, int round_num) {
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();
}

private void moveAllCars() {
for(RacingCar car : cars)
car.move();
}

private int findWinningDistance() {
winnerIndex = 0;
for(int i = 0; i < cars.length; i++)
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);
}

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);
}
}
14 changes: 14 additions & 0 deletions src/main/java/RacingCar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class RacingCar {
public String name;
public int distance;

public RacingCar(String name) {
this.name = name;
this.distance = 0;
}

public void move() {
if(( (int) (Math.random() * 10) >= 4))
distance += 1;
}
}
41 changes: 41 additions & 0 deletions src/test/java/CarRaceTest.java
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));

String actual = carRace.cars[i].name;
String expected = "푸앙이차" + (i+1);

assertThat(actual)
.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();
}
}