I have got an image folder that contains images of playing cards and I imported them as follow:
import D2 from "../image/D2.png";
import H2 from "../image/H2.png";
import S2 from "../image/S2.png";
and all the way until I reach SA (Spade Ace)
I also have key value pair dictionary such as:
1: "C2",
2: "D2",
3: "H2",
4: "S2",...
I need this dictionary to both calculate the rank of the card and to generate image.
atm I can retrieve the value by evaluating the last char of the string and convert it into int (in case of K,Q etc I have a switch case for them)
My issue is I have trouble to convert the string into the alias that I imported.
I tried doing eval(4) but it didn't work.
if I modified the dictionary into
1: C2,
2: D2,
3: H2,
4: S2,...
I can easily use this as the src of my images, but I can no longer use the card-code to obtain the value of the card.
Could you please suggest a solution or a better approach for this problem.
If I follow the question properly can't you just create the dictionary in this manner? If you intend to use card codes to obtain the value / image then you could build your dictionary like so:
dict = {
C2: { image: C2, value: 1 },
D2: { image: D2, value: 2 },
...
}
c2Image = dict['C2'].image;