6
\$\begingroup\$

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 indicate 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 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') output as follows

........
........
...M.M..
..M...M.
....N...
..M...M.
...M.M..
........

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 (zero indexed).

King:

........
........
........
...mmm..
...mKm..
...mmm..
........
........

Rook:

....m...
....m...
....m...
....m...
mmmmRmmm
....m...
....m...
....m...

Sam does not mind 1 based indexing, or even indexing follow 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 accpeted regardless of trailing or preceding whitespace (before and after input and each line)

\$\endgroup\$
  • \$\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\$ – El'endia Starman Aug 10 '16 at 20:46
  • \$\begingroup\$ related codegolf.stackexchange.com/questions/69014/… \$\endgroup\$ – Rohan Jhunjhunwala Aug 10 '16 at 20:51
  • \$\begingroup\$ @El'endiaStarman done \$\endgroup\$ – Rohan Jhunjhunwala Aug 10 '16 at 20:54
  • \$\begingroup\$ Are spaces to the right of the output ok? \$\endgroup\$ – Level River St Aug 10 '16 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\$ – Level River St Aug 10 '16 at 23:23
4
\$\begingroup\$

Ruby, 125

anonymous function, prints to stdout.

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

->x,y,z{72.times{|i|v=y-i/9;u=x-i%=9
e=u*u+v*v
$><<(i<8?e<1?z:[r=0==v*u,b=u*u==v*v,b|r,3>e,5==e]["RBQKN"=~/#{z}/]??*:?.:$/)}}

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
    )
  }
}

x=gets.to_i
y=gets.to_i
z=gets.chomp
f[x,y,z]
\$\endgroup\$
3
\$\begingroup\$

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.

Updates

Saved 38 characters by using a ternary.

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

\$\endgroup\$
  • \$\begingroup\$ for loops may be shorter \$\endgroup\$ – Rohan Jhunjhunwala Aug 10 '16 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 Aug 10 '16 at 22:10
  • \$\begingroup\$ :D (11 more characters) \$\endgroup\$ – Rohan Jhunjhunwala Aug 10 '16 at 22:10
2
\$\begingroup\$

JavaScript (ES6), 137 130 bytes

f=
(x,y,p)=>`${1e8}`.repeat(8).replace(/./g,c=>+c?(i=x*x--,z=y,`
`):i+(j=z*z--)?`.*`[+!{K:i+j>3,N:i+j-5,R:i*j,B:b=i-j,Q:b*i*j}[p]]:p)
;
<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 &&.

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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