1
\$\begingroup\$

I would like to build something that allows me to obtain potions using two identical lists of ingredients.

ingredients = ["frog", "butterflies", "stones"]
ingredients_2 = ["frog", "butterflies", "stones"]

If I combine frog and stones, for example, I get frog marmelade. If I combine butterfly and butterfly I get super butterfly and so on...

But other than a terrifyingly long list of ifs, is there a better and more clever way to implement this?

I thought about a matrix:

row_num = 4
col_num = 4

grid = [[x for x in range(col_num)] for y in range(row_num)]

def print_grid():
    for row in grid:
        print(row)
        
def get_ingredient_1_index(ingredient_1):
    index = 0
    for item in grid[0]:
        if item == ingredient_1:
            return index
        index += 1
    return -1

def get_ingredient_2_index(ingredient_2):
    index = 0
    column_headers = [row[0] for row in grid]
    for item in column_headers:
        if item == ingredient_2:
            return index
        index += 1
    return -1


def mix_ingredients(ingredient_1, ingredient_2):
    coordinates_ingredient_1 = get_ingredient_1_index(ingredient_1)
    coordinates_ingredient_2 = get_ingredient_2_index(ingredient_2)
    return grid[coordinates_ingredient_2][coordinates_ingredient_1]


grid[0][1] = "frog"
grid[0][2] = "butterfly"
grid[0][3] = "stone"

grid[1][0] = "frog"
grid[2][0] = "butterfly"
grid[3][0] = "stone"

grid[1][1] = "superfrog"
grid[1][2] = "froggerfly"
grid[1][3] = "frog marmelade"

grid[1][1] = "superfrog"
grid[1][2] = "froggerfly"
grid[1][3] = "frog marmelade"

grid[2][1] = "froggerfly"
grid[2][2] = "super butterfly"
grid[2][3] = "butterfly jam"

grid[3][1] = "frog marmelade"
grid[3][2] = "butterfly jam"
grid[3][3] = "super stone"


print(mix_ingredients("frog", "butterfly"))

But I think it is a bit of clunky and poor solution.

Can you help in pointing me into a better direction?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I think your matrix idea is viable. Although I would suggest a bit of improvements to make it easier to work with:

# step 0: define your ingredients in a list
known_ingredients = ["butterfly"      , "frog"          , "stone"]

# Step 1: reorganize your list; make it visually an actual grid. 

grid[0] = [""         , "frog"          , "butterfly"      , "stone"]
grid[1] = ["frog"     , "superfrog"     , "froggerfly"     , "frog marmelade"]
grid[2] = ["butterfly", "froggerfly"    , "super butterfly", "butterfly jam"]
grid[3] = ["stone"    , "frog marmelade", "butterfly jam"  , "super stone"]

# Step 2: remove noise and some potential errors: sort the list alphabetically and 
# remove the duplicates, that will be less maintenance for you:

grid[0] = [""         , "butterfly"      , "frog"          , "stone"]
grid[1] = ["butterfly", "super butterfly", "froggerfly"    , "butterfly jam"]
grid[2] = ["frog"     , ""               , "superfrog"     , "frog marmelade"]
grid[3] = ["stone"    , ""               , ""              , "super stone"]



def mix_ingredients(ingredient_1, ingredient_2):
    # the `index` function assumes the object is there, you might want to 
    # add a bit of error handling here. 
    coordinates_ingredient_1 = known_ingredients.index(ingredient_1) + 1
    coordinates_ingredient_2 = known_ingredients.index(ingredient_2) + 1
    
    if coordinates_ingredient_1 > coordinates_ingredient_2:
      # swap the indices, we sort them in alphabetical order
      tmp = coordinates_ingredient_2
      coordinates_ingredient_2 = coordinates_ingredient_1
      coordinates_ingredient_1 = tmp
    
    return grid[coordinates_ingredient_1][coordinates_ingredient_2]
    

Now this will work fine if you have a few ingredients, but at some point, you'll likely want to move this to something like a CSV and load it from file at game initialization.

\$\endgroup\$
2
  • \$\begingroup\$ Instead of cleaning it up, it could be used as well to make different results depending which ingredient was used first \$\endgroup\$
    – Zibelas
    Commented Apr 8, 2022 at 15:55
  • \$\begingroup\$ @Zibelas Yes, indeed. The gist of it here is, if it's not needed, then better to get rid of it to reduce the maintenance and avoid potential issues. \$\endgroup\$
    – Vaillancourt
    Commented Apr 8, 2022 at 16:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .