Skip to content

[숫자 야구 게임] cactus - MVC 패턴 적용 - #2

Open
ehlung wants to merge 32 commits into
mainfrom
cactus
Open

[숫자 야구 게임] cactus - MVC 패턴 적용#2
ehlung wants to merge 32 commits into
mainfrom
cactus

Conversation

@ehlung

@ehlung ehlung commented May 28, 2026

Copy link
Copy Markdown
Contributor

개인 목표 달성 여부

  • HTML, CSS, JavaScript 기본 문법 적용 및 체득
    • 기본 문법 활용도가 아직 부족하고, 코드 작성 경험이 오래된 탓에 일부 로직은 AI의 힌트를 받아 작성한 부분이 아쉬움
  • MVC 패턴 이해 및 적용
  • 몰랐던 개념과 새롭게 알게 된 사실 정리
  • AI와 스터디를 활용한 리팩토링
    • 스터디 이후 진행 예정

리뷰어에게

  • Model, View, Controller의 역할 분배가 적절하게 이루어졌는지 의견을 듣고 싶습니다. 특히 play 메서드가 { strike, ball } 객체를 반환하고 문자열 조립은 Controller에서 담당하도록 분리한 부분이 고민이었습니다.
  • 시간 여유가 없어 리팩토링을 과정 중간중간 진행하다 보니 미처 다듬지 못한 부분이 있습니다. README 작성 과정에서 유효성 검사 실패 메시지나 결과 렌더링 시 성공 여부 비교에 사용되는 문자열을 상수로 분리하는 방식을 추천받았는데, 리뷰어는 해당 부분을 어떤 방식으로 작성했는지 궁금합니다.

@ehlung
ehlung requested a review from meteorqz6 May 28, 2026 11:06
Comment thread index.html
<h3>📄 결과</h3>
<div id="result"></div>
<button id="game-restart-button">재시작</button>
<button id="game-restart-button" hidden>재시작</button>

@meteorqz6 meteorqz6 May 28, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[제안]
저는 hidden 말고 style="display: none" 으로 재시작 버튼 숨김 처리를 했는데 두 가지 방식의 차이점에 대해서 같이 알아볼까요?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

좋은 공부가 될 것 같습니다. 관련 내용은 마찬가지로 레포 README에 정리해두었습니다.

Comment thread src/BaseballGame.js Outdated
}

/*
문자열 변환은 Controller의 역할

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[질문]
문자열 변환을 Controller의 역할이라고 생각하신 이유가 궁금해요!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

1차로 숫자 야구 게임의 기능을 구현할 때는 play 메서드에서 문자열 변환을 한 번에 처리했는데, MVC 패턴을 적용할 때 Model의 기능을 더 엄밀하게 잡는 과정에서 Controller로 역할을 넘겨주었습니다. 숫자 야구 게임의 핵심 로직이 ballstrike의 값을 계산해 넘겨주는 것까지이고, 문자열 계산은 데이터 가공의 영역이라는 생각 때문입니다.
그런데 말씀해주신 내용을 검토하다보니 미션의 프로그래밍 요구사항이 _play 메서드는 Stirng으로 결과값을 return 한다._라고 정의되어 있는 것을 다시 확인했습니다. 요구사항에 맞춰 다시 Model로 해당 로직을 옮겨오도록 하겠습니다.

Comment thread src/View.js Outdated
Comment on lines +4 to +7
this.userInputNum = document.querySelector("#user-input");
this.submitButton = document.querySelector("#submit");
this.resultDiv = document.querySelector("#result");
this.reStartButton = document.querySelector("#game-restart-button");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[제안]
자바스크립트에서 HTML 요소를 담는 변수 이름 앞에 $를 붙이는 것은 자주 쓰이는 관례로 알고 있습니다! $가 붙어 있다면 이 변수에는 HTML 요소가 들어있다는 것을 시각적으로 빠르게 파악할 수 있어서 붙여 보면 어떨까요?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

말씀해주신 부분을 알아보니 HTML 요소를 담는 변수 이름 $접두사를 붙이면 해당 변수에 DOM 요소가 할당되어 있다는 것을 코드 상에서 직관적으로 보여줄 수 있다는 장점이 있다는 것을 알 수 있었습니다. 코드 가독성을 높일 수 있는 좋은 방법인 것 같아서 적용해보겠습니다. 감사합니다!

Comment thread src/View.js Outdated
Comment on lines +19 to +20
this.userInputNum.value = ""; // 입력창 비우기
this.userInputNum.focus(); // 커서 깜빡임

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[배움]
저는 에러 처리 시, alert만 띄우고 입력창을 비우고 포커싱을 주는 코드를 구현하지 않았는데 이 부분이 있다면 사용자가 다시 입력할 수 있게 유도할 수 있을 것 같습니다!

Comment thread src/View.js Outdated

// 컨트롤러가 넘겨준 결과(3스트라이크)를 화면에 그리기
this.renderResult = function (resultString) {
if (resultString === "3스트라이크") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[제안]
"3스트라이크"와 같은 게임 규칙은 Model에 들어가는 것이 더 바람직한 것 같아요.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

말씀해주신대로 3스트라이크와 같이 정답을 판별하는 기능은 바람직한 View의 역할과 맞지 않을 뿐더러 게임의 규칙에 해당하기 때문에 Model로 이동하는 것이 좋은 것 같습니다. 수정해보겠습니다.

Comment thread src/View.js
// 콜백 함수
this.bindSubmitEvent = function (handler) {
this.submitButton.addEventListener("click", (event) => {
event.preventDefault();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[논의]
저는 버튼 태그에 type="button"을 추가해서 해결했습니다.
스터디 세션 때 두 방법을 같이 정리하고 더 알아보면 좋을 것 같습니다!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

유성님 코드를 보니 button에 type 속성을 추가해서 해결할 수 있다는 것을 새롭게 알게 되었습니다. 두 방식의 차이점이나 장단점에 대해서는 레포 README에 작성하겠습니다.

Comment thread src/BaseballGame.js Outdated
} */

// js에서는 return A,B 형태 사용 불가 - , 마지막 값만 평가 -> 객체로 묶어줘야 함.
return { strike: strikeCnt, ball: ballCnt };

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[제안]
요구사항을 보면 play 메서드가 string을 반환하는 것으로 나와 있습니다. 객체가 아닌 실제 결과인 string을 반환하도록 수정하면 어떨까요?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

위의 답변과 같은 내용이라 참고해주시면 감사하겠습니다.

Comment thread README.md Outdated

**`play` 메서드 반환값 변경**

문자열 조립이 Model의 역할인지 Controller의 역할인지 고민했다. 문자열 조립은 표현 로직이므로 Controller에서 담당하는 것이 적절하다고 판단하여, `play` 메서드는 `{ strike, ball }` 객체를 반환하도록 수정하고 문자열 조립은 Controller에서 처리하도록 분리했다.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[제안]
이 부분도 같이 수정 필요할 것 같아요!

Comment thread src/View.js Outdated
ehlung and others added 3 commits May 29, 2026 11:14
변수명 통일성 수정

Co-authored-by: Yooseong Nam <102887277+meteorqz6@users.noreply.github.com>
@meteorqz6 meteorqz6 changed the title [숫자 야구 게임] cactus - 기능 구현 이후 MVC 패턴 적용 [숫자 야구 게임] cactus - MVC 패턴 적용 May 29, 2026
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.

2 participants