Hi,
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.