2012-02-28 87 views
0

我需要將字符串轉換(棋盤的64字符表示):紅寶石:轉換一個字符串轉換爲2維陣列

game_state = "r_____r__r___r_rr_r___r__________________b_b______b_b____b_b___b" 

成二維,8×8,陣列檢查器對象的:

[ [red_checker, nil, nil, nil, nil....and so on times 8], ] 

這是我做的方式,這是醜陋的,而不是非常喜歡紅寶石與接近尾聲的增量。

def game_state_string_to_board(game_state) 
    board = Board.new 
    game_board = board.create_test_board 

    game_state_array = [] 
    game_state.each_char { |char| game_state_array << char} 
    row_index = 0 
    game_state_array.each_slice(8) do |row| 
     row.each_with_index do |square, col_index| 
     unless square == '_' 
      game_board[row_index][col_index] = create_checker(square, row_index, col_index) 
     end 
     end 
     row_index += 1 
    end 
    game_board 
    end 

有沒有人有更清潔,更簡單,更紅寶石般的方式?謝謝。

+0

聽起來像一個家庭作業。 – 2012-02-28 18:49:59

+0

@Tian Man Nah,這實際上是我現在的工作。 – 2012-02-28 21:49:25

回答

2

像這樣的東西應該工作(紅寶石1.9.x的):

game_state = "r_____r__r___r_rr_r___r__________________b_b______b_b____b_b___b" 

game_state.split('').each_with_index.map do |square, idx| 
    square == '_' ? nil : create_checker(square, idx/8, idx % 8) 
end.each_slice(8).to_a 

輸出:

[ [ #<RedChecker(0,0)>, nil, nil, nil, nil, nil, #<RedChecker(0,6)>, nil], 
    [ nil, #<RedChecker(1,1)>, nil, nil, nil, #<RedChecker(1,5)>, nil, #<RedChecker(1,7)>], 
    [ #<RedChecker(2,0)>, nil, #<RedChecker(2,2)>, nil, nil, nil, #<RedChecker(2,6)>, nil], 
    [ nil, nil, nil, nil, nil, nil, nil, nil], 
    [ nil, nil, nil, nil, nil, nil, nil, nil], 
    [ nil, #<BlackChecker(5,1)>, nil, #<BlackChecker(5,3)>, nil, nil, nil, nil], 
    [ nil, nil, #<BlackChecker(6,2)>, nil, #<BlackChecker(6,4)>, nil, nil, nil], 
    [ nil, #<BlackChecker(7,1)>, nil, #<BlackChecker(7,3)>, nil, nil, nil, #<BlackChecker(7,7) ] 
] 

因爲它是一個有點遲鈍,我會打破它:

game_state. 
    # change the string into an Array like [ "r", "_", "_", ...] 
    split(''). 

    # `each_with_index` gives us an Enumerable that passes the index of each element... 
    each_with_index. 

    # ...so we can access the index in `map` 
    map do |square, idx| 
    square == "_" ? 
     # output nil if the input is "_" 
     nil : 
     # otherwise call create_checker 
     create_checker(square, 
     idx/8, # the row can be derived with integer division 
     idx % 8 # and the column is the modulus (remainder) 
    ) 
    end. 

    # take the result of `map` and slice it up into arrays of 8 elements each 
    each_slice(8).to_a 
1

如果你想要一個8×8的陣列,這將做到:

game_state = "r_____r__r___r_rr_r___r__________________b_b______b_b____b_b___b" 

game_state.split('').each_slice(8).to_a 

"[[\"r\", \"_\", \"_\", \"_\", \"_\", \"_\", \"r\", \"_\"], [\"_\", \"r\", \"_\", \"_\", \"_\", \"r\", \"_\", \"r\"], [\"r\", \"_\", \"r\", \"_\", \"_\", \"_\", \"r\", \"_\"], [\"_\", \"_\", \"_\", \"_\", \"_\", \"_\", \"_\", \"_\"], [\"_\", \"_\", \"_\", \"_\", \"_\", \"_\", \"_\", \"_\"], [\"_\", \"b\", \"_\", \"b\", \"_\", \"_\", \"_\", \"_\"], [\"_\", \"_\", \"b\", \"_\", \"b\", \"_\", \"_\", \"_\"], [\"_\", \"b\", \"_\", \"b\", \"_\", \"_\", \"_\", \"b\"]]" 

我不知道在你的代碼中的一些方法做,但是這應該回答你的問題