DEV Community

dev.to staff
dev.to staff

Posted on

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!

Top comments (1)

Collapse
 
candidateplanet profile image
lusen / they / them πŸ³οΈβ€πŸŒˆπŸ₯‘

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.

KING = 'β™”'
QUEEN = 'β™›'
BISHOP = '♝'
KNIGHT = 'β™ž'
ROOK = 'β™œ'
PAWN = 'β™Ÿ'
EMPTY = '.'

class Piece(object):
  def __init__(self, row, col):
    self.row = row
    self.col = col

def find_piece(board, symbol):
  for r in range(0, len(board)):
    for c in range(0, len(board)):
      if board[r][c] == symbol:
        return Piece(r, c)
  return None

def is_check(board):
  king = find_piece(board, KING)
  for r in range(0, len(board)):
    for c in range(0, len(board)):
      if board[r][c] == QUEEN:
        if diagonal_attack(board, Piece(r,c), king):
          return True
        if compass_attack(board, Piece(r,c), king):
          return True
      elif board[r][c] == BISHOP:
        if diagonal_attack(board, Piece(r,c), king):
          return True
      elif board[r][c] == KNIGHT:
        if knight_attack(board, Piece(r, c), king):
          return True
      elif board[r][c] == ROOK:
        if compass_attack(board, Piece(r, c), king):
          return True
      elif board[r][c] == PAWN:
        if pawn_attack(board, Piece(r, c), king):
          return True
  return False

def diagonal_attack(board, attacker, king):
  for row_incr, col_incr in [(1,1), (1,-1), (-1,1), (-1,-1)]:
    r = attacker.row+row_incr
    c = attacker.col+col_incr
    while 0 <= r < len(board) and 0 <= c < len(board):
      if r == king.row and c == king.col:
        return True
      elif board[r][c] != EMPTY:
        break
      r += row_incr
      c += col_incr
  return False

def compass_attack(board, attacker, king):
  for row_incr, col_incr in [(0,1), (0,-1), (1,0), (-1,0)]:
    r = attacker.row+row_incr
    c = attacker.col+col_incr
    while 0 <= r < len(board) and 0 <= c < len(board):
      if r == king.row and c == king.col:
        return True
      elif board[r][c] != EMPTY:
        break
      r += row_incr
      c += col_incr
  return False

def knight_attack(board, attacker, king):
  for row_incr, col_incr in [
    ( 1, 2),( 1,-2),
    (-1, 2),(-1,-2),
    ( 2, 1),( 2,-1),
    (-2, 1),(-2,-1)]:
    r = attacker.row+row_incr
    c = attacker.col+col_incr
    if r == king.row and c == king.col:
      return True
  return False

def pawn_attack(board, attacker, king):
  if attacker.row+1 == king.row:
    if attacker.col+1 == king.col or attacker.col-1 == king.col:
      return True
  return False

print(is_check([
  '........',
  '........',
  '........',
  '...β™›....',
  '........',
  '.β™”......',
  '........',
  '........']), True)

print(is_check([
  '........',
  '........',
  '........',
  '........',
  '...β™›....',
  '.β™”......',
  '........',
  '........']), False)

print(is_check([
  '........',
  '........',
  '........',
  '........',
  '...♝....',
  '.β™”......',
  '........',
  '...β™œ....']), False)

print(is_check([
  '........',
  '........',
  '........',
  '........',
  '........',
  '.β™”.....β™œ',
  '........',
  '........']), True)

print(is_check([
  '..β™ž..β™ž..',
  '.......β™ž',
  '........',
  '........',
  '........',
  '.β™”......',
  '........',
  '........']), False)

print(is_check([
  '........',
  '........',
  '........',
  '........',
  '...β™ž....',
  '.β™”......',
  '........',
  '........']), True)

print(is_check([
  '........',
  '........',
  '........',
  '........',
  '........',
  '.β™”.....β™Ÿ',
  '........',
  '........']), False)

print(is_check([
  '........',
  '........',
  '........',
  '........',
  'β™Ÿ.......',
  '.β™”......',
  '........',
  '........']), True)

print(is_check([
  '........',
  '........',
  '........',
  '........',
  '...β™Ÿ....',
  '..β™”.....',
  '........',
  '........']), True)

print(is_check([
  '.......♝',
  '........',
  '........',
  '........',
  '........',
  '........',
  '........',
  'β™”.......']), True);

print(is_check([
  '........',
  '........',
  '........',
  '........',
  '........',
  '.β™”......',
  '........',
  'β™ž.......']), True);

print(is_check([
  '........',
  '........',
  '........',
  '........',
  '...β™”....',
  '........',
  '........',
  '........']), False);