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

[디디] 레이싱게임 리뷰요청 드립니다. #86

Merged
merged 52 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
c993438
feat: 문자열 덧셈 계산기 첫번째 테스트 구현
include42 Feb 11, 2020
13da028
feat: 문자열 덧셈 계산기 두번째 테스트 구현
include42 Feb 11, 2020
9a2da16
feat: 문자열 덧셈 계산기 세번째 테스트 구현
include42 Feb 11, 2020
78fb041
feat: 문자열 덧셈 계산기 네번째 테스트 구현
include42 Feb 11, 2020
b1c8abc
feat: 문자열 덧셈 계산기 다섯번째 테스트 구현
include42 Feb 11, 2020
963e17a
feat: 문자열 덧셈 계산기 여섯번째 테스트 구현
include42 Feb 11, 2020
8477099
docs: README 파일 작성
include42 Feb 11, 2020
dc3016c
feat: 문자열 덧셈 계산기 일곱번째 테스트 구현
include42 Feb 11, 2020
dfe2f5d
feat: 입력 처리를 위한 InputView, Input 클래스
include42 Feb 11, 2020
ef0d1e2
refactor: StringAdder 리팩토링
include42 Feb 11, 2020
128f1a7
refactor: StringAdderTest 리팩토링
include42 Feb 11, 2020
de18aea
refactor: StringAdderTest,Input 리팩토링
include42 Feb 11, 2020
1e5178e
refactor: Input 내의 checkInputEmpty 수정
include42 Feb 11, 2020
ad00e5a
refactor: Input 클래스 리팩토링
include42 Feb 12, 2020
673f887
refactor: StringAdder 클래스 리팩토링
include42 Feb 12, 2020
58eb502
refactor: InputView 삭제 및 리팩토링
include42 Feb 12, 2020
ab8154e
refactor: Input 클래스 리팩토링
include42 Feb 12, 2020
2c80e2d
docs: README 기능목록 작성
include42 Feb 12, 2020
8f2c39e
feat: 레이싱 게임 첫번째 테스트
include42 Feb 12, 2020
64c1f48
feat: 레이싱 게임 두번째 테스트
include42 Feb 12, 2020
f133dd5
feat: 레이싱 게임 세번째 테스트
include42 Feb 12, 2020
1017249
refactor: RacingGame 코드 리팩토링
include42 Feb 12, 2020
e672def
feat: 레이싱 게임 네번째 테스트
include42 Feb 12, 2020
154d381
refactor: RacingGameTest 설계 변경 및 코드 리팩토링
include42 Feb 12, 2020
5728858
feat: Input, InputView 클래스 구현
include42 Feb 12, 2020
0d2db11
feat: 레이싱 게임 다섯번째 테스트
include42 Feb 12, 2020
16ca493
feat: 레이싱 게임 여섯번째 테스트
include42 Feb 12, 2020
062f133
feat: 레이싱 게임 일곱번째 테스트
include42 Feb 12, 2020
316334d
feat: 레이싱 게임 여덟번째 테스트
include42 Feb 12, 2020
6b35e41
feat: Output,OutputView 클래스 구현
include42 Feb 12, 2020
0141793
refactor: OutputView 외 나머지 클래스 리팩토링
include42 Feb 12, 2020
a615c2d
feat: Main 클래스 작성
include42 Feb 12, 2020
4fe2c6d
refactor: 패키지 생성 및 Input 설계 변경
include42 Feb 13, 2020
6d3910e
feat: Output 리팩토링
include42 Feb 13, 2020
a96232c
refactor: 코드 리팩토링
include42 Feb 13, 2020
0065e52
docs: README 구현단위 작성
include42 Feb 13, 2020
9c23b44
refactor: 코드 리팩토링
include42 Feb 13, 2020
866bc51
refactor: 클린코드 리팩터링
fucct Feb 13, 2020
96cad54
refactor: 오탈자 수정
fucct Feb 13, 2020
6ae1619
refactor: 축약어 제거
fucct Feb 14, 2020
83093f1
refactor: 에러메시지 상수화
fucct Feb 14, 2020
06b8a34
refactor: 클래스 이동
fucct Feb 14, 2020
de7cb6c
refactor: Random 시드 설정
fucct Feb 14, 2020
147a221
refactor: 메서드 분리
fucct Feb 15, 2020
9619d06
refactor: getDelimiter 메서드 리팩터링
fucct Feb 15, 2020
f7891c9
refactor: 일급컬렉션 생성
fucct Feb 16, 2020
722f72a
refactor: 오탈자 수정
fucct Feb 16, 2020
6d62621
refactor: 메서드 리팩터링
fucct Feb 17, 2020
7a8676f
refactor: 메서드 리팩터링
fucct Feb 17, 2020
9637bf0
refactor: 메서드 리팩터링
fucct Feb 17, 2020
5fe8b2e
refactor: 테스트 클래스, 메서드 추가
fucct Feb 17, 2020
8602a96
refactor: moveCars 메서드 리팩터링, 전략패턴 적용
fucct Feb 18, 2020
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
4 changes: 2 additions & 2 deletions src/main/java/racinggame/controller/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public static void main(String... args) {
Names names = new Names(InputView.inputName());
Repeat repeat = new Repeat(InputView.inputRepeat());
Cars cars = new Cars(names);
RacingGame racingGame = new RacingGame();
RacingGame racingGame = new RacingGame(cars);
int maxPosition = 0;

OutputView.printResultFormat();
for (int i = 0; i < repeat.getRepeat(); i++) {
maxPosition = racingGame.moveCars(cars, maxPosition);
maxPosition = racingGame.moveCars(new RandomGenerator());
OutputView.printCarsLog(cars);
}
Winners winners = cars.makeWinners(maxPosition);
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/racinggame/domain/Car.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package racinggame.domain;

public class Car {
private static final int FORWARD_NUMBER = 4;
private static final int START_POSITION = 0;
private static int FORWARD_NUMBER = 4;
private static int START_POSITION = 0;
private final String name;
private int position;

Expand All @@ -15,7 +15,8 @@ public Car(String name, int position) {
this.position = position;
}

public boolean move(int random) {
public boolean move(Strategy strategy) {
int random = strategy.generateRandom();
if (random >= FORWARD_NUMBER) {
Copy link

Choose a reason for hiding this comment

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

이 부분에 전략패턴을 적용 해 보세요!

position++;
return true;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/racinggame/domain/Cars.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public Cars(Names names) {
cars = names.makeCars();
}

public Cars(List<Car> cars){
this.cars = cars;
}

public Winners makeWinners(int maxPosition) {
Copy link

Choose a reason for hiding this comment

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

외부로 부터 maxPosition을 받아오지말고 아래처럼 내부에 있는 Car list를 통해 maxPosition을 구해도 되지 않을까요?

    public Winners makeWinners() {
        int maxPosition = getMaxPosition();

        List<Car> winners = cars.stream()
                .filter(car -> car.isSamePosition(maxPosition))
                .collect(Collectors.toList());
        return new Winners(winners);
    }

    private int getMaxPosition() {
        return cars.stream()
                    .mapToInt(Car::getPosition)
                    .max()
                    .orElse(0);
    }

List<Car> winners = new ArrayList<>();

Expand Down
16 changes: 0 additions & 16 deletions src/main/java/racinggame/domain/MoveCarsOnlyFalse.java

This file was deleted.

12 changes: 0 additions & 12 deletions src/main/java/racinggame/domain/MoveCarsOnlyTrue.java

This file was deleted.

7 changes: 7 additions & 0 deletions src/main/java/racinggame/domain/MoveGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package racinggame.domain;

public class MoveGenerator implements Strategy {
public int generateRandom() {
return 5;
}
}
16 changes: 7 additions & 9 deletions src/main/java/racinggame/domain/RacingGame.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package racinggame.domain;

import java.util.Random;

public class RacingGame implements Strategy {
public class RacingGame {
public static final int NUMBER_BOUND = 10;
private Cars cars;

public static int generateRandom() {
Random rand = new Random();
rand.setSeed(System.nanoTime());
return rand.nextInt(NUMBER_BOUND);
public RacingGame(Cars cars) {
this.cars = cars;
}

public int moveCars(Cars cars, int maxPosition) {
public int moveCars(Strategy randomGenerator) {
int maxPosition = 0;
for (Car car : cars) {
if (car.move(generateRandom())) {
if (car.move(randomGenerator)) {
Copy link

Choose a reason for hiding this comment

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

randomGenerator에 전략패턴을 사용했군요! 👍

maxPosition = car.getMaxPosition(maxPosition);
Copy link

Choose a reason for hiding this comment

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

음... 매번 움직일때 마다 MaxPosition을 선정 해 줄 필요가 있을까요?

}
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/racinggame/domain/RandomGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package racinggame.domain;

import java.util.Random;

import static racinggame.domain.RacingGame.NUMBER_BOUND;

public class RandomGenerator implements Strategy {

public int generateRandom() {
Random rand = new Random();
rand.setSeed(System.nanoTime());
return rand.nextInt(NUMBER_BOUND);
}
}
7 changes: 7 additions & 0 deletions src/main/java/racinggame/domain/StopGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package racinggame.domain;

public class StopGenerator implements Strategy {
public int generateRandom() {
return 0;
}
}
2 changes: 1 addition & 1 deletion src/main/java/racinggame/domain/Strategy.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package racinggame.domain;

public interface Strategy {
int moveCars(Cars cars, int maxPosition);
int generateRandom();
}
37 changes: 14 additions & 23 deletions src/test/java/CarTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import racinggame.domain.Car;
import racinggame.domain.MoveGenerator;
import racinggame.domain.StopGenerator;

public class CarTest {
Car car;
Expand All @@ -14,34 +16,23 @@ public class CarTest {
@BeforeEach
void initCar() {
car = new Car("moveTest");
pobiCar = new Car("pobi");
crongCar = new Car("crong");
honuxCar = new Car("honux");

pobiCar.move(4);

crongCar.move(7);
crongCar.move(7);

honuxCar.move(1);
honuxCar.move(2);
honuxCar.move(3);
honuxCar.move(4);
honuxCar.move(5);
honuxCar.move(6);
honuxCar.move(7);
pobiCar = new Car("pobi", 4);
crongCar = new Car("crong", 3);
honuxCar = new Car("honux", 7);
}

@Test
void 이동_테스트(){
Assertions.assertThat(car.move(new MoveGenerator())).isTrue();
}

@ParameterizedTest
@CsvSource(value = {"0,false", "3,false", "4,true", "9,true"})
void moveTest(int random, boolean expected) {
boolean actual = car.move(random);
Assertions.assertThat(actual).isEqualTo(expected);
@Test
void 정지_테스트(){
Assertions.assertThat(car.move(new StopGenerator())).isFalse();
}

@ParameterizedTest
@CsvSource(value = {"1,true,false,false", "2,false,true,false", "4,false," +
@CsvSource(value = {"4,true,false,false", "3,false,true,false", "7,false," +
"false,true"})
void samePositionTest(int random, boolean expected1, boolean expected2,
boolean expected3) {
Expand Down
71 changes: 16 additions & 55 deletions src/test/java/RacingGameTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import racinggame.domain.*;

Expand All @@ -8,69 +7,31 @@

public class RacingGameTest {
private static final List<Car> carList = new ArrayList<>();
private static final int WINNER_POSITION = 3;
private final String NAMES = "pobi,crong,honux";
private Names names = new Names(NAMES);
private static Cars cars;
private static RacingGame racingGame;

void initList() {
static {
carList.clear();
carList.add(new Car("pobi", 0));
carList.add(new Car("kim", 0));
carList.add(new Car("park", 0));

carList.get(0).move(4);
carList.get(0).move(4);
carList.get(0).move(4);

carList.get(1).move(4);
carList.get(1).move(4);
carList.get(1).move(4);

carList.get(2).move(4);
carList.get(2).move(2);
carList.get(2).move(1);
}

@Test
void moveCarsOnlyTrueTest() {
int maxPosition = 0;
Cars cars = new Cars(names);
Strategy racingGame = new MoveCarsOnlyTrue();

for (int i = 1; i <= 10; i++) {
Assertions.assertThat(racingGame.moveCars(cars, maxPosition)).isEqualTo(i);
}
carList.add(new Car("pobi", 4));
carList.add(new Car("crong", 7));
carList.add(new Car("honux", 3));
cars = new Cars(carList);
racingGame = new RacingGame(cars);
}

@Test
void moveCarsOnlyFalseTest() {
int maxPosition = 0;
Cars cars = new Cars(names);
Strategy racingGame = new MoveCarsOnlyFalse();

for (int i = 1; i <= 10; i++) {
Assertions.assertThat(racingGame.moveCars(cars, maxPosition)).isEqualTo(0);
}
}
void Cars_이동_테스트() {
racingGame.moveCars(new MoveGenerator());
Winners winners = cars.makeWinners(8);


@RepeatedTest(value = 50)
void 랜덤_테스트() {
int result = RacingGame.generateRandom();

Assertions.assertThat(result).isBetween(0, 9);
Assertions.assertThat(winners.getWinners()).containsExactly("crong");
}

@Test
void 우승자_확인_테스트() {
initList();
boolean chkWinner = carList.get(0).isSamePosition(WINNER_POSITION);
Assertions.assertThat(chkWinner).isEqualTo(true);

chkWinner = carList.get(1).isSamePosition(WINNER_POSITION);
Assertions.assertThat(chkWinner).isEqualTo(true);
void Cars_정지_테스트() {
racingGame.moveCars(new StopGenerator());
Winners winners = cars.makeWinners(7);

chkWinner = carList.get(2).isSamePosition(WINNER_POSITION);
Assertions.assertThat(chkWinner).isEqualTo(false);
Assertions.assertThat(winners.getWinners()).containsExactly("crong");
}
}
23 changes: 12 additions & 11 deletions src/test/java/WinnersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,29 @@
import racinggame.domain.Names;
import racinggame.domain.Winners;

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

public class WinnersTest {
private final Names names = new Names("pobi,crong,honux");
private Cars cars = new Cars(names);
private Cars cars;
private Winners winners;
private String winnerName = "honux";
private String winnerName = "crong";

@BeforeEach
void initCarsPosition() {
int i = 0;
for (Car car : cars) {
car.move(6);
car.move(i);
i += 3;
}
void initList() {
List<Car> carList = new ArrayList<>();
carList.add(new Car("pobi", 4));
carList.add(new Car("crong", 7));
carList.add(new Car("honux", 3));
cars = new Cars(carList);
}

@Test
void makeWinnersTest() {
winners = cars.makeWinners(2);
winners = cars.makeWinners(7);
List<String> winnerNames = winners.getWinners();
Assertions.assertThat(winnerNames.contains(winnerName)).isTrue();
Assertions.assertThat(winnerNames.contains("pobi")).isFalse();
Assertions.assertThat(winnerNames.contains("honux")).isFalse();
}
}