Message Boards Message Boards

How to train your Chess - pt 1

I recently started again to train blind chess. But the apps on the Play Store for it didn't cut for me.

With Mathematica, I would be able to program very quickly a GUI, and presto!

guess stats

Things for a future post:

  1. Guess the color of a square
  2. Guess if a random piece can attack a certain square
  3. Guess if two random pieces attack each other

CODE

fname = FileNameJoin[{$TemporaryDirectory, "chess-1.m"}];

columns = CharacterRange["a", "h"];
If[FileExistsQ@fname,
    stats = Import@fname
,
    stats = Association@Flatten@Table[
       {i, j} -> <|
         "name" -> StringTemplate["`1``2`"][columns[[j]], 9-i],
         "r" -> 0, "w" -> 0, "p" -> None,
         "color" -> If[Mod[i+j, 2] != 0,
                        RGBColor[0.67, 0.67, 0.67],
                        RGBColor[0.90, 0.90, 0.90]]
       |>
    , {i, 8}, {j, 8}];
    stats["count"] = 0;
];

W = 32;
showStats = False;
prevRight = True;
prevGuess = None;
guess = RandomInteger[{1, 8}, 2];

boardButton = Table[With[{i=i, j=j},
    Button[
       Dynamic@If[showStats, stats[{i, j}, "name"], ""],
       (If[{i, j} == guess,
         stats[{i, j}, "r"] += 1;
         prevRight = True,
         stats[{i, j}, "w"] += 1;
         prevRight = False 
       ];
         stats[{i, j}, "p"] = stats[{i, j}, "r"]/(stats[{i, j}, "r"] + stats[{i, j}, "w"]);
         stats[{i, j}, "color"] = Blend[{Red, Green}, stats[{i, j}, "p"]];

         stats["count"] += 1;

         prevGuess = guess;
         guess = RandomInteger[{1, 8}, 2];

         showStats = False;

         If[Mod[stats["count"], 5] == 0, Export[fname, stats]];
       ),
       Appearance -> "Palette",
       ImageSize -> {1,1}W,
       Background -> Dynamic@If[showStats, stats[{i, j}, "color"],
                If[Mod[i+j, 2] != 0, RGBColor[0.67, 0.67, 0.67], RGBColor[0.90, 0.90, 0.90]]]
    ]
], {i, 8}, {j, 8}] // Grid[#, Spacings -> {0, 0}] &;

scoreButton = {
    Button[Total[stats[#, "r"] & /@ Tuples[Range@8,2]] // Dynamic,
       showStats = !showStats,
       ImageSize -> {2,1}W, Appearance -> "Palette", Background -> RGBColor[0.79, 1., 0.73]],
    Button[Total[stats[#, "w"] & /@ Tuples[Range@8,2]] // Dynamic,
       showStats = !showStats,
       ImageSize -> {2,1}W, Appearance -> "Palette", Background -> RGBColor[1., 0.82, 0.76]],
    Button[Dynamic@If[prevGuess =!= None, stats[prevGuess, "name"], ""],
       ImageSize -> {2,1}W, Appearance -> "Palette", Enabled -> False,
       Background -> Dynamic@If[prevGuess =!= None, Lighter@If[prevRight, Green, Red], White]],
    Button[Dynamic@Style[stats[guess, "name"], Bold, Black],
       ImageSize -> {2,1}W, Appearance -> "Palette", Enabled -> False]
};
grid = {scoreButton, {boardButton, SpanFromLeft, SpanFromLeft, SpanFromLeft}} // Grid[#, Spacings -> {0, 0}] &
POSTED BY: Thales Fernandes
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract