webpackJsonp([0],{0:function(t,e,o){"use strict";function n(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function r(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function i(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}function l(t){if("web"==d.OS){var e=t.nativeEvent.changedTouches[0];return{pageX:e.pageX,pageY:e.pageY}}return{pageX:t.nativeEvent.pageX,pageY:t.nativeEvent.pageY}}var a=function(){function t(t,e){for(var o=0;o<e.length;o++){var n=e[o];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return function(e,o,n){return o&&t(e.prototype,o),n&&t(e,n),e}}(),s=o(1),u=s.AppRegistry,c=s.StyleSheet,h=s.Text,f=s.View,d=s.Platform,p=s.Animated,m=s.TouchableBounce,g=o(445),v=3,y=4,b=60,w=function(t){function e(){return n(this,e),r(this,(e.__proto__||Object.getPrototypeOf(e)).apply(this,arguments))}return i(e,t),a(e,[{key:"render",value:function(){return s.createElement(f,{style:_.cell})}}]),e}(s.Component),E=function(t){function e(){return n(this,e),r(this,(e.__proto__||Object.getPrototypeOf(e)).apply(this,arguments))}return i(e,t),a(e,[{key:"render",value:function(){return s.createElement(f,{style:_.board},s.createElement(f,{style:_.row},s.createElement(w,null),s.createElement(w,null),s.createElement(w,null),s.createElement(w,null)),s.createElement(f,{style:_.row},s.createElement(w,null),s.createElement(w,null),s.createElement(w,null),s.createElement(w,null)),s.createElement(f,{style:_.row},s.createElement(w,null),s.createElement(w,null),s.createElement(w,null),s.createElement(w,null)),s.createElement(f,{style:_.row},s.createElement(w,null),s.createElement(w,null),s.createElement(w,null),s.createElement(w,null)),this.props.children)}}]),e}(s.Component),C=function(t){function e(t){n(this,e);var o=r(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t)),i=o.props.tile;return o.state={opacity:new p.Value(0),top:new p.Value(e._getPosition(i.toRow())),left:new p.Value(e._getPosition(i.toColumn()))},o}return i(e,t),a(e,null,[{key:"_getPosition",value:function(t){return v+(t*(b+2*y)+y)}}]),a(e,[{key:"calculateOffset",value:function(){var t=this.props.tile,o={top:this.state.top,left:this.state.left,opacity:this.state.opacity};return t.isNew()?p.timing(this.state.opacity,{duration:100,toValue:1}).start():p.parallel([p.timing(o.top,{duration:100,toValue:e._getPosition(t.toRow())}),p.timing(o.left,{duration:100,toValue:e._getPosition(t.toColumn())})]).start(),o}},{key:"render",value:function(){var t=this.props.tile,e=[_.tile,_["tile"+t.value],this.calculateOffset()],o=[_.value,t.value>4&&_.whiteText,t.value>100&&_.threeDigits,t.value>1e3&&_.fourDigits];return s.createElement(p.View,{style:e},s.createElement(h,{style:o},t.value))}}]),e}(s.Component),k=function(t){function e(){return n(this,e),r(this,(e.__proto__||Object.getPrototypeOf(e)).apply(this,arguments))}return i(e,t),a(e,[{key:"render",value:function(){var t=this.props.board;if(!t.hasWon()&&!t.hasLost())return s.createElement(f,null);var e=t.hasWon()?"Good Job!":"Game Over";return s.createElement(f,{style:_.overlay},s.createElement(h,{style:_.overlayMessage},e),s.createElement(m,{onPress:this.props.onRestart,style:_.tryAgain},s.createElement(h,{style:_.tryAgainText},"Try Again?")))}}]),e}(s.Component),T=function(t){function e(t){n(this,e);var o=r(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t));return o.state={board:new g},o.startX=0,o.startY=0,o}return i(e,t),a(e,[{key:"restartGame",value:function(){this.setState({board:new g})}},{key:"handleTouchStart",value:function(t){if(!this.state.board.hasWon()){var e=l(t);this.startX=e.pageX,this.startY=e.pageY}}},{key:"handleTouchEnd",value:function(t){if(!this.state.board.hasWon()){var e=l(t),o=e.pageX-this.startX,n=e.pageY-this.startY,r=-1;Math.abs(o)>3*Math.abs(n)&&Math.abs(o)>30?r=o>0?2:0:Math.abs(n)>3*Math.abs(o)&&Math.abs(n)>30&&(r=n>0?3:1),r!==-1&&this.setState({board:this.state.board.move(r)})}}},{key:"render",value:function(){var t=this,e=this.state.board.tiles.filter(function(t){return t.value}).map(function(t){return s.createElement(C,{ref:t.id,key:t.id,tile:t})});return s.createElement(f,{style:_.container,onTouchStart:function(e){return t.handleTouchStart(e)},onTouchEnd:function(e){return t.handleTouchEnd(e)}},s.createElement(E,null,e),s.createElement(k,{board:this.state.board,onRestart:function(){return t.restartGame()}}))}}]),e}(s.Component),_=c.create({container:{flex:1,justifyContent:"center",alignItems:"center"},board:{padding:v,backgroundColor:"#bbaaaa",borderRadius:5},overlay:{position:"absolute",top:0,bottom:0,left:0,right:0,backgroundColor:"rgba(221, 221, 221, 0.5)",flex:1,flexDirection:"column",justifyContent:"center",alignItems:"center"},overlayMessage:{fontSize:40,marginBottom:20},tryAgain:{backgroundColor:"#887766",padding:20,borderRadius:5},tryAgainText:{color:"#ffffff",fontSize:20,fontWeight:"500"},cell:{width:b,height:b,borderRadius:5,backgroundColor:"#ddccbb",margin:y},row:{flexDirection:"row"},tile:{position:"absolute",width:b,height:b,backgroundColor:"#ddccbb",borderRadius:5,flex:1,justifyContent:"center",alignItems:"center"},value:{fontSize:24,color:"#776666",fontFamily:"Verdana",fontWeight:"500"},tile2:{backgroundColor:"#eeeeee"},tile4:{backgroundColor:"#eeeecc"},tile8:{backgroundColor:"#ffbb88"},tile16:{backgroundColor:"#ff9966"},tile32:{backgroundColor:"#ff7755"},tile64:{backgroundColor:"#ff5533"},tile128:{backgroundColor:"#eecc77"},tile256:{backgroundColor:"#eecc66"},tile512:{backgroundColor:"#eecc55"},tile1024:{backgroundColor:"#eecc33"},tile2048:{backgroundColor:"#eecc22"},whiteText:{color:"#ffffff"},threeDigits:{fontSize:20},fourDigits:{fontSize:18}});if(u.registerComponent("Game2048",function(){return T}),"web"==d.OS){var O=document.createElement("div");document.body.appendChild(O),u.runApplication("Game2048",{rootTag:O})}t.exports=T},445:function(t,e){"use strict";var o=function(t){for(var e=t.length,o=t[0].length,n=[],r=0;r<e;++r){n.push([]);for(var i=0;i<o;++i)n[r][i]=t[i][o-r-1]}return n},n=function i(t,e,o){this.value=t||0,this.row=e||-1,this.column=o||-1,this.oldRow=-1,this.oldColumn=-1,this.markForDeletion=!1,this.mergedInto=null,this.id=i.id++};n.id=0,n.prototype.moveTo=function(t,e){this.oldRow=this.row,this.oldColumn=this.column,this.row=t,this.column=e},n.prototype.isNew=function(){return this.oldRow===-1&&!this.mergedInto},n.prototype.hasMoved=function(){return this.fromRow()!==-1&&(this.fromRow()!==this.toRow()||this.fromColumn()!==this.toColumn())||this.mergedInto},n.prototype.fromRow=function(){return this.mergedInto?this.row:this.oldRow},n.prototype.fromColumn=function(){return this.mergedInto?this.column:this.oldColumn},n.prototype.toRow=function(){return this.mergedInto?this.mergedInto.row:this.row},n.prototype.toColumn=function(){return this.mergedInto?this.mergedInto.column:this.column};var r=function l(){this.tiles=[],this.cells=[];for(var t=0;t<l.size;++t)this.cells[t]=[this.addTile(),this.addTile(),this.addTile(),this.addTile()];this.addRandomTile(),this.setPositions(),this.won=!1};r.prototype.addTile=function(){var t=new n;return n.apply(t,arguments),this.tiles.push(t),t},r.size=4,r.prototype.moveLeft=function(){for(var t=!1,e=0;e<r.size;++e){for(var o=this.cells[e].filter(function(t){return 0!==t.value}),n=[],i=0;i<r.size;++i){var l=o.length?o.shift():this.addTile();if(o.length>0&&o[0].value===l.value){var a=l;l=this.addTile(l.value),a.mergedInto=l;var s=o.shift();s.mergedInto=l,l.value+=s.value}n[i]=l,this.won=this.won||2048===l.value,t=t||l.value!==this.cells[e][i].value}this.cells[e]=n}return t},r.prototype.setPositions=function(){this.cells.forEach(function(t,e){t.forEach(function(t,o){t.oldRow=t.row,t.oldColumn=t.column,t.row=e,t.column=o,t.markForDeletion=!1})})},r.fourProbability=.1,r.prototype.addRandomTile=function(){for(var t=[],e=0;e<r.size;++e)for(var o=0;o<r.size;++o)0===this.cells[e][o].value&&t.push({r:e,c:o});var n=Math.floor(Math.random()*t.length),i=t[n],l=Math.random()<r.fourProbability?4:2;this.cells[i.r][i.c]=this.addTile(l)},r.prototype.move=function(t){this.clearOldTiles();for(var e=0;e<t;++e)this.cells=o(this.cells);for(var n=this.moveLeft(),e=t;e<4;++e)this.cells=o(this.cells);return n&&this.addRandomTile(),this.setPositions(),this},r.prototype.clearOldTiles=function(){this.tiles=this.tiles.filter(function(t){return t.markForDeletion===!1}),this.tiles.forEach(function(t){t.markForDeletion=!0})},r.prototype.hasWon=function(){return this.won},r.deltaX=[-1,0,1,0],r.deltaY=[0,-1,0,1],r.prototype.hasLost=function(){for(var t=!1,e=0;e<r.size;++e)for(var o=0;o<r.size;++o){t=t||0===this.cells[e][o].value;for(var n=0;n<4;++n){var i=e+r.deltaX[n],l=o+r.deltaY[n];i<0||i>=r.size||l<0||l>=r.size||(t=t||this.cells[e][o].value===this.cells[i][l].value)}}return!t},t.exports=r}});
//# sourceMappingURL=game2048.js.map