-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove.rb
50 lines (41 loc) · 866 Bytes
/
move.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
#
# Move - class representing move that can be executed on the board
#
class Move
include Comparable
# Available move directions
HORIZONTAL = 0
VERTICAL = 1
attr_reader :x, :y, :word, :orientation
class << self
attr_accessor :tiles_weights
end
self.tiles_weights = Hash.new
def initialize(path, x, y, word, orientation)
@path = path # path with weights for letters to be placed
@x = x
@y = y
@word = word
@orientation = orientation
end
#
# Returns: Hash
#
def tiles_weights
self.class.tiles_weights
end
#
# Calculate score for this move
# Returns: Fixnum
#
def score
return @score unless @score.nil?
@score = 0
letters = @word.chars
loop{ @score += @path.next * tiles_weights[letters.next] }
@score
end
def <=>(other)
score <=> other.score
end
end