๐พ Archived View for m0yng.uk โบ 2020 โบ 01 โบ Pythons-and-Ladders captured on 2022-01-08 at 13:43:39. Gemini links have been rewritten to link to archived content
โฌ ๏ธ Previous capture (2021-12-03)
โก๏ธ Next capture (2022-03-01)
-=-=-=-=-=-=-
Created 2020-01-01
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!
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 ยฉ 2021 Christopher M0YNG
Code snippets are licenced under the Hippocratic License 3.0 (or later.)
Page generated 2021-12-20 by Complex 19