1
$\begingroup$

This question already has an answer here:

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

enter image description here

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}}]

enter image description here

(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.

$\endgroup$

marked as duplicate by Michael E2, m_goldberg, RunnyKine, Teake Nutma, Dr. belisarius Sep 3 '14 at 14:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 4
    $\begingroup$ Is this what you're after?: How to make a resizable chess board? $\endgroup$ – Michael E2 Sep 2 '14 at 13:42
  • 3
    $\begingroup$ Sorry, I have no idea what you mean. I certainly was not being frivolous, but trying to help. $\endgroup$ – Michael E2 Sep 2 '14 at 20:56
  • 6
    $\begingroup$ 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. $\endgroup$ – C. E. Sep 2 '14 at 21:04
  • 3
    $\begingroup$ @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. $\endgroup$ – Mr.Wizard Sep 2 '14 at 23:04
  • 3
    $\begingroup$ 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. $\endgroup$ – Michael E2 Sep 3 '14 at 0:18
10
$\begingroup$

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

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

board

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.

$\endgroup$
  • 2
    $\begingroup$ Wow, that's a slick use of CellularAutomaton! $\endgroup$ – evanb Sep 2 '14 at 21:09
  • $\begingroup$ @evanb Thanks - I'll skip the tick marks (they would take more code than the actual board...) $\endgroup$ – Jens Sep 2 '14 at 21:10
  • $\begingroup$ @Jens Thanks, much more than I expected. Leave the tick marks for me :) $\endgroup$ – eldo Sep 2 '14 at 21:17
  • 1
    $\begingroup$ 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?" $\endgroup$ – eldo Sep 2 '14 at 21:32
  • 1
    $\begingroup$ @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. $\endgroup$ – Jens Sep 2 '14 at 22:18
7
$\begingroup$

This seems much simpler than other answers presented:

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

enter image description here


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

enter image description here

$\endgroup$
  • $\begingroup$ Darn, I was just getting to that (for the other question). +1 $\endgroup$ – Michael E2 Sep 2 '14 at 22:14
  • $\begingroup$ @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. :-/ $\endgroup$ – Mr.Wizard Sep 2 '14 at 22:18
  • $\begingroup$ This answer isn't very functional in its approach, is it? $\endgroup$ – Jens Sep 2 '14 at 22:19
  • 2
    $\begingroup$ @Jens What does that even mean? I'm closing this question pending clarification. $\endgroup$ – Mr.Wizard Sep 2 '14 at 22:22
  • $\begingroup$ @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. $\endgroup$ – Mr.Wizard Sep 2 '14 at 22:24
4
$\begingroup$

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,
  PlotRangePadding -> None,
  FrameTicks -> {
    {#, #} &@ Table[{i, n - i + 1, 0}, {i, n}],
    {#, #} &@ Table[{i, FromCharacterCode[ToCharacterCode["a"] + i - 1], 0}, {i, n}]},
  FrameStyle -> Bold]

cb[8]

Mathematica graphics

$\endgroup$
3
$\begingroup$

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}]

enter image description here

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

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.)

$\endgroup$
  • $\begingroup$ Executing your coding I get a long error message $\endgroup$ – eldo Sep 2 '14 at 22:07
  • $\begingroup$ @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.) $\endgroup$ – C. E. Sep 2 '14 at 22:08
  • 1
    $\begingroup$ Now it works (for even n). +1 for using Image :) $\endgroup$ – eldo Sep 2 '14 at 22:15
2
$\begingroup$

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).

$\endgroup$

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