-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayer.coffee
37 lines (31 loc) · 1 KB
/
Player.coffee
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
tileValues =
A: 1, B: 3, C: 3, D: 2, E: 1, F: 4, G: 2, H: 4, I: 1, J: 8, K: 5, L: 1
M: 3, N: 1, O: 1, P: 3, Q: 10, R: 1, S: 1, T: 1, U: 1, V: 4, W: 4, X: 8,
Y: 4, Z: 10
scoreMove = (dictionary, swapCoordinates) ->
{x1, y1, x2, y2} = swapCoordinates
words = dictionary.wordsThroughTile(x1, y1).concat dictionary.wordsThroughTile(x2, y2)
moveScore = multiplier = 0
newWords = []
for word in words when dictionary.isWord(word) and dictionary.markUsed(word)
multiplier++
moveScore += tileValues[letter] for letter in word
newWords.push word
moveScore *= multiplier
{moveScore, newWords}
class Player
constructor: (@num, @name, dictionary) ->
@setDictionary dictionary if dictionary?
setDictionary: (@dictionary) ->
@score = 0
@moveCount = 0
makeMove: (swapCoordinates) ->
@dictionary.grid.swap swapCoordinates
@moveCount++
result = scoreMove @dictionary, swapCoordinates
@score += result.moveScore
result
toJSON: ->
{@num, @name, @score}
root = exports ? window
root.Player = Player