dojo.provide("acf.CheckerBoard"); dojo.require("acf.Board"); dojo.declare("acf.CheckerBoard",[acf.Board], { constructor: function() { this.position = this.emptyPosition; this.move = 'b'; }, // Playable squares (l - light, d - dark, b - both, n - neither) playableSquares: 'd', // PDN (portable draughts notation) string for a position fen: '', // String position, each playable square is a character // w - white man, W - white king, b - black man, B - black king, . - empty position: '', // Array of all positions positionList: null, // Whose move it is (w or b) move: '', // Array of all moves moveList: null, // Default starting position for 8x8 checkers defaultPosition: 'bbbbbbbbbbbb........wwwwwwwwwwww', // Empty 8x8 board emptyPosition: '................................', // Image for White man wmImg: dojo.moduleUrl("acf","resources/images/wm.gif"), // Image for Black man bmImg: dojo.moduleUrl("acf","resources/images/bm.gif"), // Image for White king wkImg: dojo.moduleUrl("acf","resources/images/wk.gif"), // Image for Black king bkImg: dojo.moduleUrl("acf","resources/images/bk.gif"), // Image for Deleting pieces delImg: dojo.moduleUrl("acf","resources/images/trash_can.png"), drawPosition: function() { var p = this.position; for(var i = 0; i < p.length; i++) { var s = 'square_'+(i+1); dojo.attr(this[s],'value',p[i]); // Empty all squares before re-adding pieces if(this[s].hasChildNodes()) { dojo.empty(this[s]); } switch(p[i]) { case 'b': dojo.query(dojo.create('img',{src: this.bmImg})).place(this[s]); //TODO only do this in the correct mode // Sync the DnD this[s].dnd.sync(); break; case 'B': dojo.query(dojo.create('img',{src: this.bkImg})).place(this[s]); //TODO only do this in the correct mode // Sync the DnD this[s].dnd.sync(); break; case 'w': dojo.query(dojo.create('img',{src: this.wmImg})).place(this[s]); //TODO only do this in the correct mode // Sync the DnD this[s].dnd.sync(); break; case 'W': dojo.query(dojo.create('img',{src: this.wkImg})).place(this[s]); //TODO only do this in the correct mode // Sync the DnD this[s].dnd.sync(); break; default: break; } } }, // Determine if this square should accept the incoming piece _isAccepted: function(p_source,p_nodes) { // Check board mode // Check space against piece var s = this.square; var p = dojo.attr(p_nodes[0],'value'); /* console.log(s); console.log(p); */ return true; }, // On drop of a piece, set some values _doDrop: function(p_source,p_nodes,p_copy,p_target) { if(dojo.dnd.manager().target !== this) { return; } var s = this.square; var p = dojo.attr(p_nodes[0],'value'); // If not copying, we are moving around on the board // Need to unset the value on the from square if(!p_copy) { var f = p_source.square; this.board._setPiece(f,'.'); } /* console.log(s); console.log(p); */ this.board._setPiece(s,p); }, // Change the current position _setPiece: function(p_square,p_value) { // Borrowed regex expression to replace 1 char from // http://bytes.com/topic/javascript/answers/771950-change-single-character-string // string[num] = 'new string'; does not work var p = this.position; var re = new RegExp('^(.{'+ --p_square +'}).(.*)$',''); this.position = p.replace(re,'$1'+p_value+'$2'); }, generateFEN: function() { var s = this.move.toUpperCase(); var w = ''; var b = ''; var p = this.position; for(var i = 0; i < p.length; i++) { var sq = i+1; switch(p[i]) { case 'w': w += sq+','; break; case 'W': w += 'K'+sq+','; break; case 'b': b += sq+','; break; case 'B': b += 'K'+sq+','; break; default: break; } } w = w.replace(/\,$/,"") b = b.replace(/\,$/,"") s += ':W'+w+':B'+b; this.fen = s; }, flipBoard: function() { var b = (this._board) ? this._board : this; b._flipBoard(); b.drawPosition(); }, _removeBank: function() { var b = this.bank; if(b.hasChildNodes()) { dojo.empty(b); } b.style.display = 'block'; }, _createBank: function() { var b = this.bank; b.style.backgroundImage = 'url('+this.bankImg+')'; if(b.hasChildNodes()) { dojo.empty(b); } //TODO don't allow pieces to overlap var x = new dojo.dnd.Source(b,{copyOnly: true}); x.accept = null; // var s = dojo.create('div'); dojo.attr(s,'value','w'); dojo.addClass(s,'dojoDndItem piece'); dojo.query(dojo.create('img',{src: this.wmImg})).place(s); dojo.place(s,b); // s = dojo.create('div'); dojo.attr(s,'value','b'); dojo.addClass(s,'dojoDndItem piece'); dojo.query(dojo.create('img',{src: this.bmImg})).place(s); dojo.place(s,b); // s = dojo.create('div'); dojo.attr(s,'value','W'); dojo.addClass(s,'dojoDndItem piece'); dojo.query(dojo.create('img',{src: this.wkImg})).place(s); dojo.place(s,b); // s = dojo.create('div'); dojo.attr(s,'value','B'); dojo.addClass(s,'dojoDndItem piece'); dojo.query(dojo.create('img',{src: this.bkImg})).place(s); dojo.place(s,b); // var t = dojo.create('div'); t.style.width = s.offsetWidth; var y = new dojo.dnd.Source(t,{isSource: false}); var i = dojo.create('img',{src: this.delImg}); i.style.width = s.offsetWidth; i.style.height = s.offsetHeight; dojo.place(i,t); dojo.place(t,b); // Delete all nodes on drop dojo.connect(y,"onDndDrop",function () { this.deleteSelectedNodes(); }); y.startup(); // x.startup(); b.style.display = 'table-cell'; this._resize(); }, postCreate: function() { // Have to do this here because constructor doesn't read html yet if(!this.cols) { this.cols = this.rows; } this._createBoard(); this._createToolbar(); this.toggleNumbers(this.showNumbers); this._createBank(); } });