๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป IT·๊ธฐ์ˆ ·ํ†ต๊ณ„

Python์œผ๋กœ ์ฃผ์‚ฌ์œ„ ๊ฒŒ์ž„ ๋งŒ๋“ค๊ธฐ

by nowgeun 2023. 2. 3.
728x90

์ œ๊ฐ€ ์†Œ์†ํ•œ ํŒ€์€ ์ „๋ถ€ ๋‚จ์ž๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ฌ์ง€์–ด ๋Œ€๋ฆฌ ๊ธ‰์ด ์ €๋ฅผ ํฌํ•จํ•ด์„œ ๋„ค ๋ช…์ด๋ผ ๋‚ด๊ธฐ๋ฅผ ์ž์ฃผ ํ•˜๊ณค ํ•ฉ๋‹ˆ๋‹ค.

์ปคํ”ผ ๋‚ด๊ธฐ, ์ ์‹ฌ ๋‹น์ง ๋ชฐ๋นต, ํšŒ์˜๋ก ์ž‘์„ฑ ๋‚ด๊ธฐ ๋“ฑ ํ•˜๋ฃจ๋ฅผ ๋‚ด๊ธฐ๋กœ ์‹œ์ž‘ํ•ด์„œ ๋‚ด๊ธฐ๋กœ ๋์„ ๋ƒ…๋‹ˆ๋‹ค. ๋‚ด๊ธฐ๋ฅผ ํ•  ๋•Œ๋งˆ๋‹ค ์นดํ†ก์œผ๋กœ ์‚ฌ๋‹ค๋ฆฌ๋ฅผ ํƒ€๊ณค ํ•˜๋Š”๋ฐ, ์žฌ๋ฐŒ๊ฒŒ๋„ ํ•œ ์‚ฌ๋žŒ์ด ์—ฐ์†์œผ๋กœ ๊ฑธ๋ฆฌ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•˜์Šต๋‹ˆ๋‹ค. ์‚ฌ๋‹ค๋ฆฌ ๊ฒŒ์ž„ ๊ฐœ๋ฐœ์ž๊ฐ€ ์˜๋„ํ•œ ๊ฒƒ์ธ์ง€๋Š” ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ, ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๊นŒ๋ณด์ง€๋Š” ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ณด๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

๋‹ค๋ฅธ ํฌ์ŠคํŠธ์—์„œ ๋ณด์…จ์„ ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ํ”ผ์นด์ถ” ๋ฐฐ๊ตฌ๋ฅผ ์ œ๊ฐ€ ์ฒ˜์Œ ์ œ์•ˆํ–ˆ์œผ๋‚˜ ์ฆ๊ถŒ ๋ฐ ๊ธˆ์œต์‚ฌ๋Š” ๋ง๋ถ„๋ฆฌ๊ฐ€ ๋˜์–ด์žˆ์–ด์„œ ์™ธ๋ถ€ํ”„๋กœ๊ทธ๋žจ ๋ฐ˜์ž…์ด ๋งค์šฐ ๊นŒ๋‹ค๋กญ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์–ด์ฉ” ์ˆ˜ ์—†์ด ๋งค์ผ๋งค์ผ ์‚ฌ์šฉํ•˜๋Š” Python์„ ํ™œ์šฉํ•ด์„œ ์ฃผ์‚ฌ์œ„ ๊ตด๋ฆฌ๊ธฐ ๊ฒŒ์ž„์ด๋ผ๋„ ๋งŒ๋“ค์–ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

TL;DR: ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ ค์„œ ๋‚ด๊ธฐ๋ฅผ ํ•˜์ž

 

์ฃผ์‚ฌ์œ„๊ฒŒ์ž„
์ฃผ์‚ฌ์œ„๋กœ ํ•˜๋Š” ๊ฒŒ์ž„~

 

์†Œ์Šค์ฝ”๋“œ

์•„๋ž˜ ์ฃผ์‚ฌ์œ„ ๊ฒŒ์ž„์€ 1~2๊ฐœ์˜ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ ค์„œ ๊ฐ€์žฅ ์ ์€ ์ˆ˜๊ฐ€ ๋‚˜์˜จ ์‚ฌ๋žŒ์ด ๋‹น์ฒจ๋˜๋Š” ๊ฒŒ์ž„์ž…๋‹ˆ๋‹ค.

๋™์  ์‹œ, ๋‘ ๋ช… ์ด์ƒ์˜ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ๋‹น์ฒจ๋ฉ๋‹ˆ๋‹ค~

 

main.py

import random
import time
import os

def dice_num():
    while True:
        try:
            dice_cnt = input('์ฃผ์‚ฌ์œ„ ๊ฐฏ์ˆ˜: ')
            options = ['1','2']
            if dice_cnt not in options:
                raise ValueError('1 , 2 ๋งŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค')
            else:
                return dice_cnt
        except ValueError as err:
            print(err)

def participants():
    while True:
        try:
            players = input('์ฐธ๊ฐ€์ž๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”. ex) ํ™๊ธธ๋™ ๊น€์ƒ์ˆœ\n ')
            players = [name.strip() for name in players.split()]
            return players
        except:
            raise ValueError('๋„์–ด์“ฐ๊ธฐ ๊ตฌ๋ถ„์ž๋กœ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”')
            
def play_again():
    while True:
        try:
            play = input('๋‹ค์‹œ ๋Œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๊นŒ?  ex) Yes, No ์œผ๋กœ ๋Œ€๋‹ตํ•ด์ฃผ์„ธ์š”')
            if (play.upper() == 'Y') or (play.upper() == 'YES'):
                return True
            else:
                return False
        except:
            raise ValueError('Yes ํ˜น์€ No๋กœ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”')
    
            
def roll_dice(min_n=1, max_n=6):
    min_n = 1
    max_n = 6
    LET_ME_DO_IT_AGAIN = True
    
    while LET_ME_DO_IT_AGAIN:
        os.system('cls' if os.name == 'nt' else 'clear')
        amount = dice_num()          # ์ฃผ์‚ฌ์œ„ ๊ฐœ์ˆ˜ ์„ค์ •
        players = participants() # ์ฐธ๊ฐ€์ž ์„ค์ •
        
        result = {k:[] for k in players}
        print(result)
        print('\n'*2) #๊ตฌ๋ถ„์ž 
        print('='*40) #๊ตฌ๋ถ„์ž
        print(f'์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆฝ๋‹ˆ๋‹ค...')
        print('='*40) #๊ตฌ๋ถ„์ž
        print('\n'*2) #๊ตฌ๋ถ„์ž      
        
        for i in range(0,int(amount)):
            
            for person in players:
                
                time.sleep(random.randint(1,4))  # ๊ธด์žฅ๊ฐ์„ ์œ„ํ•ด LAG ์ถ”๊ฐ€
                dice = random.randint(min_n, max_n)
                print(f'{person}   ์ฃผ์‚ฌ์œ„ {i}: ', dice)
                result[person].append(dice)
                print('='*40) #๊ตฌ๋ถ„์ž 
        
        lucky = 99
        print(result)
        print('\n'*2)
        for person in players:
            time.sleep(2)
            print(f'{person}๋‹˜์˜ ํ•ฉ๊ณ„๋Š”: ', sum(result[person]))
            print('='*40) #๊ตฌ๋ถ„์ž    
            if sum(result[person]) < lucky:
                lucky = sum(result[person])
                lucky_person = person
            elif sum(result[person]) == lucky:
                lucky_person = lucky_person + ", " + person
        print(f"๋‹น์ฒจ์ž๋Š”: {lucky_person} ์ž…๋‹ˆ๋‹ค.")  
                  
        LET_ME_DO_IT_AGAIN = play_again()

                  
if __name__ == '__main__':
    roll_dice()

 

ํ„ฐ๋ฏธ๋„์— main.py๋กœ ์‹คํ–‰ํ•˜์‹œ๋ฉด ์ฃผ์‚ฌ์œ„๊ฒŒ์ž„์„ ์ฆ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์•„๋ž˜๋Š” ์‹คํ–‰ ์˜์ƒ์ž…๋‹ˆ๋‹ค:

 

์‹คํ–‰์˜์ƒ

๊ทธ๋Ÿผ ๋ชจ๋‘ ์žฌ๋ฐŒ๋Š” ๋‚ด๊ธฐ๋กœ ํ•˜๋ฃจ๋ฅผ ๋ณด๋‚ด์„ธ์š” ใ…Žใ…Ž

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€