Problem 178013 · hard · Phase 01 Prerequisites & Setup

The Lone King

conditionals · loops · functions · strings

A chess board has columns a-h and rows 1-8; a square is written like "e4". Only three pieces are on the board: a white king, a white queen and a black king. It is black's turn. Write lone_king(wk, wq, bk) that takes the three squares and returns a verdict.

Rules you need:

  • A king attacks the up to 8 squares next to it (sideways, up/down or diagonally).
  • The queen attacks along its row, its column and both diagonals, any distance, but a piece standing in the way blocks everything behind it.
  • The black king may step to any neighbouring square on the board that is not attacked by a white piece. It may capture the queen by stepping onto her square if the white king does not attack that square. It can never step next to the white king.
  • Squares behind the black king on the queen's line are still attacked once the king steps there: the black king does not shield itself.

Verdicts:

  • "invalid": two pieces share a square, or the two kings are next to each other.
  • "checkmate": the black king is attacked by the queen and has no legal move.
  • "check": it is attacked and has at least one legal move.
  • "stalemate": it is not attacked and has no legal move.
  • "safe": it is not attacked and has at least one legal move.

Examples

Input:  wk = "f6", wq = "g7", bk = "h8"
Output: "checkmate"

Input:  wk = "a1", wq = "d1", bk = "d8"
Output: "check"

Input:  wk = "f7", wq = "g6", bk = "h8"
Output: "stalemate"

Constraints

  • each square is a lowercase letter a-h followed by a digit 1-8

Goals

  • Turn board squares into row and column numbers
  • Decide whether a line of attack is blocked
  • Combine several yes/no facts into one of five verdicts
Starting Python…