How to make a resizable chess board?
Manipulate[MatrixPlot[Table[Mod[i + j, 2], {i, 1, n}, {j, 1, n}], ColorFunction -> "Monochrome"], {{n, 8}, 1, 20}]
Nice and simple.
To make it a little more terse we can use Array
in place of Table
:
Manipulate[MatrixPlot[Plus ~Array~ {n, n} ~Mod~ 2, ColorFunction -> "Monochrome"], {{n, 8}, 1, 20}]
With correct column numbering, thanks to a shameless steal from Kuba:
Manipulate[
MatrixPlot[Table[Mod[i + j, 2], {i, 1, n}, {j, 1, n}],
ColorFunction -> "Monochrome",
FrameTicks -> {Range@n,Transpose[{#, FromCharacterCode /@ (# + 96)} &[Range[n]]]}], {{n, 8}, 1, 20}]
For even n:
MatrixPlot[
ArrayPad[DiagonalMatrix[{1, 1}], 3, "Reflected"],
PlotTheme -> "Monochrome"]
My answer:
cb[n_Integer /; n > 0] := MatrixPlot@SparseArray[{i_, j_} :> Mod[i + j, 2], {n, n}]
cb[8]
For those who desire a more traditional board:
Block[{n = 8},
MatrixPlot[
SparseArray[{i_, j_} :> Mod[1 + i + j, 2], {n, n}],
ColorFunction -> GrayLevel,
FrameTicks -> {
{#, #} &@ Table[{i, n - i + 1}, {i, n}],
{#, #} &@ Table[{i, FromCharacterCode[ToCharacterCode["a"] + i - 1]}, {i, n}]},
FrameStyle -> Bold
]
]
Thanks to @eldo, someone answered this question and bumped it to the top of the stack. I had seen it about an hour before when I referred @eldo to it, but I ignored it until it came to the top of the stack. Now we have several answers to both, each of which might be an answer to the other.