Question

In: Computer Science

Write a python script to calculate the average length of the game, Shortest game, longest game...

Write a python script to calculate the average length of the game, Shortest game, longest game and overall length of the game

we need a python script that calculates the average length of a snake and ladder game. ie the number of moves it takes a single player to win the game. Then you run the game several times and will compare the results to come up with the required data(length of the game and number of moves )

Solutions

Expert Solution

Please find the solution below.

Please give a thumbs up. I have taken a lot of time to solve this.

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

import random
snakes_dict = {17: 7, 54: 34, 62: 19, 64: 60, 87: 36, 93: 73, 95: 75, 98: 79}
ladder_dict = {1: 38, 4: 14, 9: 31, 21: 42, 28: 84, 51: 67, 72: 91, 88: 99}
# Play the game
def play():
    place = 0
    count = 0

    while place != 100:
        dice = random.randint(1, 6)
        if place + dice <= 100:
            place += dice
        if place in snakes_dict.keys():
            place = snakes_dict.get(place)
        elif place in ladder_dict.keys():
            place = ladder_dict.get(place)
        count += 1
    # return the output of number of times it took
    return count

def simulate():
    """
    This will play the game for 1000 times and keep track of min and max of the counts.
    :return: The values of (shortest, longest, avg)
    """
    games = []
    for i in range(1000):
        # Play 1000 times
        games.append(play())

    # Get the min and max counts here
    shortest = min(games)
    longest = max(games)
    avg = sum(games) / 1000.0
    return shortest, longest, avg


def main():
    """
    This will simulate the game for 10 times and prints the results.
    :return: None
    """
    print('Run Number\tAverage Length\tShortest Game\t Longest Game')
    sum = 0
    for i in range(10):
        shortest, longest, avg = simulate()
        sum += avg
        print("{:10d}\t{:.1f}\t\t{:10d}\t\t{:10d}".format(i + 1, avg, shortest, longest))
    print('Overall Average Length: {:.1f}'.format(sum / 10))


# Let's PLAY...!!!!
main()

=================

SAMPLE OUTPUTS:


Related Solutions

write a python script for rock scissors paper game
write a python script for rock scissors paper game
Write a python code to generate a random undirected weighted graph and calculate the shortest distance...
Write a python code to generate a random undirected weighted graph and calculate the shortest distance from node i to node j (shortest path algorithm) where i is not equal to j. Store the results in text file. Thank you!
The school bookstore wants you to write a Python script to calculate the point of sale...
The school bookstore wants you to write a Python script to calculate the point of sale (total cost) of their new 25$ gift cards. They are also running a special, if a customer buys a gift card they can buy all books for 5$ dollars each. The gift card cost is $25.00 plus $5.00 per book. In addition, there is a sales tax which should be applied to the subtotal and it is 8% (multiply the subtotal by 0.08.) Requirements:...
The school bookstore wants you to write a Python script to calculate the point of sale...
The school bookstore wants you to write a Python script to calculate the point of sale (total cost) of their new 25$ gift cards. They are also running a special, if a customer buys a gift card they can buy all books for 5$ dollars each. The gift card cost is $25.00 plus $5.00 per book. In addition, there is a sales tax which should be applied to the subtotal and it is 8% (multiply the subtotal by 0.08.) Requirements:...
The school bookstore wants you to write a Python script to calculate the point of sale...
The school bookstore wants you to write a Python script to calculate the point of sale (total cost) of their new 25$ gift cards. They are also running a special, if a customer buys a gift card they can buy all books for 5$ dollars each. The gift card cost is $25.00 plus $5.00 per book. In addition, there is a sales tax which should be applied to the subtotal and it is 8% (multiply the subtotal by 0.08.) Requirements:...
How do I write a script for this in python in REPL or atom, NOT python...
How do I write a script for this in python in REPL or atom, NOT python shell Consider the following simple “community” in Python . . . triangle = [ ["top", [0, 1]], ["bottom-left", [0, 0]], ["bottom-right", [2, 0]], ] This is the calling of function. >>> nearestneighbor([0, 0.6], triangle, myeuclidean) 'top' The new point is (0, 0.6) and the distance function is Euclidean. Now let’s confirm this result . . . >>> myeuclidean([0, 0.6], [0, 1]) 0.4 >>> myeuclidean([0,...
Write a Python application to find the longest ‘A’ path on a map of capital (uppercase)...
Write a Python application to find the longest ‘A’ path on a map of capital (uppercase) letters. The map is represented as a matrix (2-dimensional list) of capital letters. Starting from any point, you can go left, right, up and down (but not diagonally). A path is defined as the unbroken sequence of letters that only covers the spots with the letter A. The length of the A path is defined as the number of ‘A’s on the path. Your...
3. a) Write a script to prompt the user for the length and width of a...
3. a) Write a script to prompt the user for the length and width of a rectangle, and print its area with two decimal places. Put comments in the script. b) Convert the script in part (a) to a function.
Write the following Python script: Imagine you live in a world without modules in Python! No...
Write the following Python script: Imagine you live in a world without modules in Python! No numpy! No scipy! Write a Python script that defines a function called mat_mult() that takes two lists of lists as parameters and, when possible, returns a list of lists representing the matrix product of the two inputs. Your function should make sure the lists are of the appropriate size first - if not, your program should print “Invalid sizes” and return None. Note: it...
Write the following Python script: Imagine you live in a world without modules in Python! No...
Write the following Python script: Imagine you live in a world without modules in Python! No numpy! No scipy! Write a Python script that defines a function called mat_mult() that takes two lists of lists as parameters and, when possible, returns a list of lists representing the matrix product of the two inputs. Your function should make sure the lists are of the appropriate size first - if not, your program should print “Invalid sizes” and return None. Note: it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT