-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex.rb
69 lines (57 loc) · 1.38 KB
/
hex.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
class Hex < Catan
TYPES = %w(ore brick sheep wheat wood desert water)
TYPES.each do |type|
define_method "#{type}?" do
@type == type
end
end
attr_accessor :x, :y, :number, :type, :robbed, :port_type, :port_direction
def inspect
"<Hex: #{type} (#{number}) [#{x},#{y}]>"
end
def initialize(x, y, number, type, port_type=nil, port_direction=nil, robbed=false)
@x = x
@y = y
@number = number
@type = type
@port_type = port_type
@port_direction = port_direction
@robbed = robbed
end
def as_json
{
x: x,
y: y,
number: number,
type: type,
robbed: robbed,
port_type: port_type,
port_direction: port_direction
}
end
def coordinates
[x, y]
end
def adjacencies
[1,0,-1].permutation(2).map{|i,j| [x+i, y+j] }
end
def adjacent?(hex)
adjacencies.include?(hex.coordinates)
end
def directions
Hash[%w(botright topright bottom top botleft topleft).zip(adjacencies)]
end
def port?
water? && !port_type.nil? && !port_direction.nil?
end
def port_borders?(settlement)
return false unless port_direction
return false unless settlement.vertex.include?(coordinates)
settlement.vertex.include?(directions[port_direction])
end
def port_accepts?(resource)
return 3 if port_type == '3:1'
return 2 if port_type == resource
false
end
end