Skip to content

Instantly share code, notes, and snippets.

@hildjj
Created July 20, 2021 23:25
Show Gist options
  • Save hildjj/b7f482e618deb8e883f7ac7afd6df7c4 to your computer and use it in GitHub Desktop.
Save hildjj/b7f482e618deb8e883f7ac7afd6df7c4 to your computer and use it in GitHub Desktop.
battleship
known_ships = [
["Cruiser", 3],
["Submarine", 2]
]
cells = []
%w( A B C D ).each do |x|
(1..4).each do |y|
cells << "#{x}#{y}"
end
end
known_ships.each do |s|
name, len = s
xy = cells.sample
dir = rand(2)
others = [xy.dup]
(len-1).times do
xy[dir] = xy[dir].next
others << xy.dup
end
# if valid...
puts "#{name}: #{others}"
cells -= others
p cells
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment