Skip to main content
2 of 2
added 423 characters in body
79037662
  • 3k
  • 5
  • 3

Win a K vs KQ endgame

Summary

The goal of this challenge is to create a program or function which will win a Chess game with a King and Queen against a lone King. The user will specify three squares, representing the locations of the computer's King, the computer's Queen, and the user's King. The computer will then a output a move which will eventually lead to checkmate.

Input/Output

The program or function will first take as input three squares, representing the locations of the computer's King, the computer's Queen, and the user's King (not necessarily in that order). It can be assumed that the input is a legal position.

Parsing input is not the point of this challenge, so all reasonable forms of input/output are allowed, including but not limited to

  • Strings with algebraic chess notation such as "Ke4" or "Qf6"

  • Triples representing pieces and coordinates such as ('K', 0, 2)

After three squares are taken as input, the computer outputs a single legal move. Behaviour on invalid input is undefined.

Requirements

This procedure must terminate using your program or function:

  • User sets up a legal KQ vs K position on a physical chessboard.

  • User inputs the board position. The computer outputs a legal move. If the move is a checkmate, STOP.

  • User makes the computer's move on the physical board.

  • User makes a legal move for the lone king on the physical board.

  • User goes to step 2 and repeats.

In other words, the computer must eventually win by checkmate, through repeatedly using your program or function.

Furthermore, from any legal starting position the checkmate must occur in 50 or fewer moves by the computer.

(Of course, a physical chessboard is in no way necessary to test the code; I only mentioned it to help visualize the procedure. The chessboard could just as well be visualized in the user's head.)

Possible test cases

The squares are given in the order: computer's Queen, computer's King, user's King

  • c2, h8, a1 (must avoid stalemate)
  • a1, a2, a8
  • a8, a1, e5

Rules

  • The checkmate must occur in 50 or fewer moves by the computer, but it does not need to be as fast as possible.
  • Chess libraries are not permitted.
  • Shortest program in each language (in bytes) wins.

Questions for sandbox

How can I word the rules better if they are not currently sufficiently clear?

79037662
  • 3k
  • 5
  • 3