Validate FEN notation
Description:
Intro
Forsyth-Edwards Notation or FEN is a way to concisely represent the status of a chess game, including the board, current player, number of moves, etc. It is a string that consists of 6 fields separated by spaces:
- Piece placement (from white's perspective), rank (or row) by rank (starting with rank 8 to 1), each rank represented by a string of piece symbols from the standard English notation (`P` - pawn, `N` - knight, etc.) with uppercase letters for white figures and lowercase for black. Empty squares are represented by numbers of consecutive empty squares. For example `2P1k2Q` means - two empty squares, a white pawn, one empty square, a black king, two empty squares and a white queen. Notice, that while probably never practical, the row notation `44`, `1232` or `53` is still a valid notation. Each rank is separated by `/`.
- Active color, either `w` or `b` for white and black accordingly.
- Castling availabilty - if no player can castle, it's `-`, otherwise it's any combination of `K` (for white's king-side castling), `Q` (for white's queen-side castling), `k` (similarly for black's) and/or `q`.
- En passant target square - an optional possible square for pawn's en passant capturing in algebraic notation. If any pawn has just made a two-square move, it is a position behind the said pawn. It's `-` if there's no en passant target. It is recorded whether there's an actual pawn to make a capture or not.
- Number of halfmoves since the last capture or pawn move. It is used to determine if the fifty-move rule can be applied.
- And finally a number of full moves. Starts with 1 and increments after every black's move.
So, for instance, the FEN for starting position is:
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
Task
Your task is to write a function called validate_fen
, that takes a string with FEN notation and returns a boolean value True
if the string is a valid FEN notatation and False
otherwise. Here are the rules:
Obviously, the board has to be valid, and by valid we mean 8 rows with 8 squares each, with valid figures. Ignore the number and type of pieces present, as long as they're valid chess figures.
The color has to be valid.
The castling options have to be valid. The order doesn't matter, but duplication of any symbol is forbidden, so kKqQ
, kqKQ
or QqkK
are all valid, but KKq
is not. You don't need to check if the pieces are actually on their starting squares.
En passant has to be valid - and by valid, we mean that it is only possible after the first move of every pawn. For simplicity's sake you don't have to check that there's actually a pawn that made that move, only that this target square is supposedly valid.
And finally the moves have to be valid. We assume that fifty-moves rule has to be applied after 50 halfmoves, so any FEN with over 50 halfmoves is invalid. The number of fullmoves has to be greater than 1, but no upper limit. Also, the number of fullmoves implies the maximum number of halfmoves, so you need to check this as well.
Good luck, and feel free to comment!
Additional info
For your convenience there is a preloaded function print_board
that takes a whole notation or just the board representation and prints it to the stdout in the following fashion:
rnbqkbnr
pppppppp
........
........
........
........
PPPPPPPP
RNBQKBNR
The random tests will always produce a valid or invalid notation (with 50% chance), and the invalid cases will always have only one reason to be invalid, for instance a board with too many rows, and no more. The reason will be presented to you if the test fails.
If you enjoyed this kata, you can check a similar one which, so to say, inspired me to write this one.
Similar Kata:
Stats:
Created | Jan 31, 2020 |
Published | Feb 5, 2020 |
Warriors Trained | 117 |
Total Skips | 55 |
Total Code Submissions | 258 |
Total Times Completed | 11 |
Python Completions | 11 |
Total Stars | 2 |
% of votes with a positive feedback rating | 60% of 5 |
Total "Very Satisfied" Votes | 3 |
Total "Somewhat Satisfied" Votes | 0 |
Total "Not Satisfied" Votes | 2 |
Total Rank Assessments | 5 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 7 kyu |