Beta

Check and Mate?

244 of 483Zirgion

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:

    public enum FigureType { Pawn, King, Queen, Rook, Knight, Bishop}

    //struct to make it convenient to work with cells
    public struct Pos
    {
        public readonly sbyte X;
        public readonly sbyte Y;

        public Pos(sbyte y, sbyte x);
        public Pos(int y, int x);

        public override bool Equals(object obj);
        public override int GetHashCode();
    }

    public class Figure
    {
        public FigureType Type { get; }
        public byte Owner { get; }
        public Pos Cell { get; set; }
        public Pos? PrevCell { get; }

        public Figure(FigureType type, byte owner, Pos cell, Pos? prevCell = null);
    }
{
  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
}
{
  piece: 'king' | 'queen' | 'bishop' | 'knight' | 'rook' | 'pawn'
  owner: 0 | 1,    // 0 for white or 1 for black
  x: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7,        // 0 is the leftmost column (or "A")
  y: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7,        // 0 is the top row (or "8" in the board below)
  prevX: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7,    // presents this piece's previous x, only given if this is the piece that was just moved
  prevY: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7     // presents this piece's previous y, only given if this is the piece that was just moved
}
{
  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
}
# Array of associative arrays
[
  "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" in the board below)
  "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
]

# For example: $pieces[0]["piece"] might return "rook"
{
  'piece': string, # either "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 ("a" in the board below)
  'y': int,        # 0-7, where 0 is the top row ("8" in the board below)
}
PieceConfig[] pieces, whose properties are:
{ '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 

All fields are private and final.
You may find below the usefull available methods/informations about the PieceConfig instances:

    class PieceConfig {
        public PieceConfig(final String piece, final int owner, final int x, final int y, final int prevX, final int prevY)
        public PieceConfig(final String piece, final int owner, final int x, final int y)
        public String getPiece()  
        public int getOwner()        
        public int getX()            
        public int getY()               
        public boolean hasPrevious() 
        public int getPrevX()        // will throw a RuntimeException if invoked for an object that do not have informations about its previous move.
        public int getPrevY()        // will throw a RuntimeException if invoked for an object that do not have informations about its previous move.
                
        @Override public int hashCode()
        @Override public String toString()  //return string formated as :"piece: king, owner: 0, x: 0, y: 4" (plus prevX and prevY if needed)
        @Override public boolean equals(Object other)

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.)

Logic
Arrays
Data Structures
Games
Algorithms

Similar Kata:

Stats:

CreatedFeb 13, 2014
PublishedFeb 17, 2014
Warriors Trained5512
Total Skips1727
Total Code Submissions26713
Total Times Completed483
JavaScript Completions244
CoffeeScript Completions4
Python Completions131
Java Completions52
TypeScript Completions14
PHP Completions20
C# Completions28
Total Stars336
% of votes with a positive feedback rating96% of 167
Total "Very Satisfied" Votes154
Total "Somewhat Satisfied" Votes11
Total "Not Satisfied" Votes2
Total Rank Assessments28
Average Assessed Rank
2 kyu
Highest Assessed Rank
1 kyu
Lowest Assessed Rank
4 kyu
Ad
Contributors
  • Zirgion Avatar
  • donaldsebleung Avatar
  • kazk Avatar
  • Blind4Basics Avatar
  • Axure Avatar
  • Voile Avatar
  • csurgay Avatar
  • GodComplex_ Avatar
  • hobovsky Avatar
  • max_cn Avatar
  • user8436785 Avatar
  • dfhwze Avatar
  • brodiemark Avatar
Ad