2014-09-24 65 views
0

黑手黨遊戲助手非常簡單。它允許我首先輸入角色的數量(黑手黨,偵探,無辜等)。然後每個玩家將輸入他們的名字,電腦會隨機爲他/她選擇一個角色。第一個玩家將電腦傳遞給下一個玩家等等。最後,計算機將生成一個列表。黑手黨(派對遊戲)助手

喜歡的東西:

  • 湯姆:黑手黨

  • 約翰:偵探

這裏是我的代碼:

import random 
role=[] 
k = input("Number of mafia: ") 
p = input("Number of detective: ") 
n = input("Number of innocent: ") 

---magic--- 

random.shuffle(role) 

player = [] 
i=0 
while True: 
    name = raw_input() 
    player.append(name) 
    print role[i] 
    i += 1 

我在編程中的兩個問題日是。

  1. 如何用k'mafia'和p'detective'製作一個名爲'role'的列表?例如:如果k = 3,p = 1,n = 1,那麼列表中的角色將是['黑手黨','黑手黨','黑手黨','偵探','無辜']。我知道我可以用for循環來做,但是有沒有簡單的方法可以做到這一點?

  2. 現在有一個非常嚴重的錯誤,第二個玩家可以看到角色分配給第一個玩家。我怎樣才能解決這個問題,讓任何球員都看不到?但我必須保持結果,因爲我必須在最後列出一個清單,就像我剛纔提到的那樣。

我的朋友們很喜歡玩這個遊戲,所以我想通過這個程序給他們帶來驚喜! 謝謝大家閱讀。有一個好的一天=]

回答

1

關於你的列表產生問題

>>> k = 3 
>>> p = 1 
>>> n = 2 
>>> roles = ['mafia']*k + ['detective']*p + ['innocent']*n 
>>> roles 
['mafia', 'mafia', 'mafia', 'detective', 'innocent', 'innocent'] 

一種方法來隨機分配角色

from random import shuffle 
shuffle(roles) 
names = ['bob', 'steve', 'tom', 'jon', 'alex', 'mike'] 
players = dict(zip(names,roles)) 

>>> players 
{'mike': 'innocent', 
'alex': 'mafia', 
'steve': 'mafia', 
'tom': 'mafia', 
'bob': 'detective', 
'jon': 'innocent'} 
0

你可以分配每個人後清除輸出?這個現在應該清楚

import random 
import os 
k = input("Number of mafia: ") 
p = input("Number of detective: ") 
n = input("Number of innocent: ") 

roles = ['mafia']*k + p*['detective'] + n * ['innocent'] 

random.shuffle(roles) 

while roles: 
    print 'name:', 
    name = raw_input() 
    print 'you are a ' + roles.pop(0) 
    print 'clear?' 
    clear = raw_input() 
    os.system('cls' if os.name == 'nt' else 'clear') 

或者你也可以寫出來,以單獨文件的作用可能工作黑手黨的更好

import random 
import os 
k = input("Number of mafia: ") 
p = input("Number of detective: ") 
n = input("Number of innocent: ") 

roles = ['mafia']*k + p*['detective'] + n * ['innocent'] 

random.shuffle(roles) 

people = [] 

path = '' 

while roles: 
    print 'name:', 
    name = raw_input() 
    while name in people: 
     print 'name already_taken please take new name' 
     name = raw_input() 
    people.append(name) 
    person_file = open(os.path.join(path,'%s.txt') % (name,),'w') 
    person_file.write('you are a %s' % (roles.pop(0),)) 
    person_file.close() 
+0

數量:3 偵探數:1支 無辜數:2 姓名:湯姆 你是黑手黨 清除? tom [2J name:z 你是個偵探 明白嗎? v [2J name:q 你是黑手黨 明確嗎? b [2J name: – 2014-09-24 13:34:43

+0

這是輸出,它表示下一位玩家可以看到上一位玩家分配的角色。如何解決這個問題? =] – 2014-09-24 13:35:15

+0

編輯添加更好的清除和另一種選擇 – 2014-09-24 15:54:28