comparison ellers.rb @ 0:1eef88068f9f tip

initial commit of maze game source
author ferencd
date Sun, 15 Sep 2019 11:46:47 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1eef88068f9f
1 #---
2 # Excerpted from "Mazes for Programmers",
3 # published by The Pragmatic Bookshelf.
4 # Copyrights apply to this code. It may not be used to create training material,
5 # courses, books, articles, and the like. Contact us if you are in doubt.
6 # We make no guarantees that this code is fit for any purpose.
7 # Visit http://www.pragmaticprogrammer.com/titles/jbmaze for more book information.
8 #---
9 class Ellers
10
11 class RowState
12 def initialize(starting_set=0)
13 @cells_in_set = {}
14 @set_for_cell = []
15 @next_set = starting_set
16 end
17
18 def record(set, cell)
19 @set_for_cell[cell.column] = set
20
21 @cells_in_set[set] = [] if !@cells_in_set[set]
22 @cells_in_set[set].push cell
23 end
24
25 def set_for(cell)
26 if !@set_for_cell[cell.column]
27 record(@next_set, cell)
28 @next_set += 1
29 end
30
31 @set_for_cell[cell.column]
32 end
33
34 def merge(winner, loser)
35 @cells_in_set[loser].each do |cell|
36 @set_for_cell[cell.column] = winner
37 @cells_in_set[winner].push cell
38 end
39
40 @cells_in_set.delete(loser)
41 end
42
43 def next
44 RowState.new(@next_set)
45 end
46
47 def each_set
48 @cells_in_set.each { |set, cells| yield set, cells }
49 self
50 end
51 end
52
53 def on(grid)
54 row_state = RowState.new
55
56 grid.each_row do |row|
57 row.each do |cell|
58 next unless cell.west
59
60 set = row_state.set_for(cell)
61 prior_set = row_state.set_for(cell.west)
62
63 should_link = set != prior_set &&
64 (cell.south.nil? || rand(2) == 0)
65
66 if should_link
67 cell.link(cell.west)
68 row_state.merge(prior_set, set)
69 end
70 end
71
72 if row[0].south
73 next_row = row_state.next
74
75 row_state.each_set do |_, list|
76 list.shuffle.each_with_index do |cell, index|
77 if index == 0 || rand(3) == 0
78 cell.link(cell.south)
79 next_row.record(row_state.set_for(cell), cell.south)
80 end
81 end
82 end
83
84 row_state = next_row
85 end
86 end
87 end
88
89 end