Answer to Question #225578 in Python for Binura Thiranjaya

Question #225578


The game is played as follows:

The game consists of 3 turns, and the aim of the game is to score as many points as possible,Each turn begins with a roll of 6 standard dice,Matching dice (2 or more of the same number) can be set aside for points, and the remaining dice can then be re-rolled in an attempt to score more points 5s Points are “amount of dice * number on dice”, e.g. rolling three 5s is worth 15 points,The player chooses whether to set matching dice aside and whether to re-roll remaining dice, If the player chooses not to re-roll the dice, the points accumulated in the turn are added to their score and the turn ends, If no dice can be put aside after a roll, the player loses the points the potential points and the turn ends – hence, the player must decide how far to push their luck with additional rolls,The game involves quite a lot of luck, but there is also a small amount of strategy in choosing whether to set matching dice aside and whether to re-roll the remaining dice.



1
Expert's answer
2021-08-12T16:25:11-0400
import random

def toss(n):
    L = []
    for _ in range(n):
        L.append(random.randint(1,6))
    return L

def print_toss(L):
    for d in L:
        print(d, end=' ')
    print()

def matching(L):
    num = [0]*6
    for d in L:
        num[d-1] += 1
    return num

def turn():
    n = 6
    score = 0
    while True:
        dices = toss(n)
        print('Your toss is: ', end='')
        print_toss(dices)
        match = matching(dices)
        hasMatch = False
        for i in range(6):
            if match[i] > 1:
                hasMatch = True
                ans = input(f'Put aside {i+1}s ? (Y/n) - ')
                if ans[0] == 'y' or ans[0] == 'Y':
                    n -= match[i]
                    score += match[i]*(i+1)
                    break
        if not hasMatch:
            print('You lose your points')
            return 0
        
        print(f'Your score is {score}')
        if n == 0:
            return score
        if n < 6:
            ans = input(f'Would you like to re-toss {n} dices? (Y/n) - ')
            if ans[0] != 'y' and ans[0] != 'Y':
                return score
        print()

def game():
    tot_score = 0
    for i in range(3):
        print('=============================')
        print(f'Round {i+1}')
        tot_score += turn()
    print()
    print(f'Total score is {tot_score}')

if __name__ == '__main__':
    game()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS