Skip to content

Instantly share code, notes, and snippets.

@abedolinger
Created June 14, 2019 04:58
Show Gist options
  • Save abedolinger/c9e54d12c2dd023f711eaff3ee0645ea to your computer and use it in GitHub Desktop.
Save abedolinger/c9e54d12c2dd023f711eaff3ee0645ea to your computer and use it in GitHub Desktop.
its minesweeper boys
map = [
'..*..*...',
'.....*...' ,
'..**.....' ,
'..***....' ,
'..*.*.*..',
'...**....' ,
'.*.*.**..' ,
'...*.....',
'....*..*.' ,
]
check = [
[-1,-1],[0,-1],[1,-1],
[-1, 0], [1, 0],
[-1, 1],[0, 1],[1, 1],
]
def make_answer(map)
answer = []
map.each{|line|
# puts "map line", line
templine=[]
line.split("").size.times { |i|
templine.append(0)
}
# puts templine
answer.append(templine)
}
answer
end
def minesweeper(map, check)
answer = make_answer(map)
puts map
map.each_with_index{|line, x|
splitline=line.split("")
splitline.each_with_index{|cell, y|
if cell == "*"
answer[x][y] = "*"
puts "found * at [#{x}][#{y}]"
check.each{|spot|
puts "checking [#{x+spot[0]}][#{y+spot[1]}]"
if x + spot[0] >= 0 && y + spot[1] >= 0 && x + spot[0] < map[x].split("").length && y + spot[0] < map.length && map[x+spot[0]][y+spot[1]] != "*"
answer[x+spot[0]][y+spot[1]] += 1
puts "now [#{x+spot[0]}][#{y+spot[1]}]=#{answer[x+spot[0]][y+spot[1]]}"
end
}
end
}
}
answer.each{|line|
puts line.join('')
}
answer
end
minesweeper(map, check)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment