8
2
For this challenge, you will be writing a program or function which outputs (or prints) (in a reasonable format, i.e single string or an array of lines) a chess board which indicates all the possible moves of a piece given an empty board.
There are 5 chess pieces (for this challenge a pawn may be ignored):
- Bishop (Moves diagonally any number of squares)
- Rook (Moves orthogonally any number of squares)
- Queen (Moves diagonally or orthogonally any number of squares)
- KNight (Moves in an L shape, i.e. two in one dimension, and one in the other)
- King (Moves one step orthogonally or diagonally)
Your submission will take in three inputs: the row of the chess piece, the column of the chess piece, and the type of piece (as a character or integer).
Say we call drawBoard(4,4,'N') for the Knight using 0-indexing, the output will be as follows:
........
........
...M.M..
..M...M.
....N...
..M...M.
...M.M..
........
where the '.' and 'M' can be replaced accordingly with printable ascii characters of your choice.
Sample outputs as follows for other piece types. (Note all of these are called with input of 4,4 and are 0-indexed).
King:
........
........
........
...mmm..
...mKm..
...mmm..
........
........
Rook:
....m...
....m...
....m...
....m...
mmmmRmmm
....m...
....m...
....m...
Sam does not mind 1-based indexing, or even indexing following chess algebraic notation. However, your code must be as small as possible so Sam has enough space to think about his chess with the code next to him.
Sam has also realized that outputs will be accepted regardless of trailing or preceding whitespace (before and after input and each line)
I would advise either changing the
o
s to.
s, or changing them to spaces and adding a border. It's kinda unreadable at the moment. – El'endia Starman – 2016-08-10T20:46:55.057related http://codegolf.stackexchange.com/questions/69014/fairy-chess-leaper-movement-patterns
– Rohan Jhunjhunwala – 2016-08-10T20:51:46.030@El'endiaStarman done – Rohan Jhunjhunwala – 2016-08-10T20:54:48.813
Are spaces to the right of the output ok? – Level River St – 2016-08-10T21:02:23.253
sure, may I asky why? @LevelRiverSt – Rohan Jhunjhunwala – 2016-08-10T21:10:01.060
1@RohanJhunjhunwala I was thinking of using modular aritmetic for the bishops moves but decided against it. For example, in a 64 character string representation of a board (ignoring newlines) with a bishop standing at x=4, y=0, all the squares it can move to have either x%9=4 or x%7=4. Unfortunately this also allows for the bishop to wrap around if he walks of the board, which is not permitted in chess. A solution is to pad the end of each line to, say 16, with spaces (and a newline) and increase to x%15 and x%17 so that no wrapping occurs in the region of interest. But I abandoned the idea. – Level River St – 2016-08-10T23:23:24.700
related http://codegolf.stackexchange.com/questions/89647/chess-conversion#89647
– Rohan Jhunjhunwala – 2016-08-12T14:28:43.100