-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess.js
138 lines (95 loc) · 3.26 KB
/
chess.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
$(function(){
//configuration object
var $config = {
debug : true, //true or false (console logs)
player1 : 0, //0 - white, 1 - black
player2 : 1, //0 - white, 1 - black
mode : "knight", //knight (find game) or chess (full game)
boardX : 8,
boardY : 8,
board : [], //save board positions
active : [], //mode = knight - places to go over, mode = game - active pieces
removed : [], //mode = knight - removed tiles, mode = game - removed pieces
paths : [], //copy removed list after search is completed
pieces : { //chess pieces in unicode (0 = white, 1 = black)
king : ["9812","9818"],
queen : ["9813","9819"],
rook : ["9814","9820"],
bishop: ["9815","9821"],
knight: ["9816","9822"],
pawn : ["9817","9823"]
}//pieces
}//config
// check for debug mode
if(!$config.debug) console.log = function(){};
// build board
$("#board").ready(function(){
var i,x,y,z,alpha;
console.log("BUILDING BOARD:");
//create board
for(i=1, y=8; y >= 1; --y, ++i){
for(x=65, z=1; x <= 72; ++x, ++i, ++z){
alpha = String.fromCharCode(x);
console.log(y+alpha+" - "+z+"_"+y);
$config.board.push(z+"_"+y); //include positions on $config object
//black and white tiles
if( i%2 == 0){
$("#board").append("<dd id='"+z+"_"+y+"' target='"+alpha+y+"'></dd>");
}else{
$("#board").append("<dt id='"+z+"_"+y+"' target='"+alpha+y+"'></dt>");
}//if
}//for x
}//for y
});//ready()
// place pieces on board
$("#H1").ready(function(){ //H1 is the last position on the board
$("#wrapper").fadeIn(1000);
console.log("board rendered, placing pieces");
//checking for game mode
if($config.mode == "knight"){
console.log("mode knight");
modeKnight();
}else if($config.mode == "chess"){
console.log("mode chess");
//modeChess();
}
});
// move pieces
var moveKnight = function($from, $to){
}
//create game in mode knight
var modeKnight = function(){
var pknight = random($config.board.length-1), //random number for knight position
pking = random($config.board.length-1); //random number for king position
//place knight
placePiece( $config.pieces.knight[$config.player1],
$config.board[pknight]);
//make knight piece active
$("#"+$config.board[pknight]).addClass("knight");
$config.active.push($config.board[pknight]);
//place king
placePiece( $config.pieces.king[$config.player2],
$config.board[pking]);
};//modeKnight()
//place piece on board
var placePiece = function($piece, $position){
$("#"+$position).html("&#"+$piece).addClass($piece);
};//placePiece()
//generates random numbers
var random = function($range){
if(arguments.length == 1) return Math.floor(Math.random()*$range);
};//random()
//TESTS - DELETE LATER
$("#board dd, #board dt").live("click",function(){
if($(this).hasClass("active"))
$(this).css("background","white").removeClass("active");
else{
$(this).css("background","red").addClass("active");
}
}).live("mouseover", function(){
if( !$(this).hasClass("active")) $(this).css("background","green");
}).live("mouseout", function(){
if( !$(this).hasClass("active")) $(this).css("background","white");
});
console.log($config);
});//function()