-- Leo's gemini proxy

-- Connecting to m0yng.uk:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini;lang=en

Pythons and Ladders - M0YNG.uk

Created 2020-01-01

Tagged

Code


I get bored playing games like Snakes and Ladders, there is no skill, just luck with a die.


I wondered if I could save the time and effort of actually playing the game, and just let the computer tell me who wins. Not by random selection, but by actually playing the game for me.


So, using Python (obviously) I knocked up a thing to play it for me.


I based my code on this travel game I used to play.


A magnetic travel set of Snakes and Ladders [IMG]


It gives an output like this


$ ./index.py Player1 Player2 Player3
Play Snakes and Ladders, without actually playing.
We have 3 players!
Let's play!
Player1 rolled a 4 and moved to space 4
Space 4 was a ladder! Player1 moves to 14
Player2 rolled a 2 and moved to space 2
Player3 rolled a 6 and moved to space 6
Player1 rolled a 4 and moved to space 18
Player2 rolled a 3 and moved to space 5
Player3 rolled a 6 and moved to space 12
Player1 rolled a 2 and moved to space 20
Player2 rolled a 2 and moved to space 7
Player3 rolled a 4 and moved to space 16
Space 16 was a snake! Player3 moves to 6
Player1 rolled a 6 and moved to space 26
Player2 rolled a 6 and moved to space 13
Player3 rolled a 6 and moved to space 12
Player1 rolled a 6 and moved to space 32
Player2 rolled a 4 and moved to space 17
Player3 rolled a 1 and moved to space 13
Player1 rolled a 5 and moved to space 37
Player2 rolled a 6 and moved to space 23
Player3 rolled a 6 and moved to space 19
Player1 rolled a 5 and moved to space 42
Player2 rolled a 4 and moved to space 27
Player3 rolled a 4 and moved to space 23
Player1 rolled a 1 and moved to space 43
Player2 rolled a 3 and moved to space 30
Player3 rolled a 5 and moved to space 28
Space 28 was a ladder! Player3 moves to 84
Player1 rolled a 2 and moved to space 45
Player2 rolled a 2 and moved to space 32
Player3 rolled a 5 and moved to space 89
Player1 rolled a 5 and moved to space 50
Player2 rolled a 6 and moved to space 38
Player3 rolled a 1 and moved to space 90
Player1 rolled a 3 and moved to space 53
Player2 rolled a 5 and moved to space 43
Player3 rolled a 1 and moved to space 91
Player1 rolled a 3 and moved to space 56
Space 56 was a snake! Player1 moves to 53
Player2 rolled a 1 and moved to space 44
Player3 rolled a 6 and moved to space 97
Player1 rolled a 3 and moved to space 56
Space 56 was a snake! Player1 moves to 53
Player2 rolled a 3 and moved to space 47
Space 47 was a snake! Player2 moves to 26
Player3 rolled a 3 and moved to space 100
Player3 has won!

The code


And it works like this (I'm sure it's not optimal, but that wasn't the point)


#!/usr/bin/env python3
import random
import sys
print('Play Snakes and Ladders, without actually playing.')

if len(sys.argv) == 1:
    print('No players?.')
    print('try: snakesandladders player1 player2 player3')
    sys.exit()
elif len(sys.argv) == 2:
    print('We need more than one player!')
    sys.exit()

players = sys.argv[1:]

print('We have ' + str(len(players)) + ' players!')

print('Let\'s play!')

playerPositions = {}

# Board properties
boardSize = 100
snakesandladders = {
    1: {'goto': 38, 'type': 'ladder'},
    4: {'goto': 14, 'type': 'ladder'},
    9: {'goto': 31, 'type': 'ladder'},
    16: {'goto': 6, 'type': 'snake'},
    21: {'goto': 42, 'type': 'ladder'},
    28: {'goto': 84, 'type': 'ladder'},
    36: {'goto': 44, 'type': 'ladder'},
    47: {'goto': 26, 'type': 'snake'},
    49: {'goto': 11, 'type': 'snake'},
    51: {'goto': 67, 'type': 'ladder'},
    56: {'goto': 53, 'type': 'snake'},
    62: {'goto': 19, 'type': 'snake'},
    64: {'goto': 60, 'type': 'snake'},
    71: {'goto': 91, 'type': 'ladder'},
    80: {'goto': 100, 'type': 'ladder'},
    87: {'goto': 24, 'type': 'snake'},
    93: {'goto': 73, 'type': 'snake'},
    95: {'goto': 75, 'type': 'snake'},
    98: {'goto': 78, 'type': 'snake'},
}

# setup player positions
for player in players:
    playerPositions[player] = 0

def doTurn(player):
    thisRoll = random.randint(1, 6)
    playerPositions[player] += thisRoll
    print(player + ' rolled a ' + str(thisRoll) +
          ' and moved to space ' + str(playerPositions[player]))
    if playerPositions[player] in snakesandladders:
        print('Space ' + str(playerPositions[player]) + ' was a ' +
              snakesandladders[playerPositions[player]]['type'] + '! ' + player +
              ' moves to ' + str(snakesandladders[playerPositions[player]]['goto']))
        playerPositions[player] = snakesandladders[playerPositions[player]]['goto']
    if playerPositions[player] >= boardSize:
        print(player + ' has won!')
        return True
    else:
        return False

def playGame():
    for player in players:
        if doTurn(player):
            sys.exit()
    playGame()

playGame()

Sadly there isn't a ladder Emoji (yet) so I couldn't jazz up the output with a ladder to match the ๐Ÿ


-+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-

๐Ÿ–ค Black Lives Matter

๐Ÿ’™๐Ÿค๐Ÿ’œ Trans Rights are Human Rights

โค๏ธ๐Ÿงก๐Ÿ’›๐Ÿ’š๐Ÿ’™๐Ÿ’œ Love is Love


Copyright ยฉ 2024 Christopher M0YNG - It is forbidden to use any part of this site for crypto/NFT/AI related projects.

Code snippets are licenced under the Hippocratic License 3.0 (or later.)

Page generated 2024-03-24 by Complex 19

-- Response ended

-- Page fetched on Sat May 18 21:22:50 2024