Skip to content

Conversation

@nunomi0
Copy link

@nunomi0 nunomi0 commented Oct 27, 2025

자동차 경주

자동차 이름과 시도 횟수를 입력 받아 자동차 게임을 진행한다.

기능

입력

  • 경주할 자동차 이름(이름은 쉼표(,) 기준으로 구분) 입력
  • 시도할 횟수 입력

파싱

  • 자동차 이름 입력값을 쉼표(,) 기준으로 나누어 배열로 변환
  • 각 이름의 공백 제거

유효성 검사

  • 이름 5자 이하
  • 이름 공백 불가능
  • 이름 중복 불가능
  • 시도 횟수는 0보다 큰 정수

게임 진행

  • 자동차 생성
  • 0에서 9 사이에서 무작위 값을 구한 후 무작위 값이 4 이상인 경우 자동차 전진
  • 시도 횟수만큼 게임 진행
  • 우승자 선출

출력

  • 라운드별 실행 결과 출력
  • 게임 종료 후 우승자 안내 문구 출력

테스트

  • validator
  • car
  • RacingGame
  • output

프로젝트 구조

src/
├── App.js              # 프로그램 실행 흐름 관리 (입력 → 검증 → 게임 → 출력)
├── index.js            # 프로그램 실행 진입점
├── Car.js              # 자동차 클래스 (이름, 위치, 이동 로직)
├── RacingGame.js       # 게임 진행 및 우승자 계산
├── input.js            # 사용자 입력 처리
├── output.js           # 게임 결과 및 우승자 출력
├── validator.js        # 입력값 유효성 검사
└── utils/
    └── randomUtils.js  # 랜덤 숫자 생성 (0~9)
__tests__/              # 단위 테스트 모음

Copy link

@inseong01 inseong01 left a comment

Choose a reason for hiding this comment

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

App.js 내용이 저하고 비슷해서 놀랐습니다.. 간결하게 작성되어 있어서 수월하게 읽을 수 있어서 좋았습니다.

커밋 메시지가 한 줄로 되어 있는데 커밋 바디 부분에 작업 내용도 같이 넣어주시면 커밋이 더 명확해질 거 같아요.

2주 차 미션하시느라 수고하셨습니다~

Comment on lines +11 to +16
this.cars.forEach((car) => {
const randomValue = getRandomNumber();
if (car.canMove(randomValue)) {
car.move();
}
});

Choose a reason for hiding this comment

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

자동차 이동을 판별하는 로직을 car.move로 옮기는 건 어떨까요?

Suggested change
this.cars.forEach((car) => {
const randomValue = getRandomNumber();
if (car.canMove(randomValue)) {
car.move();
}
});
this.cars.forEach((car) => {
car.move();
printRoundResult(this.cars);
});
// Car.js
...
move() {
const randomValue = getRandomNumber();
if (car.canMove(randomValue)) {
car.move();
}
}

Copy link
Author

Choose a reason for hiding this comment

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

자동차 이동을 판별하는 로직을 car.move로 옮기는 건 어떨까요?

리뷰 감사드립니다! 다음부터는 커밋 바디도 작성해봐야겠네요 :)
이동 판별은 자동차 특성이라기보다 게임의 규칙이라고 생각해서 RacingGame에 작성했는데, 자동차가 더 적합한건가요? 혹시 관련된 이론이나 개념이 있으면 알려주시면 감사하겠습니다 🙇‍♀️

Choose a reason for hiding this comment

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

아하 그런 이유가 있었군요. 저는 자동차가 움직이는 조건을 자동차 특성으로 봤어요.
어느 방식이 적합한 지는 관점마다 다르게 적용될 수 있어서 관점에 따라서 다를 거 같아요!

async run() {}
async run() {
const carNamesRaw = await readCarNames();
const carNames = carNamesRaw.split(",").map((name) => name.trim());

Choose a reason for hiding this comment

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

메인 로직을 읽는 부분이어서 세부적인 로직을 따로 추상화하면 더 깔끔해질 거 같아요.

this.position = 0;
}

canMove(randomValue){

Choose a reason for hiding this comment

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

move 매서드하고 합쳐도 될 거 같은데 canMove 매서드로 분리하신 이유가 있을까요?

Choose a reason for hiding this comment

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

에러 항목을 굉장히 깔끔하게 처리하신 것 같습니다! 깔끔해서 읽기 정말 쉽네요.
some()에 대해 잘 몰랐었는데, 이번 기회에 알고 넘어갑니다!
궁금한 점이 하나 있는데, 조건들을 하위 함수로 분리하지 않고 하나의 함수에 넣으신 이유가 있으실까요?

@stellalee1210
Copy link

가독성이 좋아 코드 읽기가 굉장히 편했습니다! 코드 잘 봤습니다ㅎㅎ


export function printRoundResult(cars) {
cars.forEach((car) => {
const positionBar = "-".repeat(car.position);

Choose a reason for hiding this comment

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

car.position으로 바로 가져오는 것도 좋은데, Car 클래스의 인스턴스를 직접 가져오는 것 보다는 private으로 선언한 뒤, getPosition()등의 함수를 통해 값을 받아보는 건 어떨까요?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants