[Tutor] Chutes & Ladders

Keith Winston keithwins at gmail.com
Sun Dec 22 09:07:18 CET 2013


I've put together my first small program. It's a simulation of the game
Chutes & Ladders. It plays the game and amasses an array of  ([multicount]
[gamecount]) size, and then crunches simple stats on the average moves,
chutes, and ladders for all games in each high-level (multi) pass.
Hopefully the code is clear.

I don't think I really thought out the OOP element properly... I was
thinking of accomodating a multiplayer version in the future, but that
would require a significant rewrite. Perhaps Games should be a separate
class, I still don't really have OOP down (I'm learning Python, OOP, and
Linux simultaneously). There's no interaction between players in the game,
so there's not really any reason to do a multiplayer version: I was just
using this to familiarize myself with basic Python and maybe some stats.

I'd be interested in ALL feedback: aesthetic, functional, design, whatever.
I would have used numpy but I couldn't get it installed... I noticed,
belatedly,  that the math module has arrays, I didn't look to see if they
would have made sense to use, I think I remember hearing something about
them being inefficient or something. Anyway, thanks.

#Chutes & Ladders Simulation 1.0
import random

# Landing on a chute (key) causes one to slide down to the corresponding
value.
chutes = {16: 6, 47: 26, 49: 11, 56: 53, 62: 19, 64: 60, 87: 24, 93: 73,
95: 75, 98:78}

# Landing on a ladder (key) causes one to slide up to the corresponding
value.
ladders = {1: 38, 4: 14, 9: 31, 21: 42, 28: 84, 36: 44, 51: 67, 71: 91,
80:100}

class Player:
    """Player class for Chutes & Ladders."""

    def __init__(self):
        self.reset()

    def reset(self):
        self.position = 0
        self.movecount = 0
        self.numchutes = 0
        self.numladders = 0

    def move(self):
        """Single move, with chutes, ladders, & out of bounds"""
        roll = random.randint(1,6)
        self.movecount += 1
        self.position += roll
        if self.position in chutes.keys():
            self.position = chutes.get(self.position)
            self.numchutes += 1
        elif self.position in ladders.keys():
            self.position = ladders.get(self.position)
            self.numladders += 1
        elif self.position > 100:  # move doesn't count, have to land
exactly
            self.position -= roll

    def game(self):
        """Single game"""
        self.reset()
        while self.position < 100:
            self.move()

    def gameset(self, reps):
        """A set of games, returning associated stats array"""
        setstats = []
        for i in range(reps):
            self.game()
            stat = [i, self.movecount, self.numchutes, self.numladders]
            setstats.append(stat)
        return setstats

    def multisets(self, multi, reps):
        """A set of game sets, adding another dim to the stats array"""
        multistats = []
        for i in range(multi):
            set1 = p1.gameset(reps)
            multistats.append(set1)
        return multistats

p1 = Player()
gamecount = 1000
multicount = 10
games = p1.multisets(multicount, gamecount)
print("Avg moves  Avg chutes  Avg ladders")
for i in range(multicount):
    tmulti = games[i]
    summoves, sumchutes, sumladders = 0, 0, 0
    for j in range(gamecount):
        tgset = tmulti[j]
        summoves += tgset[1]
        sumchutes += tgset[2]
        sumladders += tgset[3]
    print(str(summoves/gamecount).rjust(9), \
          str(sumchutes/gamecount).rjust(12), \
          str(sumladders/gamecount).rjust(13))


Sample output is

Avg moves  Avg chutes  Avg ladders
   38.907        4.192         3.368
    38.64        4.173         3.276
   39.584        4.259         3.355
   39.254        4.243         3.411
    40.43        4.399         3.378
    39.63        4.195         3.305
   38.504        4.046         3.301
   39.917        4.265         3.281
   39.678        4.317         3.335
   39.585        4.229         3.326

Thanks for any suggestions or thoughts. I know this is a very simple
program, but I'm very pleased that, once I began to sort out the basics, it
fell together pretty readily: I really like Python, though there's a lot to
learn. FYI, I recently played C & L with a 4 y.o. friend, it is not
otherwise my game of choice ;-)

-- 
Keith
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131222/b34f525b/attachment-0001.html>


More information about the Tutor mailing list