Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

Graphics[{
  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
2  
Welcome! The reason is not by any chance homework? Then you should add the homework or assignment tag. –  Yves Klett May 7 at 18:53

6 Answers 6

up vote 13 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:

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}]
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 at 20:25
    
You could use Show[] –  nickjamesuk May 7 at 20:28
3  
@user14114 \[WhiteQueen] and \[BlackQueen] are the characters you need. –  Sjoerd C. de Vries May 7 at 20:59
1  
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 at 14:47
1  
@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 at 15:31

My answer:

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

cb[8]

Mathematica graphics


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.

share|improve this answer
    
You should of course notice that in chess one uses different labels. –  Alexey Bobrick Sep 2 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 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 at 15:31

For even n:

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

enter image description here

share|improve this answer
1  
Clever use of ArrayPad! –  Simon Woods Sep 2 at 19:31
    
@eldo,Can your solution extend to other integer like 5,6,9 etc? –  Tangshutao Sep 12 at 6:56

My solution

ChessBoard[n_?IntegerQ] := MatrixPlot[
Flatten[
 Table[
 {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

Yet another solution, inclusive labeling.

Manipulate[
 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

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]

cb[8]

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

Your Answer

 
discard

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.