# How to build a game board [duplicate]

I have - rather lazily - constructed a chessboard like this:

board :=
With[
{
a = Flatten @ Table[{1, 1, 0, 0}, {4}],
b = Flatten @ Table[{0, 0, 1, 1}, {4}]
},
{a, a, b, b, a, a, b, b, a, a, b, b, a, a, b, b}
]

board // MatrixForm


letters =
Transpose[{Range@15, Style[#, Bold, 16] & /@
{"a", "", "b", "", "c", "", "d", "", "e", "", "f", "", "g", "", "h"}, Table[{0, 0}, {15}]}];

numbers =
Transpose[{Range@15, Style[#, Bold, 16] & /@
{"8", "", "7", "", "6", "", "5", "", "4", "", "3", "", "2", "", "1"}, Table[{0, 0}, {15}]}];

MatrixPlot[
board,
ColorFunction -> "Monochrome",
ImageSize -> 400,
Mesh -> {{0, 16}, {0, 16}},
PlotLabel -> Style["Chessboard\n", 16, Bold],
FrameTicks -> {{False , numbers}, {letters , False}}]


(a) How could "board" be written in a functional style?

(b) How could such a functional solution be extended to include other boards (like a 10*10 draughtsboard or an odd 11*11 board)?

Clarification

In Mathematica it's not always easy to distinguish functional and "other" styles of programming because the language incorporates many imperative constructs such as Do, Table, Array etc. For the purposes of this question, reliance of such imperative constructs should be avoided to make the answer correspond to a more functional programming paradigm, and thereby to distinguish it from the closely related question How to make a resizable chess board?.

A particular feature of the functional approach is that loops are replaced by recursions.

• Is this what you're after?: How to make a resizable chess board? – Michael E2 Sep 2 '14 at 13:42
• Sorry, I have no idea what you mean. I certainly was not being frivolous, but trying to help. – Michael E2 Sep 2 '14 at 20:56
• FWIW I don't understand either, and the new answers to this question are answers that would be fine as answers to the other question as well. We need a clarification. – C. E. Sep 2 '14 at 21:04
• @eldo I think not. Five people agree with Pickett's comment that this is unclear; only two people have voted for your question. The democratic vote is on the side of this needing additional clarity. – Mr.Wizard Sep 2 '14 at 23:04
• This edit seems to take a very narrow view of functional programming (FP). I thought FP was primarily about avoiding mutable states. To say Array does not but CellularAutomation does avoid a mutable state is hardly fair. They seem equally functional. One might implement either as a loop, but that hardly matters to the programmer. – Michael E2 Sep 3 '14 at 0:18

At least internally, the following is a nice recursive way of thinking about the chess board:

MatrixPlot[CellularAutomaton[250, {0, 1}, {7, 7}]]


Not sure if this is what was meant by functional style. It's hard to make a one-liner functional.

To address extensibility: the dimensions of the board are directly dictated by the argument {7,7}, and the repeating pattern of the board is a consequence of the rule 250 together with an initial condition that has isolated 1s alternating with 0s on the first row. The beauty of cellular automata is of course that they can generate patterns of all sorts of boards, you just have to find the right rule (and starting point). But this difficulty of finding the right initial condition is precisely the tradeoff that you incur when trying to generate a complex result in a functional way. So I think this captures the "philosophy" of functional programming.

• Wow, that's a slick use of CellularAutomaton! – evanb Sep 2 '14 at 21:09
• @evanb Thanks - I'll skip the tick marks (they would take more code than the actual board...) – Jens Sep 2 '14 at 21:10
• @Jens Thanks, much more than I expected. Leave the tick marks for me :) – eldo Sep 2 '14 at 21:17
• Es gibt ja @Jens auch noch Heine: "Ich bin ein wahnsinniger Schachspieler: Schon beim ersten Stein habe ich die Königin verloren, und doch spiel ich noch und spiele - um die Königin. Soll ich weiterspielen?" – eldo Sep 2 '14 at 21:32
• @Mr.Wizard The question asked for functional style, and this is my interpretation of what was meant. In functional programming we do loops by recursion, and CellularAutomaton is the embodiment of nontrivial recursion. – Jens Sep 2 '14 at 22:18

This seems much simpler than other answers presented:

Array[Plus, {8, 8}] ~Mod~ 2 // MatrixPlot


Attempting to comply with the requirements of the addendum here is a recursive solution:

board[n_] := board[n - 1, {{0}}]

board[n_, a_] := board[n - 1, ArrayFlatten[{{a, #\[Transpose]}, {#, 0}}] &[{1 - Last[a]}]]

board[0, a_] := a


Example:

board[8] // MatrixPlot


• Darn, I was just getting to that (for the other question). +1 – Michael E2 Sep 2 '14 at 22:14
• @MichaelE2 "Darn" too, because looking at the other question I suppose this one should be closed, and the Accepted answer there is essentially the same as my answer here, just a little less terse. :-/ – Mr.Wizard Sep 2 '14 at 22:18
• This answer isn't very functional in its approach, is it? – Jens Sep 2 '14 at 22:19
• @Jens What does that even mean? I'm closing this question pending clarification. – Mr.Wizard Sep 2 '14 at 22:22
• @Jens Both our answers use an iterative function to generate an array. If you are implying that yours is functional and mine isn't I'd love to hear your argument. – Mr.Wizard Sep 2 '14 at 22:24

Pattern-based functional approach:

pat1 := n_Integer /; n > 1 :> Sequence[n, n - 1 /. pat1];
pat2 := v : List[__Integer] /; Max[v] > 1 :> Sequence[v, v - 1 /. pat2];

cb[n_] := MatrixPlot[
{{n}} /. pat1 /. pat2,
ColorFunction -> (GrayLevel@Mod[1 + #, 2] &),
ColorFunctionScaling -> False,
FrameTicks -> {
{#, #} &@ Table[{i, n - i + 1, 0}, {i, n}],
{#, #} &@ Table[{i, FromCharacterCode[ToCharacterCode["a"] + i - 1], 0}, {i, n}]},
FrameStyle -> Bold]

cb[8]


Expanding eldos approach for even n to all integers > 0:

cb[n_?EvenQ] :=
MatrixPlot[ArrayPad[DiagonalMatrix[{1, 1}], n/2 - 1, "Reflected"],
PlotTheme -> "Monochrome"]
cb[n_?OddQ] :=
MatrixPlot[Most /@ Most @ ArrayPad[DiagonalMatrix[{1, 1}], (n + 1)/2 - 1, "Reflected"],
PlotTheme -> "Monochrome"]

Manipulate[
cb[n],
{n, 1, 11, 1}]


• Thanks @ Karsten - I was not able to find a solution for the odds. – eldo Sep 2 '14 at 22:02

This is a functional version of board:

ones = {{1, 1}, {1, 1}};
zeros = {{0, 0}, {0, 0}};
board[n_] := Partition[Riffle[ConstantArray[ones, (n)^2/2], {zeros}], n, n - 1] // ArrayFlatten // Image[#, ImageSize -> 400] &
board[8]


(Defining ones and zeros is optional, so this side effect can be avoided. You will notice also that it only works for even n.)

• Executing your coding I get a long error message – eldo Sep 2 '14 at 22:07
• @eldo I just restarted my kernel and tried it again, and it worked. Could you try restarting your kernel as well? (MMA10 btw, but shouldn't matter.) – C. E. Sep 2 '14 at 22:08
• Now it works (for even n). +1 for using Image :) – eldo Sep 2 '14 at 22:15

Is what you mean by a functional solution a solution that uses functions? Or do you mean in the style of functional programming? If the former, then this works:

checked[n_] := Table[Mod[1 + (i + j), 2], {i, 1, n}, {j, 1, n}]
numbers[n_] := Transpose[{Range[1, n], ToString /@ (Reverse@Range[1, n])}]
letters[n_] := Transpose[{Range[1, n], Characters@StringTake["abcdefghijklmnopqrstuvwxyz", n]}]
(* letters needs to know what to do past n = 26. *)

board[n_] := ArrayPlot[checked[n],
FrameTicks -> {{False, numbers[n]}, {letters[n], False}}
]


Then, board[8] produces an image like the one you posted, while board[11] for example, gives an 11 x 11 board. I have used the convention that a8 is always black, but the function checked[n] can be adjusted.

If you really need it in a functional programming style, you could do something like

squareColor = Function[{row, col}, Mod[row + col + 1, 2]]
checked = Function[n, Outer[squareColor, Range[1, n], Range[1, n]]]
board = Function[n, ArrayPlot[checked[n],
FrameTicks -> {{False, numbers@n}, {letters@n, False}}]
]


where I have left off the implementation of rank and file labeling for this style (but which can be easily redone in a functional style using the implementations above).