Skip to content

Commit

Permalink
Fix prepend_empty_row
Browse files Browse the repository at this point in the history
After change of the grid to 1D, we need to update the prepend_empty_row
  • Loading branch information
pusewicz committed May 7, 2024
1 parent 87400e4 commit fc65806
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
17 changes: 6 additions & 11 deletions lib/wave_function_collapse/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,18 @@ def iterate
end

def prepend_empty_row
@grid = @grid.drop(@width)
@grid.each { |cell| cell.y -= 1 }
x = 0
while x < @width
@grid[x].shift
y = 0
while y < @height - 1
@grid[x][y].y -= 1
y += 1
end
new_cell = Cell.new(x, @height - 1, @tiles)
@grid[x] << new_cell
@grid << new_cell
@uncollapsed_cells_grid << new_cell
x += 1
end

@width.times do |x|
evaluate_neighbor(@grid[x][@height - 2], :up)
end
@width.times { |x|
evaluate_neighbor(cell_at(x, @height - 2), :up)
}
end

def random_cell
Expand Down
33 changes: 33 additions & 0 deletions test/test_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,37 @@ def test_initialize
assert model.solve
assert model.iterate
end

def test_prepend_empty_row
tiles = [
Tile.new(tileid: 0, wangid: [0, 0, 0, 0, 0, 0, 0, 0]),
Tile.new(tileid: 1, wangid: [0, 0, 0, 0, 0, 0, 0, 0]),
Tile.new(tileid: 2, wangid: [0, 0, 0, 0, 0, 0, 0, 0])
]
model = Model.new(tiles, 2, 2)
model.solve
model.iterate
model.iterate
model.iterate

assert model.complete?

model.prepend_empty_row

assert_equal 4, model.grid.size
assert_equal 1, model.grid[0].entropy
assert_equal 1, model.grid[1].entropy
assert_predicate model.grid[0], :collapsed?
assert_predicate model.grid[0], :collapsed?
assert_equal 3, model.grid[2].entropy
assert_equal 3, model.grid[3].entropy
refute_predicate model.grid[2], :collapsed?
refute_predicate model.grid[3], :collapsed?

assert_equal 2, model.width
assert_equal 2, model.height
assert_equal 2 * 2, model.grid.size
assert_equal 3, model.max_entropy
assert_equal 50, model.percent
end
end

0 comments on commit fc65806

Please sign in to comment.