/build/cafego.js

http://cafego.googlecode.com/ · JavaScript · 7 lines · 1 code · 0 blank · 6 comment · 1 complexity · f9002a51519bf838fa2c907a9f2a6ae7 MD5 · raw file

  1. /*
  2. * CafeGo v1.0.1
  3. * http://dev.akinao.com/cafego/
  4. * Copyright 2009 Akinao Saito
  5. * Licensed under the GPLv3.
  6. */
  7. if("undefined"==typeof akinao){var akinao={}}akinao.namespace=function(c){var b=c.split("."),e=akinao;for(var d in b){e[b[d]]=e[b[d]]||{};e=e[b[d]]}};akinao.namespace("lang");akinao.lang={isArray:function(c){if(c){var b=akinao.lang;return b.isNumber(c.length)&&b.isFunction(c.splice)&&!b.hasOwnProperty(c.length)}return false},isBoolean:function(b){return typeof b==="boolean"},isFunction:function(b){return typeof b==="function"},isNull:function(b){return b===null},isNumber:function(b){return typeof b==="number"&&isFinite(b)},isObject:function(b){return(b&&(typeof b==="object"||akinao.lang.isFunction(b)))||false},isString:function(b){return typeof b==="string"},isUndefined:function(b){return typeof b==="undefined"},hasOwnProperty:function(b,c){if(Object.prototype.hasOwnProperty){return b.hasOwnProperty(c)}return !akinao.lang.isUndefined(b[c])&&b.constructor.prototype[c]!==a[c]},clone:function clone(c){var b=function(){};b.prototype=c;return new b}};akinao.namespace("game");(function(){var f=akinao.lang;function e(g,h){this.x=g;this.y=h}e.UP=new e(0,-1);e.DOWN=new e(0,1);e.RIGHT=new e(1,0);e.LEFT=new e(-1,0);e.DIR4=[e.UP,e.RIGHT,e.DOWN,e.LEFT];e.prototype.add=function(h,g){if(f.isUndefined(g)){return new e(this.x+h.x,this.y+h.y)}else{return new e(this.x+h,this.y+g)}};e.prototype.subtract=function(h,g){if(f.isUndefined(g)){return new e(this.x-h.x,this.y-h.y)}else{return new e(this.x-h,this.y-g)}};e.prototype.multiply=function(g){return new e(this.x*g,this.y*g)};e.prototype.divide=function(g){return new e(this.x/g,this.y/g)};e.prototype.dir4=function(){return[new e(this.x,this.y-1),new e(this.x+1,this.y),new e(this.x,this.y+1),new e(this.x-1,this.y)]};e.prototype.clone=function(){return new e(this.x,this.y)};e.prototype.equals=function(g){return this.x==g.x&&this.y==g.y};function c(h,g,n){var m;if(f.isUndefined(n)){if(f.isUndefined(h.x)){this.size=new e(h,h)}else{this.size=h}m=g}else{this.size=new e(h,g);m=n}this.map=[];for(var l=0;l<this.size.x;l++){this.map[l]=[];for(var k=0;k<this.size.y;k++){this.map[l][k]=m}}}c.prototype.get=function(h,g){if(f.isUndefined(g)){return this.map[h.x][h.y]}else{return this.map[h][g]}};c.prototype.set=function(i,h,g){if(f.isUndefined(g)){this.map[h.x][h.y]=i}else{this.map[h][g]=i}};c.prototype.clear=function(k){for(var h=0;h<this.map.length;h++){for(var g=0;g<this.map[h].length;g++){this.map[h][g]=k}}};c.prototype.isOut=function(g){return 0>g.x||0>g.y||this.size.x<=g.x||this.size.y<=g.y};c.prototype.size=function(){return this.size};function d(){this._value={}}d.prototype.get=function(h,g){var i=b(h,g);return this._value[i]};d.prototype.set=function(j,h,g){var i=b(h,g);return this._value[i]=j};function b(h,g){if(f.isUndefined(g)){return""+h.x+"-"+h.y}else{return""+h+"-"+g}}akinao.game.Point=e;akinao.game.PointMap=d;akinao.game.Board=c})();akinao.namespace("game");(function(){var f=akinao.lang;function k(l){this.name="SgfParseException";this.message=l;this.description=this.name+": message "+l}k.prototype=new Error;function g(l){this.name="SgfVariationsException";this.message=l;this.description=this.name+": message "+l}g.prototype=new Error;function i(l){this.val=l;this.len=l.length;this.tg=0;this.ch=""}i.prototype={get:function(){return this.ch},index:function(){return this.tg},isSkip:function(){return -1!=" \t\n\r\\".indexOf(this.ch)},isValueStart:function(){return"["==this.ch},isValueEnd:function(){return"]"==this.ch},isNodeStart:function(){return";"==this.ch},isNodeDemiliter:function(){return -1!=";()".indexOf(this.ch)},isArrayStart:function(){return"("==this.ch},isArrayEnd:function(){return")"==this.ch},next:function(){if(this.tg<this.len){this.ch=this.val.charAt(this.tg++);return true}else{return false}}};function b(m){var o=[],l="",n=true;do{if(m.isValueStart()){l="";n=false}else{if(m.isValueEnd()){o.push(l);n=true}else{if(n&&!m.isSkip()){if(1==o.length){o=o[0]}return o}l+=m.get()}}}while(m.next());throw new k("Missing end.")}function e(l,m){var o="";do{if(l.isValueStart()){m[o]=b(l);return m}else{if(l.isSkip()){if(0<o.length){throw new k("property name is invalid.")}}else{o+=l.get()}}}while(l.next());throw new k("Missing end.")}function d(l){var m={};if(l.isNodeStart()&&!l.next()){throw new k("Missing end.")}while(true){if(l.isNodeDemiliter()){return m}else{if(l.isSkip()){if(!l.next()){throw new k("Missing end.")}}else{var m=e(l,m)}}}}function c(l){if(!l.next()){throw new k("Missing end.")}var n=[],o=[];while(true){if(l.isArrayStart()){var m=c(l);o.push(m);if(!l.next()){throw new k("Missing end.")}}else{if(l.isArrayEnd()){if(0<o.length){n.push(o)}return n}else{if(l.isSkip()){if(!l.next()){throw new k("Missing end.")}}else{n.push(d(l))}}}}}function j(m){if(!m||false==f.isString(m)||0==jQuery.trim(m).length){return[{}]}var l=new i(m);while(true){if(!l.next()){throw new k("Missing end.")}if(l.isArrayStart()){break}}var n=c(l);if(!n[0]){n=[{}]}return n}function h(l){if(f.isUndefined(l)){this.sgf=[{}]}else{this.sgf=j(l)}this.tgAry=this.sgf;this.tgIdx=0;this.tgTree=[];this.count=0;while(this.next()){this.count++}this.first()}h.prototype.get=function(p){var o=this.tgAry[this.tgIdx];if(o.length){if(p){o=o[p]}else{var l=[];for(var m in o){l.push(o[m][0])}o=l}}return o};h.prototype.info=function(){return this.sgf[0]};h.prototype.next=function(o){if(this.tgAry[this.tgIdx].length){var m=this.tgAry[this.tgIdx];if(f.isUndefined(o)){o=0}var l=m[o];if(2>l.length){return false}this.tgTree.push(this.tgAry);this.tgAry=l;this.tgIdx=1;return true}else{if(this.tgIdx<this.tgAry.length-1){this.tgIdx++;return true}}return false};h.prototype.previous=function(){if(0==this.tgIdx){return false}else{this.tgIdx--;if(0==this.tgIdx&&0<this.tgTree.length){this.tgAry=this.tgTree.pop();this.tgIdx=this.tgAry.length-1}return true}};h.prototype.first=function(){while(this.previous()){}};h.prototype.append=function(q,r){var m={};for(var p in q){m[p]=q[p].toString()}if(this.tgAry[this.tgIdx].length){if(f.isUndefined(r)){this.tgAry[this.tgIdx].push([m])}else{var l=this.tgAry[this.tgIdx];l[r].push(m);this.tgTree.push(this.tgAry);this.tgAry=l[r];this.tgIdx=1}}else{if(this.tgIdx==this.tgAry.length-1){this.tgAry.push(m);this.tgIdx++}else{var q=this.tgAry.slice(this.tgIdx+1);var l=[q,[m]];var p=this.tgAry.length-1;while(this.tgIdx<p--){this.tgAry.pop()}this.tgAry.push(l);this.tgIdx++}}};h.prototype.remove=function(p){if(this.tgAry[this.tgIdx].length){if(f.isUndefined(p)){p=0}var l=this.tgAry[this.tgIdx];var o=[];if(0<p){o.push(l.slice(0,p)[0])}if(p<l.length-1){o.push(l.slice(p+1)[0])}if(1==o.length){this.tgAry.pop();for(var m in o[0]){this.tgAry.push(o[0][m])}}else{this.tgAry[this.tgIdx]=o}}else{if(0<this.tgIdx){var m=this.tgAry.length;while(this.tgIdx<m--){this.tgAry.pop()}this.tgIdx--;if(0==this.tgIdx&&0<this.tgTree.length){this.tgAry=this.tgTree.pop();this.tgIdx=this.tgAry.length-1}}}};h.prototype.toString=function(){function m(n,s){var q="";for(var p in n){var t=n[p];if(t.length){q+=(s?"(":"")+m(t,!s)+(s?")":"")}else{q+=";"+l(t)}}return q}function l(t){var q="";for(var s in t){q+=s;if(typeof t[s]==="string"){q+="["+t[s]+"]"}else{for(var n in t[s]){q+="["+t[s][n]+"]"}}}return q}return m([this.sgf],true)};akinao.game.Sgf=h})();akinao.namespace("game.go");(function(){var h=akinao.game.Point;var e=akinao.game.PointMap;var d=akinao.game.Board;var g=akinao.lang;var b=0;var k=1;var c=2;function f(l){this.clear(l)}function j(r,v){var n=new e();var t=r.get(v);var l=false;var s=[v];var o=0;n.set(true,v);while(s.length>o){var p=s[o];for(var m in h.DIR4){var q=p.add(h.DIR4[m]);if(r.isOut(q)){continue}var u=r.get(q);if(b==u){return[]}else{if(t==u){if(g.isUndefined(n.get(q))){s.push(q);n.set(true,q)}}else{l=true}}}o++}return l?s:[]}f.prototype.set=function(s,q){if(this.board.isOut(q)){return{error:"OutOfBoard"}}if(b!=this.board.get(q)){return{error:"NotEmpty"}}this.board.set(s,q);var w=f.anotherPlayer(s);var p=(0<j(this.board,q).length);var v=[];for(var n in h.DIR4){var o=q.add(h.DIR4[n]);if(this.board.isOut(o)){continue}if(w==this.board.get(o)){var l=j(this.board,o);if(0<l.length){for(var m in l){var u=l[m];v.push(u);this.board.set(b,u)}p=false}}}if(p){this.board.set(b,q);return{error:"Dead"}}var t=[];var r=[];t.push({set:true,player:s,point:q});r.push({set:true,player:s,point:q});for(var n in v){t.push({set:false,player:w,point:v[n]});r.push({set:false,player:w,point:v[n]})}this.turnCount++;this.hamaCount[s]+=v.length;this.turns.push(t);return{values:r}};f.prototype.previous=function(){if(0==this.turns.length){return{error:"First"}}this.turnCount--;var l=[];var n=this.turns.pop();for(var m in n){var o=n[m];if(false==o.set){this.hamaCount[f.anotherPlayer(o.player)]--}this.board.set(o.set?b:o.player,o.point);l.push({set:!o.set,player:o.player,point:o.point})}return{values:l}};f.prototype.get=function(l){return this.board.get(l)};f.prototype.lastTurn=function(){if(0==this.turns.length){return{}}var l=this.turns[this.turns.length-1];return this.turns[this.turns.length-1][0]};f.prototype.getAll=function(){var n=[];for(var m=0;m<this.board.size.x;m++){for(var p=0;p<this.board.size.y;p++){var l=new h(m,p);var o=this.get(l);if(f.EMPTY!=o){n.push({set:true,player:o,point:l})}}}return{values:n}};f.prototype.clear=function(l){this.board=new d(19,b);this.turnCount=0;this.hamaCount=[];this.hamaCount[k]=0;this.hamaCount[c]=0;this.turns=[];i(l,this.board)};f.prototype.size=function(){return this.board.size};f.prototype.count=function(){return this.sgf.count()};f.anotherPlayer=function(l){return 3-l};function i(p,o){if(g.isUndefined(p)){return{}}var n=[];if(2>p||9<p){return{}}if(2<=p){n.push(new h(15,3));n.push(new h(3,15))}if(3<=p){n.push(new h(15,15))}if(4<=p){n.push(new h(3,3))}if(5==p||7==p||9==p){n.push(new h(9,9))}if(6<=p){n.push(new h(3,9));n.push(new h(15,9))}if(8<=p){n.push(new h(9,3));n.push(new h(9,15))}for(var m in n){var l=n[m];o.set(k,l)}}f.EMPTY=b;f.BLACK=k;f.WHITE=c;akinao.game.go.Go=f})();(function(){var P=akinao.lang;var z=akinao.game.go.Go;var m=akinao.game.Point;var B=akinao.game.Sgf;var g={first:1000,middle:3000,slow:5000};D.board=[];function D(U){var R=$("#"+U);var T=R.text();R.html("");this._sgf=t(T);this._go=new z(this._sgf.info().HA);this._id=U;this._element=R;var Q=400;this._view={board:Q,stone:Q/20,padding:Q/40,numbering:true};this._infoWindow=h(this);this._controlWindow=f(this);this._play={isPlay:false,interval:g.middle,timerID:null,animate:[]};this._lockControl=false;D.board[this._id]=this;var S=this._go.getAll();if(false==P.isUndefined(S.values)){F(this._id,S.values)}L(this._id)}function t(Q){try{return new B(Q)}catch(R){return new B("(;ERR[SGF is invalid])")}}D.get=function(Q){return D.board[Q]};D.prototype.next=function(){x(this._id);if(false==this._sgf.next()){return}var R=H(this._sgf.get());if(R.error){return}var Q=this._go.set(R.player,R.point);if(Q.error){return}K(this._id,Q.values);L(this._id)};D.prototype.previous=function(){x(this._id);if(false==this._sgf.previous()){return}var Q=this._go.previous();if(Q.error){return}K(this._id,Q.values);L(this._id)};D.prototype.first=function(){x(this._id);this._go.clear();this._sgf.first();G(this._id);L(this._id)};D.prototype.last=function(){x(this._id);while(this._sgf.next()){var R=H(this._sgf.get());if(R.error){return}var Q=this._go.set(R.player,R.point);if(Q.error){break}F(this._id,Q.values)}p(this._id);L(this._id)};D.prototype.play=function(){if(this._sgf.count==this._go.turnCount){this.first()}if(this._play.isPlay){x(this._id)}else{this._play.isPlay=true;c(this._id);this._controlWindow.animate({top:this._view.board},500);this._infoWindow.animate({top:-this._infoWindow.outerHeight()+"px"},500)}C(this._id)};D.prototype.interval=function(Q){if(P.isNumber(Q)){this._play.interval=Q}else{if(false==P.isUndefined(g[Q])){this._play.interval=g[Q]}else{this._play.interval=g.midlle}}};D.prototype.numbering=function(R){if(R&&this._view.numbering==R){return}this._view.numbering=!this._view.numbering;u(this._id,this._view.numbering);var Q=this._go.lastTurn();I(this._id)};D.prototype.openInfo=function(Q){s({data:{id:this._id,open:Q}})};D.prototype.openControl=function(Q){e({data:{id:this._id,open:Q}})};D.prototype.sgf=function(Q){this._sgf=t(Q);this._go=new z(this._sgf.info().HA);o(this);this.first()};function u(Q,R){var V=D.board[Q];if(R){var X=V._go.turns;for(var U=0;U<X.length;U++){var Y=X[U];for(var S in Y){var W=Y[S];if(W.set){y(Q,W.point,U+1)}}}}else{M(Q)}var T=V._go.lastTurn();p(Q)}function c(T){var Q=D.board[T];if(false==Q._play.isPlay){return}if(false==Q._sgf.next()){Q._play.isPlay=false;C(T);return}var S=H(Q._sgf.get());if(S.error){return}var R=Q._go.set(S.player,S.point);if(R.error){Q._play.isPlay=false;C(T);return}L(Q._id);Q._play.animate=R.values;r(Q._id);return}function x(S){var Q=D.board[S];if(Q._play.isPlay){Q._play.isPlay=false;C(S)}if(0<Q._play.animate.length){var R=Q._play.animate;Q._play.animate=[];K(S,R)}}function s(R){var Q=D.board[R.data.id];if(R.data.open){Q._infoWindow.css({top:-Q._infoWindow.outerHeight()});Q._infoWindow.show();Q._infoWindow.animate({top:"0px"},500)}else{Q._infoWindow.animate({top:-Q._infoWindow.outerHeight()+"px"},500)}}function e(R){var Q=D.board[R.data.id];if(R.data.open){Q._controlWindow.animate({top:Q._view.board-Q._controlWindow.outerHeight()},500)}else{Q._controlWindow.animate({top:Q._view.board},500)}}function q(R){if(this._lockControl){return}this._lockControl=true;var Q=D.board[R.data.id];switch(R.data.control){case"next":Q.next();break;case"prev":Q.previous();break;case"first":Q.first();break;case"last":Q.last();break;case"play":Q.play();break;case"number":Q.numbering();break;default:break}this._lockControl=false}function L(R){var Q=D.board[R];$("#"+R+"-control-turn").html(A(Q._go.turnCount,3));$("#"+R+"-control-turn-count").html(Q._sgf.count);$("#"+R+"-hama-black").html(A(Q._go.hamaCount[z.BLACK],2));$("#"+R+"-hama-white").html(A(Q._go.hamaCount[z.WHITE],2))}function C(S){var Q=D.board[S];var R=$("#"+Q._id+"-control-play");R.removeClass("control-"+(Q._play.isPlay?"play":"pause"));R.addClass("control-"+(!Q._play.isPlay?"play":"pause"))}function I(S){var Q=D.board[S];var R=$("#"+Q._id+"-control-number");R.removeClass("control-"+(Q._view.numbering?"nonum":"number"));R.addClass("control-"+(!Q._view.numbering?"nonum":"number"))}function r(W){var R=D.board[W];if(0==R._play.animate.length){if(null!=R._play.timerID){clearTimeout(R._play.timerID);R._play.timerID=null}if(R._play.isPlay){R._play.timerID=setTimeout(function(){c(W)},R._play.interval)}return}var U=R._play.animate.shift();if(U.set){var T=b(W,U);var V=U.point.multiply(R._view.stone).add(R._view.padding,R._view.padding);if(z.BLACK==U.player){var Q=R._view.board}else{var Q=-R._view.stone}T.css({left:V.x+"px",top:Q});R._element.append(T);T.animate({top:V.y+"px"},500,function(){p(W);r(W)})}else{var T=j(W,U.point);if(z.BLACK==U.player){var S=-R._view.stone}else{var S=R._view.board}T.animate({left:S},500,function(){l(W,U.point);r(W)})}}function F(W,S){var Q=D.board[W];for(var R in S){var U=S[R];if(U.set){var T=b(W,U);var V=U.point.multiply(Q._view.stone).add(Q._view.padding,Q._view.padding);T.css({left:V.x+"px",top:V.y+"px"});Q._element.append(T)}else{l(W,U.point)}}}function K(R,Q){F(R,Q);p(R)}function p(S){var Q=D.board[S];var R=Q._go.lastTurn();if(R.set){$("#"+S+"-stone-now").remove();j(S,R.point).append(w("now",S+"-stone-now"))}}var v=["","black","white"];function H(Q){if(Q.B){return{player:z.BLACK,point:O(Q.B)}}else{if(Q.W){return{player:z.WHITE,point:O(Q.W)}}else{return{error:"turn is invalid."}}}}function h(R){var S=w("info");R._element.append(S);R._infoWindow=S;o(R);S.hide();S.bind("click",{id:R._id,open:false},s);var Q=w("hover");R._element.append(Q);Q.bind("mouseenter",{id:R._id,open:true},s);return S}function o(Q){var R=Q._infoWindow;var S=Q._sgf.info();R.text("");if(S.ERR){R.append("Error: "+S.ERR);return}if(S.PB){var T=S.PB;if(S.BR){T+=" "+S.BR}R.append("<span class='black'>"+T+"</span><br/>")}if(S.PW){var T=S.PW;if(S.WR){T+=" "+S.WR}R.append("<span class='white'>"+T+"</span><br/>")}var T="";if(S.RE){T+="Result:"+S.RE+" "}if(S.KM){T+="Komi:"+S.KM+" "}if(S.HA&&0!=S.HA){T+="Handicap:"+S.HA+" "}if(0<T.length){R.append(T+"<br/>")}if(S.EV){var T="Event:"+S.EV;if(S.RO){T+=" Round:"+S.RO}T+="<br/>";R.append(T)}var T="";if(S.PC){T+="Place:"+S.PC+" "}if(S.DT){T+="Date:"+S.DT+" "}if(0<T.length){R.append(T+"<br/>")}if(""==R.text()){R.append("Nothing...")}}function f(U){var T=w("control");U._element.append(T);var R=["first","prev","play","next","last","number"];for(var W in R){var S=R[W];var V=w("control-"+S,U._id+"-control-"+S);V.bind("click",{id:U._id,control:S},q);T.append(V)}T.append("<div class='control-text'>"+n(null,U._id+"-control-turn",0)+"/"+n(null,U._id+"-control-turn-count",0)+"&nbsp;"+n("black",U._id+"-hama-black",0)+"&nbsp;"+n("white",U._id+"-hama-white",0)+"</div>");var Q=w("control-click");T.append(Q);Q.css({width:U._view.board,height:U._view.board});Q.bind("click",{id:U._id,open:false},e);T.css({top:U._view.board});var X=w("hover");U._element.append(X);X.css({top:U._view.board-4+"px"});X.bind("mouseenter",{id:U._id,open:true},e);return T}function b(T,R){var Q=D.get(T);var S=(Q._view.numbering&&0<Q._go.turnCount)?Q._go.turnCount:"";return w(v[R.player],T+"-stone-"+N(R.point),S)}function y(S,Q,R){j(S,Q).text(R)}function w(Q,S,R){return jQuery(J(Q,S,R))}function J(Q,S,R){return k("div",Q,S,R)}function n(Q,S,R){return k("span",Q,S,R)}function k(Q,R,U,T){var S="<"+Q;if(U){S+=" id='"+U+"'"}if(R){S+=" class='"+R+"'"}if(T){S+=">"+T+"</"+Q+">"}else{S+=" />"}return S}function M(Q){$("#"+Q).find("div.black,div.white").text("")}function j(R,Q){return $("#"+R+"-stone-"+N(Q))}function l(R,Q){$("#"+R+"-stone-"+N(Q)).remove()}function G(Q){$("#"+Q).find("div.black,div.white").remove()}function O(Q){return new m(i(Q.charAt(0)),i(Q.charAt(1)))}function N(Q){return d(Q.x)+d(Q.y)}var E="abcdefghijklmnopqrstuvwxyz";function i(Q){return E.indexOf(Q)}function d(Q){return E.charAt(Q)}function A(T,S){var U=""+T;if(U.length<S){var R=S-U.length;for(var Q=0;Q<R;Q++){U="&nbsp;"+U}}return U}window.CafeGo=D;$(document).ready(function(){$(".cafego").each(function(){new D($(this).attr("id"))})})})();