-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.rb
187 lines (157 loc) · 5.23 KB
/
board.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
class Board < Catan
NUMBER_TOKENS = [5, 2, 6, 3, 8, 10, 9, 12, 11, 4, 8, 10, 9, 4, 5, 6, 3, 11]
HEX_TYPES = %w(ore brick)*3 + %w(sheep wheat wood)*4 + %w(desert)
PORT_TYPES = %w(brick wood sheep wheat ore) + ['3:1']*4
CARD_TYPES = %w(knight)*14 + %w(victory_point)*5 + %w(monopoly road_building year_of_plenty)*2
attr_accessor :settlements, :roads, :hexes, :side_length, :development_cards
def on_island?(i,j)
l = side_length
i.between?(1, l*2-1) &&
j.between?(1, l*2-1) &&
(i+j).between?(l+1, l*3-1)
end
def any_land?(coords)
coords.any? { |i,j| on_island?(i,j) }
end
def sorted_edge_pairs
@sorted_edge_pairs ||= begin
edge_pairs = \
hexes.flatten.select(&:water?).flat_map do |water_hex|
water_hex.directions.each_with_object([]) do |(dir, (x,y)), pairs|
if on_island?(x, y)
pairs << [water_hex, hexes[x][y], dir]
end
end
end
edge_pairs.sort_by do |pair|
c = side_length # effective "center" coordinate
wx, wy = pair[0].coordinates # water hex
lx, ly = pair[1].coordinates # land hex
# Angle from center to land hex, plus a tiebreaker term
Math.atan2(lx-c, ly-c) + 0.00001 * Math.atan2(wx-c, wy-c)
end
end
end
def initialize(opts={})
@side_length = opts[:side_length] || 3
types = HEX_TYPES.dup.shuffle.cycle
tokens = NUMBER_TOKENS.dup.cycle
cards = CARD_TYPES.dup.shuffle
@hexes = \
0.upto(side_length*2).map do |i|
0.upto(side_length*2).map do |j|
type = on_island?(i, j) ? types.next : 'water'
token = (tokens.next unless %(desert water).include?(type))
Hex.new(i, j, token, type)
end
end
port_types = PORT_TYPES.dup.shuffle.cycle
port_intervals = [3,3,4].cycle
i = 0
until i >= sorted_edge_pairs.length
water_hex, land_hex, port_direction = sorted_edge_pairs[i]
water_hex.port_type = port_types.next
water_hex.port_direction = port_direction
i += port_intervals.next
end
if desert = @hexes.flatten.detect(&:desert?)
desert.robbed = true
end
@settlements = []
@roads = []
@development_cards = cards.map{|c| DevCard.new(c) }
end
def as_json
{
settlements: settlements.map(&:as_json),
roads: roads.map(&:as_json),
hexes: hexes.map{|row| row.map(&:as_json) },
size: size
}
end
def rolled(roll)
settlements.each{|s| s.rolled(roll) }
end
def robbed_hex
@hexes.flatten.detect(&:robbed)
end
def move_robber_to(x, y, player)
error 'invalid robber location' unless hexes[x] && hex = hexes[x][y]
error 'cannot pick same location' if hex == robbed_hex
robbed_hex.robbed = false
hex.robbed = true
settlements.each_with_object([]) do |s, robbable|
next unless s.hexes.include?(hex)
next if s.player == player
next if robbable.include? s.player
next if s.player.resource_cards.size == 0
robbable << s.player
end
end
def size
side_length*2 + 1
end
def hexes_adjacent_to(hex1, hex2=nil)
positions = hex1.adjacencies
positions = hex2.adjacencies & positions if hex2
positions.reject{|x,y| x >= size || y >= size}.map{|x,y| hexes[x][y] }
end
def road_to?(hex1, hex2, hex3, color)
roads_to(hex1, hex2, hex3, color).any?
end
def roads_to(hex1, hex2, hex3, color)
roads.each_with_object([]) do |road, nearby_roads|
next unless road.color == color
next unless [hex1, hex2, hex3].permutation(2).include?(road.hexes)
nearby_roads << road
end
end
def settlement_at(hex1, hex2, hex3)
settlements.detect{|s| s.hexes - [hex1, hex2, hex3] == []}
end
def settlement_at?(hex1, hex2, hex3, color=nil, just_settlements=false)
settlement = settlement_at(hex1, hex2, hex3)
return false unless settlement
return false if color && color != settlement.color
return false if just_settlements && settlement.size != 1
true
end
def road_at(hex1, hex2)
roads.detect{|r| r.hexes - [hex1, hex2] == []}
end
def road_buildable_at?(hex1, hex2, color)
return false if road_at(hex1, hex2)
for hex in hexes_adjacent_to(hex1, hex2)
trio = [hex1, hex2, hex]
return true if settlement_at?(*trio, color)
return true if road_to?(*trio, color) && !settlement_at?(*trio)
end
false
end
def settlement_near?(hex1, hex2, hex3)
[hex1, hex2, hex3].combination(2).each do |h1, h2|
return true if hexes_adjacent_to(h1,h2).any?{|h| settlement_at?(h,h1,h2) }
end
false
end
def longest_road_length(color)
roads.select{|r| r.color == color}.map{|r| longest_path_from(r)}.max || 0
end
def longest_path_from(road, visited=[road], last_vertex=[])
successors = []
hexes_adjacent_to(*road.hexes).each do |hex|
vertex = road.hexes + [hex]
settle = settlement_at(*vertex)
next if settle && settle.color != road.color
next if vertex - last_vertex == []
roads_to(*vertex, road.color).each do |r|
next if visited.include?(r)
successors << [r,vertex]
end
end
return visited.length unless successors.any?
successors.map do |r,vertex|
longest_path_from(r, visited+[r], vertex)
end.max
end
end