Created
July 20, 2021 23:25
-
-
Save hildjj/b7f482e618deb8e883f7ac7afd6df7c4 to your computer and use it in GitHub Desktop.
battleship
This file contains 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
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