Check and Mate?
Description:
Task
In this kata, you should implement two chess functions: isCheck
and isMate
.
Both functions have two parameters - player
signifies whose turn it is (0 denotes white and 1 black), and pieces
is an list or array of objects (PHP: Array of associative arrays), describing the pieces on the board.
isCheck
should return an list or array of the pieces (in any order) that threaten player
's king, or false
if the king is not threatened (Java: return a Set<Piece>
object of the threatening pieces, or an empty set if none are found).
(PHP: return an array of associative arrays, false if none are found.)
isMate
should return true
if player
can't make a move that takes their king out of check, and false
if they can make such a move, or if the position is not check.
Details
Each piece in the input array is specified in the following way:
{
piece: string, // pawn, rook, knight, bishop, queen or king
owner: int, // 0 for white or 1 for black
x: int, // 0-7 where 0 is the leftmost column (or "A")
y: int, // 0-7 where 0 is the top row (or "8" in the board below)
prevX: int, // 0-7, presents this piece's previous x, only given if this is the piece that was just moved
prevY: int // 0-7, presents this piece's previous y, only given if this is the piece that was just moved
}
Black's pieces' starting positions are at the top of the board ('y'
equals 0 and 1). The bottom ('y'
equals 6 and 7) is white's home. The initial position is shown below. The numbers in parentheses across the top correspond to the 'x'
coordinate and those along the left to the 'y'
coordinate. For example, the pawn in front of the white king is denoted by {'piece': "pawn", 'owner': 0, 'x': 4, 'y': 6}
.
(0) a | (1) b | (2) c | (3) d | (4) e | (5) f | (6) g | (7) h | |
(0) 8 | ♜ | ♞ | ♝ | ♛ | ♚ | ♝ | ♞ | ♜ |
(1) 7 | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ |
(2) 6 | ||||||||
(3) 5 | ||||||||
(4) 4 | ||||||||
(5) 3 | ||||||||
(6) 2 | ♙ | ♙ | ♙ | ♙ | ♙ | ♙ | ♙ | ♙ |
(7) 1 | ♖ | ♘ | ♗ | ♕ | ♔ | ♗ | ♘ | ♖ |
Note: if you do not see correctly the pieces on the board, please install the "Deja Vu Sans" font.
NOTE:
(1) The pieces in the input array are not in any particular order.
(2) Both functions should work in noninvasive fashion - don't change the contents of the array or any values of any piece in it.
(3) You can assume that the input array represent a valid chess position. This means there are no pawns on the first or last rank, both kings cannot be in check, no griffins, etc. The random tests sometimes generate positions that couldn't arise in a legal game of chess - for example, a king might be checked by 3 or more pieces. You don't have to detect this - your functions should still work correctly in these situations.
(4) To help with debugging, a function outputBoard(pieces)
(Java: static method OutputBoard.print(pieces)
, PHP: outputBoard($pieces)
, C#: static method void Figures.OutputBoard(IList<Figure> figures)
) is provided which displays the board in the testing output. If your code fails a test, analyze the situation on the board. If all else fails, ask for help in Discourse.
En passant
A comprehensive list of how each chess piece moves can be found at http://en.wikipedia.org/wiki/Chess#Movement. The most confusing of these rules is "en passant". Although pawns usually capture diagonally, in one situation a pawn can capture an enemy pawn on the square horizontally next to it. This is allowed if on the previous move the enemy pawn moved two squares from its initial position.
In order to determine whether "en passant" is possible, the array of pieces might have one piece with two additional attributes: 'prevX'
and 'prevY'
. These indicate that it was the most recently moved piece, with 'prevX'
and 'prevY'
giving its previous location. Your functions only need to use this information to handle "en passant".
NOTE: If a piece has 'prevX'
, 'prevY'
attributes, the outputBoard
function displays its previous location in light-gray. This is sometimes done whether or not "en passant" is possible - it makes the displayed board more informative (for example, in some of the beautiful mating positions in the sample tests.)
Similar Kata:
Stats:
Created | Feb 13, 2014 |
Published | Feb 17, 2014 |
Warriors Trained | 5512 |
Total Skips | 1727 |
Total Code Submissions | 26713 |
Total Times Completed | 483 |
JavaScript Completions | 244 |
CoffeeScript Completions | 4 |
Python Completions | 131 |
Java Completions | 52 |
TypeScript Completions | 14 |
PHP Completions | 20 |
C# Completions | 28 |
Total Stars | 336 |
% of votes with a positive feedback rating | 96% of 167 |
Total "Very Satisfied" Votes | 154 |
Total "Somewhat Satisfied" Votes | 11 |
Total "Not Satisfied" Votes | 2 |
Total Rank Assessments | 28 |
Average Assessed Rank | 2 kyu |
Highest Assessed Rank | 1 kyu |
Lowest Assessed Rank | 4 kyu |