2017-10-05 115 views
0

我試圖開發一個python骰子游戲作爲從1開始的骰子值的序列運行。從1開始值3點。如果沒有1,那麼手是0,並且一個骰子可以包含多於一次的運行。使用列表的Python骰子游戲

例如[5,6,2,4,2,3,6,4]爲0

[3,4,1,5,3,1,4,6]是6(二1)

[2,1,1,1,2,3,3,2]是24(2 1,2,3 = 18和一個1,2- = 6)

[5,3 ,2,6,4,5,1,4]是18(一個1,2,3,4,5,6)

def get_hand_score(list_of_dice): 
    score1 = 0 
    list1_sorted = list_of_dice.sort() 
    if "1" in str(list_of_dice): 
     score1+=3 
     if "2" in str(list_of_dice): 
     score1+=3 
     if "3" in str(list_of_dice): 
      score1+=3 
      if "4" in str(list_of_dice): 
      score1+=3 
      if "5" in str(list_of_dice): 
       score1+=3 
       if "6" in str(list_of_dice): 
       score1+=3 

return score1 


def test_get_hand_score(): 
    print("1. score: ", get_hand_score([5, 3, 2, 5, 5, 6, 4, 3])) 
    print("2. score: ", get_hand_score([3, 4, 1, 5, 3, 1, 4, 6])) 
    print("3. score: ", get_hand_score([5, 3, 2, 6, 4, 5, 1, 4])) 
    print("4. score: ", get_hand_score([2, 1, 1, 1, 2, 3, 3, 2])) 
    print("5. score: ", get_hand_score([3, 4, 1, 5, 2, 1, 4, 6])) 

    list1 = [5, 3, 2, 5, 5, 6, 4, 3] 
    print("6. dice: ", list1) 
    score1 = get_hand_score(list1) 
    list1.sort() 
    print(" dice_sorted: ", list1) 
    print(" score:", score1) 
    print() 

    list1 = [5, 3, 2, 6, 4, 5, 1, 4] 
    print("7. dice: ", list1) 
    list1_sorted = sorted(list1) 
    score1 = get_hand_score(list1) 
    print(" dice_sorted: ", list1_sorted) 
    print(" score:", score1) 
    print() 

    list1 = [2, 1, 1, 1, 2, 3, 3, 2]  
    print("8. dice: ", list1) 
    list1_sorted = sorted(list1) 
    score1 = get_hand_score(list1) 
    print(" dice_sorted: ", list1_sorted) 
    print(" score:", score1) 
    print() 

這給了我:

1. score: 0 
2. score: 3 
3. score: 18 
4. score: 9 
5. score: 18 
6. dice: [5, 3, 2, 5, 5, 6, 4, 3] 
    dice_sorted: [2, 3, 3, 4, 5, 5, 5, 6] 
    score: 0 

7. dice: [5, 3, 2, 6, 4, 5, 1, 4] 
    dice_sorted: [1, 2, 3, 4, 4, 5, 5, 6] 
    score: 18 

8. dice: [2, 1, 1, 1, 2, 3, 3, 2] 
    dice_sorted: [1, 1, 1, 2, 2, 2, 3, 3] 
    score: 9 

預期的結果:

1. score: 0 
2. score: 6 
3. score: 18 
4. score: 24 
5. score: 21 
6. dice: [5, 3, 2, 5, 5, 6, 4, 3] 
    dice_sorted: [2, 3, 3, 4, 5, 5, 5, 6] 
    score: 0 

7. dice: [5, 3, 2, 6, 4, 5, 1, 4] 
    dice_sorted: [1, 2, 3, 4, 4, 5, 5, 6] 
    score: 18 

8. dice: [2, 1, 1, 1, 2, 3, 3, 2] 
    dice_sorted: [1, 1, 1, 2, 2, 2, 3, 3] 
    score: 24 
+0

這裏沒有確切的問題,但對於初學者來說,str(list_of_dice)中的'if'1'不計算1的數量,只是在得分爲1時將分數加3。基本上,你現在擁有的是從一開始的最長序列長度的3倍。 –

回答

0

此代碼的工作(產生你的預期輸出):

from collections import Counter 

def get_hand_score(dice): 
    counts = Counter(dice) 

    total = 0 

    while counts[1] > 0: 
     n = 1 
     while counts[n] > 0: 
      counts[n] -= 1 
      total += 1 
      n += 1 

    return total * 3 

與您的代碼的問題是,你永遠只能算一個序列。你尋找一個1,然後是2等,但是你不會以任何方式將它們標記爲「已使用」,然後重複。所以當你的代碼看到123123時,它只計算123次。

編輯

簡單(也許?)工作代碼:

def get_hand_score(dice): 
    counts = Counter(dice) 

    n = counts[1] 
    score = 0 

    for i in range(1, 7): 
     n = min(n, counts[i]) 
     score += n 

    return score * 3 

這裏的想法是,發現每增加1分。 (至少它是1的序列的一部分)。

之後,每2到1的數量增加得分。並且,我們計算的每2到3的數字再次增加分數。