0

I am writing a murder mystery--a lot like Clue. I am using dictionaries to store all my info. My question: is there a way to shuffle dictionary values which pull from a set range of integers? I want every game to shuffle certain values within the dictionaries when starting a new game. Right now I'm focused on character placement...I'm trying to figure out the best way to shuffle character locations once each game (locations[current_room]["char"]. Once I understand how to do this, I want to apply this to a bunch of other aspects of the game--with the idea of creating a brand new mystery to solve each game. Any suggestions welcome!

EDIT Thanks for the answers! I am not trying to randomize my entire dictionary, just change the values of certain keys each game. I think I might need to solve this problem another way. I will update this post when I get things working.

FINAL EDIT I'm changing the value of specific keys I want "shuffled" by editing the dictionary. locations[1]["char"] = random.choice([0,1,2]) and then changing the other values based on a series of IF statements from the result in the random.choice. Thanks again for the help.

locations = {
1: {"name": "bedroom",
    "msg": "There is a painting on the wall. \nThe window looks out into the garden.",
    "char": 2,
    "item": "matches",
    "west": 2},
2: {"name": "living room",
    "msg" : "The room is freezing cold. The fireplace is empty.",
    "char": 1,
    "Item": "firewood",
    "east": 1},
}

characters = {
1: {"name": "doctor crichton",
    "msg": "A tall, handsome archeologist home from a dig in Africa."},
2: {"name": "the widow",
    "msg": "An beautiful woman with a deep air of sadness."},
}   
current_room = 1

current_char = locations[current_room]["char"]

def status():
    """updates player on info on current room"""
    print("You are in the " + locations[current_room]["name"])
    char_status()

def char_status():
    """compiles character infomation in current room"""
    if current_char > 0:
        char_room_info()
    else:
        print("\nThis room is empty.")

def char_room_info():
    """NPC behavior in each room"""
    print(characters[current_char]["name"].title() + " is in the room with you.")

status()

2 Answers2

0

Python dictionaries are unordered, so I dont see why you woulf want to "shuffle" them. If you want to pick random strings inside the dict, maybe you could use a list to wrap this dictionary for simplicity sake.

Like so:

Locations = [ {"name": "bedroom...}, {"name": "livingroom"...},...]

I think you get the point. So now to access them:

Locations[random.rand]["name"]

Also you could use this:

random.choice(locations.keys()) 

Which is far easier.

Piero Marini
  • 131
  • 1
  • 11
0

If Python3.x then do:

import random
random.choice(list(someDictionary.keys()))

If Python2.x then do:

import random
random.choice(someDictionary.keys())
tommy.carstensen
  • 8,962
  • 15
  • 65
  • 108