Chess Light Solver

4

1

Challenge

Write a solver for the Chess Light puzzle game, in the smallest amount of code.

The input will be a representation of a chessboard, how in yours is up to you. My examples below is textual, in which some squares may already be occupied by black peices. Some of the unoccupied squares are designated with a #, along with a list of white men to be placed. The goal is to place those men within the designated squares in such a way that every designated square is lit.

A chess piece lights up the squares which it attacks, including those occupied by men of the same colour but not those which have an interposing man, and not the one which it occupies itself. For example queen diagnol attack stops at the square occuppied by another peice and no further.

E.g. with the following board, the * show the squares which are lit. Q's square is unlit, but Q lights R's square. The squares labelled - would be lit except that R is between them and Q.

+--------+
|*    * -|
|*    *- |
|*****R**|
|*   **  |
|*  * *  |
|* *  *  |
|**   *  |
|Q*******|
+--------+

There is one important exception: a chess piece under attack by a man of the opposite colour doesn't light up any squares, it still block the light of others passing thru.

Note that pawns light up the following squares.

White Pawn

 * *
  P

Black Pawn

  p
 * *

Thus there is no en passant.

  • White is playing up the board.
  • Only one man is allowed on a square.
  • All pieces assigned to be placed must be placed on the board
  • An attacked piece, even if defended, is still attacked, but the defender does light up that square.
  • White's men are denoted with capital letters and black's with lower-case letters: KQRNBP and kqrnbp, where Nn are kNights.

The output (of mine) consists of a list of squares in the same order as the men listed in the input, using algebraic notation. Yours is up to you. * There could be no answer!


Examples

Beginner (1)

+--------+
|        |
|  #     |
|  #     |
|  #  #  |
| ###### |
|  #  #  |
|     #  |
|        |
+--------+
RR 

RR are the pieces to be placed on the board.

Solution C4, F4

Class B (19)

+--------+
|        |
|  r     |
|        |
|  ### b |
|  ###   |
|  ###   |
|  ###   |
|        |
+--------+
PPPK 

PPPK are the pieces to be placed on the board. Solution D4, E4, E2, D3

Since the output is in the same order as the men, the white king is placed on d3 and the other three squares receive white pawns.

Adam Speight

Posted 2015-01-26T19:47:32.470

Reputation: 1 234

Must all pieces be placed in a valid solution? Can pawns move in both directions or only one? – feersum – 2015-01-26T19:56:16.677

@MartinBüttner Updated the challenge – Adam Speight – 2015-01-26T20:11:42.523

Its still very unclear. What is the significance of lighting up * from the first example ? Can we place the pieces on any # . If yes, what challenge is it ? – Optimizer – 2015-01-26T20:14:33.053

@Optimizer It's not a example board but showing the blocking of the "light" by another piece on the board. White's pieces can only be placed on # squared. – Adam Speight – 2015-01-26T20:22:43.127

@Optimizer The challenge of the challenge is when placing a piece on the board, it affects what is already being lit by the other pieces already on the board. – Adam Speight – 2015-01-26T20:28:29.820

1I've rewritten to put the goal at the start, so that the first half of the question has context. But there are some things which I'm unsure about. When you say that all of White's men must be placed on the board, is there an implicit statement that the men to place will only be white? Do you require the input to have the border, or can answers dispense with that? What choice of delimiters is there for the output? What about case sensitivity in the output? (Lower-case is more common than capitals in algebraic notation, but you use capitals in your examples). – Peter Taylor – 2015-01-26T22:57:41.313

1An important one: do your two examples cover all of the corner cases (e.g. attacked men not lighting anything up, men not lighting their own squares)? And what should the output be if there is no solution, or may answers assume that this will never be the case? – Peter Taylor – 2015-01-26T22:59:52.653

If a man is threatened by the king, but defended by another man, does that count as being attacked? – Martin Ender – 2015-01-26T23:18:46.760

@MartinBüttner yes. – Adam Speight – 2015-01-27T00:38:44.983

@AdamSpeight I'm going to vote to reopen, but it would be great if you could mention that case specifically in the challenge spec. – Martin Ender – 2015-01-27T00:40:29.363

@PeterTaylor representation of input and output is user defined. Never assume a solution, but puzzles i've seen do. – Adam Speight – 2015-01-27T00:43:48.423

No answers