Skip to content

Commit

Permalink
refactor: increase reusability of random number maker
Browse files Browse the repository at this point in the history
  • Loading branch information
feb-dain committed Feb 12, 2023
1 parent ab3216a commit 4f47c29
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
8 changes: 6 additions & 2 deletions 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
10 changes: 7 additions & 3 deletions src/domain/CarGame.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { movingDistance } = require("./MovementIndicator");
const makeRandomNumber = require("../util/RandomNumberMaker");
const { COMMA } = require("../constant/Constants");
const randomNumberBetween = require("../util/RandomNumberMaker");
const { COMMA, RANDOM_NUMBER_RANGE } = require("../constant/Constants");
const { MIN, MAX } = RANDOM_NUMBER_RANGE;

class CarGame {
#carStatus = new Map([]);
Expand All @@ -11,7 +12,10 @@ class CarGame {

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
8 changes: 3 additions & 5 deletions src/util/RandomNumberMaker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const { LESS_THAN_TEN } = require("../constant/Constants");

const makeRandomNumber = () => {
return Math.floor(Math.random() * LESS_THAN_TEN); // 0 ~ 9 사이의 정수
const randomNumberBetween = (min, max) => {
return Math.floor(Math.random() * (max - min + 1) + min);
};

module.exports = makeRandomNumber;
module.exports = randomNumberBetween;

0 comments on commit 4f47c29

Please sign in to comment.