これはシンプルなサイコロを転がすゲームアプリケーションです。 ユーザーは、振るサイコロの数と各サイコロの面の数を指定できます。次に、アプリケーションはロールをシミュレートし、結果を表示します。
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 を入力すると、アプリケーションを終了できます。 これは基本的な例であり、グラフィカル ユーザー インターフェイスやより複雑なゲーム メカニクスなどの機能を含めるように拡張できます。
タグ : パズル