Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
기능 구현 목록
==========

**1.숫자를 랜덤으로 생성한다**
- 단 숫자는 1부터 9까지로 하고 3자리 자연수를 생성,각 자리 숫자는 서로 중복되어선 안된다

**2.사용자에게 숫자를 입력받는다**
- 3자리 자연수가 아니면 "잘못 입력했습니다." 출력
- 각 자리 숫자가 서로 중복될 경우 "잘못 입력했습니다." 출력
- 1부터 9까지 숫자가 아니면 "잘못 입력했습니다." 출력
- 0이하 수가 나올 경우 "잘못 입력했습니다." 출력

**3.입력받은 수와 생성된 수를 비교한다**
- 같은 숫자가 같은 자리에 있을 때:스트라이크
- 같은 숫자에 다른 자리에 있을 때:볼
- 다른 숫자일 때:낫싱

**4.비교 결과를 출력한다**
- 3스트라이크일때 게임을 종료한다
64 changes: 56 additions & 8 deletions src/baseball/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,58 @@
def main():
"""
프로그램의 진입점 함수.
여기에서 전체 프로그램 로직을 시작합니다.
"""
# 프로그램의 메인 로직을 여기에 구현
import random

def player_input() :

player_number = input("숫자를 입력해주세요: ")
if len(player_number) != 3 or not player_number.isdigit():
raise ValueError("입력은 3자리 숫자여야 합니다.")

elif len(set(player_number)) != 3:
raise ValueError("입력 값의 각 자리는 서로 달라야 합니다.")
return player_number


def game_comp(com_number, player_number) :
strike, ball, index = 0, 0, 0

for i in com_number :
if int(player_number[index]) == i :
strike += 1
elif int(player_number[index]) in com_number :
ball += 1
index += 1
return strike, ball

def game_print(com_number) :
while True:
number = player_input()
strike, ball = game_comp(com_number, number)
if strike==0 and ball==0:
print("낫싱")
continue
elif strike == 3 :
print(str(strike)+"스트라이크")
print("게임 종료")
break
else:
print(str(ball)+"볼", str(strike)+"스트라이크")
continue

def main():
while True :
com_number = random.sample(range(1,10),3)
print("숫자 야구 게임을 시작합니다.")
game_print(com_number)


result = input("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.")
if result == '1':
main()
return
elif result == '2':
print("게임을 종료합니다.")
return
else:
raise ValueError("잘못된 입력입니다. 1 또는 2를 입력하세요.")
Comment on lines +40 to +55
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Remove recursive call to prevent stack overflow

The current implementation uses recursion for game restart, which could lead to stack overflow after multiple restarts. Replace with a loop-based approach:

 def main(): 
     while True:
         com_number = random.sample(range(1,10),3)
         print("숫자 야구 게임을 시작합니다.")
         game_print(com_number)
         
         result = input("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.")
         if result == '1':
-            main()
-            return
+            continue
         elif result == '2':
             print("게임을 종료합니다.")
             return
         else:
             raise ValueError("잘못된 입력입니다. 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.

Suggested change
def main():
while True :
com_number = random.sample(range(1,10),3)
print("숫자 야구 게임을 시작합니다.")
game_print(com_number)
result = input("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.")
if result == '1':
main()
return
elif result == '2':
print("게임을 종료합니다.")
return
else:
raise ValueError("잘못된 입력입니다. 1 또는 2를 입력하세요.")
def main():
while True:
com_number = random.sample(range(1,10),3)
print("숫자 야구 게임을 시작합니다.")
game_print(com_number)
result = input("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.")
if result == '1':
continue
elif result == '2':
print("게임을 종료합니다.")
return
else:
raise ValueError("잘못된 입력입니다. 1 또는 2를 입력하세요.")


if __name__ == "__main__":
# 프로그램이 직접 실행될 때만 main() 함수를 호출
main()
main()
Comment on lines 57 to +58
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add global error handling

Add try-except block in the main execution to handle unexpected errors gracefully:

 if __name__ == "__main__":
-    main()
+    try:
+        main()
+    except KeyboardInterrupt:
+        print("\n게임이 중단되었습니다.")
+    except Exception as e:
+        print(f"오류가 발생했습니다: {str(e)}")
📝 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.

Suggested change
if __name__ == "__main__":
# 프로그램이 직접 실행될 때만 main() 함수를 호출
main()
main()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n게임이 중단되었습니다.")
except Exception as e:
print(f"오류가 발생했습니다: {str(e)}")