-
Notifications
You must be signed in to change notification settings - Fork 1
/
rule_set.rb
141 lines (112 loc) · 3.29 KB
/
rule_set.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# Author:: Max Craigie (@MaxCraigie)
# Copyright:: Copyright (c) 2017 Max Craigie
# License:: Attribution-NonCommercial 3.0 Australia (CC BY-NC 3.0 AU)
require './grid.rb'
require './rule.rb'
module Bongard
class RuleSet
class << self
def all
rules = [
anywhere,
row_related,
column_related,
corner_cell_related,
edge_cell_related,
mirror_symmetry_related,
rotation_symmetry_related,
].flatten
end
def anywhere
rules = []
Bongard::Grid.each_variety do |v|
Bongard::Grid.each_coord do |c|
rules << Bongard::Rule.new("variety #{v} @ #{c}") do |g|
g.cell_at(*c).is(v)
end
end
(0..4).each do |n|
rules << Bongard::Rule.new("#{n} of variety #{v} in any cell") do |g|
g.count { |c| c.is(v) } == n
end
end
rules << Bongard::Rule.new("any of variety #{v} @ any cell") do |g|
g.any? { |c| c.is(v) }
end
end
rules
end
def row_related
rules = []
Bongard::Grid.each_row do |row|
Bongard::Grid.each_variety do |v|
(0..3).each do |n|
rules << Bongard::Rule.new("#{n} of variety #{v} @ row #{row}") do |g|
g.cells_in_row(row).count { |c| c.is(v) } == n
end
end
end
end
rules
end
def column_related
rules = []
Bongard::Grid.each_col do |col|
Bongard::Grid.each_variety do |v|
(0..3).each do |n|
rules << Bongard::Rule.new("#{n} of variety #{v} @ col #{col}") do |g|
g.cells_in_col(col).count { |c| c.is(v) } == n
end
end
end
end
rules
end
def corner_cell_related
rules = []
Bongard::Grid.each_variety do |v|
(0..4).each do |n|
rules << Bongard::Rule.new("#{n} of variety #{v} in corner cells") do |g|
g.corner_cells.count { |c| c.is(v) } == n
end
end
rules << Bongard::Rule.new("variety #{v} @ any corner cell") do |g|
g.corner_cells.any? { |c| c.is(v) }
end
end
rules
end
def edge_cell_related
rules = []
Bongard::Grid.each_variety do |v|
(5..7).each do |n|
rules << Bongard::Rule.new("#{n} of variety #{v} in edge cells") do |g|
g.edge_cells.count { |c| c.is(v) } == n
end
end
rules << Bongard::Rule.new("any of variety #{v} in edge cells") do |g|
g.edge_cells.any? { |c| c.is(v) }
end
end
rules
end
def mirror_symmetry_related
rules = []
# TODO: add diagnonal mirroring
%i[vertical horizontal].each do |axis|
rules << Bongard::Rule.new("mirrored #{axis}") { |g| g.mirror(axis) == g }
end
rules
end
def rotation_symmetry_related
rules = []
(1..2).each do |n|
rules << Bongard::Rule.new("rotated clockwise # #{n}") do |g|
g.rotate(:clockwise, n) == g
end
end
rules
end
end
end
end