-
Notifications
You must be signed in to change notification settings - Fork 1
/
javascript.js
57 lines (46 loc) · 1.86 KB
/
javascript.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
$(document).ready(function(){
function stringColorToNumber(color){ return color == "white" ? state.WHITE : state.BLACK; }
function numberColorToString(color){ return color == state.WHITE ? "white" : "black"; }
Board.onFinish = function(winner){
alert("Winner: " + numberColorToString(winner));
};
Board.onTurnStart = function(turn){
var color = numberColorToString(turn);
$('fieldset[data-color="' + color + '"]').attr('data-turn', 1);
};
Board.onTurnEnd = function(turn){
var color = numberColorToString(turn);
$('fieldset[data-color="' + color + '"]').removeAttr('data-turn');
$('.invalid-move-message').css('visibility', 'hidden');
};
Board.onInvalidMove = function(message){
$('.invalid-move-message[data-color="' + numberColorToString(Board.state.turn) + '"]').text(message).css('visibility', 'visible');
};
var EventHandlers = {
playerTypeChange : function(evt){
Board.playerType[evt.data.color] = $(this).val();
evt.data.container.attr("data-type", $(this).val());
if(Board.state.turn == evt.data.color) Board.onMove();
},
playerLevelChange : function(evt){
Board.playerDifficulty[evt.data.color] = parseInt($(this).val());
},
restart : function(){
Board.reset();
Board.onMove();
}
};
$('#players fieldset').each(function(){
var color = stringColorToNumber($(this).attr('data-color'));
var data = {color : color, container : $(this)};
$(this).find('select[data-name="type"]').bind('change', data, EventHandlers.playerTypeChange);
$(this).find('select[data-name="level"]').bind('change', data, EventHandlers.playerLevelChange);
});
$('input[type=checkbox][data-option]').click(function(){
Board.options[$(this).attr('data-name')] = $(this).prop('checked');
});
$('#options > div:first-child').click(function(){
$(this).nextAll('ul').toggle(300);
});
$('#bt-restart').click(EventHandlers.restart);
});