-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtjam.min.js
1 lines (1 loc) · 6.18 KB
/
tjam.min.js
1
$(document).ready(function(){function e(){this.colors=["red","blue","green","orange","yellow","purple","black","pink","lightblue","star"];this.carInfo=new Array;this.err="<span class='error'>X</span>";this.addColor=function(){var e=$(".car");var t=e.length;for(var n=0;n<t;n++){if(n==t-1){e.eq(n).addClass("star");break}e.eq(n).addClass(this.colors[n])}};this.putCars=function(){var e=$("#game");var t;for(var n=0;n<this.carInfo.length;n++){t=document.createElement("div");$car=$(t);$car.addClass("car");$car.attr("data-name",n);if(this.carInfo[n].orientation=="h")$car.attr("data-or","horizontal");else $car.attr("data-or","vertical");$car.attr("data-len",this.carInfo[n].len);$car.addClass(this.colors[n]);$car.attr("style","bottom:"+this.carInfo[n].vPos+"00px;");$car.attr("style",$car.attr("style")+"left:"+this.carInfo[n].hPos+"00px; ");$($car).appendTo(e)}this.addColor();this.compile()};this.compile=function(){var e="<span class='right-btn' data-change='100'></span>";var t="<span class='left-btn' data-change='-100'></span>";var n="<span class='up-btn' data-change='100'></span>";var r="<span class='down-btn' data-change='-100'></span>";$("[data-or=horizontal]").each(function(){var n=$(this);n.empty();var r=parseInt(n.css("left"));if(r<400&&n.attr("data-len")==2)$(e).appendTo(n);else if(r<300&&n.attr("data-len")==3)$(e).appendTo(n);if(r>0)$(t).appendTo(n)});$("[data-or=vertical]").each(function(){var e=$(this);e.empty();var t=parseInt(e.css("bottom"));if(t<400&&e.attr("data-len")==2)$(n).appendTo(e);else if(t<300&&e.attr("data-len")==3)$(n).appendTo(e);if(t>0)$(r).appendTo(e)})};this.checkColission=function(e,t,n,r){e/=100;e=Math.floor(e);t/=100;t=Math.floor(t);var i={x:e,y:t};for(var s=0;s<this.carInfo.length;s++){var o=this.carInfo[s];var u=o.hPos;var a=o.vPos;var f=o.len;var l=o.orientation;var c=[{x:u,y:a}];if(l=="h"){c.push({x:u+(f-1),y:a});if(f==3)c.push({x:u+(f-2),y:a})}else{c.push({x:u,y:a+(f-1)});if(f==3)c.push({x:u,y:a+(f-2)})}for(var h=0;h<c.length;h++){if(i.x==c[h].x&&i.y==c[h].y)return false}}return true};this.checkWin=function(e,t){return e==400&&t==300};this.error=function(t){t.parent().append(e.err);var n=t.parent().find(".error");if(t.hasClass("right-btn"))n.css({right:"0",top:"10px"});else if(t.hasClass("left-btn"))n.css({left:"0",top:"10px"});else if(t.hasClass("up-btn"))n.css({top:"0",left:"10px"});else n.css({bottom:"0",left:"10px"});n.hide().fadeIn("slow").delay(250).fadeOut("slow",function(){$(this).remove()})}}function t(e,t,n,r){this.hPos=e;this.vPos=t;this.orientation=n;this.len=r}function n(){this.board1=[{hPos:0,vPos:2,orientation:"h",len:3},{hPos:2,vPos:3,orientation:"v",len:3},{hPos:4,vPos:5,orientation:"h",len:2},{hPos:5,vPos:0,orientation:"v",len:3},{hPos:0,vPos:3,orientation:"h",len:2}];this.board2=[{hPos:0,vPos:0,orientation:"h",len:3},{hPos:0,vPos:1,orientation:"v",len:2},{hPos:1,vPos:1,orientation:"h",len:2},{hPos:3,vPos:0,orientation:"v",len:2},{hPos:4,vPos:0,orientation:"v",len:2},{hPos:5,vPos:0,orientation:"v",len:3},{hPos:2,vPos:2,orientation:"v",len:3},{hPos:3,vPos:2,orientation:"h",len:2},{hPos:5,vPos:3,orientation:"v",len:2},{hPos:0,vPos:3,orientation:"h",len:2}];this.board3=[{hPos:0,vPos:1,orientation:"h",len:3},{hPos:1,vPos:0,orientation:"h",len:2},{hPos:2,vPos:2,orientation:"v",len:3},{hPos:0,vPos:5,orientation:"h",len:3},{hPos:3,vPos:2,orientation:"h",len:2},{hPos:3,vPos:4,orientation:"v",len:2},{hPos:4,vPos:0,orientation:"v",len:2},{hPos:5,vPos:2,orientation:"v",len:2},{hPos:5,vPos:4,orientation:"v",len:2},{hPos:0,vPos:3,orientation:"h",len:2}];this.board4=[{hPos:0,vPos:0,orientation:"v",len:2},{hPos:1,vPos:0,orientation:"v",len:2},{hPos:2,vPos:0,orientation:"v",len:2},{hPos:0,vPos:2,orientation:"h",len:3},{hPos:2,vPos:3,orientation:"v",len:2},{hPos:4,vPos:0,orientation:"h",len:2},{hPos:4,vPos:1,orientation:"h",len:2},{hPos:4,vPos:2,orientation:"v",len:3},{hPos:5,vPos:2,orientation:"v",len:2},{hPos:0,vPos:3,orientation:"h",len:2}]}var r=new n;var e=new e;$(".carData").keyup(function(){var e=1;if($(this).val().length>e){$(this).val($(this).val().substring(0,1))}$(this).next().focus();if($(this).index()==$(".carData").length-1)$(".carData").eq(0).focus()});$(".addCar").on("click",function(){var n=$(".carData");var r,i,s,o;var u=false;for(var a=0;a<n.length;a++){var f=n.eq(a).val();if(f==""){u=true;break}else if(a==0){if(f>5){u=true;break}r=f}else if(a==1){if(f>5){u=true;break}i=f}else if(a==2){if(f!="h"&&f!="v"){u=true;break}s=f}else{if(f!=2&&f!=3){u=true;break}o=f;e.carInfo.push(new t(r,i,s,o))}n.eq(a).val("")}if(u)alert("Your inputs are malformed. Integers [0,4] for X and Y, h or v for O, and 2 or 3 for L.");else{$(".preMade").slideUp();$("#game").children(":not(.middleBorder)").remove();e.putCars()}});$(".finish").on("click",function(){$(".wrapInputs, .options").slideUp();$(".preMade").slideUp();$("#game").children(":not(.middleBorder)").remove();e.putCars()});$(".right-btn, .left-btn").live("click",function(){var t=$(this);var n=parseInt(t.parent().css("left"));var r=parseInt(t.attr("data-change"));var i=parseInt(t.parent().css("bottom"));var s=parseInt(t.parent().attr("data-len"));var o=t.parent().attr("data-or");var u=parseInt(t.parent().attr("data-name"));var a;if(t.hasClass("right-btn"))a=e.checkColission(n+100*(s-1)+r,i,s,o);else a=e.checkColission(n+r,i,s,o);if(a){t.parent().css("left",n+r+"px");e.compile();e.carInfo[u].hPos=(n+r)/100;if(t.hasClass("right-btn")){if(e.checkWin(n+100,i)){alert("You've won! Play another game! I dare you.");document.location.reload(true)}}}else e.error(t)});$(".up-btn, .down-btn").live("click",function(){var t=$(this);var n=parseInt(t.parent().css("left"));var r=parseInt(t.attr("data-change"));var i=parseInt(t.parent().css("bottom"));var s=t.parent().attr("data-len");var o=parseInt(t.parent().attr("data-name"));var u=t.parent().attr("data-or");if(t.hasClass("up-btn"))valid=e.checkColission(n,i+100*(s-1)+r,s,u);else valid=e.checkColission(n,i+r,s,u);if(valid){t.parent().css("bottom",i+r+"px");e.compile();e.carInfo[o].vPos=(i+r)/100}else e.error(t)});$(".preMade").find("li").on("click",function(){var t=parseInt($(this).attr("data-board"));switch(t){case 1:e.carInfo=r.board1;break;case 2:e.carInfo=r.board2;break;case 3:e.carInfo=r.board3;break;case 4:e.carInfo=r.board4;break;case 5:e.carInfo=r.board5;break}$(".finish").trigger("click")})})