我最近和一些朋友一起玩了 Werewolf 游戲,為了減輕 Game Master 的作業,我想創建一個簡單的 Python 程式,允許快速隨機化角色。
我不是專業人士,也不是具有高 Python 技能的人,我只是了解演算法的作業原理,所以我缺乏塑造我的想法的詞匯,但我設法創建了一個有效的代碼:
import random
Roles = ['a werewolf', 'the werewolf seer', 'the fool', 'the seer', 'the witch', 'the hunter', 'the priest', 'cupidon', 'the little girl', 'the bodyguard']
List = []
Counter = 0
Players = int(input("How many player are there ? "))
while Counter < Players:
print("Player", Counter 1, end=", ")
Name = (input("What is your name ? "))
List.append(Name)
Counter = Counter 1
for i in range(Players):
print(random.choice(List), ", You are", random.choice(Roles))
問題是,角色和名稱是隨機的,所以我不能告訴我的程式只排除列出的值,因此一些名稱和一些角色是重復的。
我有3個問題:
- 如何從串列中參考輸入值?
- 如何從串列中參考隨機值?
- 我如何告訴我的程式從串列中洗掉剛剛列出的值(隨機或輸入)以避免重復?
uj5u.com熱心網友回復:
我認為這對你有用:)
import random
Roles = ['a werewolf', 'the werewolf seer', 'the fool', 'the seer', 'the witch', 'the hunter', 'the priest', 'cupidon', 'the little girl', 'the bodyguard']
List = []
Counter = 0
Players = int(input("How many player are there ? "))
while Counter < Players:
print("Player", Counter 1, end=", ")
Name = (input("What is your name ? "))
List.append(Name)
Counter = Counter 1
for i in range(Players):
rand_names = random.choice(List)
rand_roles = random.choice(Roles)
List.remove(rand_names)
Roles.remove(rand_roles)
print(rand_names, ", You are", rand_roles)
uj5u.com熱心網友回復:
我不是 100% 確定這就是你想要的,我假設你想要每個玩家都有不同的角色。此外,如果是這種情況,應該檢查玩家數量不超過角色數量。
import random
Roles = ['a werewolf', 'the werewolf seer', 'the fool', 'the seer', 'the witch', 'the hunter', 'the priest', 'cupidon', 'the little girl', 'the bodyguard']
PlayerNames = []
Counter = 0
Players = int(input("How many player are there ? "))
while Counter < Players:
print("Player", Counter 1, end=", ")
Name = (input("What is your name ? "))
PlayerNames.append(Name)
Counter = Counter 1
for player in PlayerNames:
role = random.choice(Roles)
Roles.remove(role)
print(player, ", You are", random.choice(Roles))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/347436.html
