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

[이영진] 자동차게임 재구현 #9

Open
wants to merge 3 commits into
base: youngreal
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'eclipse'

group = 'camp.nextstep'
version = '1.0.0'
sourceCompatibility = '1.8'
sourceCompatibility = "11"

repositories {
mavenCentral()
Expand All @@ -17,3 +17,4 @@ dependencies {
test {
useJUnitPlatform()
}
targetCompatibility = JavaVersion.VERSION_11
4 changes: 2 additions & 2 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo carLocation of your Java installation.

goto fail

Expand All @@ -60,7 +60,7 @@ echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo carLocation of your Java installation.

goto fail

Expand Down
426 changes: 426 additions & 0 deletions hs_err_pid10196.log

Large diffs are not rendered by default.

354 changes: 354 additions & 0 deletions replay_pid10196.log

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions src/main/java/game/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package game;

import java.util.regex.Pattern;

public class Car {
private static final Pattern CAR_NAME_PATTERN = Pattern.compile("^[a-zA-Z]*$");
private static final int CAR_MAX_LENGTH = 5;
private final String carName;
private static final int ADVANCE_RANDOM_NUMBER = 4;
private final StringBuffer stringLocation = new StringBuffer("-");

public Car(String carName) {
if (carName.length() > CAR_MAX_LENGTH) {
throw new IllegalArgumentException("자동차 이름의 길이는 5자리를 넘을수 없다");
}

if (isNotMatchCarNamePattern(carName) || carName.isBlank()) {
throw new IllegalArgumentException("자동차 이름은 영문만 올수있습니다");
}
this.carName = carName;
}

public StringBuffer advanceCarLocation(int randomNum) {
if (randomNum >= ADVANCE_RANDOM_NUMBER) {
return this.stringLocation.append("-");
}
return this.stringLocation;
}

public String getCarName() {
return carName;
}

private boolean isNotMatchCarNamePattern(String carName) {
return !CAR_NAME_PATTERN.matcher(carName).matches();
}

public StringBuffer getStringLocation() {
return stringLocation;
}
}
31 changes: 31 additions & 0 deletions src/main/java/game/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package game;

import game.storage.CarStorage;
import game.util.Util;

import java.util.Scanner;

public class Game {
private final Scanner sc = new Scanner(System.in);
private CarStorage carStorage;

public void gameStart() {
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표를 기준으로 구분)");
String[] splitCarNames = sc.nextLine().split(",");

System.out.println("시도할 회수는 몇회인가요?");
int gameCount = sc.nextInt();

System.out.println("실행 결과");
Util util = new Util(splitCarNames);
ResultView resultView = new ResultView(splitCarNames,gameCount);

while (gameCount > 0) {
carStorage = util.addCarsToCarStorage();
resultView.print(gameCount, carStorage);
System.out.println("");
gameCount--;
}
resultView.findWinner(splitCarNames,carStorage);
}
}
8 changes: 8 additions & 0 deletions src/main/java/game/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package game;

public class Main {
public static void main(String[] args) {
Game game = new Game();
game.gameStart();
}
}
63 changes: 63 additions & 0 deletions src/main/java/game/ResultView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package game;

import game.storage.CarStorage;

public class ResultView {
private int max =0;
private final int initialGameCountValue;
private final StringBuffer answer = new StringBuffer("");
private final String[] split;

public ResultView(String[] split, int initialGameCountValue) {
this.split = split;
this.initialGameCountValue = initialGameCountValue;
}

public void print(int gameCount, CarStorage carStorage) {
for (int i = 0; i < split.length; i++) {
if (initialGameCountValue == gameCount) {
System.out.println(carStorage.getCarName(i) + " : " + carStorage.getNotAdvanceCar(i));
continue;
}
System.out.println(carStorage.getCarName(i) + " : " + carStorage.getAdvanceCar(i));
calculateMaxValue(carStorage, i);
}
}

public void findWinner(String[] split, CarStorage carStorage) {
for (int i = 0; i < split.length; i++) {
if (isWinnerLength(carStorage, i)) {
answer.append(carStorage.getCarName(i));
}
}
printWinner();
}

private void calculateMaxValue(CarStorage carStorage, int i) {
if (isOverMaxLocation(carStorage, i)) {
max= carStorage.getNotAdvanceCar(i).length();
}
}

private boolean isOverMaxLocation(CarStorage carStorage, int i) {
return carStorage.getNotAdvanceCar(i).length() > max;
}
Comment on lines +36 to +44

Choose a reason for hiding this comment

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

현재 구현체에서 carStorage.getNotAdvanceCar(i)의 length() 값을 가져오는 방식보다 carStorage 안에엇 특정한 자동차의 length를 제공하는 메서드를 만드는 방식이 더 좋아보입니다. ☺️


private void printWinner() {
for (int i = 0; i < answer.length(); i++) {
System.out.print(answer.charAt(i));
if (isNotFinalSequence(i)) {
System.out.print(",");
}
}
Comment on lines +47 to +52

Choose a reason for hiding this comment

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

한 메서드에 오직 한 단계의 들여쓰기만 한다라는 객체 지향 생활 원칙을 지켜보는건 어떨까요? ☺️

System.out.print("가 최종 우승했습니다");
}

private boolean isNotFinalSequence(int i) {
return i != answer.length() - 1;
}

private boolean isWinnerLength(CarStorage carStorage, int i) {
return carStorage.getNotAdvanceCar(i).length() == max;
}
}
32 changes: 32 additions & 0 deletions src/main/java/game/storage/CarStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package game.storage;

import game.Car;

import java.util.Collections;
import java.util.List;

import static game.util.Util.generateRandomNumber;

public class CarStorage {
private final List<Car> cars;

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

public List<Car> getCars() {
return Collections.unmodifiableList(cars);
}

public String getCarName(int number) {
return getCars().get(number).getCarName();
}

public StringBuffer getAdvanceCar(int number) {
return getCars().get(number).advanceCarLocation(generateRandomNumber());
}

public StringBuffer getNotAdvanceCar(int number) {
return getCars().get(number).getStringLocation();
}
}
28 changes: 28 additions & 0 deletions src/main/java/game/util/Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package game.util;

import game.Car;
import game.storage.CarStorage;

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

public class Util {
private final String[] split;
private final List<Car> cars = new ArrayList<>();

public Util(String[] split) {
this.split = split;
}

public CarStorage addCarsToCarStorage() {
for (String s : this.split) {
Car car = new Car(s);
cars.add(car);
}
return new CarStorage(cars);
}

public static int generateRandomNumber() {
return (int) (Math.random() * 10);

Choose a reason for hiding this comment

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

요구사항에는 전진하는 조건은 0에서 9 사이에서 random 값을 구한 후 random 값이 4이상일 경우이다.이라 적혀있지만, 해당 난수 생성은 0 ~ 10 사이의 정수를 반환하고 있어 요구 사항과 달라보입니다. 🥺

}
}
21 changes: 21 additions & 0 deletions src/test/java/CarLocationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import game.Car;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class CarLocationTest {
Car car = new Car("pobi");

@Test
@DisplayName("랜덤값이 4이상일때 -가 하나 추가된다 : --출력")
void location_MoreThan_2_Then_AddStringBuffer() {
assertEquals("--",car.advanceCarLocation(4).toString() );
}

@Test
@DisplayName("랜덤값이 4미만일때는 초기값(-) 그대로를 출력한다 : -출력")
void location_MoreLess_2_Then_InitStringBuffer() {
assertEquals("-", car.advanceCarLocation(3).toString());
}
}
39 changes: 39 additions & 0 deletions src/test/java/CarTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import game.Car;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.*;

class CarTest {

@Test
@DisplayName("자동차 이름은 5자를 넘을수 없음")
void carNameLength_IsExceed_Then_Exception() {
assertThrows(IllegalArgumentException.class, () -> {
inputCarName("aaaaaaaa");
});
}

@ParameterizedTest
@ValueSource(strings = {" ","","a, b, c"})
@DisplayName("자동차 이름은 공백이 포함될수 없음")
void carName_isEmptyOrNull_Then_Exception(String carName) {
assertThrows(IllegalArgumentException.class, () -> {
inputCarName(carName);
});
}

@Test
@DisplayName("자동차 이름엔 특수문자가 들어갈수 없음")
void carName_Include_SpecialCharacters_Then_Exception() {
assertThrows(IllegalArgumentException.class, () ->{
inputCarName("!@#$");
});
}

private void inputCarName(String carName) {
Car car = new Car(carName);
}
}