Posted: Sat Dec 11, 2010 8:09 pm Post subject: RE:FeedBack Please.
and..
Ruby:
ROWS, COLUMNS = 5, 5
MINE_PERCENTAGE = 0.2
@mine_grid = Array.new(ROWS + 2){Array.new(COLUMNS+2){}} # scatter some mines on the field (1..ROWS).eachdo |r|
(1..COLUMNS).eachdo |c|
@mine_grid[r][c] = rand < MINE_PERCENTAGE ? "*" : nil end
end
# method to print the field def print_grid
(1..ROWS).eachdo |r|
(1..COLUMNS).eachdo |c|
print[nil,0].include?(@mine_grid[r][c]) ? "." : @mine_grid[r][c] end print"\n" end
end
# method to count the mines around any particular cell def count_cell(r,c) (@mine_grid[r-1][c-1..c+1] +
@mine_grid[r][c-1..c+1] +
@mine_grid[r+1][c-1..c+1]).count{|a| a == "*"} end
# solve everything (1..ROWS).eachdo |r|
(1..COLUMNS).eachdo |c|
@mine_grid[r][c] = count_cell(r,c)unless @mine_grid[r][c] == "*" end
end