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
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

72 changes: 65 additions & 7 deletions src/baseball/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,68 @@
import random
RESTART_GAME = "1"
END_GAME = "2"

def main():
"""
프로그램의 진입점 함수.
여기에서 전체 프로그램 로직을 시작합니다.
"""
# 프로그램의 메인 로직을 여기에 구현
while True :
computer = generateNum()

print("숫자 야구 게임을 시작합니다.")
while(True) :
print("숫자를 입력해주세요 : ", end='')
user = input()
checkInput(user)

if (calculate(user, computer)) :
break

print("3개의 숫자를 모두 맞히셨습니다! 게임 종료")
print("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.")
select = input()
if select == RESTART_GAME :
continue
elif select == END_GAME :
break
else :
raise ValueError

Check warning on line 26 in src/baseball/main.py

View check run for this annotation

Codecov / codecov/patch

src/baseball/main.py#L26

Added line #L26 was not covered by tests

def generateNum() :
num = random.sample(range(1,10), 3)
computer = "".join(map(str, num))
return computer

def checkInput(user:str) -> bool: # 매개변수와 반환값 타입 명시
if not user.isdigit() :
raise ValueError("숫자만 입력 가능합니다.")

Check warning on line 35 in src/baseball/main.py

View check run for this annotation

Codecov / codecov/patch

src/baseball/main.py#L35

Added line #L35 was not covered by tests

num = int(user)
if num < 100 or num > 999 :
raise ValueError("3자리 숫자만 입력 가능합니다.")

check = list(user)
if len(set(check)) != 3 :
raise ValueError("서로 다른 숫자만 입력 가능합니다.")

Check warning on line 43 in src/baseball/main.py

View check run for this annotation

Codecov / codecov/patch

src/baseball/main.py#L43

Added line #L43 was not covered by tests

return True

def calculate(user, computer) : # ball, strike 계산
strike = sum(1 for i in range(3) if user[i] == computer[i])
ball = sum(1 for b in user if b in computer) - strike # 합이므로 변수를 0으로 미리 초기화하지 않아도 괜찮음

return format(strike, ball)

def format(strike, ball) : # 출력 포맷
if strike == 3 :
print("3스트라이크")
return True

output = []
if ball > 0 :
output.append(f"{ball}볼")
if strike > 0 :
output.append(f"{strike}스트라이크") #자동으로 문자열로 만들어주는 기능

print(" ".join(output) if output else "낫싱")
return False

if __name__ == "__main__":
# 프로그램이 직접 실행될 때만 main() 함수를 호출
main()
main()

Check warning on line 68 in src/baseball/main.py

View check run for this annotation

Codecov / codecov/patch

src/baseball/main.py#L68

Added line #L68 was not covered by tests
Loading