Chess Board Analyzer
Find the winner (W
or B
) for the given chess board. If there isn't a King in checkmate position output N
.
You have to make several assumptions:
- The given boards are legal and are assuming the official Chess rules
- In every board there is a winner (no draws) or the board is not terminal (the game could be continued)
- An attacked King could be saved only by moving himself to a safe square (not by using another piece from the King's team)
- White pawns are moving upwards, while black pawns are moving downwards
- You may assume that blocking check with other pieces is not allowed
Example board:
........
.......k
........
........
........
......R.
.K.....R
........
In this example the white rooks (uppercase R
letters) are attacking all the squares the black king (lowercase k
letter) could move onto, so the black king is in checkmate position and the white (W
) player wins.
Input & Output
- You are given 8 lines of input. Each line contains 8 characters, which are representing a row on the board to analyze
- Output
W
orB
for the winning player orN
if the game could be continued,W
representing white andB
representing black - You are allowed to take in the input as a list/array of 8 elements
Characters
. - Not occupied square on the board
R - White Rook
N - White Knight
B - White Bishop
Q - White Queen
K - White King
P - White Pawn
r - Black Rook
n - Black Knight
b - Black Bishop
q - Black Queen
k - Black King
p - Black Pawn
Test cases
rnbqkbnr
pppppppp
........
........
........
........
PPPPPPPP
RNBQKBNR
->
N
........
.......k
........
........
........
......R.
.K.....R
........
->
W
......kR
......P.
.....K..
........
........
........
........
........
->
W
........
..k.....
........
........
........
........
.....PPP
..r...K.
->
B
rnb.kbnr
pppp.ppp
........
....p...
......Pq
.....P..
PPPPP..P
RNBQKBNR
->
B
You may assume that there has to be at least 2 pieces on the board (main pieces being the kings)
This is code-golf, so shortest code wins!
Credits to this puzzlehttps://codegolf.stackexchange.com/questions/242986/chess-board-analyzer