-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.coffee
51 lines (43 loc) · 1.19 KB
/
Game.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#START:imports
{Dictionary} = require './Dictionary'
{Grid} = require './Grid'
{Words} = require './OWL2'
{Player} = require './Player'
#END:imports
class Game
@TURN_TIME = 30000 #milliseconds
constructor: ->
@grid = new Grid
@dictionary = new Dictionary(Words, @grid)
@currPlayer = @player1 = new Player(1, 'Player 1', @dictionary)
@player2 = @otherPlayer = new Player(2, 'Player 2', @dictionary)
@player1.id = @player2.id = null
@players = [@player1, @player2]
@wasPlayed = false
@timer = @interval = null
reset: ->
# reset scores and grid
player.score = 0 for player in @players
@dictionary.setGrid(@grid)
addPlayer: (sessionId) ->
@player1.id = sessionId
else
@player2.id = sessionId
removePlayer: (sessionId) ->
@playerWithId(sessionId).id = null
isFull: ->
if @player1.id and @player2.id then true else false
playerWithId: (sessionId) ->
if sessionId is @player1.id
@player1
else if sessionId is @player2.id
@player2
endTurn: ->
@wasPlayed = true
if @currPlayer is @player1
[@currPlayer, @otherPlayer] = [@player2, @player1]
else
[@currPlayer, @otherPlayer] = [@player1, @player2]
root = exports ? window
root.Game = Game