I would like to convert a few Python lines on Ruby, from this excellent article signed by Thomas Guest at: http://wordaligned.org/articles/drawing-chess-positions
(note: I’m a really bigger Python noob)
Here is a copy of the original Python version:
def expand_blanks(fen):
'''Expand the digits in an FEN string into spaces
>>> expand_blanks("rk4q3")
'rk q '
'''
def expand(match):
return ' ' * int(match.group(0))
return re.compile(r'\d').sub(expand, fen)
def outer_join(sep, ss):
'''Like string.join, but encloses the result with outer separators.
Example:
>>> outer_join('|', ['1', '2', '3'])
'|1|2|3|'
'''
return '%s%s%s' % (sep, sep.join(ss), sep)
def ascii_draw_chess_position(fen):
'''Returns an ASCII picture of pieces on a chessboard.'''
pieces = expand_blanks(fen).replace('/', '')
divider = '+-+-+-+-+-+-+-+-+\n'
rows = ((outer_join('|', pieces[r: r + 8]) + '\n')
for r in range(0, 8 * 8, 8))
return outer_join(divider, rows)
Usage example:
>>> fen = "r2q1rk1/pp2ppbp/1np2np1/2Q3B1/3PP1b1/2N2N2/PP3PPP/3RKB1R"
>>> print ascii_draw_chess_position(fen)
+-+-+-+-+-+-+-+-+
|r| | |q| |r|k| |
+-+-+-+-+-+-+-+-+
|p|p| | |p|p|b|p|
+-+-+-+-+-+-+-+-+
| |n|p| | |n|p| |
+-+-+-+-+-+-+-+-+
| | |Q| | | |B| |
+-+-+-+-+-+-+-+-+
| | | |P|P| |b| |
+-+-+-+-+-+-+-+-+
| | |N| | |N| | |
+-+-+-+-+-+-+-+-+
|P|P| | | |P|P|P|
+-+-+-+-+-+-+-+-+
| | | |R|K|B| |R|
+-+-+-+-+-+-+-+-+
I have trying to do some modifications to convert each line in Ruby… And I wonder if it’s bad start :s But I publish it anyway:
def expand_blanks(fen)
def expand(match)
return ' ' * int(match.group(0))
end
# bugged:
re.compile(r'\d').sub(expand, fen)
end
def outer_join(sep, ss)
sep + sep.join(ss) + sep
end
def ascii_draw_chess_position(fen)
pieces = expand_blanks(fen).replace('/', '')
puts pieces.class
divider = "+-+-+-+-+-+-+-+-+\n"
# bugged lines:
rows = ((outer_join('|', pieces[r, r + 8]) + '\n')
for r in range(0, 8 * 8, 8))
return outer_join(divider, rows)
end
end
fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"
puts ascii_draw_chess_position(fen)
If you see some lines that I can fix, I would be cool for me. Thank you all.
Looking at each method in turn, starting with
outer_join.In Python,
joinis a method on strings that takes a sequence and joins the elements delimited by the string. e.g.'|'.join(['p, 'p', 'p'])In Ruby,
joinis a method on arrays that takes the delimiter as an argument e.g.['p', 'p', 'p'].join('|')so the Ruby version of
outer_joinwould be:Next look at
expand_blanks, the intention here is to replace a digit with the equivalent number of spaces. In Ruby this can be achieved usinggsubto replace all occurrences of a pattern in a string.gsubcan be called with a block that will be passed each match substring and returns the string that the match should be replaced with. So the Ruby code would be:Finally in
ascii_draw_chess_positionwe can usegsubagain as an equivalent to Python’sreplacemethod and use Ruby’smapfunction in place of what was achieved using a list comprehension in Python as follows:Let me know if you need any more details on the above.