Skip to content

Instantly share code, notes, and snippets.

@javierfernandes
Created September 3, 2017 15:12
Show Gist options
  • Select an option

  • Save javierfernandes/13ffac98e9e63e93e0499b9aeb47ecce to your computer and use it in GitHub Desktop.

Select an option

Save javierfernandes/13ffac98e9e63e93e0499b9aeb47ecce to your computer and use it in GitHub Desktop.
O3 - Ayuda para TP "SinCity": cálculo de celdas vecinas
class Board
def initialize width, height
@width = width
@height = height
end
def find_neighboring x, y
xrange = neighboring_area(x, @width)
yrange = neighboring_area(y, @height)
xrange.product(yrange) - [[x, y]]
end
def neighboring_area coord, bound
([0, coord - 1].max .. [coord + 1, bound - 1].min).to_a
end
end
require_relative '../Board.rb'
describe Board do
it "should find all sourrounding positions for a cell in the middle" do
board = Board.new(5, 8)
expect(board.find_neighboring(2, 2)).to eq([
[1, 1], [1, 2], [1, 3],
[2, 1], [2, 3],
[3, 1], [3, 2], [3, 3]
])
end
it "shouldn't give out of the board cells for (1, 1)" do
board = Board.new(5, 5)
expect(board.find_neighboring(0, 0)).to eq([
[0, 1], [1, 0], [1, 1]
])
end
it "shouldn't give out of the board cells for a cell next to y-axis" do
board = Board.new(5, 5)
expect(board.find_neighboring(0, 3)).to eq([
[0, 2], [0, 4], [1, 2], [1, 3], [1, 4]
])
end
it "shouldn't give out of the board cells for a cell next to x-axis" do
board = Board.new(5, 5)
expect(board.find_neighboring(3, 0)).to eq([
[2, 0], [2, 1], [3, 1], [4, 0], [4, 1]
])
end
it "should work with a cell too close to the end corner of the board" do
board = Board.new(5, 5)
expect(board.find_neighboring(4, 4)).to eq([
[3, 3], [3, 4], [4, 3]
])
end
it "neighboring_area()" do
board = Board.new(5, 5)
expect(board.neighboring_area(4, 5)).to eq([3, 4])
expect(board.neighboring_area(0, 5)).to eq([0, 1])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment