Help Sam understand chess

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]

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


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 &&.