Class: RouteObject

Inherits:
Object
  • Object
show all
Includes:
Colorful
Defined in:
lib/ttr/objects/route.rb

Overview

Copyright © 2011 Jesse Sielaff

Instance Attribute Summary collapse

Attributes included from Colorful

#color

Instance Method Summary collapse

Methods included from Colorful

#exact_match?, #match?

Constructor Details

#initialize(city_objs, length, color) ⇒ RouteObject

Returns a new instance of RouteObject.



7
8
9
10
11
12
13
14
15
# File 'lib/ttr/objects/route.rb', line 7

def initialize (city_objs, length, color)
  @city_objs = city_objs
  @color = color
  @length = length
  @player_obj = nil
  
  x = length.to_f
  @points = (Math.sin(4*x/3)*x*x*x/250 + 23*x*x/78 + 7*x/13).round
end

Instance Attribute Details

#city_objsObject (readonly)

Returns the value of attribute city_objs.



20
21
22
# File 'lib/ttr/objects/route.rb', line 20

def city_objs
  @city_objs
end

#lengthObject (readonly)

Returns the value of attribute length.



20
21
22
# File 'lib/ttr/objects/route.rb', line 20

def length
  @length
end

#player_objObject

Returns the value of attribute player_obj.



19
20
21
# File 'lib/ttr/objects/route.rb', line 19

def player_obj
  @player_obj
end

#pointsObject (readonly)

Returns the value of attribute points.



20
21
22
# File 'lib/ttr/objects/route.rb', line 20

def points
  @points
end

Instance Method Details

#connects?(*city_objs) ⇒ Boolean

Returns true if the Route connects the given Cities, false otherwise.

Returns:

  • (Boolean)


24
25
26
# File 'lib/ttr/objects/route.rb', line 24

def connects? (*city_objs)
  (@city_objs & city_objs).length == 2
end

#into?(city_obj) ⇒ Boolean

Returns true if the Route connects to the given City, false otherwise.

Returns:

  • (Boolean)


30
31
32
# File 'lib/ttr/objects/route.rb', line 30

def into? (city_obj)
  @city_objs.include?(city_obj)
end

#twinObject

If there are two Routes connecting the two Cities at the end of this Route, returns the other Route. Otherwise, returns nil.



37
38
39
# File 'lib/ttr/objects/route.rb', line 37

def twin
  (@city_objs[0].routes_to(@city_objs[1]) - [self])[0]
end