python - Creating a list of dictionaries where each dictionary contains another dictionary as values -


im writing algorithm in python plays this game.

the current state of board of tiles in game dictionary in form of:

{     <tile_id>: {                 'counters': <number of counters on tile or none>,                 'player': <player id of player holds tile or none>,                 'neighbours': <list of ids of neighbouring tile>                },                ... } 

i have dictionary stores of tiles 'full' (i.e. tile has 1 less counter number of neighbours , player me) dictionary, full_tiles, in same form board dictionary above.

i trying create list, chains, each element in list dictionary of full tiles neighbouring @ least 1 other full tile (i.e chain of full tiles). list of seperate chains on board.

here code far:

for tile_id, tile in full_tiles.items(): #iterates through full tiles     current_tile = {tile_id : tile}      #temporarily stores current tile     if not chains:                       #if chains list empty         chains.append(current_tile)      #begin list     else:                                #if list not empty         index, chain in enumerate(chains):            #iterate though list of chains             if not (current_tile in chain):               #if current tile not in current chain                 tile_id2, tile2 in chain.items():     #iterate through tiles in current chain                     neighbour in tile2["neighbours"]: #iterate through each neighbour of current tile                                                           #neighbour in form of tile_id                         if neighbour in chain:            #if current tile's neighbour in chain                             chain[tile_id] = tile         #add tile chain 

it difficult me test , debug code , check if working correctly code can run in application simulates game. can see, there quite lot going on in block of code of nested loops difficult follow. cannot seem think straight @ minute , cannot determine if mess, in honesty, function hope.

while writing this, have realised - on line 7 of code - checking if current tile not in current chain , there intersecting chains which, of course, mess. instead of this, need first check if current tile in not in any of chains, not one.

apart error, code achieve attempting? or can recommend simpler, neater way it? (there has be!)

also, let me know if have not given enough information on how code run or if need explain further, such contents of board dictionary.

thank in advance.

edit: unfortunately, under time constraint complete project, , first time ever working python, lack knowledge in language optimise solution using sources given below. here final extremely ugly , messy solution problem which, in end, worked fine , wasn't terribly inefficient given small data set code works on.

for x in range(0, len(my_hexplode_chains) - 1):     match_found = false     y in range(x + 1, len(my_hexplode_chains)):         tile_id_x, tile_x in my_hexplode_chains[x].items():             #compare each chain in list             tile_id_y, tile_y in my_hexplode_chains[y].items():         #to every other chain                 neighbour in tile_x["neighbours"]:                      #if tiles in different lists                     if neighbour == tile_id_y:                              #are neighbours                         match_found = true                         my_hexplode_chains[x].update(my_hexplode_chains[y]) #append 1 chain other                         del my_hexplode_chains[y]                           #delete appended chain                 if match_found:                                             #continue loop @ next chain                     break                                                   #very ugly way             if match_found:                 break         if match_found:             break     if match_found:         break 

how optimization?

def find_match (my_hexplode_chains):      x =  0     len_chain = len(my_hexplode_chains)     while x <= len_chain:         y = x + 1         tile_id_x, tile_x in my_hexplode_chains[x].items():             tile_id_y, tile_y in my_hexplode_chains[y].items():                 if tile_id_y in tile_x["neighbours"]:                     my_hexplode_chains[x].update(my_hexplode_chains[y])                     del my_hexplode_chains[y]                     return true         x += 1     return false 

you pass function after each move in game , trace output.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -