Created
June 14, 2019 04:58
-
-
Save abedolinger/c9e54d12c2dd023f711eaff3ee0645ea to your computer and use it in GitHub Desktop.
its minesweeper boys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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