간단한 주사위 굴리기 게임 애플리케이션입니다. 사용자는 굴릴 주사위 수와 각 주사위의 면 수를 지정할 수 있습니다. 그러면 애플리케이션이 롤을 시뮬레이션하고 결과를 표시합니다.
Python에서 가능한 구현은 다음과 같습니다.
import random
def roll_dice(num_dice, num_sides):
"""Simulates rolling multiple dice.
Args:
num_dice: The number of dice to roll.
num_sides: The number of sides on each die.
Returns:
A list of integers representing the results of each die roll. Returns an empty list if input is invalid.
"""
if num_dice <= 0 or num_sides <= 0:
return []
results = []
for _ in range(num_dice):
results.append(random.randint(1, num_sides))
return results
def main():
while True:
try:
num_dice = int(input("Enter the number of dice to roll (or 0 to quit): "))
if num_dice == 0:
break
num_sides = int(input("Enter the number of sides on each die: "))
results = roll_dice(num_dice, num_sides)
if not results:
print("Invalid input. Please enter positive integers.")
continue
print("Results:", results)
print("Total:", sum(results))
except ValueError:
print("Invalid input. Please enter integers only.")
if __name__ == "__main__":
main()
이 애플리케이션은 사용자에게 주사위 수와 면 수를 묻는 메시지를 표시합니다. 그런 다음 random.randint() 기능을 사용하여 롤을 시뮬레이션하고 개별 결과와 롤의 합계를 표시합니다. 잘못된 입력을 관리하기 위해 오류 처리가 포함됩니다. 사용자는 주사위 개수를 0으로 입력하여 애플리케이션을 종료할 수 있습니다. 이는 기본적인 예이며 그래픽 사용자 인터페이스나 보다 복잡한 게임 메커니즘과 같은 기능을 포함하도록 확장될 수 있습니다.
태그 : 퍼즐