I need to convert a string (64 character representation of a checker board):
game_state = "r_____r__r___r_rr_r___r__________________b_b______b_b____b_b___b"
into a two dimensional, 8 X 8, array of checker objects:
[ [red_checker, nil, nil, nil, nil....and so on times 8], ]
This is my way of doing it, which is ugly and not very ruby like with the increment near the end.
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
Does anyone have a cleaner, simpler, more ruby-ish way? Thanks.