For this challenge, you will be writing a program or function which outputs (or prints) (in a reasonable format, i.e single string or an array of lines) a chess board which indicates all the possible moves of a piece given an empty board.

There are 5 chess pieces (for this challenge a pawn may be ignored):

  • Bishop (Moves diagonally any number of squares)
  • Rook (Moves orthogonally any number of squares)
  • Queen (Moves diagonally or orthogonally any number of squares)
  • KNight (Moves in an L shape, i.e. two in one dimension, and one in the other)
  • King (Moves one step orthogonally or diagonally)

Your submission will take in three inputs: the row of the chess piece, the column of the chess piece, and the type of piece (as a character or integer).

Say we call drawBoard(4,4,'N') for the Knight using 0-indexing, the output will be as follows:


where the '.' and 'M' can be replaced accordingly with printable ascii characters of your choice.

Sample outputs as follows for other piece types. (Note all of these are called with input of 4,4 and are 0-indexed).





Sam does not mind 1-based indexing, or even indexing following chess algebraic notation. However, your code must be as small as possible so Sam has enough space to think about his chess with the code next to him.

Sam has also realized that outputs will be accepted regardless of trailing or preceding whitespace (before and after input and each line)

  • \$\begingroup\$ I would advise either changing the os to .s, or changing them to spaces and adding a border. It's kinda unreadable at the moment. \$\endgroup\$ Commented Aug 10, 2016 at 20:46
  • \$\begingroup\$ related codegolf.stackexchange.com/questions/69014/… \$\endgroup\$ Commented Aug 10, 2016 at 20:51
  • \$\begingroup\$ @El'endiaStarman done \$\endgroup\$ Commented Aug 10, 2016 at 20:54
  • \$\begingroup\$ Are spaces to the right of the output ok? \$\endgroup\$ Commented Aug 10, 2016 at 21:02
  • 1
    \$\begingroup\$ @RohanJhunjhunwala I was thinking of using modular aritmetic for the bishops moves but decided against it. For example, in a 64 character string representation of a board (ignoring newlines) with a bishop standing at x=4, y=0, all the squares it can move to have either x%9=4 or x%7=4. Unfortunately this also allows for the bishop to wrap around if he walks of the board, which is not permitted in chess. A solution is to pad the end of each line to, say 16, with spaces (and a newline) and increase to x%15 and x%17 so that no wrapping occurs in the region of interest. But I abandoned the idea. \$\endgroup\$ Commented Aug 10, 2016 at 23:23

5 Answers 5


Ruby, 125

anonymous function, prints to stdout.

RevB: golfed, but a bug fix brought it back to the same length as before.


Prints each of the 64 squares + 8 newlines = 72 characters individually. Relies on e, the square of the Euclidean distance between the current square and the given coordinates for checking king moves and knight moves (and also for printing the piece value z when the Euclidean distance is zero.)

Ungolfed in test program

