-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.js
executable file
·140 lines (105 loc) · 3.05 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var squareWidth = 0;
var checkerBoard;
$(document).ready(function(){
initialize();
registerEvents();
});
function initialize(){
squareWidth = $(".checkersSquare").eq(1).width();
var whiteTemplate = $($("#piecesTemplateBlack").html());
var blackTemplate = $($("#piecesTemplateWhite").html());
//Create the board
checkerBoard = new CheckerBoard($("#checkersBoard"), whiteTemplate, blackTemplate);
var pieces = checkerBoard.pieces;
//Init the board
checkerBoard.board = createMultiDimensionalArray(8, 8);
//Create the pieces
var pieceDiv;
//Reset
checkerBoard.resetGame();
//checkerBoard.pieces = pieces;
checkerBoard.setBoardFromPieces();
$(".checkersSquare").each(function(){
var me = $(this);
var mePosition = me.position();
var position = {
x: (Math.floor(mePosition.left / squareWidth)),
y: (Math.floor(mePosition.top / squareWidth))
};
me.data("position", position);
});
}
//EVENTS
function registerEvents(){
$(".piece").click(pieceClicked);
$(".checkersSquare").click(squareClicked);
$("#newGameButton").click(newGameButtonClicked);
$("#closeNotification").click(closeNotificationClicked);
}
function pieceClicked(e){
var me = $(this);
var checkerObj = me.data("checker");
checkerBoard.checkerClicked(checkerObj);
}
function closeNotificationClicked(e){
$("#notificationDiv").hide("300");
}
function squareClicked(e){
var me = $(this);
var position = me.data("position");
checkerBoard.squareClicked(position.x, position.y);
callWhenDone(function(){
if (checkerBoard.currentPlayer == 1) {
calculateAIMove(checkerBoard);
}
//Chainjumper
if (checkerBoard.currentPlayer == 1) {
calculateAIMove(checkerBoard);
}
}, function(){
return !checkerBoard.movingPiece;
})
}
function newGameButtonClicked(e){
closeNotificationClicked(e);
checkerBoard.resetGame();
}
//Utilities
function createMultiDimensionalArray(rows, cols){
var result = new Array(rows);
for (var i = 0; i < rows; ++i)
result[i] = new Array(cols);
return (result);
}
function copyMultiDimensionalArray(array){
var copy = new Array(array.length);
for (var i = 0; i < array.length; ++i) {
copy[i] = new Array(array[i].length);
for (var j = 0; j < array[i].length; ++j)
copy[i][j] = array[i][j];
}
return copy;
}
//Call a function with a delay
function callWhenDone(callback, waitingFun){
if (waitingFun()) {
callback();
}
else {
setTimeout(function(){
callWhenDone(callback, waitingFun);
}, 300);
}
}
//HTML Alert
function gameAlert(message){
var container = $("#notificationDiv");
$(".notificationMessage", container).html(message);
container.show(300);
}
//Log function
function log(message){
if (!($.browser.msie)) {
console.log(message);
}
}