Getting a seeded value from a list

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Nov 21 04:37:09 CET 2012


On Tue, 20 Nov 2012 18:18:17 -0800, frednotbob wrote:


> The problem, in a nutshell, is this:
> 
> When the player starts a new game, make_map() randomly generates level
> 'Foo' as the player's starting floor.  Floor 'Bar' is similarly
> generated as the player descends from 'Foo' to 'Bar.
> 
> Each time the player goes from 'Foo' to 'Bar' (or vice versa), make_map
> randomly generates a new layout for either level.
>
> What I'd like to do is select values from 'levelSeed' and assign them to
> the levels that make_map generates so that the player goes back to the
> same 'Foo' and 'Bar' each time (at least until the player repopulates
> levelSeed with new values by whatever method I eventually decide to
> use).

Just store a mapping between levels and seeds:

levels = {}  # starts out blank

Now, each time you enter a level, see if there is a seed recorded, and if 
so, use that. Here I assume each level has a name, and you look up by 
name.

name = "Lock And Load"
if name in levels:
    seed = levels[name]
else:
    # Never seen this level before.
    seed = pick_a_new_seed_somehow()
    levels[name] = seed
    map = make_map(name, seed)  # whatever arguments needed
    redraw(map)



-- 
Steven


More information about the Python-list mailing list