Chessboards

Here’s a chessboard. Each square is 4×4 characters

    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX

What’s the most Emacsy way of producing the above?

First Line

Let’s start with the first line

XXXX    XXXX    XXXX    XXXX

You could do the following: 

C-4 <space> 4 Spaces
C-4 X       4 Xs
C-a         Jump to start of line
C-k         Kill line
C-y         Yank
C-x z z z   Repeat last command three times

Another way is to use a macro:

F3          Start recording
C-4 <space> 
C-4 X       
F4          Stop recording
F4 F4 F4    Run the macro three times

Once you have one line, you could copy it and then yank it three times to get the first line of squares.

Here’s another way to get a line of squares, this time using rectangles. See this post to remind yourself about rectangles.

Start with just the black squares:

C-16 X 
Kill and yank to get the following:

XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX

Now insert the white squares:

Go to start of the pattern

Set the region to cover the first square
C-x r o to insert blank space to fill the space of the region-rectangle

    XXXXXXXXXXXXXXXX
    XXXXXXXXXXXXXXXX
    XXXXXXXXXXXXXXXX
    XXXXXXXXXXXXXXXX

Now move the point forward 8 so its at the correct position to open the next square

C-8 C-f

You can record a macro of the above and then run it 3 times. Don’t forget to add the C-8 C-f at the end to move the point to the correct starting position.

2 The Second Line

Once you have the first line of squares, the second is quite easy. Copy one line of squares beneath itself to get the following:

    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX

And then use C-x r k to kill the white square at the start of the second line.

    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX

Now you can just kill and yank four times to get the complete chessboard.

Of course, you could just do it in LISP:

(defun one-line(pattern)
   "Insert PATTERN 4 times"
   (dotimes (count 4)
     (insert pattern)))

 (defun four-lines(pattern)
   "Insert 4 lines of PATTERN"
   (dotimes (count 4)
     (one-line pattern)
     (insert "\n")))

 (defun chess-board()
   (interactive)
   (dotimes (count 4)
     (four-lines "    XXXX")
     (four-lines "XXXX    ")))

M-x chess-board

Can you think of a more efficient method than the ones above? Why not post it below?

2 Comments

  1. Omer says:

    I think, copying first 22 squares of the chessboard and then pasting repeatedly as rectangles to produce 88 board is a better way.

    You can produce 2*2 square by cua-rectangle-mark-mode.

    First produce 12 of the block (the upper
    half) (it is 4
    8 as chars)
    ****XXXX
    ****XXXX
    ****XXXX
    ****XXXX

    Then the lower half
    XXXX****
    XXXX****
    XXXX****
    XXXX****

    Then we got 22 chess board, now we can copy and paste it repeatedly. First make a 28 board. Then copy the 28 to produce a 88 chess board.

    Like

    1. Tony says:

      Nice suggestion. Thanks, Omer!

      Like

Leave a reply to Tony Cancel reply