If you want to check the surrounding tiles in a series of array.
Instead of using this:
if input[index_r-1]
result += 1 if input[index_r-1][index_e-1] && input[index_r-1][index_e-1] == "*"
result += 1 if input[index_r-1][index_e] == "*"
result += 1 if input[index_r-1][index_e+1] && input[index_r-1][index_e+1] == "*"
end
result += 1 if input[index_r][index_e-1] && input[index_r][index_e-1] == "*"
result += 1 if input[index_r][index_e+1] && input[index_r][index_e+1] == "*"
if input[index_r+1]
result += 1 if input[index_r+1][index_e-1] && input[index_r+1][index_e-1] == "*"
result += 1 if input[index_r+1][index_e] == "*"
result += 1 if input[index_r+1][index_e+1] && input[index_r+1][index_e+1] == "*"
end
Please use this
[input[x-1][y-1], input[x-1][y], input[x-1][y+1], input[x][y-1], input[x][y+1], input[x+1][y-1], input[x+1][y], input[x+1][y+1]].count('*')
Instead of using this:
if input[index_r-1]
result += 1 if input[index_r-1][index_e-1] && input[index_r-1][index_e-1] == "*"
result += 1 if input[index_r-1][index_e] == "*"
result += 1 if input[index_r-1][index_e+1] && input[index_r-1][index_e+1] == "*"
end
result += 1 if input[index_r][index_e-1] && input[index_r][index_e-1] == "*"
result += 1 if input[index_r][index_e+1] && input[index_r][index_e+1] == "*"
if input[index_r+1]
result += 1 if input[index_r+1][index_e-1] && input[index_r+1][index_e-1] == "*"
result += 1 if input[index_r+1][index_e] == "*"
result += 1 if input[index_r+1][index_e+1] && input[index_r+1][index_e+1] == "*"
end
Please use this
[input[x-1][y-1], input[x-1][y], input[x-1][y+1], input[x][y-1], input[x][y+1], input[x+1][y-1], input[x+1][y], input[x+1][y+1]].count('*')
Comments
Post a Comment