[Tutor] More or less final Chutes & Ladders
spir
denis.spir at gmail.com
Sat Jan 4 11:07:25 CET 2014
On 01/04/2014 05:47 AM, Keith Winston wrote:
> Here is what I think will be about the final version of C and L. I
> rearranged it quite a bit (into 2 classes), fixed a bug or two, and
> generally cleaned it up a bit. I haven't really polished it, but hopefully
> it will be less difficult to read... which is to say, if anyone wants to go
> through it AGAIN (at your leisure) I would appreciate comments on style,
> etc.
A few notes:
* You don't need, for lists, names likes 'self.chutes_list': 'self.chutes' does
the job and is de facto standard in Python. Similarly, for a dict like one
mapping names to phones, one can use 'names_phones' (read: "names' phones" or
"names to phones").
* Try to find a satisfying personal convention (there is no standard in Python)
for indexes (number of A thing) and counts (numbers of things), which constantly
come up programming. (I use i_thing & n_things, or just i & n when in context
there is no possible ambiguity.)
* What is the point of method reset apart from __init__? (Even if you need to
reset a game after it has started, you could call __init__ for that. Rarely
needed, but happens, esp. in games when eg a trap or bad luck brings the player
back to start.)
* Make a class for results (see example below). Not only it is semantically
correct (a result is a composite object, not a collection, but youy use a list),
not only it makes code cleaner, but it allows more simply modifying and
extending. Also, you could add there, directly in the concerned class, methods
that deal with results (in addition to ones for result output, as in the
example, which I also write first).
* When posting code, place your signature (-- Keith) _after_. Else, it causes
weird bugs in email readers (eg mine, Thunderbird); I cannot even copy-paste it,
for some weird reason.
Denis
=== class Result example ==============================
class Result:
''' result of an individual game
Fields:
* no : 'numéro' (?) of the game
* n_moves : number of moves
* chutes : list of chute positions
* ladders : list of ladder positions
Methods:
* __repr__ : notation, as in code
* __str__ : writing with field names
'''
def __init__ (self, no, n_moves, chutes, ladders):
''' Store game stat data. '''
self.no = no
self.n_moves = n_moves
self.chutes = chutes
self.ladders = ladders
# output
def __repr__ (self):
''' notation, as in code (for programme feedback) '''
return "Result(%r, %r, %r, %r)" % \
(self.no, self.n_moves, self.chutes, self.ladders)
def __str__ (self):
''' writing with field names (eg for user info in UI) '''
return "{no:%s n-moves:%s chutes:%s ladders:%s}" % \
(self.no, self.n_moves, self.chutes, self.ladders)
# fake example
res = Result(3, 333, [1,2,3], [99,33,11])
print("\n%r\n\n%s\n" %(res, res))
""" writes out:
Result(3, 333, [1, 2, 3], [99, 33, 11])
{no:3 n-moves:333 chutes:[1, 2, 3] ladders:[99, 33, 11]}
"""
More information about the Tutor
mailing list