-
Notifications
You must be signed in to change notification settings - Fork 4
/
game.js
149 lines (132 loc) · 4.04 KB
/
game.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
140
141
142
143
144
145
146
147
148
/* Chess
* By Dave Fowler
*/
/* Note, all functions are tacked on to the instance instead of the class prototype because of the odd way chess.js does objects. */
var chess = new Chess();
/* Utility Functions */
_.mixin({
// same as _.range but returns the alphabetic equivalent
alphaRange: function(start, stop) {
var alpha = "abcdefghijklmnopqrstuvwxyz";
return _.map(_.range(start, stop), function(i) { return alpha[i] });
},
// return a string with the first instance of the character replaced
removeFirst: function(str, c) {
var i = str.indexOf(c);
return str.slice(0, i) + str.slice(i+1, str.length);
},
// a boolean of whether a character is upper case
isUpperCase: function(c) {
return c == c.toUpperCase();
},
tally: function(arr) {
var r = {};
_.each(arr, function(c) {
r[c] = r[c] ? r[c] + 1 : 1;
});
return r;
},
});
/* END Utility Functions */
chess.symbols = {'w':
{'k': '♔',
'q': '♕',
'r': '♖',
'b': '♗',
'n': '♘',
'p': '♙',
},
'b':
{'k': '♚',
'q': '♛',
'r': '♜',
'b': '♝',
'n': '♞',
'p': '♟',
}
}
chess.boardTemplate = "<table><% "
+ "_.each(_.range(8, 0, -1), function(i) { "
+ "%> <tr> <%"
+ "_.each(_.alphaRange(0, 8), function(a) { "
+ "var piece = chess.get(a+i); %>"
+ "<td class='square <%= chess.square_color(a+i) %>' data-square='<%= a+i %>' > <%"
+ "if (piece) { %> "
+ "<span class='piece' data-color='<%= piece.color %>' data-type='<%= piece.type %>' >"
+ "<%= chess.symbols[piece.color][piece.type] %>"
+ "</span> <% "
+ "} %>"
+ "</td><% "
+ "}); "
+ "%> </tr> <%"
+ "}); %>"
+ "</table>";
chess.renderBoard = function (node) {
node = node || "#board";
$(node).html(_.template(chess.boardTemplate));
$('.piece').draggable({
'cursor': 'hand',
});
$('.square').droppable({
accept: '.piece',
hoverClass: 'hover',
drop: function(event, ui) {
var piece = ui.draggable;
var origional_square = $(piece).parent().data('square');
chess.move({from: origional_square, to: $(this).data('square'), promotion: $('#promotion option:selected').val() });
chess.render(node);
},
});
}
chess.render = function() {
chess.renderBoard();
$("#turn").html(chess.symbols[chess.turn()]['k']);
$('#moves').html('<li>' + chess.pgn({newline_char: '</li><li>', 'max_width': 5}) + '</li>');
var dead = chess.dead();
$('#dead').html(
'<li>' + _.reduce(_.map(['w', 'b'], function(color) {
return _.reduce(dead[color], function(memo, piece) {
console.log('piece dead', memo, piece);
return memo + " " + piece; }, "")
}), function (m, c) { return m + c + '</li><li>' }, '')
+ '</li>');
if (chess.game_over()) {
result = chess.turn() == 'b' ? 'White Wins!' : 'Black Wins!';
if (chess.in_draw()) {
result = "Draw";
} else if (chess.in_stalemate()) {
result = "Stalemate";
} else if (chess.in_threefold_repetition()) {
result = "Threefold Repetition";
} else if (chess.insufficient_material()) {
result = "Insufficient Material";
}
$("#turn").html(
"<span class='option-title gameover'>Game Over - " + result + "</span>"
);
}
if (chess.turn() == 'b' && $('#vs option:selected').val() == 'computer') {
setTimeout(function() {
var move = startAlphaBeta(chess, 1);
chess.move(move);
chess.render();
}, 0);
}
}
// Return the fen characters for the peices that are dead
chess.dead_fen = function() {
var dead = 'rnbqkbnrppppppppPPPPPPPPRNBQKBNR';
var pieces = fen_pieces(chess);
_.each(_.toArray(pieces), function(piece) {
dead = _.removeFirst(dead, piece);
});
return dead;
}
chess.dead = function() {
dead = {'w': [], 'b': []};
_.map(_.toArray(this.dead_fen()), function(piece) {
var color = _.isUpperCase(piece) ? 'w' : 'b';
dead[color].unshift(chess.symbols[color][piece.toLowerCase()]);
});
return dead;
}