9
$\begingroup$

How to define an image as a number?

for instance I want to do something like this

enter image description here

where

enter image description here

but there is an error massage

enter image description here

Ten = WolframAlpha["10 of spades", {{"Image", 1}, "Content"},
InputAssumptions -> {"*MC.%7E-_*CardRank-"}]

enter image description here

and

Ace = WolframAlpha["Ace of hearts", {{"Image", 1}, "Content"}, 
InputAssumptions -> {"*MC.%7E-_*CardRank-"}]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ I'm sure there are better ways, but I guess you could go with upvalues. Define a wrapper e.g. card and do something like: card /; Plus[card[x],card[y]] := CardRank[x] + CardRank[y]. $\endgroup$ – AndreasP Sep 26 '16 at 10:10
  • 2
    $\begingroup$ @AndreasP it should be /: :/ $\endgroup$ – Wjx Sep 28 '16 at 0:16
15
$\begingroup$

To minimize confusion I've reduced the code to one approach.

ClearAll[Ten, Ace];

getImage = WolframAlpha[
   #, {{"Image", 1}, "Content"}, InputAssumptions -> {"*MC.%7E-_*CardRank-"}
] &;

With[{  pic = ToBoxes@getImage[#]  }, 
    #2 /: MakeBoxes[#2, fmt_] := InterpretationBox[pic, #3]
] & @@@ {
  {"10 of spades", Ten, 10}, 
  {"Ace of hearts", Ace, 11}
}

Defer[Ten + Ace/Ten]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.