Beta

Mate with King & Queen

Description:

Task:

Checkmate the opponent's king using a king and queen.

Input:

Three strings representing the locations of your king, your queen and the opponent's king (in that order). There are no other pieces on the board. The position is guaranteed to be legal, and the opponent's king will not be in check. It is your move.

Squares are indicated using algebraic notation, where "a1" is the bottom-left corner of the board and "h8" is the top-right.

Example: The input "e5", "b2", "d7" represents the following position:

8 ........
7 ...k....
6 ........
5 ....K...
4 ........
3 ........
2 .Q......
1 ........               
  abcdefgh

Output:

The move you want to play, a string of 3 characters. The first character shows the piece moved, either "K" or "Q". The remaining 2 characters identify the square to move it.

For example, in the position above the move "Qb7"leads to the following position, checking (but not mating) the opponent's king:

8 ........
7 .Q.k....
6 ........
5 ....K...
4 ........
3 ........
2 ........
1 ........               
  abcdefgh

Testing:

First your function is called on the initial position. After its output is received, the testing framework makes a move for the opponent's king. It then calls your function on the updated position to get your next move. This process continues until the opponent's king is checkmated. Your function does not need to determine if mate occurs - the testing framework does this.

The function fails if any of the following occur:

  1. It makes a illegal move.
  2. Its move leaves the king in stalemate i.e. has no legal moves but is not in check.
  3. Its move allows the king to capture your queen. The opponent will do so!
  4. 50 moves elapse without the king being checkmated.

Example:

Here is a sequence of moves from the first position above resulting in checkmate. The move numbers are not part of the kata requirement - they just make the moves easier to read.

1.Qb8 ke7 2.Qc8 kf7 3.Qd8 kg7 4.Qe8 kh7 5.Kf6 kh6 6.Qg6 is the following checkmate:

8 ........
7 ........
6 .....KQk
5 ........
4 ........
3 ........
2 ........
1 ........               
  abcdefgh

This kata was inspired by Valid Checkmate.

Also related are Check and Mate? and Is the King in check ?

For a much harder checkmating problem, break your brain on Chess - checkmate with rook in 16 moves

To program a problem-solving agent for a non-chess puzzle, try Play Nerdle - It's Wordle for Calculations.

Games
Strings
Algorithms

More By Author:

Check out these other kata created by brodiemark

Stats:

CreatedMay 31, 2024
PublishedMay 31, 2024
Warriors Trained14
Total Skips0
Total Code Submissions12
Total Times Completed4
Python Completions4
Total Stars2
% of votes with a positive feedback rating100% of 1
Total "Very Satisfied" Votes1
Total "Somewhat Satisfied" Votes0
Total "Not Satisfied" Votes0
Total Rank Assessments1
Average Assessed Rank
5 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
5 kyu
Ad
Contributors
  • brodiemark Avatar
Ad