-
Notifications
You must be signed in to change notification settings - Fork 0
/
tafl-controller.js
46 lines (35 loc) · 1.49 KB
/
tafl-controller.js
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
/*jslint browser:true */
/*globals team */
(function (exports, team) {
"use strict";
exports.BoardController = (function BoardControllerClosure() {
function BoardController(boardModel, boardView) {
var activePlayer = boardModel.activePlayer;
function redrawCells(boardModel) {
boardModel.state.forEach(function (row, r) {
row.forEach(function (col, c) {
var type = boardModel.state[r][c];
boardView.draw(type.symbol, r, c);
});
});
}
boardView.onPlayerChange(activePlayer);
boardModel.addEventListener("newturn", function (player) {
redrawCells(boardModel);
boardView.onPlayerChange(player);
});
boardView.addEventListener("playermove", function (move) {
try {
boardModel.update({ player: activePlayer, from: move.from, to: move.to });
activePlayer = (activePlayer === team.attackers) ? team.defenders : team.attackers;
} catch (err) {
boardView.onError(err);
}
});
boardModel.addEventListener("victory", boardView.onWin.bind(boardView));
boardModel.addEventListener("capture", boardView.onCapture.bind(boardView));
redrawCells(boardModel);
}
return BoardController;
}());
}(this, team));