28
2
Jack and Jane decided to play a game of chess to while away time. Unfortunately, Jack is rather bad at visualizing. He finds it tough to figure the moves possible for a given piece other than a pawn, of course!
Your challenge is to help find Jack the possible options for a given piece (other than a pawn).
In case one has forgotten, the various pieces are denoted by:
- K: King
- Q: Queen
- N: Knight
- B: Bishop
- R: Rook
As an example, in the following image the Knight is located at d4
and can move to c2
, b3
, b5
, c6
, e6
, f5
, f3
, e2
. For a given input:
Nd4
you would produce:
Nc2 Nb3 Nb5 Nc6 Ne6 Nf5 Nf3 Ne2
Rules:
- The order of the output doesn't matter as long as all the possible moves are listed
- The possible moves can be separated by whitespaces, newlines or any other delimiter
- The input can be passed to the program as a parameter or via
STDIN
- Whitespace in the program shall be counted, so make optimum use of it
This is code golf. (Please avoid making use of any tools/utilities specifically designed for the purpose.) The shortest answer wins!
1I believe this would work just fine as a code golf – John Dvorak – 2014-03-29T07:04:08.317
@JanDvorak I was bit confused about that. Agree with you. – devnull – 2014-03-29T07:04:53.183
3Code golf is the better choice. The obvious is always forgotten: I understand that we can submit either a function or a program, and that input/output can be either stdin/stout or parameters/return value. I'm thinking recursion might be useful here for the queen:
f(x)... case "Q": {f("B");f("R")}
If the function requires any #includes these should be part of the byte count. – Level River St – 2014-03-29T08:38:51.1604The font in that graphic. xD – cjfaure – 2014-03-29T09:07:18.030
1Do the possible moves have to be separated by spaces or are newlines OK as well? – Dennis – 2014-03-29T22:31:36.583
1the legal moves for a pawn are more convoluted than any other piece (en passant, diagonal capturing, and the 2-square initial move). so i assume jack also has the castling rules memorized? – ardnew – 2014-03-31T17:07:40.060
@ardnew The complexity involved with pawn movements was the precise reason for excluding that. Moreover, keeping that would have implied keeping track of color and direction too. (What's Jack? You mean rook, don't you?) – devnull – 2014-03-31T17:09:47.950
jack is the chap with poor visualization skills! and you're right- it wouldn't be possible to identify all valid moves without a full board and some limited move history either. so we should ignore all special moves, correct? – ardnew – 2014-03-31T17:21:04.097
@ardnew You are right. Making the problem overly complicated doesn't seem like a good code-golf problem. Feeding a chess board as input wouldn't have made much sense. – devnull – 2014-03-31T21:19:01.387