-
Notifications
You must be signed in to change notification settings - Fork 10
[숫자 야구 게임] 김지성 미션 제출합니다. #4
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
base: main
Are you sure you want to change the base?
Conversation
|
""" Walkthrough이번 변경 사항은 Changes
Sequence Diagram(s)sequenceDiagram
participant U as 사용자
participant G as 게임 컨트롤러
participant R as randomnumber()
participant F as 피드백 함수
participant P as pick()
U->>G: 게임 시작 요청
G->>G: gamestart() 호출 (시작 메시지 출력)
G->>R: randomnumber() 호출 (랜덤 숫자 생성)
G->>U: 사용자 입력 요청
U->>G: 세 자리 숫자 입력
G->>F: 결과 평가 후 적절한 피드백 함수 호출 (nothing, onlystrike, 등)
G->>U: 결과 메시지 출력
G->>P: pick() 호출 (재시작/종료 선택)
P->>G: 선택 결과 전달
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 5
🧹 Nitpick comments (4)
src/baseball/main.py (4)
3-4:randomnumber()함수의 성능 개선이 필요합니다.현재 구현은 정확하지만,
range(1, 10)은 매 호출마다 새로운 range 객체를 생성합니다. 상수로 정의하여 재사용하는 것이 더 효율적일 것 같습니다.다음과 같이 수정하는 것을 제안합니다:
+NUMBERS = list(range(1, 10)) def randomnumber(): - return random.sample(range(1, 10), 3) + return random.sample(NUMBERS, 3)
6-17: 출력 함수들의return print()패턴이 불필요합니다.
print()는 이미None을 반환하므로,return을 사용할 필요가 없습니다.다음과 같이 수정하는 것을 제안합니다:
def nothing(): - return print("낫싱") + print("낫싱") def onlystrike(x): - return print("%d스트라이크"%x) + print(f"{x}스트라이크") def onlyball(x): - return print("%d볼"%x) + print(f"{x}볼") def ballandstrike(x,y): - return print("%d볼 %d스트라이크"%(x,y)) + print(f"{x}볼 {y}스트라이크")참고: f-string을 사용하면 더 현대적이고 가독성 있는 코드가 됩니다.
18-23: 게임 제어 함수들도 불필요한return print()패턴을 사용합니다.앞서 언급한 것과 같은 이유로 수정이 필요합니다.
다음과 같이 수정하는 것을 제안합니다:
def gamestart(): - return print("숫자 야구 게임을 시작합니다") + print("숫자 야구 게임을 시작합니다") def pick(): - return print("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.") + print("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.")
65-77: 결과 처리 로직의 조건문 순서와 구조를 개선할 수 있습니다.현재 조건문의 순서가 최적화되어 있지 않고, 일부 조건은 중복됩니다.
다음과 같이 수정하는 것을 제안합니다:
- if ball == 0 and strike == 0: - nothing() - elif strike == 3: + if strike == 3: onlystrike(strike) print('게임 종료') break - elif ball == 0: - onlyball(ball) - elif strike == 0: - onlystrike(strike) - elif ball != 0 and strike != 0: + elif ball == 0 and strike == 0: + nothing() + elif ball > 0 and strike > 0: ballandstrike(ball, strike) + elif ball > 0: + onlyball(ball) + else: # strike > 0 + onlystrike(strike)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
docs/README.md(1 hunks)src/baseball/main.py(1 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
src/baseball/main.py
30-30: Ambiguous variable name: l
(E741)
42-42: Ambiguous variable name: l
(E741)
| def main(): | ||
| """ | ||
| 프로그램의 진입점 함수. | ||
| 여기에서 전체 프로그램 로직을 시작합니다. | ||
| """ | ||
| print('숫자 야구 게임을 시작합니다') | ||
| while 1: | ||
|
|
||
| strike=0 | ||
| ball=0 | ||
| l=[] | ||
| computer=randomnumber() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
코드 개선이 필요한 부분이 있습니다.
- 게임 시작 메시지가 중복됩니다 (
gamestart()함수가 있는데도 직접 출력). - 변수명
l은 숫자 1이나 대문자 I와 혼동될 수 있어 가독성이 떨어집니다.
다음과 같이 수정하는 것을 제안합니다:
def main():
- print('숫자 야구 게임을 시작합니다')
+ gamestart()
while 1:
strike = 0
ball = 0
- l = []
+ user_numbers = []
computer = randomnumber()📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| def main(): | |
| """ | |
| 프로그램의 진입점 함수. | |
| 여기에서 전체 프로그램 로직을 시작합니다. | |
| """ | |
| print('숫자 야구 게임을 시작합니다') | |
| while 1: | |
| strike=0 | |
| ball=0 | |
| l=[] | |
| computer=randomnumber() | |
| def main(): | |
| gamestart() | |
| while 1: | |
| strike=0 | |
| ball=0 | |
| user_numbers = [] | |
| computer=randomnumber() |
🧰 Tools
🪛 Ruff (0.8.2)
30-30: Ambiguous variable name: l
(E741)
| a=input('숫자를 입력해주세요 :') | ||
|
|
||
| if len(a) !=3 : | ||
| raise ValueError | ||
|
|
||
| for r in range(len(a)) : | ||
| l.append(int(a[r])) | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
입력 값 검증이 불충분합니다.
현재는 길이만 확인하고 있습니다. 다음 사항들도 검증해야 합니다:
- 입력값이 숫자인지 확인
- 중복된 숫자가 없는지 확인
- 1-9 범위의 숫자인지 확인
다음과 같은 개선된 구현을 제안합니다:
a = input('숫자를 입력해주세요 :')
if len(a) != 3:
- raise ValueError
+ raise ValueError("3자리 숫자를 입력해주세요")
+
+ if not a.isdigit():
+ raise ValueError("숫자만 입력 가능합니다")
+ numbers = [int(x) for x in a]
+ if len(set(numbers)) != 3:
+ raise ValueError("중복되지 않은 숫자를 입력해주세요")
+
+ if not all(1 <= x <= 9 for x in numbers):
+ raise ValueError("1부터 9까지의 숫자만 입력 가능합니다")
+
- for r in range(len(a)):
- l.append(int(a[r]))
+ user_numbers = numbers📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| a=input('숫자를 입력해주세요 :') | |
| if len(a) !=3 : | |
| raise ValueError | |
| for r in range(len(a)) : | |
| l.append(int(a[r])) | |
| a = input('숫자를 입력해주세요 :') | |
| if len(a) != 3: | |
| raise ValueError("3자리 숫자를 입력해주세요") | |
| if not a.isdigit(): | |
| raise ValueError("숫자만 입력 가능합니다") | |
| numbers = [int(x) for x in a] | |
| if len(set(numbers)) != 3: | |
| raise ValueError("중복되지 않은 숫자를 입력해주세요") | |
| if not all(1 <= x <= 9 for x in numbers): | |
| raise ValueError("1부터 9까지의 숫자만 입력 가능합니다") | |
| user_numbers = numbers |
src/baseball/main.py
Outdated
| press=input() | ||
| if press!='1': | ||
| break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
게임 재시작 로직이 일관성이 없습니다.
pick()함수를 정의했지만 사용하지 않습니다.- 입력값 검증이 부족합니다.
다음과 같이 수정하는 것을 제안합니다:
- press = input()
- if press != '1':
+ pick()
+ press = input().strip()
+ if press not in ['1', '2']:
+ raise ValueError("1 또는 2만 입력 가능합니다")
+ if press == '2':
break📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| press=input() | |
| if press!='1': | |
| break | |
| pick() | |
| press = input().strip() | |
| if press not in ['1', '2']: | |
| raise ValueError("1 또는 2만 입력 가능합니다") | |
| if press == '2': | |
| break |
src/baseball/main.py
Outdated
| for i in range(len(computer)-1,-1,-1) : | ||
| if computer[i] == l[i] : | ||
| strike+=1 | ||
| computer.pop(i) | ||
| l.pop(i) | ||
|
|
||
|
|
||
|
|
||
| for k in range(len(l)) : | ||
| if l[k] in computer : | ||
| ball+=1 | ||
|
|
||
| computer=computer1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
게임 로직을 더 효율적으로 개선할 수 있습니다.
현재 구현은 불필요한 복사 연산과 복잡한 로직을 포함하고 있습니다.
다음과 같은 개선된 구현을 제안합니다:
- for i in range(len(computer)-1,-1,-1):
- if computer[i] == l[i]:
- strike += 1
- computer.pop(i)
- l.pop(i)
-
- for k in range(len(l)):
- if l[k] in computer:
- ball += 1
-
- computer = computer1
+ # 스트라이크 계산
+ strike = sum(1 for i in range(3) if user_numbers[i] == computer[i])
+
+ # 볼 계산
+ ball = sum(1 for i in range(3) for j in range(3)
+ if i != j and user_numbers[i] == computer[j])이렇게 하면:
- 불필요한 리스트 조작을 피할 수 있습니다
- 코드가 더 명확해집니다
- 성능이 개선됩니다
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| for i in range(len(computer)-1,-1,-1) : | |
| if computer[i] == l[i] : | |
| strike+=1 | |
| computer.pop(i) | |
| l.pop(i) | |
| for k in range(len(l)) : | |
| if l[k] in computer : | |
| ball+=1 | |
| computer=computer1 | |
| # 스트라이크 계산 | |
| strike = sum(1 for i in range(3) if user_numbers[i] == computer[i]) | |
| # 볼 계산 | |
| ball = sum(1 for i in range(3) for j in range(3) | |
| if i != j and user_numbers[i] == computer[j]) |
| 입력 기능 | ||
| -서로 다른 3자리 수 | ||
| -게임이 끝난 후 1,2 입력 | ||
|
|
||
|
|
||
| 출력 기능 | ||
| -게임 시작문구 "숫자 야구 게임을 시작합니다." 출력 | ||
| -입력한 수에 대한 결과 출력 | ||
| -볼, 스트라이크 출력 | ||
| -아무 것도 없을 시 "낫싱"출력 | ||
| -일치 시 "3개의 숫자를 모두 맞히셨습니다! 게임 종료" 출력 | ||
| -"게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."출력 | ||
|
|
||
| 예외 처리 및 종료 기능능 | ||
| -잘못된 값 입력 시 ValueError 발생, 프로그램 종료 | ||
| - |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
README.md 파일의 개선이 필요합니다.
다음과 같은 문제점들이 있습니다:
- "예외 처리 및 종료 기능능"에 오타가 있습니다.
- 입력값 제한사항(1-9 사이의 숫자만 가능)이 명시되어 있지 않습니다.
- 마크다운 형식을 제대로 활용하지 않아 가독성이 떨어집니다.
다음과 같이 수정하는 것을 제안합니다:
-입력 기능
-서로 다른 3자리 수
-게임이 끝난 후 1,2 입력
+# 숫자 야구 게임
+## 입력 기능
+- 서로 다른 3자리 수 입력
+ - 1부터 9까지의 숫자만 사용 가능
+ - 중복되지 않은 숫자여야 함
+- 게임이 끝난 후 1(새 게임) 또는 2(종료) 입력
+## 출력 기능
+- 게임 시작 문구: "숫자 야구 게임을 시작합니다." 출력
+- 입력한 수에 대한 결과 출력:
+ - 볼과 스트라이크 개수 출력
+ - 아무것도 없을 시 "낫싱" 출력
+ - 모두 일치 시 "3개의 숫자를 모두 맞히셨습니다! 게임 종료" 출력
+ - "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요." 출력
-예외 처리 및 종료 기능능
-잘못된 값 입력 시 ValueError 발생, 프로그램 종료
--
+## 예외 처리 및 종료 기능
+- 다음과 같은 경우 ValueError 발생 후 프로그램 종료:
+ - 3자리가 아닌 숫자 입력
+ - 1-9 범위를 벗어난 숫자 입력
+ - 중복된 숫자 입력
+ - 숫자가 아닌 값 입력
+ - 게임 종료 시 1,2 이외의 값 입력📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| 입력 기능 | |
| -서로 다른 3자리 수 | |
| -게임이 끝난 후 1,2 입력 | |
| 출력 기능 | |
| -게임 시작문구 "숫자 야구 게임을 시작합니다." 출력 | |
| -입력한 수에 대한 결과 출력 | |
| -볼, 스트라이크 출력 | |
| -아무 것도 없을 시 "낫싱"출력 | |
| -일치 시 "3개의 숫자를 모두 맞히셨습니다! 게임 종료" 출력 | |
| -"게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."출력 | |
| 예외 처리 및 종료 기능능 | |
| -잘못된 값 입력 시 ValueError 발생, 프로그램 종료 | |
| - | |
| # 숫자 야구 게임 | |
| ## 입력 기능 | |
| - 서로 다른 3자리 수 입력 | |
| - 1부터 9까지의 숫자만 사용 가능 | |
| - 중복되지 않은 숫자여야 함 | |
| - 게임이 끝난 후 1(새 게임) 또는 2(종료) 입력 | |
| ## 출력 기능 | |
| - 게임 시작 문구: "숫자 야구 게임을 시작합니다." 출력 | |
| - 입력한 수에 대한 결과 출력: | |
| - 볼과 스트라이크 개수 출력 | |
| - 아무것도 없을 시 "낫싱" 출력 | |
| - 모두 일치 시 "3개의 숫자를 모두 맞히셨습니다! 게임 종료" 출력 | |
| - "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요." 출력 | |
| ## 예외 처리 및 종료 기능 | |
| - 다음과 같은 경우 ValueError 발생 후 프로그램 종료: | |
| - 3자리가 아닌 숫자 입력 | |
| - 1-9 범위를 벗어난 숫자 입력 | |
| - 중복된 숫자 입력 | |
| - 숫자가 아닌 값 입력 | |
| - 게임 종료 시 1,2 이외의 값 입력 |
… 3 and the game ends when all 3 numbers are guessed correctly
Codecov ReportAttention: Patch coverage is
|
Summary by CodeRabbit