2 * Copyright (c) 2012, Intel Corporation.
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
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',
25 playButton:'play_button',
26 winMessage:'win_panel',
27 settingPanel:'setting_panel',
28 helpPanel:'help_panel',
30 directs: [[1,0],[0,1],[0,-1],[-1,0],],
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],
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],],
56 for (var i=0; i<this.bounder; i++){
58 for (var j=0; j<this.bounder; j++){
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;
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',
81 for (var snd in soundSource){
82 this.sounds[snd] = new Audio(soundSource[snd]);
83 //this.sounds[snd].load();
85 this.sounds['dida'] = new gamesound('dida');
87 this.disable('.'+this.winMessage);
88 this.enable('.'+this.playButton);
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';
98 this.player['black'] = new player('black');
99 this.player['white'] = new player('white');
100 this.current = this.player['white'];
104 this.isSetting =false;
105 this.isRestart = false;
107 this.isEndMsg = true;
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();
117 $('.setting_restart img').attr('src', this.texture['uncheck'])
118 .removeClass('setting_restart_checked')
119 .addClass('setting_restart_check');
122 Go.playSound = function(snd){
124 var audio = this.sounds[snd];
125 if (audio.paused == false) {
127 audio.currentTime = 0;
133 Go.stop = function(){
134 this.isStart = false;
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'));
146 $('#win_player').html('');
147 $('#win_result').html(getMessage('draw', 'Draw'));
150 this.playSound('end');
151 this.enable('.'+this.winMessage);
152 this.isEndMsg = false;
156 Go.exit = function(){
162 Go.restart = function(){
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');
180 this.playSound('dida');
181 if (this.current.restTime <= 0) {
183 } else if (this.current.restTime == 5) {
184 this.playSound('positive');
189 Go.stopTimer = function(){
191 clearTimeout(this.timer);
192 // this.sounds['dida'].pause();
196 Go.disable = function(which) {
197 $(which).addClass('display_none');
200 Go.enable = function(which) {
201 $(which).removeClass('display_none');
204 Go.toggleSetting = function(){
205 if ($('.'+this.winMessage).hasClass('display_none')) {
211 this.playSound('settingbtn');
215 Go.showSetting = function(){
216 this.enable('.setting_panel');
217 setTimeout("$('.setting_panel').addClass('setting_panel_in')", 100);
218 this.isSetting = true;
220 this.playSound('settingon');
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;
233 Go.toggleSound = function(){
234 var img = $('.setting_sound img');
236 img.attr('src', this.texture['uncheck']);
237 img.removeClass('setting_sound_checked');
238 img.addClass('setting_sound_check');
239 this.isSound = false;
241 img.attr('src', this.texture['checked']);
242 img.addClass('setting_sound_checked');
243 img.removeClass('setting_sound_check');
246 this.playSound('settingck');
249 Go.toggleTimer = function(){
250 var img = $('.setting_timer img');
252 img.attr('src', this.texture['uncheck']);
253 img.removeClass('setting_timer_checked');
254 img.addClass('setting_timer_check');
255 this.isTimer = false;
257 img.attr('src', this.texture['checked']);
258 img.addClass('setting_timer_checked');
259 img.removeClass('setting_timer_check');
262 this.playSound('settingck');
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;
275 img.attr('src', this.texture['checked']);
276 img.addClass('setting_restart_checked');
277 img.removeClass('setting_restart_check');
278 this.isRestart = true;
285 this.playSound('settingck');
290 Go.toggleQuit = function(){
291 var img = $('.setting_quit img');
292 var conflict = false;
294 img.attr('src', this.texture['uncheck']);
295 img.removeClass('setting_quit_checked');
296 img.addClass('setting_quit_check');
299 img.attr('src', this.texture['checked']);
300 img.addClass('setting_quit_checked');
301 img.removeClass('setting_quit_check');
307 this.toggleRestart();
309 this.playSound('settingck');
313 Go.toggleHelp = function() {
314 $('.'+this.helpPanel).toggleClass('display_none');
317 Go.drawBoard = function(){
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++){
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++){
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>';
334 $('.'+this.boardview).html(str);
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];
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]) {
348 this.board[i][j] = 'board';
351 for (var n in takes) {
352 this.board[takes[n][0]][takes[n][1]] = 'board';
356 if (takes.length == 0 && this.isContained(i, j, this.current.color)) {
357 this.board[i][j] = 'board';
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;
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;
376 var liberty = this.getLiberty();
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'];
383 status.restTime = {};
384 status.restTime['black'] = this.player['black'].restTime;
385 status.restTime['white'] = this.player['white'].restTime;
386 this.manual.push(status);
393 Go.undue = function(color_) {
394 if (!this.isSetting && this.isStart && color_ != this.current.color) {
395 var status = this.manual.pop();
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;
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++;
411 if (status.liberty) {
412 var liberty = status.liberty;
413 this.player['black'].liberty = liberty['black'];
414 this.player['white'].liberty = liberty['white'];
416 this.player['black'].liberty = [];
417 this.player['white'].liberty = [];
419 player.place = status.userPlace;
422 this.isPass = status.isPass;
424 // this.player['black'].restTime = status.restTime['black'];
425 // this.player['white'].restTime = status.restTime['white'];
431 Go.getTake = function(place_) {
432 var p = place_ || this.place;
433 var checkColor = (this.current.color=='black'?'white':'black');
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) {
441 if (this.isContained(i, j, checkColor, t)) {
443 if (!this.isContain(t[n], takes)) {
450 //console.log(takes);
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_ || [];
459 except.push([parseInt(i_),parseInt(j_)]);
461 for (var d in this.directs) {
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') {
470 } else if (this.board[i][j] == color) {
471 if (!this.isContained(i, j, color, contains, except)) {
478 contains.push([i_, j_]);
482 Go.getLiberty = function(){
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)) {
492 var liberty = this.countLiberty(i,j,except_);
493 if (!liberty.hasOwnProperty('none')) {
495 if (liberty.hasOwnProperty('black')) {
497 } else if (liberty.hasOwnProperty('white')) {
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]);
509 for (var n in except_) {
510 if (!this.isContain(except_[n], except)) {
511 except.push(except_[n]);
521 Go.countLiberty = function(i_,j_,except){
523 except.push([i_,j_]);
524 for (var d in this.directs) {
525 if (ret.hasOwnProperty('none'))
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')) {
535 } else if (liberty.hasOwnProperty('black')){
536 if (ret.hasOwnProperty('white')) {
540 if (!ret.hasOwnProperty('black'))
542 for (var n in liberty['black']){
543 if (!this.isContain(liberty['black'][n], ret['black'])) {
544 ret['black'].push(liberty['black'][n]);
548 } else if (liberty.hasOwnProperty('white')) {
549 if (ret.hasOwnProperty('black')) {
553 if (!ret.hasOwnProperty('white'))
555 for (var n in liberty['white']){
556 if (!this.isContain(liberty['white'][n], ret['white'])) {
557 ret['white'].push(liberty['white'][n]);
561 } else if (liberty.hasOwnProperty('neutral')) {
562 if (!ret.hasOwnProperty('neutral'))
564 for (var n in liberty['neutral']){
565 if (!this.isContain(liberty['neutral'][n], ret['neutral'])) {
566 ret['neutral'].push(liberty['neutral'][n]);
570 } else if (this.board[i][j] == 'black') {
571 if (ret.hasOwnProperty('white')) {
574 } else if (!ret.hasOwnProperty('black')) {
577 } else if (this.board[i][j] == 'white') {
578 if (ret.hasOwnProperty('black')) {
581 } else if (!ret.hasOwnProperty('white')) {
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);
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);
609 if (!ret.hasOwnProperty('neutral'))
611 if (!this.isContain([i_,j_], ret['neutral']))
612 ret['neutral'].push([i_,j_]);
618 Go.isContain = function(place, _array) {
619 var heat = _array || [];
621 if (heat[i][0] == place[0] && heat[i][1] == place[1]) {
628 Go.drawMessage = function(){
630 this.current = this.player[(this.current.color=='black'?'white':'black')];
631 this.current.start();
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');
641 var status = {'color': this.current.color,
644 status.restTime = {};
645 status.restTime['black'] = this.player['black'].restTime;
646 status.restTime['white'] = this.player['white'].restTime;
647 this.manual.push(status);
654 function player(color_) {
656 var side = (color_ == 'black'?'left':'right');
657 this.restTime = 20*60;
659 this.place = [-1,-1];
660 this.stoneNum = Math.floor(Go.bounder*Go.bounder);
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'],
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]+'" />';
682 $('.'+this.resource['pit']).html(str);
684 $('.'+this.resource['timer']+' span').html('20:00');
686 this.currentStone = this.stoneNum-1;
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');
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');
704 this.start = function(){
705 $('.'+this.resource['arrow']+' img').attr('src', this.texture['hi_arrow']);
712 this.stop = function(){
713 $('.'+this.resource['arrow']+' img').attr('src', this.texture['arrow']);
719 this.timeDida = function(){
724 this.updateTimer = function() {
725 if (this.restTime >= 0){
726 var s = this.restTime%60;
727 var m = Math.floor(this.restTime/60);
729 var s2 = Math.floor(s/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);
738 this.getScore = function() {
739 return (this.score+this.liberty.length);
742 this.updateScore = function(){
744 var s = this.getScore();
745 for (var i=0; i<4; i++){
747 s = Math.floor(s/10);
749 $('#'+this.resource['score']).html(str);
753 player.prototype.texture = {
754 'arrow':'images/GO_PlayerArrow_010612_a.png',
755 'hi_arrow':'images/GO_PlayerArrow_012012_b.png',
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);
763 if (typeof this.messages == 'undefined') {
764 $.getJSON("_locales/en/messages.json", function(data){
765 this.messages = data;
770 if (this.messages && (this.messages.hasOwnProperty(key)) && (this.messages[key].hasOwnProperty('message'))) {
771 ret = this.messages[key].message;
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);
801 //Pass game, continue pass cause the end
802 $('.left_skip').click(function(){
806 $('.right_skip').click(function(){