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

[2단계 - 자동차 경주 리팩터링] 야미(이다인) 미션 제출합니다. #224

Merged
merged 7 commits into from
Feb 13, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions __tests__/MovementIndicator.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { movingDistance } = require("../src/Domain/MovementIndicator");
const { MOVEMENT } = require("../src/Constants/Constants");
const movingDistance = require("../src/domain/MovementIndicator");
const { MOVEMENT } = require("../src/constant/Constants");
const { FORWARD_DISTANCE } = MOVEMENT;

test.each([
Expand Down
17 changes: 17 additions & 0 deletions __tests__/RandomNumberMaker.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const randomNumberBetween = require("../src/util/RandomNumberMaker");

test.each([
[0, 9],
[1, 5],
[2, 22],
[3, 100],
[4, 90],
[5, 40],
[6, 30],
[7, 9],
[8, 10],
[9, 29],
])("%i ~ %i 사이의 난수 반환", (min, max) => {
expect(randomNumberBetween(min, max)).toBeGreaterThanOrEqual(min);
expect(randomNumberBetween(min, max)).toBeLessThanOrEqual(max);
});
2 changes: 1 addition & 1 deletion __tests__/SplitAndTrimString.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const splitAndTrimString = require("../src/Utils/SplitAndTrimString");
const splitAndTrimString = require("../src/util/SplitAndTrimString");

test("입력받은 자동차 이름 콤마(,)로 분리 후 앞뒤 공백 제거 테스트", () => {
const names = "야미,클린 ,레고, 타미";
Expand Down
2 changes: 1 addition & 1 deletion __tests__/Validator.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Validator = require("../src/Validator/Validator");
const Validator = require("../src/validator/Validator");
const { inputCarNameValidator, tryCountValidator } = Validator;

const checkException = (testList, checkFunction) => {
Expand Down
16 changes: 8 additions & 8 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const Utils = require("./Utils/Utils");
const splitAndTrimString = require("./Utils/SplitAndTrimString");
const { readCarName, readTryCount } = require("./UI/InputView");
const Utils = require("./util/Utils");
const splitAndTrimString = require("./util/SplitAndTrimString");
const { readCarName, readTryCount } = require("./view/InputView");
const {
printResultMessage,
printCarMovement,
printWinner,
} = require("./UI/OutputView");
} = require("./view/OutputView");
const {
inputCarNameValidator,
tryCountValidator,
} = require("./Validator/Validator");
const { errorCatcher } = require("./Validator/ErrorCatcher");
} = require("./validator/Validator");
const errorCatcher = require("./validator/ErrorCatcher");

const CarGame = require("./Domain/CarGame");
const CarGame = require("./domain/CarGame");

class App {
#game = new CarGame();
Expand Down Expand Up @@ -47,7 +47,7 @@ class App {

acceptValidTryCount(count) {
this.#round = count;
this.showGameResult(count);
this.showGameResult();
}

playRounds = () => {
Expand Down
7 changes: 0 additions & 7 deletions src/Utils/RandomNumberMaker.js

This file was deleted.

8 changes: 6 additions & 2 deletions src/Constants/Constants.js → src/constant/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ const CAR_NAME_LENGTH = Object.freeze({
MAXIMUM_CAR_NAME_LENGTH: 5,
});

const RANDOM_NUMBER_RANGE = Object.freeze({
MIN: 0,
MAX: 9,
});

const MINIMUM_TRY_COUNT = 1;
const MINIMUM_NUMBER_OF_CARS = 2;
const LESS_THAN_TEN = 10;

const ERROR_MESSAGE = Object.freeze({
NAME_LENGTH_LIMIT: `자동차 이름은 ${CAR_NAME_LENGTH.MINIMUM_CAR_NAME_LENGTH}글자 이상, ${CAR_NAME_LENGTH.MAXIMUM_CAR_NAME_LENGTH}글자 이하로 입력해 주세요.`,
Expand All @@ -44,9 +48,9 @@ module.exports = {
LINE_BREAK,
GAME_MESSAGE,
CAR_NAME_LENGTH,
RANDOM_NUMBER_RANGE,
MINIMUM_TRY_COUNT,
MINIMUM_NUMBER_OF_CARS,
LESS_THAN_TEN,
ERROR_MESSAGE,
REGEX,
MOVEMENT,
Expand Down
16 changes: 10 additions & 6 deletions src/Domain/CarGame.js → src/domain/CarGame.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
const { movingDistance } = require("./MovementIndicator");
const makeRandomNumber = require("../Utils/RandomNumberMaker");
const { COMMA } = require("../Constants/Constants");
const movingDistance = require("./MovementIndicator");
const randomNumberBetween = require("../util/RandomNumberMaker");
const { COMMA, RANDOM_NUMBER_RANGE } = require("../constant/Constants");
const { MIN, MAX } = RANDOM_NUMBER_RANGE;

class CarGame {
#carStatus = new Map([]);

initializeCarStatus = (carNames) => {
initializeCarStatus(carNames) {
carNames.forEach((name) => this.#carStatus.set(name, 0));
};
}

moveCar() {
[...this.#carStatus.entries()].forEach(([name, count]) => {
this.#carStatus.set(name, count + movingDistance(makeRandomNumber()));
this.#carStatus.set(
name,
count + movingDistance(randomNumberBetween(MIN, MAX))
);
});

return this.#carStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { MOVEMENT } = require("../Constants/Constants");
const { MOVEMENT } = require("../constant/Constants");
const { FORWARD_CONDITION_NUMBER, FORWARD_DISTANCE } = MOVEMENT;

const movingDistance = (randomNumber) => {
return randomNumber >= FORWARD_CONDITION_NUMBER ? FORWARD_DISTANCE : 0;
};

module.exports = { movingDistance };
module.exports = movingDistance;
5 changes: 5 additions & 0 deletions src/util/RandomNumberMaker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const randomNumberBetween = (min, max) => {
return Math.floor(Math.random() * (max - min + 1) + min);
};
Comment on lines +1 to +3

Choose a reason for hiding this comment

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

min, max 값에 default Value를 선언해서 상수에 정의한 수를 주입하면 좀 더 확장성 있게 만들 수 있을 것 같네요!

Suggested change
const randomNumberBetween = (min, max) => {
return Math.floor(Math.random() * (max - min + 1) + min);
};
const randomNumberBetween = (min = RANDOM_NUMBER_RANGE.MIN, max = RANDOM_NUMBER_RANGE.MAX) =>
Math.floor(Math.random() * (max - min + 1) + min);


module.exports = randomNumberBetween;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { COMMA } = require("../Constants/Constants");
const { COMMA } = require("../constant/Constants");

const splitAndTrimString = (string) => {
return string.split(COMMA).map((str) => str.trim());
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Utils = require("../Utils/Utils");
const Utils = require("../util/Utils");

const errorCatcher = (validator, readInput, acceptValidInput) => {
try {
Expand All @@ -10,4 +10,4 @@ const errorCatcher = (validator, readInput, acceptValidInput) => {
}
};

module.exports = { errorCatcher };
module.exports = errorCatcher;
6 changes: 3 additions & 3 deletions src/Validator/Validator.js → src/validator/Validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {
CAR_NAME_LENGTH,
REGEX,
MINIMUM_TRY_COUNT,
} = require("../Constants/Constants");
} = require("../constant/Constants");
const {
MINIMUM_CAR_COUNT,
NAME_DUPLICATE,
Expand Down Expand Up @@ -48,14 +48,14 @@ class Validator {
}
}

isNumber(number) {
isStringNumber(number) {
if (ONLY_NUMBER.test(number)) {
throw new Error(NOT_POSITIVE_NUMBER);
}
}

isValidTryCount(count) {
this.isNumber(count);
this.isStringNumber(count);

if (count < MINIMUM_TRY_COUNT) {
throw new Error(NOT_POSITIVE_NUMBER);
Expand Down
4 changes: 2 additions & 2 deletions src/UI/InputView.js → src/view/InputView.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Utils = require("../Utils/Utils");
const { GAME_MESSAGE } = require("../Constants/Constants");
const Utils = require("../util/Utils");
const { GAME_MESSAGE } = require("../constant/Constants");
const { INPUT_CAR_NAME, INPUT_TRY_COUNT } = GAME_MESSAGE;

const InputView = {
Expand Down
4 changes: 2 additions & 2 deletions src/UI/OutputView.js → src/view/OutputView.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { print } = require("../Utils/Utils");
const { GAME_MESSAGE, LINE_BREAK } = require("../Constants/Constants");
const { print } = require("../util/Utils");
const { GAME_MESSAGE, LINE_BREAK } = require("../constant/Constants");
const { RUN_RESULT_MESSAGE, GAME_RESULT, COLON, MOVEMENT_UNIT } = GAME_MESSAGE;

const OutputView = {
Expand Down