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
13 var numtext = ["0", "1", "2", "3", "4", "5"];
16 this.array = [[-1, -1, -1, -1, -1, -1],
17 [-1, -1, -1, -1, -1, -1],
18 [-1, -1, -1, -1, -1, -1],
19 [-1, -1, -1, -1, -1, -1],
20 [-1, -1, -1, -1, -1, -1],
21 [-1, -1, -1, -1, -1, -1],
22 [-1, -1, -1, -1, -1, -1]];
23 this.colindex = [0, 0, 0, 0, 0, 0, 0, 0];
26 function no_more_moves() {
27 var array = Game.activegame.array;
28 for(var i = 0; i < 7; i++)
29 for(var j = 0; j < 6; j++)
35 function get_random_unfilled_column() {
36 var array = Game.activegame.array;
37 var colindex = Game.activegame.colindex;
39 var columns = new Array();
40 for(var i = 0; i < 7; i++)
44 if(columns.length <= 0)
47 var tgt = (Math.random() * columns.length)|0;
51 function winning_move_possible(player) {
52 var array = Game.activegame.array;
53 var colindex = Game.activegame.colindex;
59 for(i = 0; i < 7; i++)
62 for(j = 0; j < colindex[i]; j++)
63 if(array[i][j] == player)
69 console.log("POSSIBLE WIN: player "+player+" column "+(i+1));
74 /* horizontal check */
75 for(j = 0; j < 6; j++)
77 for(i = 0; i < 4; i++)
81 for(k = 0; k < 4; k++)
82 if(array[i+k][j] == player)
86 else if((array[i+k][j] < 0)&&(colindex[i+k] == j)&&(column < 0))
96 if((matches == 4)&&(column >= 0))
98 console.log("POSSIBLE WIN: player "+player+" column "+(column+1));
107 function computermove() {
110 /* if we can win, do it */
111 col = winning_move_possible(Game.activeplayer)
114 startmove(col, true);
118 /* if the opponent is about to win, block him */
119 col = winning_move_possible((Game.activeplayer+1)%2)
122 startmove(col, true);
126 /* do a random move */
127 col = get_random_unfilled_column();
130 startmove(col, true);
135 function gamesound(file) {
137 this.soundobj = new Array();
138 this.soundobj.push(new Audio(file));
139 this.soundobj.push(new Audio(file));
141 this.play = function play() {
142 /* create two instances of the file to play sequentially if calls */
143 /* come too fast, otherwise the second call will be ignored */
144 this.soundobj[this.idx].play();
145 this.idx = (this.idx + 1)%2;
149 function settype(type) {
150 var tags = ["#type_onegame", "#type_bestofthree",
153 Game.gametype = type;
154 localStorage.setItem("sweetspot_gametype", type);
156 for(var i = 0; i < 3; i++)
159 $(tags[i]).addClass("selectedtype");
161 $(tags[i]).removeClass("selectedtype");
167 function setcolor(player, tgt) {
168 if(((player == 1)&&(Game.playercolor[1] == tgt))||
169 ((player == 2)&&(Game.playercolor[0] == tgt)))
174 var tags = ["#player_player"+player+"orange",
175 "#player_player"+player+"red",
176 "#player_player"+player+"blue",
177 "#player_player"+player+"green"];
178 var solid = ["orangesolid", "redsolid",
179 "bluesolid", "greensolid"];
183 Game.playercolor[0] = tgt;
184 localStorage.setItem("sweetspot_player1_color", tgt);
188 Game.playercolor[1] = tgt;
189 localStorage.setItem("sweetspot_player2_color", tgt);
192 for(var i = 0; i < 4; i++)
196 $(tags[i]).addClass(solid[i]);
200 $(tags[i]).removeClass(solid[i]);
207 function test_player_ready() {
208 if((Game.player1name && (Game.player1name != ""))&&
209 (Game.player2name && (Game.player2name != ""))&&
210 ((Game.playercolor[0] >= 0) && (Game.playercolor[0] < 4))&&
211 ((Game.playercolor[1] >= 0) && (Game.playercolor[1] < 4)))
213 $("#player_nextbutton").removeClass("inactive_button");
214 $("#player_nextbutton").addClass("active_button");
218 $("#player_nextbutton").removeClass("active_button");
219 $("#player_nextbutton").addClass("inactive_button");
223 function test_type_ready() {
224 if((Game.gametype >= 0) && (Game.gametype < 3))
226 $("#type_startbutton").removeClass("inactive_button");
227 $("#type_startbutton").addClass("active_button");
231 $("#type_startbutton").removeClass("active_button");
232 $("#type_startbutton").addClass("inactive_button");
236 function init_game() {
237 if(localStorage.getItem("sweetspot_player1"))
238 Game.player1name = localStorage.getItem("sweetspot_player1");
240 Game.player1name = "Player1";
242 if(localStorage.getItem("sweetspot_player2"))
243 Game.player2name = localStorage.getItem("sweetspot_player2");
245 Game.player2name = "Player2";
247 $('#player1name').val(Game.player1name);
248 $('#player2name').val(Game.player2name);
250 if(localStorage.getItem("sweetspot_player1_color"))
251 setcolor(1, parseInt(localStorage.getItem("sweetspot_player1_color")));
255 if(localStorage.getItem("sweetspot_player2_color"))
256 setcolor(2, parseInt(localStorage.getItem("sweetspot_player2_color")));
260 if(localStorage.getItem("sweetspot_gametype"))
261 settype(parseInt(localStorage.getItem("sweetspot_gametype")));
266 function ignore_user_input() {
267 Game.ignore_input = true;
270 function enable_user_input() {
271 Game.ignore_input = false;
274 /* this represents a user making a move by clicking a column */
275 function startmove(column, computer) {
276 /* if we're in the middle of an animation, do nothing */
277 if(Game.ignore_input&&(computer!==true))
280 updateselector(column);
282 var row = Game.activegame.colindex[column];
284 /* if this column is full, do nothing */
288 /* figure out what color piece we're using */
289 var colorwheel = ["game_piece_orange", "game_piece_red",
290 "game_piece_blue", "game_piece_green"];
291 var piececolor = colorwheel[Game.playercolor[Game.activeplayer]];
293 /* set the move data */
294 Game.activegame.array[column][row] = Game.activeplayer;
295 Game.activegame.colindex[column]++;
297 /* trigger a call to test the board after the animation */
299 window.setTimeout("Game.movecomplete()", 600);
301 /* start the move animation */
302 $("#game_grid").append("<div id=\"game_column"+
303 column+"_row"+row+"\" class=\"game_col"+
304 column+" game_row"+row+" "+
305 piececolor+"\"></div>");
308 function start_new() {
309 $("#game_grid").empty();
310 Game.activegame = new GameData();
314 function game_over(player) {
315 var color = Game.playercolor[player];
316 var banner = ["win_banner_orange", "win_banner_red",
317 "win_banner_blue", "win_banner_green"];
318 var name = ["win_playername_orange", "win_playername_red",
319 "win_playername_blue", "win_playername_green"];
320 var playagain = ["win_playagain_orange", "win_playagain_red",
321 "win_playagain_blue", "win_playagain_green"];
322 var startover = ["win_startover_orange", "win_startover_red",
323 "win_startover_blue", "win_startover_green"];
325 $("#win_playername").text(Game.player1name);
327 $("#win_playername").text(Game.player2name);
329 for(var i = 0; i < 4; i++)
333 $("#win_banner").addClass(banner[i]);
334 $("#win_playername").addClass(name[i]);
335 $("#win_playagain").addClass(playagain[i]);
336 $("#win_startover").addClass(startover[i]);
340 $("#win_banner").removeClass(banner[i]);
341 $("#win_playername").removeClass(name[i]);
342 $("#win_playagain").removeClass(playagain[i]);
343 $("#win_startover").removeClass(startover[i]);
346 $("#game_page").hide();
347 $("#win_page").show();
350 function end_game() {
352 $("#quit_dlg").hide();
353 $("#game_grid").empty();
354 Game.activegame = new GameData();
355 Game.activeplayer = 0;
356 Game.playerwins = [0, 0];
357 $("#game_player1_wins").text(numtext[0]);
358 $("#game_player2_wins").text(numtext[0]);
359 $("#game_player1active").show();
360 $("#game_player2active").hide();
361 updateselector(Game.activecolumn);
362 $("#win_page").hide();
363 $("#game_page").hide();
364 $("#intro_page").show();
368 function restart_game() {
370 $("#game_grid").empty();
371 Game.activegame = new GameData();
372 Game.activeplayer = 0;
373 Game.playerwins = [0, 0];
374 $("#game_player1_wins").text(numtext[0]);
375 $("#game_player2_wins").text(numtext[0]);
376 $("#game_player1active").show();
377 $("#game_player2active").hide();
378 updateselector(Game.activecolumn);
379 $("#win_page").hide();
380 $("#game_page").show();
384 function win(player, i1, j1, i2, j2, i3, j3, i4, j4) {
385 Game.playerwins[player]++;
387 $("#game_column"+i1+"_row"+j1).addClass("game_piece_win1");
388 $("#game_column"+i2+"_row"+j2).addClass("game_piece_win2");
389 $("#game_column"+i3+"_row"+j3).addClass("game_piece_win3");
390 $("#game_column"+i4+"_row"+j4).addClass("game_piece_win4");
391 $("#game_player"+(player+1)+"_wins").text(numtext[Game.playerwins[player]]);
392 if(Game.playerwins[player] >= Game.gametype+1)
394 window.setTimeout("Game.game_over("+player+")", 1000);
398 window.setTimeout("Game.start_new()", 1000);
402 function movecomplete() {
403 var array = Game.activegame.array;
404 var colindex = Game.activegame.colindex;
405 var matches = [0, 0];
410 for(i = 0; i < 7; i++)
414 for(j = 0; j < 6; j++)
417 var tgt = array[i][j];
419 matches[(tgt+1)%2] = 0;
420 if(matches[tgt] >= 4)
422 win(tgt, i, j-3, i, j-2, i, j-1, i, j);
433 /* horizontal check */
434 for(j = 0; j < 6; j++)
438 for(i = 0; i < 7; i++)
441 var tgt = array[i][j];
443 matches[(tgt+1)%2] = 0;
444 if(matches[tgt] >= 4)
446 win(tgt, i-3, j, i-2, j, i-1, j, i, j);
457 /* diagonal right check */
458 var p = [[0, 2], [0, 1], [0, 0], [1, 0], [2, 0], [3, 0]];
459 for(k = 0; k < 6; k++)
463 for(i = p[k][0], j = p[k][1]; (i < 7)&&(j < 6); i++, j++)
466 var tgt = array[i][j];
468 matches[(tgt+1)%2] = 0;
469 if(matches[tgt] >= 4)
471 win(tgt, i-3, j-3, i-2, j-2, i-1, j-1, i, j);
482 /* diagonal left check */
483 p = [[3, 0], [4, 0], [5, 0], [6, 0], [6, 1], [6, 2]];
484 for(k = 0; k < 6; k++)
488 for(i = p[k][0], j = p[k][1]; (i >= 0)&&(j < 6); i--, j++)
491 var tgt = array[i][j];
493 matches[(tgt+1)%2] = 0;
494 if(matches[tgt] >= 4)
496 win(tgt, i+3, j-3, i+2, j-2, i+1, j-1, i, j);
507 /* set the board to indicate it's the next player's move */
508 Game.activeplayer = (Game.activeplayer+1)%2;
509 if(Game.activeplayer == 0)
511 $("#game_player1active").show();
512 $("#game_player2active").hide();
516 $("#game_player1active").hide();
517 $("#game_player2active").show();
520 /* if there are no more valid moves, it's a stalemate */
526 /* is it the computer's move? */
527 if(Game.computer&&(Game.activeplayer==1))
533 updateselector(Game.activecolumn);
538 function updateselector(column) {
539 Game.activecolumn = column;
540 var colorwheel = ["game_piece_orange", "game_piece_red",
541 "game_piece_blue", "game_piece_green"];
542 var piececolor = colorwheel[Game.playercolor[Game.activeplayer]];
544 $("#game_column_selector").attr("class", "game_selcol"+column);
545 $("#game_column_selector").show();
546 $("#game_column_selector_candy").attr("class", "game_col"+column+" "+piececolor);
547 $("#game_column_selector_candy").show();
551 Game.move_sound.play();
555 Game.menunav_sound.play();
558 function selectsnd() {
559 Game.select_sound.play();
563 Game.win_sound.play();
566 function player1namechange() {
567 Game.player1name = $('#player1name').val();
568 localStorage.setItem("sweetspot_player1", Game.player1name);
572 function player2namechange() {
573 Game.player2name = $('#player2name').val();
574 localStorage.setItem("sweetspot_player2", Game.player2name);
578 $(document).ready(function()
581 license_init("license", "intro_page");
582 help_init("main_help", "help_");
583 Game.player1name = "";
584 Game.player2name = "";
585 Game.computer = false;
586 Game.playercolor = [-1, -1];
588 Game.activegame = new GameData();
589 Game.activecolumn = 0;
590 Game.activeplayer = 0;
591 Game.playerwins = [0, 0];
592 Game.ignore_input = false;
593 Game.movecomplete = movecomplete;
594 Game.game_over = game_over;
595 Game.start_new = start_new;
596 Game.move_sound = new gamesound("audio/GamePiece.wav");
597 Game.menunav_sound = new gamesound("audio/MenuNavigation.wav");
598 Game.select_sound = new gamesound("audio/Select.wav");
599 Game.win_sound = new gamesound("audio/Winner.wav");
601 if (window.chrome&&window.chrome.i18n)
603 $("#intro_playbutton").text(chrome.i18n.getMessage("play"));
604 $("#player_player1_static").text(chrome.i18n.getMessage("player1"));
605 $("#player_player2_static").text(chrome.i18n.getMessage("player2"));
606 $("#player_text_chooseyour").text(chrome.i18n.getMessage("chooseyour"));
607 $("#player_text_color").text(chrome.i18n.getMessage("color"));
608 $("#player_nextbutton").text(chrome.i18n.getMessage("next"));
609 $("#type_onegame_text").text(chrome.i18n.getMessage("1game"));
610 $("#type_bestofthree_text").text(chrome.i18n.getMessage("2game"));
611 $("#type_bestoffive_text").text(chrome.i18n.getMessage("3game"));
612 $("#type_startbutton").text(chrome.i18n.getMessage("start"));
613 $("#quit_dlg_img").text(chrome.i18n.getMessage("startoverquestion"));
614 $("#quit_dlg_no").text(chrome.i18n.getMessage("no"));
615 $("#quit_dlg_yes").text(chrome.i18n.getMessage("yes"));
616 $("#win_banner").text(chrome.i18n.getMessage("winner"));
617 $("#win_playagain_text").text(chrome.i18n.getMessage("playagain"));
618 $("#win_startover_text").text(chrome.i18n.getMessage("startover"));
619 numtext[0] = chrome.i18n.getMessage("num0");
620 numtext[1] = chrome.i18n.getMessage("num1");
621 numtext[2] = chrome.i18n.getMessage("num2");
622 numtext[3] = chrome.i18n.getMessage("num3");
623 numtext[4] = chrome.i18n.getMessage("num4");
624 numtext[5] = chrome.i18n.getMessage("num5");
627 /* intro page interaction */
629 $("#intro_playbutton").click(function() {
631 $("#intro_page").hide();
632 $("#players_page").show();
635 /* player page interaction */
637 $('#player_player1orange').mousedown(function() {navsnd();setcolor(1, 0);});
638 $('#player_player1red').mousedown(function() {navsnd();setcolor(1, 1);});
639 $('#player_player1blue').mousedown(function() {navsnd();setcolor(1, 2);});
640 $('#player_player1green').mousedown(function() {navsnd();setcolor(1, 3);});
641 $('#player_player2orange').mousedown(function() {navsnd();setcolor(2, 0);});
642 $('#player_player2red').mousedown(function() {navsnd();setcolor(2, 1);});
643 $('#player_player2blue').mousedown(function() {navsnd();setcolor(2, 2);});
644 $('#player_player2green').mousedown(function() {navsnd();setcolor(2, 3);});
646 var p1name = document.getElementById("player1name");
647 var p2name = document.getElementById("player2name");
648 p1name.onkeyup = player1namechange;
649 p2name.onkeyup = player2namechange;
650 p1name.onblur = player1namechange;
651 p2name.onblur = player2namechange;
652 p1name.onchange = player1namechange;
653 p2name.onchange = player2namechange;
655 $("#player_nextbutton").click(function() {
656 if($("#player_nextbutton").hasClass("active_button"))
659 $("#players_page").hide();
660 $("#type_page").show();
664 /* game type page interaction */
666 $("#type_startbutton").click(function() {
667 if($("#type_startbutton").hasClass("active_button"))
670 updateselector(Game.activecolumn);
671 var color1class = ["game_player1orange", "game_player1red",
672 "game_player1blue", "game_player1green"];
673 var color2class = ["game_player2orange", "game_player2red",
674 "game_player2blue", "game_player2green"];
676 for(var i = 0; i < 4; i++)
678 var c1 = Game.playercolor[0];
679 var c2 = Game.playercolor[1];
681 $("#game_player1marquee").addClass(color1class[i]);
683 $("#game_player1marquee").removeClass(color1class[i]);
686 $("#game_player2marquee").addClass(color2class[i]);
688 $("#game_player2marquee").removeClass(color2class[i]);
691 $("#game_player1name").text(Game.player1name);
692 $("#game_player2name").text(Game.player2name);
693 $("#type_page").hide();
694 $("#game_page").show();
698 $('#type_onegame').mousedown(function() {navsnd();settype(0);});
699 $('#type_bestofthree').mousedown(function() {navsnd();settype(1);});
700 $('#type_bestoffive').mousedown(function() {navsnd();settype(2);});
704 $('#game_column1').mousedown(function() {startmove(0);});
705 $('#game_column2').mousedown(function() {startmove(1);});
706 $('#game_column3').mousedown(function() {startmove(2);});
707 $('#game_column4').mousedown(function() {startmove(3);});
708 $('#game_column5').mousedown(function() {startmove(4);});
709 $('#game_column6').mousedown(function() {startmove(5);});
710 $('#game_column7').mousedown(function() {startmove(6);});
712 $('#game_column1').mouseover(function() {updateselector(0);});
713 $('#game_column2').mouseover(function() {updateselector(1);});
714 $('#game_column3').mouseover(function() {updateselector(2);});
715 $('#game_column4').mouseover(function() {updateselector(3);});
716 $('#game_column5').mouseover(function() {updateselector(4);});
717 $('#game_column6').mouseover(function() {updateselector(5);});
718 $('#game_column7').mouseover(function() {updateselector(6);});
720 $('#game_quit').click(function() {$("#quit_dlg").show();});
721 $('#quit_dlg_no').click(function() {$("#quit_dlg").hide();});
722 $('#quit_dlg_yes').click(function() {end_game()});
723 $('#win_playagain_text').click(function() {restart_game()});
724 $('#win_startover_text').click(function() {end_game()});