Skip to content

Instantly share code, notes, and snippets.

@vkryukov
Created April 23, 2014 02:29
Show Gist options
  • Save vkryukov/11200965 to your computer and use it in GitHub Desktop.
Save vkryukov/11200965 to your computer and use it in GitHub Desktop.
Display boards filled with black and white stone in Mathematica
randomPoints[n_] := RandomSample[Block[{nn = Ceiling[Sqrt[n]]},
Flatten[Table[{i, j}, {i, 1, nn}, {j, 1, nn}], 1]], n];
(* n is number of moves = 2 * number of points *)
randomBoard[n_] := Module[
{points = randomPoints[2 n]},
Join[
Take[points, n] /. {x_, y_} -> black[x, y],
Take[points, -n] /. {x_, y_} -> white[x, y]
]]
grid[minX_, minY_, maxX_, maxY_] :=
Line[Join[
Table[{{minX - 1.5, y}, {maxX + 1.5, y}}, {y, minY - 1.5, maxY + 1.5,
1}],
Table[{{x, minY - 1.5}, {x, maxY + 1.5}}, {x, minX - 1.5, maxX + 1.5,
1}]]];
displayBoard[board_] := Module[
{minX = Min[First /@ board], maxX = Max[First /@ board],
minY = Min[#[[2]] & /@ board], maxY = Max[#[[2]] & /@ board], n},
Graphics[{
grid[minX, minY, maxX, maxY],
board /. {
black[n__] -> {Black, Disk[{n}, .4]},
white[n__] -> {Thick, Circle[{n}, .4], White, Disk[{n}, .4]}
}}, ImageSize -> Small, Frame -> True]];
displayBoard[board_, points_] := Show[
displayBoard[board],
Graphics[
Map[{Red, Disk[{#[[1]], #[[2]]}, .2]} &,
points /. {black -> List, white -> List}]]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment