[Tutor] How to have unique identifiers for multiple object instances of a given class?

boB Stepp robertvstepp at gmail.com
Sun Aug 26 22:29:36 EDT 2018


On Sun, Aug 26, 2018 at 7:48 PM Steven D'Aprano <steve at pearwood.info> wrote:
>
> On Sun, Aug 26, 2018 at 05:38:52PM -0500, boB Stepp wrote:
>
> > I feel that I may be missing something truly obvious.  I am pondering
> > the design of a solitaire scorekeeper program.  It is just meant to be
> > an electronic scorekeeper for hands of solitaire that I plan with a
> > "real" deck of cards, instead of a computer game.  I might want to
> > play and track a variety of solitaire games.  And I might want to play
> > one game for a bit and then switch to another.
>
>
> What you say is a little ambiguous. When you say solitaire, do you mean
> the specific card game known as "Solitaire", or do you mean the generic
> term for dozens of different solitaire-type single person card games?
>
> When you describe changing games, do you mean changing from (say)
> "Grandfather's Clock" to "Spider" to "Solitaire", or do you mean
> start a new game by reshuffling the cards and dealing out a new hand?

There are probably hundreds if not thousands of games that generically
fit under the description solitaire.  I am only interested in ones
that I might play that will have a score result for each hand played
with all such scores added together to form a cumulative score for a
particular game.

> It might help if you explain how you currently track these games, on
> paper.
The example that I partially gave in my original posting was a game I
was told was called "Mastery" when I was a child.  In it you count out
a pile of 13 cards that is placed to the player's left, face up.  Then
to the right of this "pile" are placed 4 cards in a row, which is the
area where you can play red on black, black on red in descending
sequence.  Finally you place a single card face up above all of this.
In many games, this is where aces are placed and you eventually build
up stacks of each suit until you, if fortunate, exhaust the entire
deck in this area, where you would have four suit stacks going "A, 2,
3, ... , J, Q, K" in that order.  In Mastery, the starting card can be
any of the 13 cards.  Say it was a 5.  Then you would try to get all
of the cards into segregated suit stacks where the bottom-most card
was the 5 of each suit.  So the sequence in a perfectly played game
would be "5, 6, 7, ... , J, Q, K, A, 2, 3, 4"  In the end any cards in
the left-most pile count 2 points against you, while every card in the
top-most up to 4 stacks count 1 point for you.  So the worst score
possible on a hand would be "-26", while the best score possible would
be "+52".  I hope that this example is clearer than mud!

The adult who taught me this particular game when I was a kid kept a
spiral bound notebook which he divided into 13 sections, one for each
rank of card (A, 2, 3, ... , J, Q, K).  Any one of these ranks might
start the top-most piles of a particular hand.  So for each hand
played he would write down the new cumulative score for that section
of his notebook.

But there are other types of solitaire that I play that might be
scored differently.  I want my program to work with any such game
where a given played hand can have a worst score, a best score, or any
integer in between, where the cumulative score over all hands played
of that particular game type would reflect the current state of the
game.

The way the scorekeeper program would work as I currently envisage it
would be the player opens an existing game from disk, starts a new
game or switches to a game already open.  If it is a new game the
player will be asked for the minimum possible score per hand, the
maximum possible score per hand, and a name for that particular
solitaire game he/she wishes to keep track of the cumulative scores
for.

As for the persistent storage I will have two files per game, a ".cfg"
file storing the min and max possible scores (Later if I add features
there might be more things in this file.) and a ".csv" file which will
retain in played order the date played, time played, and score for the
hand played.  I figure cumulative scores can be calculated on the fly
from this information.  Later on if I like what I've done I might add
the ability to do various statistical analyses of the hands played,
such as average score per hand, number of instances of a particular
score or scores, etc.

But right now I'm stuck on how to identify each active object with a
valid Python identifier.  Alan's suggestion of a dictionary of objects
sounds like a possibility, but I have no idea if that is the "best "
way to do what I am trying to do.


-- 
boB


More information about the Tutor mailing list