Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For some reason I need to create a chess board. I can create a chess board by following command:

  EdgeForm[Thick], Black, Rectangle[{0, 0}],
  EdgeForm[Thick], White, Rectangle[{1, 0}],
  EdgeForm[Thick], , Black, Rectangle[{2, 0}],

  EdgeForm[Thick], White, Rectangle[{0, 1}],
  EdgeForm[Thick], Black, Rectangle[{1, 1}],
  EdgeForm[Thick], White, Rectangle[{2, 1}],

  EdgeForm[Thick], Black, Rectangle[{0, 2}],
  EdgeForm[Thick], White, Rectangle[{1, 2}],
  EdgeForm[Thick], , Black, Rectangle[{2, 2}],


Problem is it has to be resizable (Manipulate command). And I should put the numbers for columns and rows. I am going to give one parameter such as board size. The code will generate the board. I believe there is a code for that, but I couldn't find on the internet. Thanks for the help.

This is an example, chess board.

Example chess board

share|improve this question
Welcome! The reason is not by any chance homework? Then you should add the homework or assignment tag. – Yves Klett May 7 '14 at 18:53
up vote 16 down vote accepted
Manipulate[MatrixPlot[Table[Mod[i + j, 2], {i, 1, n}, {j, 1, n}], ColorFunction -> "Monochrome"], {{n, 8}, 1, 20}]

enter image description here

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:

 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}]
share|improve this answer
Thank you so much for the code. :) Now, I need to put queen pictures some of the squares. Is there any quick way to do it? – forumcash May 7 '14 at 20:25
You could use Show[] – nickjamesuk May 7 '14 at 20:28
@user14114 \[WhiteQueen] and \[BlackQueen] are the characters you need. – Sjoerd C. de Vries May 7 '14 at 20:59
This answer doesn't quite do what O.P. asked/showed: still need letters rather than numbers to label columns; index origin for vertical labels to be at bottom; and squares gray instead of black (so that chess pieces would show when on the dark squares). – murray Sep 2 '14 at 14:47
@murray The OP states, "And I should put the numbers for columns and rows." And this answer was accepted. The image the OP used as an example is taken from the web, I would guess, and does not represent a strict requirement. – Michael E2 Sep 2 '14 at 15:31

For even n:

 ArrayPad[DiagonalMatrix[{1, 1}], 3, "Reflected"],
 PlotTheme -> "Monochrome"]

enter image description here

share|improve this answer
Clever use of ArrayPad! – Simon Woods Sep 2 '14 at 19:31
@eldo,Can your solution extend to other integer like 5,6,9 etc? – Shutao TANG Sep 12 '14 at 6:56
@ShutaoTANG I tried some alternative approaches where Method 2 uses ArrayPad while Method 1 tries to be as succint as possible, Method 1 has a similar issue as with eldo such that the colour traversal by one due to different oddity. Method 2 does not have this issue. – hhh Sep 26 '15 at 23:14

My answer:

cb[n_Integer /; n > 0] := MatrixPlot@SparseArray[{i_, j_} :> Mod[i + j, 2], {n, n}]


Mathematica graphics

For those who desire a more traditional board:

Block[{n = 8},
  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.

share|improve this answer
You should of course notice that in chess one uses different labels. – Alexey Bobrick Sep 2 '14 at 15:12
@AlexeyBobrick I did notice. But I also noticed the accepted answer. Which does the OP want? What is the standard chess labelling for a 30 x 30 board? – Michael E2 Sep 2 '14 at 15:19
Well, I don't know, but a chess board is a chess board:) In the question example it is ok. – Alexey Bobrick Sep 2 '14 at 15:31

My solution

ChessBoard[n_?IntegerQ] := MatrixPlot[
 {Flatten@Table[{1, 0}, {n}], 
 RotateLeft[Flatten@Table[{1, 0}, {n}], 1]}, {n}], 1],
 ColorFunction -> "Monochrome"]

Manipulate[ChessBoard[n], {n, 2, 5, 1}]

enter image description here

share|improve this answer

This is one of those problems that has many solutions:

cb[n_] := MatrixPlot[
  Range@ConstantArray[n, n] + Range[n],
  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]


Mathematica graphics

Other ways to generate the matrix:

Apply[Plus, Outer[List, Range[n], Range[n]], {2}],
Total[Outer[List, Range[n], Range[n]], {-1}],
share|improve this answer

Yet another solution, inclusive labeling.

 MatrixPlot[Table[If[EvenQ[i + j], 1, 0], {i, 1, n}, {j, 1, n}], 
  FrameTicks -> Transpose[{#, {#, CharacterRange["A", "Z"][[#]]}} & /@ Range[n]],
  PlotTheme -> "Monochrome"], {{n, 8}, 1, 26}]

enter image description here

share|improve this answer

Method 1. 63 chars (and 39 without beautifying to black-white)

This considers the system as graph traversal where the number of steps is the sum and each step you wear different suit.

63 chars

Method 2. 87 chars with Array manipulation (63)

I remixed Eldo's idea about reflection, better idea is probably to find a command to repeat a pattern like "repeat 10 N amount of times and fill the array with the content"

enter image description here

and the good side of this method is that you can very easily get different kinds of boards by changing oddity

enter image description here

and to be enough enterntaining: Method 2 results into jail stripes with even number :)

enter image description here

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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