In: Computer Science
I am struggling to write a python script that
calculates the average length ,max length shortest length and
overall average length of of a snake and ladder game. I use python
3
The length is the number the dice is rolled until the max is
reached
We need a python script that calculates the number of moves it will take for a single player to reach 100. We have to simulate a snake and ladder game using python language. So one person plays the game several times and will compare the number of moves per each played game for us to be able to come up with the required data
Thanks for the question, here is the code, that plays snake and ladders, Its a starting code for you, The program keeps on playing the game until the user lands in 100. DUring the play it tracks the number of turns and at the end it prints the count.
For solving the max and shortest length, you need to do this experiment a large number of times. and then track the smallest and largest number.
If you need help on this also, please comment and i will assist you. I think you can do it from here.
Below is the code with screenshot.
===========================================================================
import  random
# contains the key values
# for example if user lands to 17 it will take user to 7, if lands on 54: will take user to 34
snakes_dict = {17: 7, 54: 34, 62: 19, 64: 60, 87: 36, 93: 73, 95: 75, 98: 79}
# when user lands in 1 user will be taken to 38, if 4 then 14 like wise
ladder_dict = {1: 38, 4: 14, 9: 31, 21: 42, 28: 84, 51: 67, 72: 91, 88: 99}
place=0
count=0
while place!=100: # here max is 100
    dice = random.randint(1,6)
    place+=dice
    print('You rolled: {} you went to : {}'.format(dice,place))
    # check the place for a snake or ladder
    if place in snakes_dict.keys():
        place = snakes_dict.get(place)
        print('Oops. There was a snake. You went down to {}'.format(place))
    elif  place in ladder_dict.keys():
        place = ladder_dict.get(place)
        print('Congrats. You got a ladder. You climbed to {}'.format(place))
    if place>100:place-=dice
    count += 1
print('It took {} turns to reach to 100'.format(count))
==============================================================


