๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋ชฉ์ฐจ

    728x90

    ์Šคํ„ฐ๋””์›์ด ์ˆซ์ž์•ผ๊ตฌ ๋งŒ๋“ค์–ด๋ณด๊ณ  ์‹ถ๋‹คํ•˜์—ฌ ํ•œ ๋ฒˆ ํ•ด๋ดค์Šต๋‹ˆ๋‹ค!

    from random import *
    import time
    
    def insufficient(numlist): # ๋ถ€์กฑํ•  ๊ฒฝ์šฐ
        insufficient_cnt = 4 - len(numlist) # ๋ถ€์กฑํ•œ ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์‚ฐ
        print(f"์ค‘๋ณต๋œ ์ˆ˜ ํ˜น์€ ์ ์€ ์–‘์˜ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ถ”๊ฐ€๋กœ {insufficient_cnt}๊ฐœ์˜ ๋‹ค๋ฅธ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
        print("ํ˜„์žฌ ์ž…๋ ฅ๋œ ์ˆ˜", end=' : ')
        numlist.sort()
        for i in range(len(numlist)):
            print(numlist[i], end=' ')
        print('')
    
        while True:
            num = input(": ") # ์ž…๋ ฅ!
    
            if len(num) != insufficient_cnt: # ๋ถ€์กฑํ•œ ๋งŒํผ๋ณด๋‹ค ๋œ ์ž…๋ ฅํ–ˆ๊ฑฐ๋‚˜ ๋” ์ž…๋ ฅํ•œ ๊ฒฝ์šฐ(์ค‘๋ณตํฌํ•จ)
                print(f"๋‹ค์‹œ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”. ({insufficient_cnt}๊ฐœ์˜ ์ˆซ์ž ํ•„์š”)")
                print("ํ˜„์žฌ ์ž…๋ ฅ๋œ ์ˆ˜", end=' : ')
                for i in range(len(numlist)):
                    print(numlist[i], end=' ')
                print('')
                continue      
            else: # ๋ถ€์กฑํ•œ ๋งŒํผ ์ž˜ ์ž…๋ ฅํ–ˆ๋‹ค๋ฉด?(์ค‘๋ณต ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ  ๊ฐœ์ˆ˜๋งŒ)
                list_in_inspecting = numlist[:] # ์กฐ์‚ฌ๋ฆฌ์ŠคํŠธ์— ํ˜„์žฌ ๋ฆฌ์ŠคํŠธ ๋„ฃ๊ธฐ
                for i in range(insufficient_cnt): # ์ž…๋ ฅ์ˆซ์ž ๋„ฃ์–ด์ฃผ๊ธฐ
                    list_in_inspecting.append(num[i])
                list_in_inspecting = list(set(list_in_inspecting)) # ์ค‘๋ณต์ œ๊ฑฐ
            
    
            if len(list_in_inspecting) == 4: # ๋ถ€์กฑํ•œ ์ˆ˜ ์ž…๋ ฅ ์‹œ ์ค‘๋ณต์ด ์—†์—ˆ๋‹ค๋ฉด?
                numlist = list_in_inspecting[:]
                break
            else :
                print(f"๋‹ค์‹œ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”. (์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ์ˆซ์ž {insufficient_cnt}๊ฐœ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”)")
                continue
        return numlist
    
    def exceed(numlist): # ๋„˜์น  ๊ฒฝ์šฐ
        exceed_cnt = len(numlist) - 4 # ๋„˜์นœ ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์‚ฐ
        print(f"์ˆ˜๋ฅผ ์ดˆ๊ณผํ•˜์—ฌ ์ž…๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ œ์™ธํ•  ์ˆ˜ {exceed_cnt}๊ฐœ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
        while True:
            print("ํ˜„์žฌ ์ž…๋ ฅ๋œ ์ˆ˜", end=' : ')
            numlist.sort()
            for i in range(len(numlist)):
                print(numlist[i], end=' ')
            print("")
    
            num = input(": ")
            if len(num) != exceed_cnt: # ๋„˜์นœ ๋งŒํผ ๋ณด๋‹ค ๋œ ํ˜น์€ ๋” ์ž…๋ ฅํ–ˆ์„ ๊ฒฝ์šฐ
                print(f"๋‹ค์‹œ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”. ({exceed_cnt}๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.)")
                continue # ๋‹ค์Œ ๋ฐ˜๋ณต์œผ๋กœ ๋„˜๊ธฐ๊ธฐ
            
            # ๋„˜์นœ ๋งŒํผ ์ž˜ ์ž…๋ ฅํ–ˆ๋‹ค๋ฉด?
            for i in range(exceed_cnt):
                if num[i] in numlist:
                    numlist.remove(num[i])
            if len(numlist) == 4: # 4์ž๋ฆฌ์˜ ์ˆ˜๊ฐ€ ์™„์„ฑ์ด ๋˜์—ˆ๋‹ค๋ฉด
                result = numlist[:]
                break # ๋ฐ˜๋ณต๋ฌธ ๋
            else :
                print("๋‹ค์‹œ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”. (์ œ์™ธํ•  ์ˆ˜๋ฅผ ์ •ํ™•ํžˆ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”)") # ์ด๋ฏธ ์žˆ๋Š” ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ
                continue
        return result
            
    
    def inspect(numlist):
        numlist = list(set(numlist))
        if len(numlist) < 4:
            result = insufficient(numlist)
            result.sort()
            print("์ž…๋ ฅ๋œ ์ˆ˜", end=' : ')
            for i in range(4):
                print(result[i], end=' ')
            print('')
            return result
        elif len(numlist) > 4:
            result = exceed(numlist)
            result.sort()
            print("์ž…๋ ฅ๋œ ์ˆ˜", end=' : ')
            for i in range(4):
                print(result[i], end=' ')
            print('')
            return result
        else:
            result = numlist
            return result
    
    def insert():
        num = input("4์ž๋ฆฌ ์ˆซ์ž๋ฅผ ์ค‘๋ณต์—†์ด ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”. [ex) 1234] : ")
        num = str(int(num))
        numlist = []
        for i in range(len(num)):
            numlist.append(num[i])
        numlist = list(set(numlist))
        numlist.sort()
        return inspect(numlist)
    
    def answer():
        base = []
        for i in range(4):
            num = str(randint(1,9)) # 1
            while num in base:
                num = str(randint(1,9))
            base.append(num)
        base.sort()
        return base
    
    def strike_ball(userchoice, base):
        strike = 0; ball = 0
        for i in range(4):
            if userchoice[i] == base[i]:
                strike += 1
            elif userchoice[i] in base:
                ball += 1
        for i in range(3):
            print('.', end=' ')
            time.sleep(0.7)
        print(f'\n{strike}์ŠคํŠธ๋ผ์ดํฌ {ball}๋ณผ')
        time.sleep(0.7)
        
    
    #ใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…ก
    # main
    start = 1
    info = """
    |๊ฒŒ์ž„ ๊ทœ์น™|----------------------------------------------------
    1.์ด ๋„์ „ ํšŸ์ˆ˜๋Š” 10ํšŒ ์ž…๋‹ˆ๋‹ค                               
    2.์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜๋ฉด '์ŠคํŠธ๋ผ์ดํฌ ์นด์šดํŠธ'์™€ '๋ณผ ์นด์šดํŠธ'๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค.
    (์ˆซ์ž๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ์ ์–ด์ฃผ์„ธ์š”!)
    ---------------------------------------------------------------
    """
    while start == 1:
        print("---------------------------------------------------------------")
        print("<ํ˜ผ์ž ์ฆ๊ธฐ๋Š” ์ˆซ์ž ์•ผ๊ตฌ ๊ฒŒ์ž„>")
    
        print(info, 'ํ…Œ์ŠคํŠธ์šฉ ์ž…๋‹ˆ๋‹ค. ์ง€์šฐ๋ฉด๋จ!') # ์ •๋‹ต๋ถ€๋ถ„(ํ…Œ์ŠคํŠธ์šฉ)
    
        for i in range(3):
            print('.', end=' ')
            time.sleep(0.7)
        print("\n๊ฒŒ์ž„์„ ์‹œ์ž‘ ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? Y/N ")
        while True:
            gamestart = input(": ").upper()
            if gamestart == "Y" or 'N':
                break
            else:
                print("๋‹ค์‹œ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
                continue
        if gamestart == 'Y':
            base = answer()
            print(base)
            print("์‹œ์ž‘!")
            cnt = 0
            while cnt < 10:
                cnt += 1
                print("---------------------------------------------------------------")
                print(f"{cnt}๋ฒˆ์งธ ์‹œ๋„")
                userchoice = insert()
                userchoice.sort()
                if userchoice == base:
                    print(f"์„ฑ๊ณต!")
                    break
                else:
                    strike_ball(userchoice,base)
                    continue
            if cnt == 10:
                time.sleep(0.7)
                print("---------------------------------------------------------------")
                print("์ €๋Ÿฐ... 10๋ฒˆ์˜ ๊ธฐํšŒ ์•ˆ์— ์ˆซ์ž๋ฅผ ๋งž์ถ”์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.")
                time.sleep(0.7)
                print("์ •๋‹ต์€ ๋ฐ”๋กœ!")
                for i in range(4):
                    print(base[i], end=' ')
                    time.sleep(0.7)
                print("์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค.")
                print("GAME OVER")
                time.sleep(2)
                    
                
        elif gamestart == 'N':
            print("END")
            start = 0
        else:
            print("๋‹ค์‹œ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")

    ์Šคํ„ฐ๋””์—์„œ ์•„์ง ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋‚˜ ํด๋ž˜์Šค ๋ถ€๋ถ„์„ ๋‹ค๋ฃจ์ง€ ์•Š์•„์„œ ์ผ๋‹จ ํ•จ์ˆ˜๋กœ๋งŒ ๊ตฌํ˜„์„ ํ•ด๋ณด์•˜๋Š”๋ฐ

    ๋‚˜์ค‘์— ์ด๊ฑธ ๋‹ค์‹œ ๋ณธ๋‹ค๋ฉด ์•„๋งˆ ์ง€์ €๋ถ„ํ•œ ๋ถ€๋ถ„์ด ๋งŽ์ง€ ์•Š์„๊นŒ ๋ผ๋Š” ๋Š๋‚Œ??

    ๋Œ“๊ธ€