DEV Community

dev.to staff
dev.to staff

Posted on

3 1

Daily Challenge #181 - Is King in Check?

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!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (1)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay