I'm currently working on a program that tries to produce a crossword puzzle. I've seen various examples online that use some sort of board such that we can assign values to specific coordinates. Is there a way to create said board in Mathematica? I'm thinking of a code like this

baseGrid = 
   Table[{FromCharacterCode[i], j}, {i, 97, 106}], {j, 1, 9}], 
  Frame -> All, Alignment -> Center, ItemSize -> All, 
  Spacings -> {0.5, 2}]

However, it is quite inefficient and messy when it comes to actually manipulating the "board". Is there a function or something in Mathematica to achieve this "board"? Also, how do I implement a coordinate system?

[EDITED]I wish to create a crossword puzzle generator in Mathematica, which it is able to assign x and y coordinates to each letter in the crossword and be able to summon such letter whenever i type in the coordinates. Is there a function or method I can implement such coordinate system?

I've read the Mathematica chessboard article already but I feel that it doesn't really suit my needs. How to make a resizable chess board?

  • 3
    $\begingroup$ It might be best if you add further information. For example what do you mean by "manipulating the board?" Maybe hand draw what you have in mind and post? $\endgroup$ Commented Feb 1, 2018 at 3:53
  • 1
    $\begingroup$ I don't know what "chessboard article" you are referring to, I would appreciate if you made that a link in the question. $\endgroup$
    – C. E.
    Commented Feb 1, 2018 at 13:17
  • $\begingroup$ Re. your update: You have to tell us what you want to do with your data. We get that you have some kind of board. But that can be represented in a lot of different ways. How it should be represented depends on what you want to do with the board. Look at my answer below, what are the operations that correspond to movePiece, removePiece etc.? "Insert word", "does word fit at this location"? What you've already mentioned can be accomplished by just putting the characters into a matrix and using Part... $\endgroup$
    – C. E.
    Commented Feb 2, 2018 at 19:45

1 Answer 1


Here's a suggestion of how it can be done. We start by getting the graphics that we shall need:

dark = RGBColor[0.8196, 0.5451, 0.2784];
light = RGBColor[1, 0.8078, 0.6196];
sprite = Import["https://upload.wikimedia.org/wikipedia/commons/thumb/b/b2/Chess_Pieces_Sprite.svg/640px-Chess_Pieces_Sprite.svg.png"];

  {wking, wqueen, wbishop, wknight, wtower, wpawn},
  {bking, bqueen, bbishop, bknight, btower, bpawn}
} = ImagePartition[sprite, {106.5, 106.5}];

This defines (with inspiration from my previous answer here) the colors of the chessboard and images representing the different chess pieces.

We now create "objects" (as in object-oriented programming; expressions that encapsulate information) corresponding to the various pieces. At this point we only care about the position, image, and type of each piece:

wpawns = Table[pawn[wpawn, {i, 2}], {i, 8}];
bpawns = Table[pawn[bpawn, {i, 7}], {i, 8}];
towers = {tower[wtower, {1, 1}], tower[wtower, {8, 1}], tower[btower, {1, 8}], tower[btower, {8, 8}]};
knights = {knight[wknight, {2, 1}], knight[wknight, {7, 1}], knight[bknight, {2, 8}], knight[bknight, {7, 8}]};
bishops = {tower[wbishop, {3, 1}], tower[wbishop, {6, 1}], tower[bbishop, {3, 8}], tower[bbishop, {6, 8}]};
queens = {queen[wqueen, {5, 1}], queen[bqueen, {5, 8}]};
kings = {king[wking, {4, 1}], king[bking, {4, 8}]};

pieces = Join[wpawns, bpawns, towers, knights, bishops, queens, kings];

With all this information gathered in a convenient format, drawing the board is not too complicated:

drawPiece[_[sprite_, {i_, j_}]] := Inset[sprite, {i - 1, j - 1}, {i - 1, j - 1}, 1]

range = Partition[Range[64], 8];
range = MapAt[Boole[EvenQ[#]] &, range, 1 ;; 8 ;; 2];
range = MapAt[Boole[OddQ[#]] &, range, 2 ;; 8 ;; 2];

drawBoard[pieces_] := ArrayPlot[
  ColorRules -> {0 -> light, 1 -> dark},
  Epilog -> drawPiece /@ pieces


Mathematica graphics

Now we'd like to do various operations with the pieces. One operation that we need to do is to move a piece, this is not too hard either:

movePiece[pieces_, {i_, j_}, {i2_, j2_}] := pieces /. h_[sprite_, {i, j}] :> h[sprite, {i2, j2}]

gameState = pieces;
gameState = movePiece[gameState, {5, 2}, {5, 4}];
gameState = movePiece[gameState, {4, 7}, {4, 5}];

Mathematica graphics

Another operation that we need is to remove a piece:

removePiece[pieces_, {i_, j_}] := DeleteCases[pieces, _[_, {i, j}]]

gameState = removePiece[gameState, {4, 5}];
gameState = movePiece[gameState, {5, 4}, {4, 5}];

Mathematica graphics

Finally, to implement the logic of the game we might need to check if certain locations on the board are occupied. We can do this as follows:

isOccupied[pieces_, {i_, j_}] := MemberQ[pieces, _[_, {i, j}]]

Echo@isOccupied[gameState, {5, 1}];
gameState = movePiece[gameState, {5, 1}, {5, 2}];
Echo@isOccupied[gameState, {5, 1}];


These were just some examples of board manipulations that can be easily implemented. It demonstrates that with the right way of storing the state of the game (i.e. storing data about pieces), things shouldn't have to be difficult.

I pontificated on how to make another board game here.


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