[Community Puzzle] Is the King In Check? (Part 1)

https://www.codingame.com/training/easy/is-the-king-in-check-part-1

Send your feedback or ask for help here!

Created by @DrQuarius,validated by @PaarthThePro,@1337_KD3R and @tuna_in_cream.
If you have any issues, feel free to ping them.

Good clean family fun. Thumbs up!

I recommend adding the tags 2D array and possibly Parsing and loops.

While you certainly could use a 2D array, this puzzle does not require it. Simply noting the positions of the two pieces is enough.

I do think a test should be added that checks for a knight that is across the board from the king (i.e. too far away too capture). My current solution would fail that case because, as the instructions point out, the knight basically hits the spots that the queen cannot (in a 5x5 grid), and all I’m doing when checking the knight is checking to see if the queen can capture from the same position.

Basically, I intentionally left my solution as such to see if it was covered in the validators when I submitted, but it wasn’t.

3 Likes

Thanks for pointing this out… That’s certainly worth modifying to protect against.

Hi,

I have a problem with validator 5, i pass everything but this. Could someone tell me my mistake?

for i in range(8):
    for j,c in enumerate(input()):
        if c=="K":
            King=(i,j//2)
        elif c in "BNRQ":
            Ennemy=c
            pos=(i,j//2)

Check=False

v=(King[0]-pos[0],King[1]-pos[1])

if Ennemy=="B" or Ennemy=="Q":
    if v[0]-v[1]==0 or v[0]+v[1]==0:
        Check=True
elif Ennemy=="R" or Ennemy=="Q":
    if v[0]==0 or v[1]==0:
        Check=True
elif Ennemy=="N":
    if v[0]**2+v[1]**2==5:
        Check=True
if Check:
    print("Check")
else:
    print("No Check")

If Ennemy==“Q”, the code will move on to the first check (diagonal) but will never reach the second check (horizontal/vertical).

You are totally right! Thank you.
By the way, it’s the first time i finally understand the logic between the movement of the knights! They can only move on a exact circle around them, all the squares at a distance sqrt(5)! Does it make the move more logical?

8 x 8 space-separated character rows of a chessboard with only two pieces on the board . Your King and some enemy piece.

Constraints

Only two pieces on the board. Only non-space characters in input are _, one K and one of B/N/R/Q. Always an 8x8 size board, always two pieces.

TEST 5 with no king, only one piece.

:confused:

EDIT : my mistake. There is indeed a king.

You may check the inputs by clicking the button as indicated below. There IS a king.

1 Like

I can pass all the test cases however my code fails validator’s 4 & 5. Any tips for passing these two?

Nothing special really. You’d better create custom cases to test your code and make sure it covers all possible scenarios.

Thanks for the tip :slight_smile: I should remember to do this more. Turns out my knight checks were wrong and i’d left some test code in breaking my queen check

1 Like

Hello, any tips for the validator 2 please? Every others are passed for me :slight_smile:

Just try every possible combination of the positions of B and K, and see in which combinations your code fails to provide the correct answers.

Thanks it was the bishop/queen (diagonaly) case :slight_smile:

Bishop/king actually :wink:

Hello,
The output is binary : check or no check, are we ok with that ?

The contribution guidelines state:

Binary or language-specific outputs should be avoided in CoC.

This is a puzzle, not a clash of code. So, it should be ok.