-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
74 lines (61 loc) · 1.65 KB
/
main.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
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
var boardObj;
var gridWidth = 8;
var STATES = {
NEW_GAME : 0,
BLACK_TURN : 1, // computer turn
RED_TURN : 2, // player turn
BLACK_WON : 3,
RED_WON : 4
}
var curState;
var setState = function(state) {
curState = state;
if (state == STATES.NEW_GAME ) {
} else if (state == STATES.BLACK_TURN) {
setTimeout(function() {
boardObj.makeRandomMove('black');
setState(STATES.RED_TURN);
}, Math.random() * 1000);
} else if (state == STATES.RED_TURN) {
} else if (state == STATES.BLACK_WON) {
} else if (state == STATES.RED_WON) {
}
}
$(document).ready(function() {
boardObj = new Board(gridWidth);
view = new View(boardObj);
boardObj.init();
var sqrWidth = view.sqrWidth;
setState(STATES.RED_TURN);
var selectedChecker;
$('#clickable').mousedown(function(e) {
var coords = view.getCoordsFromPoint(e.pageX, e.pageY);
var checker = boardObj.getChecker(coords[1], coords[0]);
if (curState == STATES.RED_TURN) {
if (checker && checker.isRed()) {
selectedChecker = checker;
}
}
});
$('#clickable').mousemove(function(e) {
if (selectedChecker) {
view.dragChecker(e.pageX, e.pageY, selectedChecker);
}
});
$('#clickable').mouseup(function(e) {
if (selectedChecker) {
var coords = view.getCoordsFromPoint(e.pageX, e.pageY);
var row = coords[1]; var col = coords[0];
if (boardObj.isValidMove(row, col, selectedChecker)) {
var loc = selectedChecker.loc;
boardObj.moveChecker(row, col, selectedChecker);
selectedChecker = null;
setState(STATES.BLACK_TURN);
} else {
var loc = selectedChecker.loc;
boardObj.moveChecker(loc[0], loc[1], selectedChecker);
selectedChecker = null;
}
}
});
});