Tizen 2.0 Release
[samples/web/Go.git] / js / go.js
1 /*
2  * Copyright (c) 2012, Intel Corporation.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9
10 var Go = {
11         texture: {
12                 'black':'images/GO_BlackPiece_010612_a.png',
13                 'black1':'images/GO_BlackPiece_010612_b.png',
14                 'black2':'images/GO_BlackPiece_010612_c.png',
15                 'white':'images/GO_WhitePiece_010612_a-2.png',
16                 'white1': 'images/GO_WhitePiece_010612_b.png',
17                 'white2': 'images/GO_WhitePiece_010612_c.png',
18                 'uncheck':'images/GO_Checkbox_010612_a.png',
19                 'checked':'images/GO_CheckboxChecked_010612_a.png',
20                 'board':'images/Go_Board.png',
21         },
22         bounder: 13,
23         boardview:'board',
24         board:[],
25         playButton:'play_button',
26         winMessage:'win_panel',
27         settingPanel:'setting_panel',
28         helpPanel:'help_panel',
29         place: [],
30         directs: [[1,0],[0,1],[0,-1],[-1,0],],
31         player:{},
32         manual:[],
33         isTimer: false,
34         isSound: false,
35         isSetting: false,
36         isStart: false,
37         isStop: false,
38         isPass: false,
39         isExit: false,
40         isRestart: false,
41         isEndMsg: false,
42 };
43
44 Go.init = function(){
45         var welcome = {
46                 'black':[[1,3],[1,4],[2,2],[3,2],
47                                  [4,2],[5,2],[5,4],[5,5],
48                                  [6,2],[6,5],[7,2],[7,5],[8,3],
49                                  [8,4],],
50
51                 'white':[[1,8],[1,9],[2,7],[2,10],
52                                  [3,7],[3,10],[4,7],[4,10],
53                                  [5,7],[5,10],[6,7],[6,10],
54                                  [7,7],[7,10],[8,8],[8,9],],
55         };
56         for (var i=0; i<this.bounder; i++){
57                 var line=new Array();
58                 for (var j=0; j<this.bounder; j++){
59                         line[j] = 'board';
60                 }
61                 this.board[i] = line;
62         }
63         for (var color in welcome){
64                 var matrix = welcome[color];
65                 for (var p in matrix) {
66                         this.board[matrix[p][0]][matrix[p][1]] = color;
67                 }
68         }
69         this.drawBoard();
70         var soundSource = {
71                 'end': 'sounds/GameEndChimes.wav',
72                 'setStone': 'sounds/Pieces_SinglePlaced.wav',
73                 'dropStone': 'sounds/PiecesFillPocket.wav',
74                 'positive': 'sounds/PositiveSound.wav',
75                 'settingbtn': 'sounds/SettingsButton.wav',
76                 'settingck': 'sounds/SettingsButtonCheck.wav',
77                 'settingon': 'sounds/SettingsAppearWoodSlide.wav',
78         };
79
80         this.sounds = {};
81         for (var snd in soundSource){
82                 this.sounds[snd] = new Audio(soundSource[snd]);
83                 //this.sounds[snd].load();
84         }
85         this.sounds['dida'] = new gamesound('dida');
86
87         this.disable('.'+this.winMessage);
88         this.enable('.'+this.playButton);
89
90 }
91
92 Go.start = function(){
93         for (var i=0; i<this.bounder; i++){
94                 for (var j=0; j<this.bounder; j++){
95                         this.board[i][j] = 'board';
96                 }
97         }
98         this.player['black'] = new player('black');
99         this.player['white'] = new player('white');
100         this.current = this.player['white'];
101         this.isStart = true;
102         this.isStop = false;
103         this.isPass = false;
104         this.isSetting =false;
105         this.isRestart = false;
106         this.isExit = false;
107         this.isEndMsg = true;
108         this.drawBoard();
109         this.drawMessage();
110         this.disable('.'+this.playButton);
111         this.disable('.'+this.winMessage);
112         this.disable('.'+this.settingPanel);
113         this.disable('.'+this.helpPanel);
114         this.playSound('settingbtn');
115         // this.startTimer();
116
117         $('.setting_restart img').attr('src', this.texture['uncheck'])
118                 .removeClass('setting_restart_checked')
119                 .addClass('setting_restart_check');
120 }
121
122 Go.playSound = function(snd){
123         if (this.isSound) {
124                 var audio = this.sounds[snd];
125                 if (audio.paused == false) {
126                         audio.pause();
127                         audio.currentTime = 0;
128                 }
129                 audio.play();
130         }
131 }
132
133 Go.stop = function(){
134         this.isStart = false;
135         this.isStop = true;
136         this.stopTimer();
137         var blackScore = this.player['black'].getScore();
138         var whiteScore = this.player['white'].getScore();
139         if (blackScore > whiteScore){
140                 $('#win_player').html(' '+getMessage('one', 'One'));
141                 $('#win_result').html(getMessage('win', 'Wins'));
142         } else if (blackScore < whiteScore) {
143                 $('#win_player').html(' '+getMessage('two', 'Two'));
144                 $('#win_result').html(getMessage('win', 'Wins'));
145         } else {
146                 $('#win_player').html('');
147                 $('#win_result').html(getMessage('draw', 'Draw'));
148         }
149         if (this.isEndMsg) {
150                 this.playSound('end');
151                 this.enable('.'+this.winMessage);
152                 this.isEndMsg = false;
153         }
154 }
155
156 Go.exit = function(){
157         if (this.isExit) {
158                 window.close();
159         }
160 }
161
162 Go.restart = function(){
163         if (this.isRestart){
164                 if (this.isEndMsg) {
165                         this.stop();
166                 } else {
167                         this.start();
168                 }
169         }
170 }
171
172 Go.startTimer = function(){
173         if (this.isTimer && this.isStart) {
174                 this.timer = setTimeout('Go.startTimer()',1000);
175                 this.current.timeDida();
176                 // var audio = this.sounds['dida'];
177                 // if (audio.paused) {
178                 //      this.playSound('dida');
179                 // }
180                 this.playSound('dida');
181                 if (this.current.restTime <= 0) {
182                         this.stop();
183                 } else if (this.current.restTime == 5) {
184                         this.playSound('positive');
185                 }
186         }
187 }
188
189 Go.stopTimer = function(){
190         if (this.isTimer) {
191                 clearTimeout(this.timer);
192                 // this.sounds['dida'].pause();
193         }
194 }
195
196 Go.disable = function(which) {
197         $(which).addClass('display_none');
198 }
199
200 Go.enable = function(which) {
201         $(which).removeClass('display_none');
202 }
203
204 Go.toggleSetting = function(){
205         if ($('.'+this.winMessage).hasClass('display_none')) {
206                 if (this.isSetting){
207                         this.exitSetting();
208                 } else {
209                         this.showSetting();
210                 }
211                 this.playSound('settingbtn');
212         }
213 }
214
215 Go.showSetting = function(){
216         this.enable('.setting_panel');
217         setTimeout("$('.setting_panel').addClass('setting_panel_in')", 100);
218         this.isSetting = true;
219         this.stopTimer();
220         this.playSound('settingon');
221 }
222
223 Go.exitSetting = function(){
224         this.disable('.setting_panel');
225         $('.'+this.helpPanel).addClass('display_none');
226         $('.setting_panel').removeClass('setting_panel_in');
227         this.isSetting = false;
228         this.exit();
229         this.restart();
230         this.startTimer();
231 }
232
233 Go.toggleSound = function(){
234         var img = $('.setting_sound img');
235         if (this.isSound) {
236                 img.attr('src', this.texture['uncheck']);
237                 img.removeClass('setting_sound_checked');
238                 img.addClass('setting_sound_check');
239                 this.isSound = false;
240         } else {
241                 img.attr('src', this.texture['checked']);
242                 img.addClass('setting_sound_checked');
243                 img.removeClass('setting_sound_check');
244                 this.isSound = true;
245         }
246         this.playSound('settingck');
247 }
248
249 Go.toggleTimer = function(){
250         var img = $('.setting_timer img');
251         if (this.isTimer) {
252                 img.attr('src', this.texture['uncheck']);
253                 img.removeClass('setting_timer_checked');
254                 img.addClass('setting_timer_check');
255                 this.isTimer = false;
256         } else {
257                 img.attr('src', this.texture['checked']);
258                 img.addClass('setting_timer_checked');
259                 img.removeClass('setting_timer_check');
260                 this.isTimer = true;
261         }
262         this.playSound('settingck');
263 }
264
265 Go.toggleRestart = function(){
266         if (this.isStart || this.isStop) {
267                 var conflict = false;
268                 var img = $('.setting_restart img');
269                 if (this.isRestart) {
270                         img.attr('src', this.texture['uncheck']);
271                         img.removeClass('setting_restart_checked');
272                         img.addClass('setting_restart_check');
273                         this.isRestart = false;
274                 } else {
275                         img.attr('src', this.texture['checked']);
276                         img.addClass('setting_restart_checked');
277                         img.removeClass('setting_restart_check');
278                         this.isRestart = true;
279                         if (this.isExit)
280                                 conflict = true;
281                 }
282                 if (conflict) {
283                         this.toggleQuit();
284                 } else {
285                         this.playSound('settingck');
286                 }
287         }
288 }
289
290 Go.toggleQuit = function(){
291         var img = $('.setting_quit img');
292         var conflict = false;
293         if (this.isExit) {
294                 img.attr('src', this.texture['uncheck']);
295                 img.removeClass('setting_quit_checked');
296                 img.addClass('setting_quit_check');
297                 this.isExit = false;
298         } else {
299                 img.attr('src', this.texture['checked']);
300                 img.addClass('setting_quit_checked');
301                 img.removeClass('setting_quit_check');
302                 this.isExit = true;
303                 if (this.isRestart)
304                         conflict = true;
305         }
306         if (conflict) {
307                 this.toggleRestart();
308         } else {
309                 this.playSound('settingck');
310         }
311 }
312
313 Go.toggleHelp = function() {
314         $('.'+this.helpPanel).toggleClass('display_none');
315 }
316
317 Go.drawBoard = function(){
318         var str = '';
319         var style=['margin:1px 0px 9px 0px;','margin:0px 10px 9px 0px;','margin:0px 9px 10px 0px;'];
320         for (var i=0; i<this.bounder; i++){
321                 var margin=style[2];
322                 if (i<6) margin = style[0];
323                 str += '<div style="width:595px;height:36px;'+margin+'">';
324             for (var j=0; j<this.bounder; j++){
325                         margin = style[2];
326                         if (j<6) margin = style[1];
327                         if (i>8) margin += 'position:relative;top:-5px;';
328                         str += '<span style="float:left;width:36px;height:36px;'+margin
329                                 +'"><a onClick="javascript:Go.click('+i+','+j+');" class="img_style" ><img src="'
330                                 +this.texture[this.board[i][j]]+'" id="a'+i+j+'" class="board_img" /></a></span>';
331             }
332                 str += '</div>';
333         }
334         $('.'+this.boardview).html(str);
335 }
336
337 Go.click = function(i, j){
338         if (this.board[i][j]==='board' && !this.isSetting && this.isStart) {
339                 var revColor = (this.current.color=='black'?'white':'black');
340                 var p = this.player[revColor];
341
342                 this.board[i][j] = this.current.color;
343                 var takes = this.getTake([i, j]);
344                 if (takes.length == 1 && p.takes.length == 1
345                         && p.takes[0][0] == i && p.takes[0][1] == j
346                         && takes[0][0] == p.place[0] && takes[0][1] == p.place[1]) {
347                         //Can't do mirror go
348                         this.board[i][j] = 'board';
349                         return;
350                 }
351                 for (var n in takes) {
352                         this.board[takes[n][0]][takes[n][1]] = 'board';
353                         p.dropStone();
354                         p.score--;
355                 }
356                 if (takes.length == 0 && this.isContained(i, j, this.current.color)) {
357                         this.board[i][j] = 'board';
358                 } else {
359                         var status = {'color':this.current.color,};
360                         this.playSound('setStone');
361                         if (takes.length > 0)
362                                 this.playSound('dropStone');
363                         status.setPlace = [i, j];
364                         status.place = this.place;
365                         this.place = [i, j];
366                         this.current.score++;
367                         status.userPlace = this.current.place;
368                         this.current.place = this.place;
369                         this.current.takes = takes;
370                         status.takes = takes;
371                         this.current.pickStone();
372                         status.isPass = this.isPass;
373                         this.isPass = false;
374
375                         // Get Liberty
376                         var liberty = this.getLiberty();
377                         status.liberty = {};
378                         status.liberty['black'] = this.player['black'].liberty;
379                         status.liberty['white'] = this.player['white'].liberty;
380                         this.player['black'].liberty = liberty['black'];
381                         this.player['white'].liberty = liberty['white'];
382
383                         status.restTime = {};
384                         status.restTime['black'] = this.player['black'].restTime;
385                         status.restTime['white'] = this.player['white'].restTime;
386                         this.manual.push(status);
387                         this.drawBoard();
388                         this.drawMessage();
389                 }
390         }
391 }
392
393 Go.undue = function(color_) {
394         if (!this.isSetting && this.isStart && color_ != this.current.color) {
395                 var status = this.manual.pop();
396                 if (status) {
397                         var color = status.color;
398                         var revColor = (color == 'black'?'white':'black');
399                         var player = this.player[color];
400                         if (status.setPlace.length > 0) {
401                                 this.place = status.place;
402                                 player.dropStone();
403                                 player.score--;
404                                 this.board[status.setPlace[0]][status.setPlace[1]] = 'board';
405                                 for (var p in status.takes) {
406                                         var take = status.takes[p];
407                                         this.player[revColor].pickStone();
408                                         this.board[take[0]][take[1]] = revColor;
409                                         this.player[revColor].score++;
410                                 }
411                                 if (status.liberty) {
412                                         var liberty = status.liberty;
413                                         this.player['black'].liberty = liberty['black'];
414                                         this.player['white'].liberty = liberty['white'];
415                                 } else {
416                                         this.player['black'].liberty = [];
417                                         this.player['white'].liberty = [];
418                                 }
419                                 player.place = status.userPlace;
420                                 this.drawBoard();
421                         }
422                         this.isPass = status.isPass;
423                         // Reset the timer
424                         // this.player['black'].restTime = status.restTime['black'];
425                         // this.player['white'].restTime = status.restTime['white'];
426                         this.drawMessage();
427                 }
428         }
429 }
430
431 Go.getTake = function(place_) {
432         var p = place_ || this.place;
433         var checkColor = (this.current.color=='black'?'white':'black');
434         var takes = [];
435         for (var d in this.directs) {
436                 var i = parseInt(p[0])+parseInt(this.directs[d][0]);
437                 var j = parseInt(p[1])+parseInt(this.directs[d][1]);
438                 if (i>=0 && j>= 0 && i<this.bounder && j<this.bounder
439                         && this.board[i][j] === checkColor) {
440                         var t = [];
441                         if (this.isContained(i, j, checkColor, t)) {
442                                 for (var n in t){
443                                         if (!this.isContain(t[n], takes)) {
444                                                 takes.push(t[n]);
445                                         }
446                                 }
447                         }
448                 }
449         }
450         //console.log(takes);
451         return takes;
452 }
453
454 Go.isContained = function(i_, j_, color_, contains_, except_) {
455         var color = color_ || (this.current.color == 'black'?'white':'black');
456         var except = except_ || [];
457         var contains = contains_ || [];
458         var ret = true;
459         except.push([parseInt(i_),parseInt(j_)]);
460
461         for (var d in this.directs) {
462                 if (!ret)
463                         break;
464                 var i = parseInt(i_)+parseInt(this.directs[d][0]);
465                 var j = parseInt(j_)+parseInt(this.directs[d][1]);
466                 if ( i>=0 && j>= 0 && i<this.bounder && j<this.bounder
467                         && !this.isContain([i,j], except)) {
468                         if (this.board[i][j] == 'board') {
469                                 ret = false;
470                         } else if (this.board[i][j] == color) {
471                                 if (!this.isContained(i, j, color, contains, except)) {
472                                         ret = false;
473                                 }
474                         }
475                 }
476         }
477         if (ret)
478                 contains.push([i_, j_]);
479         return ret;
480 }
481
482 Go.getLiberty = function(){
483         var except = [];
484         var contain = {
485                 'black':[],
486                 'white':[],
487         };
488         for (var i=0; i<this.bounder; i++){
489                 for (var j=0; j<this.bounder; j++){
490                         if (this.board[i][j] == 'board' && !this.isContain([i,j], except)) {
491                                 var except_ = [];
492                                 var liberty = this.countLiberty(i,j,except_);
493                                 if (!liberty.hasOwnProperty('none')) {
494                                         var color = 'none';
495                                         if (liberty.hasOwnProperty('black')) {
496                                                 color = 'black';
497                                         } else if (liberty.hasOwnProperty('white')) {
498                                                 color = 'white';
499                                         }
500                                         if (color != 'none') {
501                                                 var needpush = liberty[color];
502                                                 for (var n in needpush){
503                                                         if (!this.isContain(needpush[n], contain[color])) {
504                                                                 contain[color].push(needpush[n]);
505                                                         }
506                                                 }
507                                         }
508                                 }
509                                 for (var n in except_) {
510                                         if (!this.isContain(except_[n], except)) {
511                                                 except.push(except_[n]);
512                                         }
513                                 }
514                         }
515                 }
516         }
517
518         return contain;
519 }
520
521 Go.countLiberty = function(i_,j_,except){
522         var ret = {};
523         except.push([i_,j_]);
524         for (var d in this.directs) {
525                 if (ret.hasOwnProperty('none'))
526                         break;
527                 var i = parseInt(i_)+parseInt(this.directs[d][0]);
528                 var j = parseInt(j_)+parseInt(this.directs[d][1]);
529                 if (i>=0 && j>= 0 && i<this.bounder && j<this.bounder && !this.isContain([i,j], except)) {
530                         if (this.board[i][j] == 'board') {
531                                 var liberty = this.countLiberty(i,j,except);
532                                 if (liberty.hasOwnProperty('none')) {
533                                         ret['none'] = [];
534                                         break;
535                                 } else if (liberty.hasOwnProperty('black')){
536                                         if (ret.hasOwnProperty('white')) {
537                                                 ret['none'] = [];
538                                                 break;
539                                         } else {
540                                                 if (!ret.hasOwnProperty('black'))
541                                                         ret['black'] = [];
542                                                 for (var n in liberty['black']){
543                                                         if (!this.isContain(liberty['black'][n], ret['black'])) {
544                                                                 ret['black'].push(liberty['black'][n]);
545                                                         }
546                                                 }
547                                         }
548                                 } else if (liberty.hasOwnProperty('white')) {
549                                         if (ret.hasOwnProperty('black')) {
550                                                 ret['none'] = [];
551                                                 break;
552                                         } else {
553                                                 if (!ret.hasOwnProperty('white'))
554                                                         ret['white'] = [];
555                                                 for (var n in liberty['white']){
556                                                         if (!this.isContain(liberty['white'][n], ret['white'])) {
557                                                                 ret['white'].push(liberty['white'][n]);
558                                                         }
559                                                 }
560                                         }
561                                 } else if (liberty.hasOwnProperty('neutral')) {
562                                         if (!ret.hasOwnProperty('neutral'))
563                                                 ret['neutral'] = [];
564                                         for (var n in liberty['neutral']){
565                                                 if (!this.isContain(liberty['neutral'][n], ret['neutral'])) {
566                                                         ret['neutral'].push(liberty['neutral'][n]);
567                                                 }
568                                         }
569                                 }
570                         } else if (this.board[i][j] == 'black') {
571                                 if (ret.hasOwnProperty('white')) {
572                                         ret['none'] = [];
573                                         break;
574                                 } else if (!ret.hasOwnProperty('black')) {
575                                         ret['black'] = [];
576                                 }
577                         } else if (this.board[i][j] == 'white') {
578                                 if (ret.hasOwnProperty('black')) {
579                                         ret['none'] = [];
580                                         break;
581                                 } else if (!ret.hasOwnProperty('white')) {
582                                         ret['white'] = [];
583                                 }
584                         }
585                 }
586         }
587         if (!ret.hasOwnProperty('none')) {
588                 if (ret.hasOwnProperty('black')) {
589                         if (!this.isContain([i_,j_], ret['black']))
590                                 ret['black'].push([i_,j_]);
591                         if (ret.hasOwnProperty('neutral')){
592                                 for (var p = ret['neutral'].shift(); typeof p != 'undefined'; p = ret['neutral'].shift()) {
593                                         if (!this.isContain(p, ret['black'])) {
594                                                 ret['black'].push(p);
595                                         }
596                                 }
597                         }
598                 } else if (ret.hasOwnProperty('white')) {
599                         if (!this.isContain([i_,j_], ret['white']))
600                                 ret['white'].push([i_,j_]);
601                         if (ret.hasOwnProperty('neutral')){
602                                 for (var p = ret['neutral'].shift(); typeof p != 'undefined'; p = ret['neutral'].shift()) {
603                                         if (!this.isContain(p, ret['white'])) {
604                                                 ret['white'].push(p);
605                                         }
606                                 }
607                         }
608                 } else {
609                         if (!ret.hasOwnProperty('neutral'))
610                                 ret['neutral'] = [];
611                         if (!this.isContain([i_,j_], ret['neutral']))
612                                 ret['neutral'].push([i_,j_]);
613                 }
614         }
615         return ret;
616 }
617
618 Go.isContain = function(place, _array) {
619         var heat = _array || [];
620         for (var i in heat){
621                 if (heat[i][0] == place[0] && heat[i][1] == place[1]) {
622                         return true;
623                 }
624         }
625         return false;
626 }
627
628 Go.drawMessage = function(){
629         this.current.stop();
630         this.current = this.player[(this.current.color=='black'?'white':'black')];
631         this.current.start();
632 }
633
634 Go.skip = function(color_){
635         var color = color_ || this.current.color;
636         if (this.current.color == color && !this.isSetting && this.isStart) {
637                 this.playSound('settingbtn');
638                 if (this.isPass) {
639                         this.stop();
640                 } else {
641                         var status = {'color': this.current.color,
642                                                   'setPlace': [],
643                                                   'isPass': false,};
644                         status.restTime = {};
645                         status.restTime['black'] = this.player['black'].restTime;
646                         status.restTime['white'] = this.player['white'].restTime;
647                         this.manual.push(status);
648                         this.drawMessage();
649                         this.isPass = true;
650                 }
651         }
652 }
653
654 function player(color_) {
655         this.color = color_;
656         var side = (color_ == 'black'?'left':'right');
657         this.restTime = 20*60;
658         this.score = 0;
659         this.place = [-1,-1];
660         this.stoneNum = Math.floor(Go.bounder*Go.bounder);
661         this.takes = [];
662         this.liberty = [];
663         this.resource = {
664                 'pit':side+'_pit',
665                 'arrow':side+'_arrow',
666                 'timer':side+'_timer',
667                 'score':side+'_score',
668                 'stone':Go.texture[this.color],
669                 'stone1':Go.texture[this.color+'1'],
670                 'stone2':Go.texture[this.color+'2'],
671         };
672
673         var str = '';
674         $('.'+this.resource['pit']).html(str);
675         for (var i=0; i<this.stoneNum; i++){
676                 var x = Math.floor(Math.random()*76)+20;
677                 var y = Math.floor(Math.random()*296)+20;
678                 var ext = Math.floor(Math.random()*16);
679                 if (ext != 1 && ext != 2) ext = '';
680                 str += '<img class="stone_in_pit" id="'+this.color+'stone'+i+'" style="top:'+y+'px;left:'+x+'px;" src="'+this.resource['stone'+ext]+'" />';
681         }
682         $('.'+this.resource['pit']).html(str);
683
684         $('.'+this.resource['timer']+' span').html('20:00');
685
686         this.currentStone = this.stoneNum-1;
687
688         this.dropStone = function() {
689                 var x = Math.floor(Math.random()*76)+20;
690                 var y = Math.floor(Math.random()*296)+20;
691                 // var id = Math.floor(Math.random()*this.stoneNum);
692                 var id = this.currentStone++;
693                 var ext = Math.floor(Math.random()*16);
694                 if (ext != 1 && ext != 2) ext = '';
695                 $('#'+this.color+'stone'+id).attr('style', 'top:'+y+'px;left:'+x+'px;').attr('src', this.resource['stone'+ext]).removeClass('display_none');
696         }
697
698         this.pickStone = function(){
699                 // var id = Math.floor(Math.random()*this.stoneNum);
700                 var id = this.currentStone--;
701                 $('#'+this.color+'stone'+id).addClass('display_none');
702         }
703
704         this.start = function(){
705                 $('.'+this.resource['arrow']+' img').attr('src', this.texture['hi_arrow']);
706                 this.updateScore();
707                 this.updateTimer();
708                 Go.startTimer();
709                 //this.pickStone();
710         }
711
712         this.stop = function(){
713                 $('.'+this.resource['arrow']+' img').attr('src', this.texture['arrow']);
714                 this.updateScore();
715                 Go.stopTimer();
716                 this.updateTimer();
717         }
718
719         this.timeDida = function(){
720                 this.restTime--;
721                 this.updateTimer();
722         }
723
724         this.updateTimer = function() {
725                 if (this.restTime >= 0){
726                         var s = this.restTime%60;
727                         var m = Math.floor(this.restTime/60);
728                         var s1 = s%10;
729                         var s2 = Math.floor(s/10);
730                         var m1 = m%10;
731                         var m2 = Math.floor(m/10);
732                         var t = ''+m2+m1+':'+s2+s1;
733                         //console.log(this.color+'['+t+']');
734                         $('.'+this.resource['timer']+' span').html(t);
735                 }
736         }
737
738         this.getScore = function() {
739                 return (this.score+this.liberty.length);
740         }
741
742         this.updateScore = function(){
743                 var str = '';
744                 var s = this.getScore();
745                 for (var i=0; i<4; i++){
746                         str = s%10 + str;
747                         s = Math.floor(s/10);
748                 }
749                 $('#'+this.resource['score']).html(str);
750         }
751 }
752
753 player.prototype.texture = {
754         'arrow':'images/GO_PlayerArrow_010612_a.png',
755         'hi_arrow':'images/GO_PlayerArrow_012012_b.png',
756 }
757
758 function getMessage(key, alter) {
759         var ret = alter || '';
760         if (window.chrome && window.chrome.i18n && window.chrome.i18n.getMessage) {
761                 ret = chrome.i18n.getMessage(key);
762         } else {
763                 if (typeof this.messages == 'undefined') {
764                         $.getJSON("_locales/en/messages.json", function(data){
765                                 this.messages = data;
766                         }).error(function(){
767                                         return ret;
768                         });
769                 }
770         if (this.messages && (this.messages.hasOwnProperty(key)) && (this.messages[key].hasOwnProperty('message'))) {
771                         ret = this.messages[key].message;
772                 }
773         }
774         return ret;
775 }
776
777 $(document).ready(function(){
778         license_init("license", "theworld");
779         $('title').html(getMessage('name', 'Go'));
780         $('#playerone').html(getMessage('player', 'Player')+' '+getMessage('one', 'One'));
781         $('#playertwo').html(getMessage('player', 'Player')+' '+getMessage('two', 'Two'));
782         $('.left_arrow span[name="score"]').html(getMessage('score', 'Score'));
783         $('.right_arrow span[name="score"]').html(getMessage('score', 'Score'));
784         $('.left_skip').html(getMessage('skip', 'Skip'));
785         $('.right_skip').html(getMessage('skip', 'Skip'));
786         $('.play_button_text').html(getMessage('play', 'Play'));
787         $('.setting_arrow span').html(getMessage('settings', 'Settings'));
788         $('.setting_sound span').html(getMessage('sound_setting', 'Sound FX....'));
789         $('.setting_timer span').html(getMessage('timer_setting', 'Timer...........'));
790         $('.setting_restart span').html(getMessage('restart', 'Restart'));
791         $('.setting_quit span').html(getMessage('quit', 'Quit'));
792         $('.setting_resume').html(getMessage('resume', 'Submit'));
793         $('.help_title').html(getMessage('rules', 'Rules'));
794         $('.replay').html(getMessage('newGame', 'New Game'));
795         $('span[name="player"]').html(getMessage('player', 'Player'));
796         var help = getMessage('help');
797         if (help.length > 0) {
798                 $('.help_text').html(help);
799         }
800
801         //Pass game, continue pass cause the end
802         $('.left_skip').click(function(){
803                 Go.skip('black');
804         });
805
806         $('.right_skip').click(function(){
807                 Go.skip('white');
808         });
809
810         Go.init();
811 });
812
813