Skip to content
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

Step3 - 자동차 경주 #2336

Merged
merged 6 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/main/java/racingcar/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package racingcar;

import java.util.ArrayList;
import java.util.List;

public class Car {
private final static int GO_VALUE = 4;
private int moveSpace;
Comment on lines +7 to +8

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private final static int GO_VALUE = 4;
private int moveSpace;
private static final int GO_VALUE = 4;
private int moveSpace;

static이 final보다 앞에 오는 것이 컨벤션입니다!
또한 클래스 변수와 인스턴스 변수 사이에 공백을 추가한다면 가독성이 상승합니다 :)


public Car(int moveSpace) {
this.moveSpace = moveSpace;
}

public int getMoveSpace() {
return moveSpace;
}

public int advanceOrStop(int randomValue) {
if(randomValue >= GO_VALUE) {
return 1;
}
return 0;
}

public int move(int randomValue) {
moveSpace += advanceOrStop(randomValue);
return moveSpace;
}

public static List<Car> createCars(int carNumber) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#2273 (comment) 해당 코멘트를 확인해주세요!

List<Car> cars = new ArrayList<>();
for(int i = 0; i < carNumber; i++) {
cars.add(new Car(0));
}
return cars;
}

}
20 changes: 20 additions & 0 deletions src/main/java/racingcar/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package racingcar;

import java.util.Scanner;

/*
* 자동차 경주 Input
*/
public class InputView {
Scanner sc = new Scanner(System.in);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Scanner sc = new Scanner(System.in);
private final Scanner sc = new Scanner(System.in);

접근제어자를 생략한 것은 의도된 내용일까요!?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scanner 클래스를 사용할때 무의식적으로 접근제어자를 생략하는 버릇이 들었습니다.
항상 접근제어자를 써보도록 노력할게요


public int getNumberOfCars() {
System.out.println("자동차 대수는 몇 대 인가요?");
return Integer.parseInt(sc.nextLine());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Integer.parseInt(sc.nextLine());
return sc.nextInt();

와 같이 작성해볼 수 있을 것 같네요 :)

}

public int getNumberOfTry() {
System.out.println("시도할 횟수는 몇 회 인가요?");
return Integer.parseInt(sc.nextLine());
}
}
55 changes: 55 additions & 0 deletions src/main/java/racingcar/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## 기능 요구사항
- 초간단 자동차 경주 게임을 구현한다.
- 주어진 횟수 동안 n대의 자동차는 전진 또는 멈출 수 있다.
- 사용자는 몇 대의 자동차로 몇 번의 이동을 할 것인지를 입력할 수 있어야 한다.
- 전진하는 조건은 0에서 9 사이에서 랜덤 값을 구한 후 랜덤 값이 4이상일 경우이다.
- 자동차의 상태를 화면에 출력한다. 어느 시점에 출력할 것인지에 대한 제약은 없다.

### 실행 결과
- 위 요구사항에 따라 3대의 자동차가 5번 움직였을 경우 프로그램을 실행한 결과는 다음과 같다.
```
자동차 대수는 몇 대 인가요?
3
시도할 회수는 몇 회 인가요?
5

실행 결과
-
-
-

--
-
--

---
--
---

----
---
----

----
----
-----
```

### 힌트
- 값을 입력 받는 API는 Scanner를 이용한다.
```java
Scanner scanner = new Scanner(System.in);
String value = scanner.nextLine();
int number = scanner.nextInt();
```
- 랜덤 값은 자바 java.util.Random 클래스의 nextInt(10) 메소드를 활용한다.

## 프로그래밍 요구사항
- 모든 로직에 단위 테스트를 구현한다. 단, UI(System.out, System.in) 로직은 제외
- 핵심 로직을 구현하는 코드와 UI를 담당하는 로직을 구분한다.
- UI 로직을 InputView, ResultView와 같은 클래스를 추가해 분리한다.
- 자바 코드 컨벤션을 지키면서 프로그래밍한다.
- 참고문서: https://google.github.io/styleguide/javaguide.html 또는 https://myeonguni.tistory.com/1596
- else 예약어를 쓰지 않는다.
- 힌트: if 조건절에서 값을 return하는 방식으로 구현하면 else를 사용하지 않아도 된다.
- else를 쓰지 말라고 하니 switch/case로 구현하는 경우가 있는데 switch/case도 허용하지 않는다.
22 changes: 22 additions & 0 deletions src/main/java/racingcar/RacingGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package racingcar;

import java.util.List;

public class RacingGame {
private static final int MAX_VALUE = 10;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사용하지 않는 변수는 제거해주세요~


public static void main(String[] args) {
InputView inputView = new InputView();
ResultView resultView = new ResultView();

int carNumber = inputView.getNumberOfCars();
int tryNumber = inputView.getNumberOfTry();
List<Car> cars = Car.createCars(carNumber);

for(int i = 0; i < tryNumber; i++) {
//5회

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 주석은 어떠한 정보를 나타내기 위함일까요?

resultView.printAll(cars);
System.out.println();
}
}
}
31 changes: 31 additions & 0 deletions src/main/java/racingcar/ResultView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package racingcar;

import java.util.List;
import java.util.Random;

/*
* 자동차 경주 결과 View
*/
public class ResultView {

private static final int MAX_VALUE = 10;

public void print(int move) {
for (int i = 0; i < move; i++) {
System.out.print("-");
}
System.out.println();
}

public void printAll(List<Car> cars) {
for(Car car: cars) {
Random random = new Random();
int randomNumber = random.nextInt(MAX_VALUE);
int moveNumber = car.move(randomNumber);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

View에서 도메인 기능을 사용하는 것이 올바른 설계일지 고민해보면 좋겠어요~
도메인과 View를 분리하였을 때 어떠한 장점이 있을지를 중점으로 접근해보면 도움이 될 것 같습니다 😄

//3회
print(moveNumber);
}
}


}
46 changes: 46 additions & 0 deletions src/test/java/racingcar/CarTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package racingcar;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Random;

import static org.assertj.core.api.Assertions.assertThat;

class CarTest {
Car car;
Random random;
ResultView resultView;

@BeforeEach
void setUpCar() {
car = new Car(0);
random = new Random();
resultView = new ResultView();
}

@Test
@DisplayName("랜덤값 4이상의 경우 전진 테스트")
void randomValueTest() {
int tryNumber = 5;
int result = 0;

for(int i = 0; i < tryNumber; i++){
int randomNumber = random.nextInt(10);
result = car.move(randomNumber);
}

assertThat(car.getMoveSpace()).isEqualTo(result);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

car.move에서 위치를 반환하기 때문에 사실상 getMoveSpace()의 결과와 같은 값이 나올 것 같은데요!
해당 테스트로 move 메서드가 잘 동작하는지 테스트가 가능할지 고민해보면 좋을 것 같습니다 :)

}

@Test
@DisplayName("3대의 차 생성하기")
void createCarsTest() {
int carNumber = 3;
List<Car> cars = Car.createCars(3);

assertThat(cars.size()).isEqualTo(carNumber);
}
}
27 changes: 27 additions & 0 deletions src/test/java/racingcar/RacingGameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package racingcar;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

class RacingGameTest {
InputView inputView;

@BeforeEach
void setUp() {
inputView = new InputView();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InputView는 어떠한 이유로 생성해주셨을까요!?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드를 수정한 후에 삭제를 안했습니다.

}

@Test
@DisplayName("자동차 생성 테스트")
void createCarTest() {
int carNumber = 1;
List<Car> cars = Car.createCars(carNumber);

assertThat(cars.get(0).getMoveSpace()).isEqualTo(0);
}
}