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