2010-01-24 145 views
2

我想對Ruby的幾行Python的轉換,從由托馬斯客戶簽署本優秀的文章:http://wordaligned.org/articles/drawing-chess-positions代碼翻譯,用Python和Ruby

(注:我是一個真正做大的Python小白)

這裏是原來的Python版本的副本:

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) 

用例:

>>> 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| 
+-+-+-+-+-+-+-+-+ 

我已經嘗試做了一些修改,以在Ruby中的每一行轉換......我不知道,如果是糟糕的開局:秒,但我無論如何發佈:

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) 

如果你看到一些線,我可以解決,我會對我很酷。謝謝你們。

回答

2

依次查看每種方法,從outer_join開始。

在Python中,join是一個字符串方法,它接受一個序列並加入由字符串分隔的元素。例如'|'.join(['p, 'p', 'p'])

在Ruby中,join是將分隔符作爲參數的一種方法,例如['p', 'p', 'p'].join('|')

所以outer_join Ruby的版本是:

def outer_join(sep, ss): 
    sep + ss.join(sep) + sep 
end 

expand_blanks接下來看看,這裏的意圖是用空格當量數更換數字。在Ruby中,可以使用gsub來替換字符串中所有出現的模式。 gsub可以用一個將被傳遞每個匹配子字符串的塊來調用,並返回匹配應該被替換的字符串。所以Ruby代碼將是:

def expand_blanks(fen) 
    fen.gsub(/\d/) { |match| ' ' * match.to_i } 
end 

最後在ascii_draw_chess_position我們可以再次使用gsub作爲等同於Python的replace方法和使用Ruby的map功能的地方使用Python的列表理解如下達到什麼樣的:

def ascii_draw_chess_position(fen) 
    pieces = expand_blanks(fen).gsub('/', '') 
    divider = "+-+-+-+-+-+-+-+-+\n" 
    rows = (0...8).map do |i| 
    row = pieces[i * 8...(i + 1) * 8].split('') 
    outer_join("|",row) + "\n" 
    end 
    puts outer_join(divider, rows) 
end 

讓我知道你是否需要關於上述任何更多細節。

+0

非常感謝Mikej和Grifaton。現在我得到了我所要做的每件事。再次感謝! 此致敬禮。 – Denis 2010-01-24 23:57:08

1

這會做你想要什麼:

def expand(match) 
    ' ' * match.to_i 
end 

def expand_blanks(fen) 
    fen.gsub(/\d/) {|d| expand d} 
end 

def outer_join(sep, ss) 
    sep + ss.join(sep) + sep 
end 

def ascii_draw_chess_position(fen) 
    pieces = expand_blanks(fen).gsub('/', '') 
    divider = "+-+-+-+-+-+-+-+-+\n" 
    rows = (0...8).collect do |i| 
    row = pieces[i*8...(i+1)*8].split('') 
    outer_join("|",row) + "\n" 
    end 
    puts outer_join(divider, rows) 
end 

讓我知道如果這裏有你不明白任何代碼 - 但要確保你已經看過了在ruby docs

如果方法您對Ruby和Python的區別感興趣,請參閱https://stackoverflow.com/questions/234721/what-are-the-biggest-differences-between-python-and-ruby-from-a-philosophical-perhttp://www.ruby-lang.org/en/documentation/ruby-from-other-languages/to-ruby-from-python/,有關一些很好的示例,請參閱http://ruby.brian-amberg.de/editierdistanz/

+0

非常感謝,Grifaton!我會研究並看到不同之處。 – Denis 2010-01-24 23:55:41