FEN is a chess board position notation. Here is an initial chess board setup in FEN:
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
Note that there are 7 /
separating the 8 rows (ranks) of a chess board. Each row has either piece letters denoting which columns
have which pieces or a number denoting the number of consecutive blank columns in that row. For this challenge you can ignore everything after
the first space in the FEN.
You should submit a git repo with your solution using the programming language of your choice. Please include in the repo instructions for how to run your solution. This can be submitted as a zip file containing the repo or a url to a public repository on an internet git hosting solution (github, bitbucket, etc).
We want to see your solution to the problems. Feel free to use third party libraries where appropriate, but algorithm for the FEN parsing (task 1) should be your own code.
You should complete both tasks in your solution and submit it before the phone interview.
There is also a stretch goal that asks you to combine the two solutions into one. This is optional.
Task: Write a program that will parse a provided FEN string and display the appropriate board representation.
For example for the FEN:
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
The generated board should look like this:
---------------------------------
8 | r | n | b | q | k | b | n | r |
|-------------------------------|
7 | p | p | p | p | p | p | p | p |
|-------------------------------|
6 | | | | | | | | |
|-------------------------------|
5 | | | | | | | | |
|-------------------------------|
4 | | | | | | | | |
|-------------------------------|
3 | | | | | | | | |
|-------------------------------|
2 | P | P | P | P | P | P | P | P |
|-------------------------------|
1 | R | N | B | Q | K | B | N | R |
---------------------------------
a b c d e f g h
Another example, for the FEN,
rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2
The board output should be:
---------------------------------
8 | r | n | b | q | k | b | n | r |
|-------------------------------|
7 | p | p | | p | p | p | p | p |
|-------------------------------|
6 | | | | | | | | |
|-------------------------------|
5 | | | p | | | | | |
|-------------------------------|
4 | | | | | P | | | |
|-------------------------------|
3 | | | | | | N | | |
|-------------------------------|
2 | P | P | P | P | | P | P | P |
|-------------------------------|
1 | R | N | B | Q | K | B | | R |
---------------------------------
a b c d e f g h
You should only need the first part of the FEN for this (everything before the first space) and your board output should look exactly like the examples above.
Task Write a program that will return an updated FEN string given an initial FEN string and a single move to make provided by an API call.
The chess moves API can be called by sending an HTTP GET request to https://syzygy-tables.info/api/v2?fen=[FEN]
where [FEN]
is replaced by a valid FEN string.
We want you to call the chess moves API with a provided FEN string and get the first suggested move from the API response. Then return an updated FEN string based on the provided FEN and the returned move.
Given the inital FEN,
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
The API will return a2a3
. This means that the first move in the moves
section of the response is suggesting the piece
on a2
needs to move to a3
. To understand this it is helpful to know that the rows are labeled 8-1
(going left to right
in the FEN string) and the columns are labeled a-h
(going left to right in the FEN string).
Given the initial FEN and the move a2a3
provided by the API call, your program should return the updated FEN,
rnbqkbnr/pppppppp/8/8/8/P7/1PPPPPPP/RNBQKBNR w KQkq - 0 1
Another example is with the FEN
rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2
The API will return a7a5
as the first move, so the updated FEN should be,
rnbqkbnr/1p1ppppp/8/p1p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2
Just in case the API is down or you want to implement the call later, here are a few of the suggested moves from the API for some FENs:
FEN | Move |
---|---|
8/4npk1/5p1p/1Q5P/1p4P1/4r3/7q/3K1R2 b - - 1 49 | b4b3 |
5r1k/6pp/4Qpb1/p7/8/6PP/P4PK1/3q4 b - - 4 37 | a5a4 |
8/8/2P5/4B3/1Q6/4K3/6P1/3k4 w - - 5 67 | b4a3 |
r2q1rk1/pp2ppbp/2p2np1/6B1/3PP1b1/Q1P2N2/P4PPP/3RKB1R b K - 0 13 | a7a5 |
Combine the solutions to tasks 1 and 2 into a complete solution that does both. Given a FEN string, get the suggested move, update the FEN string, and display both the updated FEN string as well as the chess board position of the updated FEN string.