f=->x,y,z{                         #x,y,character
  72.times{|i|                     #8 rows of (8 chars + newline) = 72 chars
    v=y-i/9;                       #vertical diff between y coord and current square
    u=x-i%=9                       #horizontal diff between x coord and current square. note i%=8
    e=u*u+v*v                      #square of euclidean distance
    $><<(                          #$> is stdout. send to it....
      i<8?                         #if not the newline column,
        e<1?z:                       #if the euclidean distance is 0, print the symbol for the piece, else
        [r=0==v*u,                   #TRUE if v or u is 0 (rook)
        b=u*u==v*v,                  #TRUE if abs(u)==abs(v) (bishop)
        b|r,                         #TRUE if either of the above are true (queen)
        3>e,                         #TRUE if e == 1 or 2 (king)
        5==e                         #TRUE if e == 5 (knight)
        ]["RBQKN"=~/#{z}/]??*:?.:    #select value from array corresponding to piece and print * or . accordingly
      $/                           #if newline column, print a newline


JavaScript (ES6), 137 130 bytes

<div onchange=if(+x.value&&+y.value&&p.value)o.textContent=f(x.value,y.value,p.value)><input id=x type=number placeholder=X><input id=y type=number placeholder=Y><select id=p><option value=>Piece<option value=B>Bishop<option value=K>King<option value=N>Knight<option value=Q>Queen<option value=R>Rook</select><div><pre id=o></pre>

Note: Outputs one leading newline.

Explanation: Builds and scans though the string 100000000100000000100000000100000000100000000100000000100000000100000000. Each 1 indicates a new line where the relative coordinate x is decremented and the relative coordinate z is reset. Each 0 indicates a new square where the relative coordinate z is decremented. (y is reserved to reset z.) Then uses the fact that many of the moves can be categorised by the squares i and j of the relative coordinates (before they were decremented):

  • (initial square) i + j == 0
  • B: i == j
  • K: i + j < 3
  • N: i + j == 5
  • Q: i == j || !i || !j
  • R: !i || !j

Because - is shorter than == it's golfier to compute the negation and invert it later. (This lets me use the cute '.*'[] expression although it's actually the same length as a boring ?: expression.) * is also golfier than &&.

  • \$\begingroup\$ How about using 0 and 1 as we can replace . and M with other ASCII characters? \$\endgroup\$ Commented Dec 21, 2019 at 7:41
  • \$\begingroup\$ @ShieruAsakoto Sorry, I'm not following you; have you commented on the wrong answer or something? \$\endgroup\$
    – Neil
    Commented Dec 21, 2019 at 11:02
  • \$\begingroup\$ Oh sorry, I meant the '.*' part could be omitted and output 0 and 1 in place of . and * \$\endgroup\$ Commented Dec 22, 2019 at 11:49
  • 1
    \$\begingroup\$ @ShieruAsakoto Thanks, I see what you meant now, but I think I probably chose those characters to make the output look nice and I can't see myself switching even though it would save 6 bytes. \$\endgroup\$
    – Neil
    Commented Dec 22, 2019 at 12:27

Java 8 lambda, 473 435 289 characters

Looks like this:

(R,C,f)->{String b="";for(int r=0,c,d,D;r<8;r++){for(c=0;c<8;c++){d=R-r<0?r-R:R-r;D=C-c<0?c-C:C-c;b+=R==r&&C==c?f:((f=='R'||f=='Q')&&(R==r||C==c))||((f=='B'||f=='Q')&&d==D)||(f=='K'&&((R==r&&D==1||C==c&&d==1)||(d==D&&d==1)))||(f=='N'&&(d==2&&D==1||d==1&&D==2))?"M":".";}b+="\n";}return b;}

Or ungolfed into a class:

public class Q89429 {

    static String chessMoves(int row, int column, char figure) {
        String board = "";

        for (int r = 0, c, deltaRow, deltaColumn; r < 8; r++) {
            for (c = 0; c < 8; c++) {
                deltaRow = row - r < 0 ? r - row : row - r;
                deltaColumn = column - c < 0 ? c - column : column - c;
                board += row == r && column == c ?
                        figure :
                        ((figure == 'R' || figure == 'Q') && (row == r || column == c))
                        || ((figure == 'B' || figure == 'Q') && deltaRow == deltaColumn)
                        || (figure == 'K' && (
                                (row == r && deltaColumn == 1 || column == c && deltaRow == 1)
                                || (deltaRow == deltaColumn && deltaRow == 1)))
                        || (figure == 'N' && (deltaRow == 2 && deltaColumn == 1 || deltaRow == 1 && deltaColumn == 2))
                        ? "M" : ".";
            board += "\n";

        return board;

This is a TriFunction. It returns the chess field as a printable String. I wanted to use streams, and it looks quite good. It's like a 2D iteration, may be shorter without the streams. Switched to classic loops and saved a lot!

It can definitely be shortened by using a ternary, I will do that now.


Saved 38 characters by using a ternary.

Saved 146 characters by using good old loops. We should all abandon streams ;)

  • \$\begingroup\$ for loops may be shorter \$\endgroup\$ Commented Aug 10, 2016 at 21:56
  • \$\begingroup\$ @RohanJhunjhunwala I also thought about that. Currently working on the ternary. After that I will switch to loops, just needed some practice in using streams :D \$\endgroup\$
    – Frozn
    Commented Aug 10, 2016 at 22:10
  • \$\begingroup\$ :D (11 more characters) \$\endgroup\$ Commented Aug 10, 2016 at 22:10

Ruby, 209 Bytes

->v,a,b{R=->x,y{x==a||y==b};B=->x,y{x-y==a-b||a-x==y-b};Q=->x,y{R.(x,y)||B.(x,y)};K=->x,y{((x-a).i+y-b).abs<2};N=->x,y{((a-x)*(b-y)).abs==2};(r=(0..7)).map{|x|r.map{|y|[x,y]==[a,b]?v: eval(v+".(x,y)??x:?o")}}}

This is a lambda that makes use of other lambdas, defined inside.


x86 machine code, 114 103 bytes


  • AL - ASCII piece type (N/B/R/Q/K)
  • BP - 0x88 index
  • DI - Buffer


  • DI - Last character + 1

Assembly source code:

    mov di, offset disp_db - 3
    mov cx, 8
    mov bx, cx
    repne scasb
    mov dx, di
    sub dx, offset disp_db - 1
    pop si
    mov di, si

    mov ax, '.'
    mov cl, 8
    rep stosb
    mov al, 9
    mov cl, 7
    rep stosb
    inc ax
    dec bx
    jnz init_loop

    push di
    mov di, si

    mov si, offset moves_knight - 2
    add si, dx
    add si, ax


    mov bx, bp

    add bx, ax
    test bl, 088h
    jnz vec_cont
    mov byte ptr [di + bx], '*'
    test dl, dl
    jp dest_loop

    neg ax
    js sign_loop
    cmp al, 80h
    jnz vec_loop

    mov [di + bp - 128], al

disp_db db "NBKRQ"

moves_knight db vec_knight - moves_knight - 1
moves_bishop db vec_bishop - moves_bishop - 1
moves_king   db vec_king   - moves_king   - 1
moves_rook   db vec_rook   - moves_rook   - 1
moves_queen  db vec_king   - moves_queen  - 1

vec_knight db 0Eh, 12h, 1Fh, 21h, 80h
vec_bishop db 0Fh, 11h, 80h
vec_king   db 0Fh, 11h
vec_rook   db 01h, 10h, 80h

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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