Skip to content

Commit

Permalink
[2단계 - 자동차 경주 리팩터링] 야미(이다인) 미션 제출합니다. (#224)
Browse files Browse the repository at this point in the history
* rename: rename folders

* rename: rename folders

removed all cache to enable case sensitivity in git

* refactor: increase reusability of random number maker

* refactor: remove useless code

* test: create test code for random number maker

* refactor: rename method

* style: unify method style
  • Loading branch information
feb-dain authored Feb 13, 2023
1 parent d363711 commit efbccfa
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 39 deletions.
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);
};

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

0 comments on commit efbccfa

Please sign in to comment.