forked from davidegp/5x5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.coffee
77 lines (66 loc) · 1.7 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
{Dictionary} = require './Dictionary'
{Grid} = require './Grid'
{Player} = require './Player'
{Words} = require './nonfetlc.js'
class Game
@TURN_TIME = 60000 # milliseconds
@MAX_MOVES = 10
constructor: (@player1, @player2) ->
@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
for player in @players
player.score = 0
player.moveCount = 0
@dictionary.setGrid(@grid)
addPlayer: (sessionId, username) ->
@player1.id = sessionId
@player1.name = username
@player1.num = 1
else
@player2.id = sessionId
@player2.name = username
@player2.num = 2
removePlayer: (sessionId) ->
@playerWithId(sessionId).id = null
isFull: ->
if @player1.id and @player2.id
true
else
false
isGameOver: ->
if (@player1.moveCount >= Game.MAX_MOVES) and (@player2.moveCount >= Game.MAX_MOVES)
true
else
false
# Returns the winner of the game. Null if there is no winner.
winner: ->
if not @isGameOver()
null
else if @player1.score > @player2.score
@player1
else if @player1.score < @player2.score
@player2
else
null
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