equivalent of enum?

Remco Gerlich scarblac-spamtrap at pino.selwerd.cx
Mon Sep 13 05:05:53 EDT 1999


Skip Montanaro <skip at mojam.com> wrote:
> If you expect to have properties associated with particular pieces
> (position, strength, etc), you might want to consider representing them by
> instances of a specific ChessPiece class instead:

I'm not going to make a chess playing engine, mostly store games,
put comments into them, etc. But a property that they do have is
the way they move.

I'm new to Python, this is the first thing of more than a few lines
that I want to program, and I'm still searching for the right mindset :-).

Maybe an empty ChessPiece superclass, and then subclasses for each
piece type, so it can return a list of squares it can move to?

Hmm. I think I'll just make some functions for that, OO is overkill
there, in my opinion.
>     whiterook = Rook(5, "h", "8", "white")
>     whitenight = Knight(3, "h", "7", "white")
>     ...

This is probably over-killing a bit. The base object that I'll use
is the Position; most move possibilities depend on the rest of the
position anyway. It'll just hold a list of squares, with piece
numbers. This is also easier because you can give a rook that hasn't 
moved yet a different number from one that has moved, because of
castling rules; to let an object change itself like that feels
icky to me :).

This may give rise to a lot of if/elsif/elsif/elsif/if/... type
of code, but then, the rules of chess are unlikely to change
soon so it's not a big problem :).

> In more direct answer to your question, there is no enum type in Python.
> It's simple enough to declare objects that you treat as constants, however:
> 
>     whiterook = 1
>     whiteknight = 2

Yes, that is what I'll do. I like the 
emptysquare, whiterook, whiteknight = range(3)
idiom from the other post :).

-- 
Remco Gerlich,  scarblac at pino.selwerd.cx
  Murphy's Rules, "Life in the fast lane":
   In the fifth edition of FBI's Tunnels & Trolls, an average man may
   make 10-second sprints topping 60 miles per hour. An exceptional
   runner could sprint at 118 mph.




More information about the Python-list mailing list