4
$\begingroup$

Is there a way to achieve the following without listing all of the dynamic pieces so verbosely / manually listing all the dynamic variables?

pieces=Import["https://upload.wikimedia.org/wikipedia/commons"<>#]&/@{"/c/cd/Chess_pdt60.png","/0/04/Chess_plt60.png",
"/f/f1/Chess_ndt60.png","/2/28/Chess_nlt60.png",
"/8/81/Chess_bdt60.png","/9/9b/Chess_blt60.png",
"/a/a0/Chess_rdt60.png","/5/5c/Chess_rlt60.png",
"/a/af/Chess_qdt60.png","/4/49/Chess_qlt60.png",
"/e/e3/Chess_kdt60.png","/3/3b/Chess_klt60.png"};

chess=DynamicModule[{
lk={5,1},
dk={5,8},
lq={4,1},
dq={4,8},
lr1={1,1},lr2={8,1},
dr1={1,8},dr2={8,8},
lb1={3,1},lb2={6,1},
db1={3,8},db2={6,8},
ln1={2,1},ln2={7,1},
dn1={2,8},dn2={7,8},

lp1={1,2},lp2={2,2},lp3={3,2},lp4={4,2},lp5={5,2},lp6={6,2},lp7={7,2},lp8={8,2},
dp1={1,7},dp2={2,7},dp3={3,7},dp4={4,7},dp5={5,7},dp6={6,7},dp7={7,7},dp8={8,7}

},Module[{dark,light},dark=Hue[{0.57,0.4,0.6}];
light=Nest[Lighter,dark,5];Deploy@Graphics[{Table[{FaceForm[None],EdgeForm[dark],Rectangle[{1,1}-.5,{9,9}-.5],FaceForm[If[OddQ[i+j],light,dark]],Rectangle[{i,j}-.5,{i+1,j+1}-.5]},{i,1,8,1},{j,1,8,1}],

Locator[Dynamic[lk,(lk=Round/@#)&],pieces[[12]]],
Locator[Dynamic[dk,(dk=Round/@#)&],pieces[[11]]],

Locator[Dynamic[lq,(lq=Round/@#)&],pieces[[10]]],
Locator[Dynamic[dq,(dq=Round/@#)&],pieces[[9]]],

Locator[Dynamic[lr1,(lr1=Round/@#)&],pieces[[8]]],
Locator[Dynamic[lr2,(lr2=Round/@#)&],pieces[[8]]],
Locator[Dynamic[dr1,(dr1=Round/@#)&],pieces[[7]]],
Locator[Dynamic[dr2,(dr2=Round/@#)&],pieces[[7]]],

Locator[Dynamic[lb1,(lb1=Round/@#)&],pieces[[6]]],
Locator[Dynamic[lb2,(lb2=Round/@#)&],pieces[[6]]],
Locator[Dynamic[db1,(db1=Round/@#)&],pieces[[5]]],
Locator[Dynamic[db2,(db2=Round/@#)&],pieces[[5]]],

Locator[Dynamic[ln1,(ln1=Round/@#)&],pieces[[4]]],
Locator[Dynamic[ln2,(ln2=Round/@#)&],pieces[[4]]],
Locator[Dynamic[dn1,(dn1=Round/@#)&],pieces[[3]]],
Locator[Dynamic[dn2,(dn2=Round/@#)&],pieces[[3]]],

Locator[Dynamic[lp1,(lp1=Round/@#)&],pieces[[2]]],
Locator[Dynamic[lp2,(lp2=Round/@#)&],pieces[[2]]],
Locator[Dynamic[lp3,(lp3=Round/@#)&],pieces[[2]]],
Locator[Dynamic[lp4,(lp4=Round/@#)&],pieces[[2]]],
Locator[Dynamic[lp5,(lp5=Round/@#)&],pieces[[2]]],
Locator[Dynamic[lp6,(lp6=Round/@#)&],pieces[[2]]],
Locator[Dynamic[lp7,(lp7=Round/@#)&],pieces[[2]]],
Locator[Dynamic[lp8,(lp8=Round/@#)&],pieces[[2]]],

Locator[Dynamic[dp1,(dp1=Round/@#)&],pieces[[1]]],
Locator[Dynamic[dp2,(dp2=Round/@#)&],pieces[[1]]],
Locator[Dynamic[dp3,(dp3=Round/@#)&],pieces[[1]]],
Locator[Dynamic[dp4,(dp4=Round/@#)&],pieces[[1]]],
Locator[Dynamic[dp5,(dp5=Round/@#)&],pieces[[1]]],
Locator[Dynamic[dp6,(dp6=Round/@#)&],pieces[[1]]],
Locator[Dynamic[dp7,(dp7=Round/@#)&],pieces[[1]]],
Locator[Dynamic[dp8,(dp8=Round/@#)&],pieces[[1]]]
},
Frame->False]]
];
chess
$\endgroup$
3
  • 3
    $\begingroup$ Yes, it is possible. But instead of having all those different dynamic variables, create only one and let it be a list of all your pieces, DynamicModule[{p = {{1, 1}, {2, 1}, {3, 1}, ...}}, ...]. Then, use Table like: Table[With[{i = i}, Locator[Dynamic[p[[i]], (p[[i]] = Round /@ #) &], pieces[[i]]]], {i, 32}] $\endgroup$
    – Domen
    Sep 14, 2022 at 21:31
  • $\begingroup$ @Domen many thanks - worked great. Delete question do you think? $\endgroup$
    – martin
    Sep 14, 2022 at 21:48
  • 1
    $\begingroup$ Well, perhaps one of the more experienced users might come up with an actual solution of how to generate dynamic variables from a list (there is probably some hackery with scoping). $\endgroup$
    – Domen
    Sep 14, 2022 at 22:18

1 Answer 1

4
$\begingroup$

Thanks to @Domen's comment:

chessfen@"2r3k1/pb4p1/4p3/1p3p1q/5Pn1/P1NQb2P/1P4P1/R1B2R1K w - - 4 24"

with

chessfen[fen_,size_]:=DynamicModule[{poslist,pieces,piecesAll},poslist=Select[Thread[{DeleteCases[Characters[StringReplace[StringExtract[fen,1],n_/;DigitQ[n]:>StringJoin@ConstantArray[" ",ToExpression[n]]]],"/"],Flatten[Reverse[Table[{j,i},{i,8},{j,8}]],1]}],#[[1]]!=" "&];
pieces=Import["https://upload.wikimedia.org/wikipedia/commons"<>#]&/@{"/c/cd/Chess_pdt60.png","/0/04/Chess_plt60.png","/f/f1/Chess_ndt60.png","/2/28/Chess_nlt60.png","/8/81/Chess_bdt60.png","/9/9b/Chess_blt60.png","/a/a0/Chess_rdt60.png","/5/5c/Chess_rlt60.png","/a/af/Chess_qdt60.png","/4/49/Chess_qlt60.png","/e/e3/Chess_kdt60.png","/3/3b/Chess_klt60.png"};
piecesAll=poslist[[All,1]]/.Thread[Characters["rnbqkpRNBQKP"]->Part[pieces,{8,4,6,10,12,2,7,3,5,9,11,1}]];
DynamicModule[{p=poslist[[All,2]]},Module[{dark,light},dark=Hue[{0.57,0.4,0.6}];
light=Nest[Lighter,dark,5];Deploy[Graphics[{Table[{FaceForm[None],EdgeForm[dark],Rectangle[{1,1}-.5,{9,9}-.5],FaceForm[If[OddQ[i+j],light,dark]],Rectangle[{i,j}-.5,{i+1,j+1}-.5]},{i,1,8,1},{j,1,8,1}],Table[With[{i=i},Locator[Dynamic[p[[i]],(p[[i]]=Round/@#)&],ImageResize[#,Floor[size/6.5]]&@piecesAll[[i]]]],{i,Length@poslist}]},Frame->False,ImageSize->size]]]]];
chessfen[fen_]:=chessfen[fen,380];

enter image description here

Also seems to work fine inside Manipulate:

(*https://www.chess-poster.com/english/lt_pgn_to_fen/lt_pgn_fen.htm*)
anand13=chessfen/@StringSplit[#,"\n"]&@Uncompress["1:eJyd1sGOqjAUBuB5gHmIzgMY/c8pjJO7Y2tCqnd7V703szFhwA2vfyu0ULG0RY3Gzf/l72kRPvTP5fev97e3W6O7q25u+9a+9kfzZiXNt1IK5qP2l7o6n6r6IrQ4na+d+MfiIPDnAPLjsHFu5SrQW6A0AMUAUlagYIO/HABoyFNbRAVbYWcATgNFTQsCU4mdgE/AJ6ihBDL1ICFzEKrJZ7Dswk8MjwraDMabSpHHQA0Oh9s4BhODBhugfh5xmQtVS4i8QpRwjubHquJN5zOlVOFF0cNwFgo9IGjLGGPL6Ps1dEw5nJ4MPIV8RKcR7xB/vYTgcoKpYk/wZOB2xUakN8JOSIHDJuRMVqGxyR0pHELW0NiCjE1KAeQgCqOCYJWDVej2mjJ2gQDFFVljdnilDK0xbJWW40zv1sQBpp6ZIt0lhrAzzN7Hq5jByKQC9cDwosv3cDeMMmaP2ogyjaVIIHMVGWoSMeRA6CJqTDMpc43iTqBC6KgsEPYINBxHxiYs8JlGqhkJDmSdoLugV4VpW44vCTjZDogJ5ZBvwvmpwZfL1y6PnLxeycspzi2lCnwPD2sHK5AHwFztScLNwAnsAV0O0NsDRcgDpBoFPI0hJKDLEdxGEG0Q6IynEggS406giwvTIDhLMAswhHyqwHGgcHFNy7y7xdLwZ/cfcQVLLA=="];
Manipulate[anand13[[a]],
{{a,1},1,46,1,Appearance->{"Open"}}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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