야구 게임(baseball game) - 파이썬(python)

2022. 12. 24. 15:59기록지/기타

간단한 야구 게임 (숫자 맞추기)

 

세자리수 and 네자리수 맞추기 게임

 

1. 3자리

 

# 야구게임
# 3자리 숫자 맞추기 게임
# x입력: 카운트 리스트 출력
# ==================================================================
import random
import time

print("***Welcome to baseball game!***")
time.sleep(1)

print("you have just 15 chances. ")
time.sleep(0.8)
print("good luck!")
time.sleep(0.8)

i=dict()
count=1
numlist=[0,1,2,3,4,5,6,7,8,9]
anslist = random.sample(numlist, 3)
ans=str(anslist[0])+str(anslist[1])+str(anslist[2])
# print(ans)


while 1:
    time.sleep(1)
    print("input three number (no overlap) or input \'x\' show count list")
    print("quit:\"q\"")

    n = input()
    if n=='q':
        break
    if n=='x':
        print(i)
        continue
   
    if len(n)!=3:
        print("input again.(000~999)")
        continue
    elif n[0]==n[1] or n[0]==n[2] or n[1]==n[2]:
        print("Each number must not overlap.input again.")
        continue
    elif "0"<=n[0]<="9" and "0"<=n[1]<="9" and "0"<=n[2]<="9" :
        if n==ans:
            if count>=0 and count<6:
                print("Success!! You win!! you just used ",count, " times. ")
            elif count>=6 and count<11:
                print("You win. you used ",count," times.")
            else:
                print("You win. you can do better next time.")
        else:
            if count>=16:
                print("lose.. you have no chance left.")
                print("bye.")
            else:
                st=0
                ball=0
                if n[0]==ans[0]:
                    st+=1
                if n[1]==ans[1]:
                    st+=1
                if n[2]==ans[2]:
                    st+=1
                if n[0]==ans[1] or n[0]==ans[2]:
                    ball+=1
                if n[1]==ans[0] or n[1]==ans[2]:
                    ball+=1
                if n[2]==ans[0] or n[2]==ans[1]:
                    ball+=1
                print(st,"S",ball,"B")
                i[n]=str(st)+"S"+str(ball)+"B"
                count+=1
                continue
           
    else:
        print("input 000~999 number only")
        time.sleep(0.5)
        continue
   
    break
 
 

=============================================

 

2. 네자리

import random
import time
import sys

print("***Welcome to baseball game!***")
time.sleep(1)

print("you have just 15 chances. ")
time.sleep(0.8)
print("good luck!")
time.sleep(0.8)

inf=dict()
count=1
numlist=[0,1,2,3,4,5,6,7,8,9]
anslist = random.sample(numlist, 4)
ans=str(anslist[0])+str(anslist[1])+str(anslist[2])+str(anslist[3])
#print(ans)


while 1:
    time.sleep(1)
    print("input four number (no overlap) or input \'x\' show count list")
    print("quit:\"q\"")

    n = sys.stdin.readline().rstrip()
    t=[]
    if len(n)==4:
        for i in n:
            t.append(i)
    t=set(t)

    if n=='q':
        break
    if n=='x':
        print(inf)
        continue
   
    if len(n)!=4:
        print("input again.(0000~9999)")
        continue
    elif len(t)!=4:
        print("Each number must not overlap.input again.")
        continue
    elif "0"<=n[0]<="9" and "0"<=n[1]<="9" and "0"<=n[2]<="9" and "0"<=n[3]<="9":
        if n==ans:
            if count>=0 and count<6:
                print("Success!! You win!! you just used ",count, " times. ")
            elif count>=6 and count<11:
                print("You win. you used ",count," times.")
            else:
                print("You win. you can do better next time.")
        else:
            if count>=16:
                print("lose.. you have no chance left.")
                print("bye.")
            else:
                st=0
                ball=0
                if n[0]==ans[0]:
                    st+=1
                if n[1]==ans[1]:
                    st+=1
                if n[2]==ans[2]:
                    st+=1
                if n[3]==ans[3]:
                    st+=1
                if n[0]==ans[1] or n[0]==ans[2] or n[0]==ans[3]:
                    ball+=1
                if n[1]==ans[0] or n[1]==ans[2] or n[1]==ans[3]:
                    ball+=1
                if n[2]==ans[0] or n[2]==ans[1] or n[2]==ans[3]:
                    ball+=1
                if n[3]==ans[0] or n[3]==ans[1] or n[3]==ans[2]:
                    ball+=1
                print(st,"S",ball,"B")
                inf[n]=str(st)+"S"+str(ball)+"B"
                count+=1
                continue
           
    else:
        print("input 0000~9999 number only")
        time.sleep(0.5)
        continue
   
    break

 

 
728x90

'기록지 > 기타' 카테고리의 다른 글

개미수열 - 파이썬(python)  (0) 2023.02.01
3,6,9 게임  (0) 2023.02.01
MySQL WITH RECURSIVE 정리  (0) 2023.01.02
MySQL JOIN 정리  (0) 2022.12.27
소수(prime number) 판별 코드 - 파이썬(python)  (0) 2022.12.24