Any better way of doing this?

Question:

I am making a board game and have to change the x/y coordinates of a pawn currently I have 32 lines that do this due to there being 16 pawns in the game. I simply check if the pawn selected is correct if so change it if not go to the next if. This is obviously horribly inefficient and also a waste of lines (in turn maybe making it harder to read). I have been searching a lot but have been unable to find a way to shorten it or at least make it more efficient.
language: Python/pygame

pionXY is defined in another function and can be for example: Geel_1 or Groen_3
If needed this can change and I am open to all suggestions.

this is the current piece of code I am using.

if pionXY == "Geel_1":
    Geel_1.x, Geel_1.y = constants.COORDINATEN[constants.STAPPEN[pion]]
elif pionXY == "Geel_2":
    Geel_2.x, Geel_2.y = constants.COORDINATEN[constants.STAPPEN[pion]]
elif pionXY == "Geel_3":
    Geel_3.x, Geel_3.y = constants.COORDINATEN[constants.STAPPEN[pion]]
elif pionXY == "Geel_4":
    Geel_4.x, Geel_4.y = constants.COORDINATEN[constants.STAPPEN[pion]]
elif pionXY == "Groen_1":
    Groen_1.x, Groen_1.y = constants.COORDINATEN[constants.STAPPEN[pion]]
elif pionXY == "Groen_2":
    Groen_2.x, Groen_2.y = constants.COORDINATEN[constants.STAPPEN[pion]]
elif pionXY == "Groen_3":
    Groen_3.x, Groen_3.y = constants.COORDINATEN[constants.STAPPEN[pion]]
elif pionXY == "Groen_4":
    Groen_4.x, Groen_4.y = constants.COORDINATEN[constants.STAPPEN[pion]]
elif pionXY == "Rood_1":
    Rood_1.x, Rood_1.y = constants.COORDINATEN[constants.STAPPEN[pion]]
elif pionXY == "Rood_2":
    Rood_2.x, Rood_2.y = constants.COORDINATEN[constants.STAPPEN[pion]]
elif pionXY == "Rood_3":
    Rood_3.x, Rood_3.y = constants.COORDINATEN[constants.STAPPEN[pion]]
elif pionXY == "Rood_4":
    Rood_4.x, Rood_4.y = constants.COORDINATEN[constants.STAPPEN[pion]]
elif pionXY == "Blauw_1":
    Blauw_1.x, Blauw_1.y = constants.COORDINATEN[constants.STAPPEN[pion]]
elif pionXY == "Blauw_2":
    Blauw_2.x, Blauw_2.y = constants.COORDINATEN[constants.STAPPEN[pion]]
elif pionXY == "Blauw_3":
    Blauw_3.x, Blauw_3.y = constants.COORDINATEN[constants.STAPPEN[pion]]
elif pionXY == "Blauw_4":
    Blauw_4.x, Blauw_4.y = constants.COORDINATEN[constants.STAPPEN[pion]]
Asked By: Gwen

||

Answers:

You can make this code more efficient by using a loop to iterate through the list of pawns. For example, if you are using Python you could use a for loop like this:

for pionXY in ["Geel_1", "Geel_2", "Geel_3", "Geel_4", "Groen_1", "Groen_2", "Groen_3", "Groen_4", "Rood_1", "Rood_2", "Rood_3", "Rood_4", "Blauw_1", "Blauw_2", "Blauw_3", "Blauw_4"]:
    pawn = eval(pionXY)  # evaluate the string as a variable name
    pawn.x, pawn.y = constants.COORDINATEN[constants.STAPPEN[pion]]

This loop will iterate over the list of pawns and assign each one the correct coordinates. This is much more efficient than having to write out each line manually.

Answered By: Lewis Munene

One way to reduce the amount of code needed is to use a dictionary to store the objects, and use the pionXY value as the key to access the relevant object.
Here’s an example:

pieces = {
    "Geel_1": Geel_1,
    "Geel_2": Geel_2,
    "Geel_3": Geel_3,
    "Geel_4": Geel_4,
    "Groen_1": Groen_1,
    "Groen_2": Groen_2,
    "Groen_3": Groen_3,
    "Groen_4": Groen_4,
    "Rood_1": Rood_1,
    "Rood_2": Rood_2,
    "Rood_3": Rood_3,
    "Rood_4": Rood_4,
    "Blauw_1": Blauw_1,
    "Blauw_2": Blauw_2,
    "Blauw_3": Blauw_3,
    "Blauw_4": Blauw_4
}

piece = pieces.get(pionXY)
if piece:
    piece.x, piece.y = constants.COORDINATEN[constants.STAPPEN[pion]]
Answered By: GodFather

U can easily use a dictionary!

pawns = {
    "Geel_1": Geel_1,
    "Geel_2": Geel_2,
    ...
    "Blauw_4": Blauw_4,
}

pawn = pawns[pionXY]
pawn.x, pawn.y = constants.COORDINATEN[constants.STAPPEN[pion]]
Answered By: Nova

I would consider using a dict for constant time lookup into the set of pawn objects.

pawns = {"Geel_1"  : Geel_1, 
         "Geel_2"  : Geel_2,
         "Geel_3"  : Geel_3,
         # ...all the rest here...
         "Blauw_4" : Blauw_4}

With this structure in place you can then do the following with no if/else at all:

pawns[pion].x, pawns[pion].y = constants.COORDINATEN[constants.STAPPEN[pion]]
Answered By: Ted B
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.