Setup
Implement a function that takes for input an 8x8 chessboard in the form of a bi-dimensional array. It should return true
if the black king is in check or false
if it is not.
The array will include 64 squares which can contain the following characters:
♔ for the black King; ♛ for a white Queen; ♝ for a white Bishop; ♞ for a white Knight; ♜ for a white Rook; ♟ for a white Pawn; a space if there is no piece on that square.
The board is oriented from Black's perspective. There will always be only one king (yours), all the other pieces will be white. Remember line of sight and attack patterns of chess pieces. Input will always be valid.
Examples
Check by Queen
chessboard=[[' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ','♟',' ',' ',' ',' '], [' ',' ','♔',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ']];
kingIsInCheck(chessboard)
=> true
Check by Bishop
chessboard=[[' ',' ',' ',' ',' ',' ',' ','♝'], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], ['♔',' ',' ',' ',' ',' ',' ',' ']];
kingIsInCheck(chessboard)
=> true
Tests
Check by Rook
chessboard=[[' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ','♔',' ',' ','♜',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ']];
Check by Knight
chessboard=[[' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ','♔',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], ['♞',' ',' ',' ',' ',' ',' ',' ']];
King Alone
chessboard=[[' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ','♔',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ']];
Good luck!
This challenge comes from trashy_incel on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (1)
In Python.
(a) I didn't want to write a complete set of unit tests, so maybe there is a typo-style logic bug in here... but you get the idea.
(b) I changed the input to make it more readable when writing tests.