Created
December 15, 2015 14:31
-
-
Save kryhtin/9341f9d6640469fc7e10 to your computer and use it in GitHub Desktop.
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
require 'pp' | |
def try_to_place(x, y, grid, size) | |
if grid[x][y] | |
# Generate new X and Y | |
false | |
else | |
if can_place?(x, y, grid) | |
temp_grid = grid | |
temp_grid[x][y] = true | |
# Try to place next cell. | |
# 1. Define possible values | |
# xy = [[-1, 0], [0, 1], [1, 0], [0, -1]].sample | |
# 2. Select one | |
# 3. Try to place next cell (try_to_place) | |
else | |
# Generate new X and Y | |
end | |
end | |
end | |
def can_place?(x, y, grid) | |
[[1, -1], [1, 0], [1, 1], [0, 1], [1, -1], [-1, 0], [-1, -1], [0, -1]].each do |checks| | |
return false if grid[checks[0]][checks[1]] | |
end | |
true | |
end | |
grid = Array.new(10) do | |
Array.new(10) do | |
false | |
end | |
end | |
x = 1 | |
y = 0 | |
ship_size = 1 | |
try_to_place(x, y, grid, ship_size) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment