{"id":216517,"date":"2014-04-03T10:08:03","date_gmt":"2014-04-03T06:08:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=216517"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=216517","title":{"rendered":"<span class=\"post_title\">2048 \u043d\u0430 Erlang<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t<img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/371\/aaa\/1cf\/371aaa1cf74630ed981f46c07cf49682.png\" alt=\"image\" align=\"left\"\/>\u041d\u0430\u0432\u0435\u0440\u043d\u043e\u0435 \u043d\u0430 \u043d\u0435\u0434\u0435\u043b\u044e \u0438\u0433\u0440\u044b 2048 \u043d\u0430 \u0445\u0430\u0431\u0440\u0435 \u0443\u0436\u0435 \u043d\u0435 \u0443\u0441\u043f\u0435\u0432\u0430\u044e, \u043d\u043e \u0441\u0442\u0430\u0442\u044c\u044f \u043d\u0435 \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u043e \u0438\u0433\u0440\u0435 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e websocket \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u043d\u0430 Erlang. \u041d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f \u043f\u0440\u0435\u0434\u044b\u0441\u0442\u043e\u0440\u0438\u044f. \u041a\u043e\u0433\u0434\u0430 \u043d\u0430\u0447\u0430\u043b \u0438\u0433\u0440\u0430\u0442\u044c \u0432 2048, \u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0435 \u043c\u043e\u0433 \u043f\u0440\u0435\u043a\u0440\u0430\u0442\u0438\u0442\u044c. \u0412 \u0443\u0449\u0435\u0440\u0431 \u0440\u0430\u0431\u043e\u0442\u0435 \u0438 \u0441\u0435\u043c\u044c\u0435. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438\u043d\u044f\u043b \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u0447\u0442\u043e \u0438\u0433\u0440\u0430\u0442\u044c \u0437\u0430 \u043c\u0435\u043d\u044f \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u043e\u0442. \u041d\u043e \u0437\u0430\u0433\u0432\u043e\u0437\u0434\u043a\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0438\u0433\u0440\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0430\u044f, \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u043d\u0435 \u0432\u0435\u0434\u0435\u0442\u0441\u044f \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0439\u0442\u0438\u043d\u0433 \u0438 \u043d\u0435 \u0442\u0430\u043a \u0443\u0434\u043e\u0431\u043d\u043e \u0438\u0433\u0440\u0430\u0442\u044c \u0431\u0435\u0437 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0438 \u0440\u0435\u0448\u0438\u043b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c, \u0433\u0434\u0435 \u0431\u044b\u043b \u0431\u044b \u0440\u0435\u0439\u0442\u0438\u043d\u0433. \u0418 \u0433\u0434\u0435 \u043c\u043e\u0433 \u0431\u044b \u0438\u0433\u0440\u0430\u0442\u044c \u043c\u043e\u0439 \u0431\u043e\u0442 \u0431\u0435\u0437 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430.<br \/>  <a name=\"habracut\"><\/a><\/p>\n<p>  \u041e\u0442\u043c\u0435\u0447\u0443, \u0447\u0442\u043e \u044d\u0442\u043e \u043c\u043e\u0439 \u043f\u0435\u0440\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u043d\u0430 Erlang. \u041c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u043e\u0432 \u0431\u043e\u0438\u0442\u0441\u044f Erlang, \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u044f, \u0447\u0442\u043e \u044d\u0442\u043e \u0441\u043b\u043e\u0436\u043d\u043e. \u041d\u043e \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u044d\u0442\u043e \u043d\u0435 \u0442\u0430\u043a. \u041f\u043b\u044e\u0441, \u044f \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u0432\u044b\u0441\u0432\u0435\u0442\u043b\u0438\u0442\u044c \u043c\u043e\u043c\u0435\u043d\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b \u043d\u043e\u0432\u0438\u0447\u043a\u0443 \u0432 Erlang.<\/p>\n<p>  \u0414\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u043c\u043d\u043e\u0433\u043e \u0447\u0435\u0433\u043e \u0437\u0430\u0445\u0430\u0440\u0434\u043a\u043e\u0436\u0435\u043d\u043e. \u041d\u043e \u044f \u0432\u0441\u0435\u0433\u0434\u0430 \u0440\u0430\u0434 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u0438\u0432\u043d\u043e\u0439 \u043a\u0440\u0438\u0442\u0438\u043a\u0435 \u0438 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u043c.<br \/>  \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 github \u2014 <a href=\"https:\/\/github.com\/peinguin\/erl2048\">erl2048<\/a>.<br \/>  \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0440\u0430\u0431\u043e\u0447\u0438\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u2014 <a href=\"http:\/\/2048.php.poltava.ua\/\">erl2048<\/a>. \u041d\u043e, \u0434\u0443\u043c\u0430\u044e, \u043f\u043e\u0434 \u0445\u0430\u0431\u0440\u0430\u044d\u0444\u0444\u0435\u043a\u0442\u043e\u043c \u043f\u0440\u043e\u0436\u0438\u0432\u0435\u0442 \u043e\u043d \u043d\u0435\u0434\u043e\u043b\u0433\u043e.<\/p>\n<h4>JavaScript<\/h4>\n<p>  \u041a\u0430\u043a \u043d\u0438 \u0441\u0442\u0440\u0430\u043d\u043d\u043e \u2014 \u043d\u0430\u0447\u043d\u0443 \u0441 JS. \u042f \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u044f\u043b \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0444\u0430\u0439\u043b\u044b, \u0447\u0442\u043e\u0431\u044b \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u043e\u0433\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f, \u0435\u0441\u043b\u0438 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f. \u042f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b:  <\/p>\n<ul>\n<li>main.css;<\/li>\n<li>animframe_polyfill.js \u0434\u043b\u044f requestAnimationFrame;<\/li>\n<li>html_actuator.js \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0439<\/li>\n<li>keyboard_input_manager.js \u0434\u043b\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u043a\u043b\u0430\u0432\u0438\u0430\u0442\u0443\u0440\u044b, \u0438, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043b\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430, \u0437\u0440\u044f;<\/li>\n<\/ul>\n<p>  \u042f \u0441\u043e\u0437\u0434\u0430\u043b \u0444\u0430\u0439\u043b \u00abmain.js\u00bb. \u041b\u043e\u0433\u0438\u043a\u0430 \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u2014 \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u0448\u043b\u0435\u0442 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u0441\u043e\u0431\u044b\u0442\u0438\u044f, \u0438 \u043f\u043e\u0442\u043e\u043c \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442 \u043f\u043e\u043b\u0435. \u0411\u043b\u0430\u0433\u043e, animframe_polyfill \u0441\u043e\u0437\u0434\u0430\u043d \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0447\u0442\u043e \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 grid.<\/p>\n<p>  \u0427\u0442\u043e \u044f \u0434\u043e\u0431\u0430\u0432\u0438\u043b. \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"javascript\">var websocket = new Websocket(SERVER);   websocket   .connect()   .done(function(){     var myGame = new MyGame(websocket);       }); <\/code><\/pre>\n<p>  \u041d\u0430 \u0441\u043a\u043e\u0440\u0443\u044e \u0440\u0443\u043a\u0443 \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u043e\u0431\u0435\u0440\u0442\u043a\u0443 \u043d\u0430\u0434 \u00abWebsocket\u00bb. \u041e\u043d\u0430 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u0437\u0434\u0435\u0441\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434.<br \/>  \u041d\u0430\u0447\u0430\u043b\u043e \u043d\u043e\u0432\u043e\u0439 \u0438\u0433\u0440\u044b:<\/p>\n<pre><code class=\"javascript\">self.restart = function(evt){   websocket.send(JSON.stringify({     action:'start'   })); }; <\/code><\/pre>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u0434\u0435\u043b\u0430\u0442\u044c \u0445\u043e\u0434:<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"javascript\">self.move = function(direction){   \/\/ 0: up, 1: right, 2:down, 3: left   if(!toMove){     return false;   }   if(direction === 0){     direction = 'up';   }else if(direction === 1){     direction = 'right';   }else if(direction === 2){     direction = 'down';   }else if(direction === 3){     direction = 'left';   }   websocket.send(JSON.stringify({     action:'move',     value: direction   })); }; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0418 \u0441\u0430\u043c\u044b\u0439 \u0431\u043e\u043b\u044c\u0448\u043e\u0439.  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0442\u0432\u0435\u0442\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430:<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"javascript\">self.wsHandler = function(evt){   var game = JSON.parse(evt.data);    if(game.grid){      var grid = {cells: []};     game.grid.forEach(function (column, y) {       var row = [];       column.forEach(function (cell, x) {         if(cell){           if(cell.mergedFrom){             cell.mergedFrom.forEach(function(tile){               tile['x'] = x;               tile['y'] = y;             });           }           row.push({             value:            cell.value,             x:                x,             y:                y,             previousPosition: cell.previousPosition,             mergedFrom:       cell.mergedFrom           });         }       });       grid.cells.push(row);     });      var scores = game.scores,       bestScore = 0;     if(scores && scores.length&gt;0){       bestScore = scores[0].score;        while (scoresEl.firstChild) {         scoresEl.removeChild(scoresEl.firstChild);       }        scores.forEach(function(score){         var div = document.createElement('Div');         var name = document.createElement('Div');         var scoreEl = document.createElement('Div');          div.setAttribute(&quot;class&quot;, 'score');         name.setAttribute(&quot;class&quot;, 'name');         scoreEl.setAttribute(&quot;class&quot;, 'score');          name.appendChild(document.createTextNode(score.name));         scoreEl.appendChild(document.createTextNode(score.score));          div.appendChild(name);         div.appendChild(scoreEl);         scoresEl.appendChild(div);       });     }      actuator.actuate(grid, {       score:     game.score,       bestScore: bestScore,       score: game.score,       won: game.won,       over: game.over,       keepPlaying: game.keepPlaying     });   }    \/\/playername actuator   if(game.user){     if(playername.value !== playername){       playername.value = game.user.name;     }   } }; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e, \u0438\u0433\u0440\u0430 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432\u0441\u0435 \u0440\u0430\u0441\u0447\u0435\u0442\u044b \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u044f\u0442 \u0442\u0430\u043c. \u041d\u0435 \u0442\u0430\u043a \u043a\u0430\u043a, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u043c\u043e\u0435\u0439 \u0438\u0433\u0440\u0435 <a href=\"https:\/\/github.com\/peinguin\/tictoe\">\u041a\u0440\u0435\u0441\u0442\u0438\u043a\u0438 \u043d\u043e\u043b\u0438\u043a\u0438<\/a>, \u0433\u0434\u0435 \u043b\u043e\u0433\u0438\u043a\u0430 \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f.<br \/>  \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435, \u043d\u0435 \u043f\u043e\u043d\u044f\u043b, \u0437\u0430\u0447\u0435\u043c \u0432 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f x \u0438 y \u0432 Tile, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u0435\u0440\u0432\u0435\u0440 \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0431\u0435\u0437 \u043d\u0438\u0445. \u0410 \u043d\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0435 \u0443\u0436\u0435 \u0434\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u044e, \u0447\u0442\u043e\u0431\u044b actuator \u0441\u044c\u0435\u043b.<br \/>  \u0422\u0430\u043a\u0436\u0435 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u0442\u043e\u043f10 \u043b\u0443\u0447\u0448\u0438\u0445 \u0438\u0433\u0440\u043e\u043a\u043e\u0432. \u042d\u0442\u043e \u043d\u043e\u0432\u043e\u0432\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043c\u043e\u0435\u0439 \u0432\u0435\u0440\u0441\u0438\u0438. \u0418 \u0435\u0449\u0435 \u0438\u0433\u0440\u043e\u043a \u043c\u043e\u0436\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0441\u0432\u043e\u0439 \u043d\u0438\u043a. \u041d\u0438\u043a\u0430\u043a\u0438\u0445 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0439 \u0438 \u0437\u0430\u0449\u0438\u0442. \u0412\u0432\u0435\u043b \u0438\u043c\u044f \u0438 \u0438\u0433\u0440\u0430\u0439. \u041d\u0443\u0436\u043d\u043e \u043d\u0430\u0432\u0435\u0441\u0442\u0438 \u043d\u0430 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u043a \u0441 best score \u0447\u0442\u043e\u0431\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043e\u0431\u0449\u0438\u0439 \u0440\u0435\u0439\u0442\u0438\u043d\u0433. \u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u044d\u0442\u043e \u0442\u0430\u043a.<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/676\/d55\/7c5\/676d557c538760bde84b61e70816b413.png\"\/><\/p>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u043e\u0434\u043d\u043e\u0439 keyboard_input_manager \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u043e. \u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u0432 \u043f\u043e\u043b\u0435 \u0432\u0432\u043e\u0434\u0430 \u043d\u0438\u043a\u043d\u0435\u0439\u043c\u0430 \u043c\u043e\u0436\u043d\u043e \u0432\u0432\u043e\u0434\u0438\u0442\u044c \u043d\u0435 \u0432\u0441\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b. \u041d\u043e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0441\u0432\u043e\u0439 \u043d\u0438\u043a \u0441 \u0431\u0443\u0444\u0435\u0440\u0430 \u043e\u0431\u043c\u0435\u043d\u0430.<br \/>  \u041f\u043b\u044e\u0441, \u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b \u043d\u0435 \u0432\u0435\u0441\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b. \u0427\u0430\u0441\u0442\u044c, \u0447\u0442\u043e \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u00ab\u043f\u0440\u043e\u0438\u0433\u0440\u044b\u0448\u00bb \u043f\u043e\u043a\u0430 \u0437\u0430\u043a\u0440\u044b\u0442\u0430 \u0437\u0430\u0433\u043b\u0443\u0448\u043a\u043e\u0439, \u043d\u043e \u044d\u0442\u043e \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0432\u043b\u0438\u044f\u0435\u0442 \u043d\u0430 \u0438\u0433\u0440\u043e\u0432\u043e\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441. \u0418 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0438\u0433\u0440\u0443 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0438\u0433\u0440\u044b\u0448\u0430 \u043f\u043e\u043a\u0430 \u043d\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438. \u041d\u043e \u0432\u044b\u0438\u0433\u0440\u0430\u0442\u044c \u0435\u0449\u0435 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c.<\/p>\n<h4>Erlang<\/h4>\n<p>  \u042d\u0442\u0430 \u0447\u0430\u0441\u0442\u044c \u0431\u0443\u0434\u0435\u0442 \u0431\u043e\u043b\u0435\u0435 \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u043e \u0440\u0430\u0441\u043f\u0438\u0441\u0430\u043d\u0430. \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0443\u0436\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c rebar. \u0421\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/github.com\/basho\/rebar\">\u043e\u0442\u0441\u044e\u0434\u0430<\/a>. Rebar \u043c\u043e\u0436\u0435\u0442 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0444\u0430\u0439\u043b\u044b, \u043d\u043e \u044f \u0438\u0445 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u043b \u0432\u0440\u0443\u0447\u043d\u0443\u044e.<br \/>  \u00abrebar.config\u00bb \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044f \u0438 \u0441\u0431\u043e\u0440\u043a\u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439.  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"erlang\">% The next option is required so we can use lager.   {erl_opts, [{parse_transform, lager_transform}]}.   {lib_dirs,[&quot;deps&quot;]}.   % Our dependencies.   {deps, [         {'lager', &quot;.*&quot;, {           git, &quot;git:\/\/github.com\/basho\/lager.git&quot;, &quot;master&quot;}       },     {'cowboy', &quot;.*&quot;, {           git, &quot;git:\/\/github.com\/extend\/cowboy.git&quot;, &quot;master&quot;}       },     {'mochiweb', &quot;.*&quot;, {     \tgit, &quot;git:\/\/github.com\/mochi\/mochiweb.git&quot;, &quot;master&quot;}     },     {'sqlite3', &quot;.*&quot;, {     \tgit, &quot;git:\/\/github.com\/alexeyr\/erlang-sqlite3.git&quot;, &quot;master&quot;}     } ]}.   <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<pre><code class=\"bash\"># rebar g-d # rebar co <\/code><\/pre>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u0438 \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u00ablibsqlite3-dev\u00bb \u0434\u043b\u044f sqlite \u0434\u0440\u0430\u0439\u0432\u0435\u0440\u0430.<\/p>\n<p>  \u0414\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e:  <\/p>\n<pre><code class=\"bash\"># rebar compile skip_deps=true; erl -pa ebin deps\/*\/ebin -eval 'starter:start().' -noshell -detached <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0438\u0433\u0440\u0430 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430 \u043d\u0430 8080 \u043f\u043e\u0440\u0442\u0443. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435, \u043d\u0430\u0443\u0447\u0438\u0442\u0441\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0442 \u0431\u044b\u043b\u043e \u0441\u0430\u043c\u044b\u043c \u0441\u043b\u043e\u0436\u043d\u044b\u043c. \u0414\u0430\u043b\u044c\u0448\u0435 \u2014 \u043b\u0435\u0433\u0447\u0435. \u042f \u0441\u043e\u0437\u0434\u0430\u043b \u0441\u043f\u0435\u0446\u0438\u043b\u044c\u043d\u044b\u0439 \u043c\u043e\u0434\u0443\u043b\u044c \u00abstarter\u00bb, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0432\u0441\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435.<\/p>\n<pre><code class=\"erlang\">-module(starter). -export([start\/0]).  start() -&gt; \tapplication:start(ranch), \tapplication:start(crypto), \tapplication:start(cowlib), \tapplication:start(cowboy), \tapplication:start(inets), \tapplication:start(mochiweb), \tapplication:start(erl2048). <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u044e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 \u00absrc\u00bb. \u041f\u0435\u0440\u0432\u043e\u0435 \u2014 \u0444\u0430\u0439\u043b \u00aberl2048.app.src\u00bb. \u041d\u0435 \u0437\u043d\u0430\u044e, \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435, \u0434\u043b\u044f \u0447\u0435\u0433\u043e \u043e\u043d \u043d\u0443\u0436\u0435\u043d, \u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u0438 \u0441\u0432\u043e\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u043d\u0430 \u0432\u0441\u044f\u043a\u0438\u0439 \u0441\u043b\u0443\u0447\u0430\u0439.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"erlang\">{application, erl2048, [ {description, &quot;2048 game server.&quot;}, {vsn, &quot;1&quot;}, {modules, []}, {registered, [erl2048_sup]}, {applications, [ kernel, stdlib, cowboy ]}, {mod, {erl2048_app, []}}, {env, []} ]}. <\/code><\/pre>\n<\/div>\n<\/div>\n<div class=\"spoiler\"><b class=\"spoiler_title\">erl2048_sup.erl<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"erlang\">%% Feel free to use, reuse and abuse the code in this file.  %% @private -module(erl2048_sup). -behaviour(supervisor).  %% API. -export([start_link\/0]).  %% supervisor. -export([init\/1]).  %% API.  -spec start_link() -&gt; {ok, pid()}. start_link() -&gt;     supervisor:start_link({local, ?MODULE}, ?MODULE, []).  %% supervisor.  init([]) -&gt;     Procs = [],     {ok, {{one_for_one, 10, 10}, Procs}}.  <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u042f \u0442\u0430\u043a \u043f\u043e\u043d\u0438\u043c\u0430\u044e, \u0447\u0442\u043e \u044d\u0442\u0430 \u0448\u0442\u0443\u043a\u0430 \u0441\u043b\u0435\u0434\u0438\u0442, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0435 \u043f\u0430\u0434\u0430\u043b\u043e \u0438 \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0430\u0434\u043e\u0431\u043d\u043e\u0441\u0442\u0438. \u0412\u0437\u044f\u043b \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u2014 \u0440\u0435\u0448\u0438\u043b \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c.<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u0444\u0430\u0439\u043b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u2014 \u00aberl2048_app.erl\u00bb.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"erlang\">%% Feel free to use, reuse and abuse the code in this file.  %% @private -module(erl2048_app). -behaviour(application).  %% API. -export([start\/2]). -export([stop\/1]).  %% API. start(_Type, _Args) -&gt;     Dispatch = cowboy_router:compile([         {'_', [             {&quot;\/&quot;, cowboy_static, {file, &quot;..\/client\/index.html&quot;}},             {&quot;\/websocket&quot;, ws_handler, []},             {&quot;\/static\/[...]&quot;, cowboy_static, {dir, &quot;..\/client\/static&quot;}}         ]}     ]),     {ok, _} = cowboy:start_http(http, 100, [{port, 8080}],         [{env, [{dispatch, Dispatch}]}]),     {ok, _} = db:start_link(),     erl2048_sup:start_link().  stop(_State) -&gt;     {ok, _} = db:stop(),     ok.  <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u0417\u0434\u0435\u0441\u044c \u044f \u0443\u0436\u0435 \u043c\u043e\u0433\u0443 \u043a\u043e\u0435-\u0447\u0442\u043e \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u0442\u044c. \u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0440\u043e\u0443\u0442\u044b \u0434\u043b\u044f cowboy. \u041f\u043e\u0442\u043e\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f cowboy \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445.<br \/>  \u0412 \u0440\u043e\u043b\u0438 \u0441\u0443\u0431\u0434 \u0432\u044b\u0441\u0442\u0443\u043f\u0430\u0435\u0442 sqlite. \u042f \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u043b \u0435\u0449\u0435 Postgresql, mongoDB \u0438 Redis. \u041d\u043e \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0441\u044f \u043d\u0430 sqlite, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u043d \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439. \u041f\u043b\u044e\u0441 \u0445\u0440\u0430\u043d\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u043a\u0430. \u041d\u043e, \u0434\u0443\u043c\u0430\u044e, \u0441\u043e\u0437\u0434\u0430\u0441\u0442 \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u043d\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u043e\u043d\u043e \u0441\u043a\u043e\u0440\u0435\u0435 \u043b\u044f\u0436\u0435\u0442. \u041a\u0430\u043a \u0431\u044b \u0442\u0430\u043c \u043d\u0438 \u0431\u044b\u043b\u043e \u2014 \u043a\u043e\u0434 \u043c\u043e\u0434\u0443\u043b\u044f:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"erlang\">-module(db).  -export([start_link\/0,stop\/0]). -export([insert\/2, select\/0, createUser\/1, changeName\/2]).  start_link() -&gt;     {ok, PID} = sqlite3:open(db, [{file, &quot;db.sqlite3&quot;}]),      Tables = sqlite3:list_tables(db),      case lists:member(&quot;scores&quot;, Tables) of false -&gt;         sqlite3:create_table(db, scores, [{id, integer, [{primary_key, [asc, autoincrement]}]}, {userid, integer}, {score, integer}])     end,      case lists:member(&quot;users&quot;, Tables) of false -&gt;         sqlite3:create_table(db, users, [{id, integer, [{primary_key, [asc, autoincrement]}]}, {name, text}])     end,      {ok, PID}.  stop() -&gt;     sqlite3:close(db).  select() -&gt;     Ret = sqlite3:sql_exec(db, &quot;select users.name, scores.score from scores LEFT JOIN users ON (users.id = scores.userid) ORDER BY score desc;&quot;),     [{columns,_},{rows,Rows}] = Ret,     formatScores(Rows).  insert(Score, Player) -&gt;     [{columns,_},{rows,Rows}] = sqlite3:sql_exec(db, &quot;SELECT score FROM scores WHERE userid = ?&quot;, [{1,Player}]),     DBScore = if         length(Rows) &gt; 0  -&gt; element(1,hd(Rows));         true -&gt; 0     end, erlang:display({Score,DBScore}),      if Score &gt; DBScore -&gt;         sqlite3:delete(db, scores, {userid, Player}),         sqlite3:write(db, scores, [{userid, Player}, {score, Score}]),         sqlite3:sql_exec(db, &quot;DELETE FROM scores WHERE id IN (SELECT id FROM scores ORDER BY score desc LIMIT 1 OFFSET 10)&quot;);         true -&gt; undefined     end.  formatScores([]) -&gt;     []; formatScores([{Name, Score} | Rows]) -&gt;     [{struct, [{name, Name},{score, Score}]} | formatScores(Rows)].  createUser(UserName) -&gt;     sqlite3:write(db, users, [{name, UserName}]).  changeName(Id, NewName) -&gt;     sqlite3:update(db, users, {id, Id}, [{name, NewName}]). <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u041f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u043c\u043e\u0434\u0443\u043b\u044e, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 websocket \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">ws_handler.erl<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"erlang\">-module(ws_handler). -behaviour(cowboy_websocket_handler).  -export([init\/3]). -export([websocket_init\/3]). -export([websocket_handle\/3]). -export([websocket_info\/3]). -export([websocket_terminate\/3]).  init({tcp, http}, _Req, _Opts) -&gt;     {upgrade, protocol, cowboy_websocket}.  websocket_init(_TransportName, Req, _Opts) -&gt;     State = {struct, [          { user, { struct, [{id, null},{name, &lt;&lt;&quot;Player&quot;&gt;&gt;}] } }      ]},     {ok, Req, State}.  websocket_handle({text, Msg}, Req, State) -&gt;     Message = mochijson2:decode(Msg, [{format, proplist}]),     Action =  binary_to_list(proplists:get_value(&lt;&lt;&quot;action&quot;&gt;&gt;, Message)),     {NewState, Response} = case Action of         &quot;start&quot; -&gt;             TmpState = game:init(State),             {TmpState, TmpState};         &quot;move&quot;  -&gt;             TmpState = game:move(list_to_atom(binary_to_list(proplists:get_value(&lt;&lt;&quot;value&quot;&gt;&gt;, Message))), State),             {TmpState, TmpState};         &quot;newName&quot; -&gt;             NewName = proplists:get_value(&lt;&lt;&quot;value&quot;&gt;&gt;, Message),             JsonData = element(2, State),              User = proplists:get_value(user, JsonData),             {struct,UserJsonData} = User,              Id = proplists:get_value(id, UserJsonData),              db:changeName(Id, NewName),              TmpState = {struct, [                     { user, { struct, [ { name, NewName },{ id, Id } ] } }                     | proplists:delete(user, JsonData)                 ]},             {                 TmpState,                 {struct, [{ user, { struct, [ { name, NewName },{ id, Id } ] } }]}             };         _Else -&gt; State     end,     {reply, {text, mochijson2:encode(Response)}, Req, NewState};  websocket_handle(_Data, Req, State) -&gt;     {ok, Req, State}.  websocket_info({send, Msg}, Req, State) -&gt;     {reply, {text, Msg}, Req, State}; websocket_info(_Info, Req, State) -&gt;     {ok, Req, State}.  websocket_terminate(_Reason, _Req, _State) -&gt;     ok. <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u041f\u043e\u043d\u0430\u0447\u0430\u043b\u0443 \u044f \u043d\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u043b \u043a\u0430\u043a \u043e\u043d\u043e \u0432\u0441\u0435 \u0443\u0441\u0442\u0440\u043e\u0435\u043d\u043e. \u041e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0432\u0441\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e. \u0415\u0441\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0437\u0430\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f. \u0418 \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u0430\u0436\u0434\u044b\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0441\u0432\u043e\u0435. \u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043c\u0435\u0442\u043e\u0434 \u0437\u0434\u0435\u0441\u044c \u044d\u0442\u043e \u00abwebsocket_handle\u00bb. \u041e\u043d \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0442\u0432\u0435\u0442 \u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435.<br \/>  \u0414\u043b\u044f \u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0444\u043e\u0440\u043c\u0430\u0442 JSON. \u0412 Erlang \u043e\u043d \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 \u0442\u0438\u043f\u0430:<\/p>\n<pre><code class=\"erlang\">{struct, [   {key1, Value1},   {key2, Value2},   .... ]} <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0444\u0430\u0439\u043b\u044b \u0438\u0433\u0440\u044b. \u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u00abtile.erl\u00bb.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">tile.erl<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"erlang\">-module(tile).  -export([init\/1, init\/0, prepare\/2]).  prepare(null, _) -&gt;     null; prepare(Tile, { X, Y }) -&gt;     {         struct,         [             {value, proplists:get_value(value, element(2, Tile))},             {mergedFrom, null},             {previousPosition, {struct, [{ x, X - 1},{ y, Y - 1 }]}}         ]     }. init(Value) -&gt;     {         struct,         [             {value, Value},             {mergedFrom, null},             {previousPosition, null}         ]     }. init() -&gt;     init(2). <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u0422\u043e\u043b\u044c\u043a\u043e \u0438 \u0443\u043c\u0435\u0435\u0442, \u0447\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0442\u0430\u0439\u043b \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e.<br \/>  \u00abgrid.erl\u00bb \u0443\u0436\u0435 \u043f\u043e\u0441\u043b\u043e\u0436\u043d\u0435\u0435.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">grid.erl<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"erlang\">-module(grid).  -export([     build\/0,     cellsAvailable\/1,     randomAvailableCell\/1,     insertTile\/3,     availableCells\/1,     cellContent\/2,     removeTile\/2,     moveTile\/3,     size\/0,     withinBounds\/1,     cellAvailable\/2 ]).  -define(SIZE, 4).  size() -&gt;     ?SIZE.  build() -&gt;     [[null || _ &lt;- lists:seq(1, ?SIZE)] || _ &lt;- lists:seq(1, ?SIZE)].  availableCells(Grid) -&gt;     lists:append(         setY(             availableCells(Grid, 1)         )     ).  availableCells([Grid | Tail ], N) when is_list(Grid) -&gt;     [{availableCells(Grid, 1), N} | availableCells(Tail, N +1)]; availableCells([Grid | Tail ], N) -&gt;     case Grid =:= null of         true -&gt; [ N | availableCells(Tail, N +1)];         false -&gt;  availableCells(Tail, N +1)     end; availableCells([], _) -&gt;     [].  setY([{Cell, Y}|Tail]) -&gt;      [ setY(Cell, Y) | setY(Tail)]; setY([]) -&gt;      []. setY([Head | Tail], Y) -&gt;     [ {Head, Y} | setY(Tail, Y)]; setY([], _) -&gt;     [].  cellsAvailable(Grid) -&gt;     length(availableCells(Grid)) &gt; 0.  randomAvailableCell(Grid) -&gt;     Cells = availableCells(Grid),     lists:nth(random:uniform(length(Cells)) ,Cells).  insertTile({X, Y}, Tile, Grid) -&gt;     Row = lists:nth(Y,Grid),     lists:sublist(Grid,Y - 1) ++ [ lists:sublist(Row,X - 1) ++ [Tile] ++ lists:nthtail(X,Row)] ++ lists:nthtail(Y,Grid).  cellContent({ X, Y }, Grid) -&gt;     case withinBounds({ X, Y }) of         true -&gt; lists:nth(X,lists:nth(Y,Grid));         false -&gt; null     end.  removeTile({ X, Y }, Grid) -&gt;     insertTile({X, Y}, null, Grid).  moveTile(Cell, Cell, Grid) -&gt;     Grid; moveTile(Cell, Next, Grid) -&gt;     insertTile(Next, grid:cellContent(Cell, Grid), removeTile(Cell, Grid)).  withinBounds({X, Y}) when     (X &gt; 0), (X =&lt; ?SIZE),      (Y &gt; 0), (Y =&lt; ?SIZE) -&gt;     true; withinBounds(_) -&gt;     false.  cellAvailable(Cell, Grid) -&gt;     case grid:withinBounds(Cell) of         true -&gt; cellContent(Cell, Grid) =:= null;         false -&gt; false     end. <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 availableCells. \u0412 Erlang \u043d\u0443\u0436\u043d\u043e \u043f\u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e. \u041d\u043e \u0437\u0434\u0435\u0441\u044c \u044f \u0441\u0430\u043c \u0441\u0435\u0431\u044f \u043f\u0435\u0440\u0435\u043c\u0443\u0434\u0440\u0438\u043b. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043b \u043b\u0438\u0441\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043b \u043b\u0438\u0441\u0442\u044b \u0441 \u043e\u0434\u043d\u043e\u0439 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u043e\u0439 \u0438 \u0432\u0442\u043e\u0440\u0443\u044e \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u0443. \u0410 \u043f\u043e\u0442\u043e\u043c \u0432\u043d\u043e\u0441\u0438\u043b \u0432\u0442\u043e\u0440\u0443\u044e \u043a \u043f\u0435\u0440\u0432\u043e\u0439. \u042f \u0440\u0435\u0448\u0438\u043b \u0431\u043e\u043b\u044c\u0448\u0435 \u0442\u0430\u043a \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c. \u041e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0434\u0443\u043c\u0430\u044e, \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b.<br \/>  \u0418, \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0444\u0430\u0439\u043b \u0438\u0433\u0440\u044b. \u0422\u0430\u043a \u0438 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u00abgame.erl\u00bb.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">game.erl<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"erlang\">-module(game).  -export([init\/1, move\/2]).  init(State) -&gt;      StateUser = proplists:get_value(user, element(2, State)),     StateUserJsonData = element(2, StateUser),      User = case proplists:get_value(id, StateUserJsonData) of         null -&gt;             Name = proplists:get_value(name, StateUserJsonData),             {rowid, Id} = db:createUser(Name),             { struct, [{name, Name},{id, Id}]};         _Else -&gt;             StateUser     end,      {         struct,         [             {grid ,addStartTiles(grid:build())},             {user , User},             {score,0},             {scores, db:select()},             {won, false},             {over, false},             {keepPlaying, false}         ]     }.  addStartTiles(Grid, 0) -&gt;      Grid; addStartTiles(Grid, N) -&gt;      NewGrid = addRandomTile(Grid),     addStartTiles(NewGrid, N - 1). addStartTiles(Grid) -&gt;     addStartTiles(Grid, 2).  addRandomTile(Grid) -&gt;     random:seed(now()),     case grid:cellsAvailable(Grid) of         true -&gt;              case random:uniform(10) &lt; 9 of                 true -&gt; Tile = tile:init();                 false -&gt; Tile = tile:init(grid:size())             end,             grid:insertTile(grid:randomAvailableCell(Grid), Tile, Grid);         false -&gt; Grid     end.  getVector(left) -&gt;     { -1, 0 }; getVector(up) -&gt;     { 0,  -1 }; getVector(right) -&gt;     { 1,  0 }; getVector(down) -&gt;     { 0,  1 }.  buildTraversals() -&gt;     Traver = lists:seq(1, grid:size()),     { Traver, Traver }. buildTraversals({ 1 , _ }) -&gt;     { T1, T2} = buildTraversals(),     { lists:reverse(T1), T2 }; buildTraversals({ _ , 1 }) -&gt;     { T1, T2} = buildTraversals(),     { T1, lists:reverse(T2) }; buildTraversals({ _ , _ }) -&gt;     buildTraversals().  prepareTiles( [{_Key, _Value} | _Tail ] ) -&gt;     JsonData = [{_Key, _Value} | _Tail ],     [{ grid, prepareTiles(proplists:get_value(grid, JsonData)) } | proplists:delete(grid, JsonData) ]; prepareTiles( Grid ) -&gt;     prepareTiles( Grid, 1). prepareTiles([], _) -&gt;     []; prepareTiles([Row | Tail], Y) -&gt;     [ prepareTileY(Row, 1, Y) | prepareTiles(Tail, Y + 1)]. prepareTileY([], _, _) -&gt;     []; prepareTileY([Cell | Tail], X, Y) -&gt;     [prepareTileX(Cell, X, Y) | prepareTileY(Tail, X + 1, Y) ]. prepareTileX(Tile, X, Y) -&gt;     tile:prepare(Tile, {X, Y}).  process_travesals_y([], _, _, JsonData) -&gt;     JsonData; process_travesals_y(_, [], _, JsonData) -&gt;     JsonData; process_travesals_y([ Y | Tail ], TraversalsX, Vector, JsonData) -&gt;     process_travesals_y(         Tail,         TraversalsX,         Vector,         process_travesals_y( Y, TraversalsX, Vector, JsonData)     ); process_travesals_y(Y, [ X | Tail ], Vector, JsonData) -&gt;     process_travesals_y(Y, Tail, Vector, process_travesals_y( Y, X, Vector, JsonData )); process_travesals_y( Y, X, Vector, JsonData ) -&gt;     moveTile({ X, Y }, Vector, JsonData).  findFarthestPosition({X, Y}, {VecX, VecY}, Grid) -&gt;      Next = { X + VecX, Y + VecY },      case grid:cellAvailable(Next, Grid) of         true -&gt;              findFarthestPosition(Next, {VecX, VecY}, Grid);         false -&gt;              {                 {X, Y},                 Next % Used to check if a merge is required             }     end.  moveTile(Cell, Vector, JsonData) -&gt;      Grid = proplists:get_value(grid, JsonData),     Tile = grid:cellContent(Cell, Grid),      case Tile =:= null of         true -&gt; JsonData;         false -&gt;             { Farthest, Next } = findFarthestPosition(Cell, Vector, Grid),              {struct, CurrJsonData} = Tile,             CurrValue = proplists:get_value(value, CurrJsonData),              NextTile = if                 Next =:= null -&gt; null;                 true -&gt;                     grid:cellContent(Next, Grid)             end,              {NextValue, NextMerged} = if                 NextTile =:= null -&gt; {null, null};                 true -&gt;                     NextJsonData = element(2, NextTile),                     {proplists:get_value(value, NextJsonData), proplists:get_value(mergedFrom, NextJsonData)}             end,              if  CurrValue =:= NextValue,                 NextMerged =:= null                 -&gt;                     MergedValue = CurrValue * 2,                     Merged = {                         struct,                         [                             {value, MergedValue},                             {mergedFrom, [Tile,NextTile]},                             {previousPosition, null}                         ]                     },                     NewGrid = grid:insertTile(Next, Merged, grid:removeTile(Cell, Grid)),                      % Update the score                     Score = proplists:get_value(score, JsonData) + MergedValue,                      % The mighty 2048 tile                     Won = if                         MergedValue =:= 2048 -&gt; true;                         true -&gt; false                     end,                      Removed = proplists:delete(score, proplists:delete(won, proplists:delete(grid, JsonData))),                      [                         {grid,NewGrid},                         {won,Won},                         {score,Score} |                         Removed                     ];                 true -&gt;                     [                         {                             grid,                             grid:moveTile(Cell, Farthest, proplists:get_value(grid, JsonData))                         }                         | proplists:delete(grid, JsonData)                     ]             end     end.  move(left, State) -&gt;     move(getVector(left), State); move(right, State) -&gt;      move(getVector(right), State); move(up, State) -&gt;      move(getVector(up), State); move(down, State) -&gt;      move(getVector(down), State); move(Vector, State) -&gt;     {struct, JsonData} = State,      case          proplists:get_value(over, JsonData) or (             proplists:get_value(won, JsonData) and (not proplists:get_value(keepPlaying, JsonData))         )     of         true -&gt; State;         _Else -&gt;             PreparedJsonData = updateBestScore(prepareTiles(JsonData)),              { TraversalsX, TraversalsY } = buildTraversals(Vector),              NewJsonData = process_travesals_y(                 TraversalsY,                 TraversalsX,                 Vector,                 PreparedJsonData             ),              if                 PreparedJsonData =\/= NewJsonData -&gt; %If changed - add new tile                     Grid = proplists:get_value(grid, NewJsonData),                     {struct, UserJsonData} = proplists:get_value(user, NewJsonData),                      NewScore = proplists:get_value(score, NewJsonData),                     Score = proplists:get_value(score, PreparedJsonData),                      case NewScore &gt; Score of true -&gt;                         db:insert(                             proplists:get_value(score, NewJsonData),                             proplists:get_value(id, UserJsonData)                         );                         _Else -&gt; undefined                     end,                      Over = case movesAvailable(Grid) of                         true -&gt; false;                         fale -&gt; true % Game over!                     end,                     Removed = proplists:delete(grid, proplists:delete(over, NewJsonData)),                     {struct,[{ grid, addRandomTile(Grid) }, { over, Over } | Removed ]};                 true -&gt; %return state otherwise                     {struct,PreparedJsonData}             end     end .  movesAvailable(_) -&gt;     true.  updateBestScore(JsonData) -&gt;     [{ scores, db:select() } | proplists:delete(scores, JsonData) ]. <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f init \u2014 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043d\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0435\u0441\u043b\u0438 \u0442\u043e\u0442 \u043d\u0435 \u0431\u044b\u043b \u0441\u043e\u0437\u0434\u0430\u043d. \u0418\u043b\u0438 \u0431\u0435\u0440\u0435\u0442 \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0438\u0433\u0440\u044b.<\/p>\n<pre><code class=\"erlang\">init(State) -&gt;      StateUser = proplists:get_value(user, element(2, State)),     StateUserJsonData = element(2, StateUser),      User = case proplists:get_value(id, StateUserJsonData) of         null -&gt;             Name = proplists:get_value(name, StateUserJsonData),             {rowid, Id} = db:createUser(Name),             { struct, [{name, Name},{id, Id}]};         _Else -&gt;             StateUser     end,      {         struct,         [             {grid ,addStartTiles(grid:build())},             {user , User},             {score,0},             {scores, db:select()},             {won, false},             {over, false},             {keepPlaying, false}         ]     }. <\/code><\/pre>\n<p>  \u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u2014 move. \u041e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u043f\u0435\u0440\u0435\u0441\u0447\u0435\u0442 \u0438\u0433\u0440\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044f. \u0417\u0434\u0435\u0441\u044c \u0431\u044b\u043b\u0438 \u0442\u0440\u0443\u0434\u043d\u043e\u0449\u0438, \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0438\u0437-\u0437\u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u043e\u043f\u044b\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<pre><code class=\"erlang\">move(left, State) -&gt;     move(getVector(left), State); move(right, State) -&gt;      move(getVector(right), State); move(up, State) -&gt;      move(getVector(up), State); move(down, State) -&gt;      move(getVector(down), State); move(Vector, State) -&gt;     {struct, JsonData} = State,      case          proplists:get_value(over, JsonData) or (             proplists:get_value(won, JsonData) and (not proplists:get_value(keepPlaying, JsonData))         )     of         true -&gt; State;         _Else -&gt;             PreparedJsonData = updateBestScore(prepareTiles(JsonData)),              { TraversalsX, TraversalsY } = buildTraversals(Vector),              NewJsonData = process_travesals_y(                 TraversalsY,                 TraversalsX,                 Vector,                 PreparedJsonData             ),              if                 PreparedJsonData =\/= NewJsonData -&gt; %If changed - add new tile                     Grid = proplists:get_value(grid, NewJsonData),                     {struct, UserJsonData} = proplists:get_value(user, NewJsonData),                      NewScore = proplists:get_value(score, NewJsonData),                     Score = proplists:get_value(score, PreparedJsonData),                      case NewScore &gt; Score of true -&gt;                         db:insert(                             proplists:get_value(score, NewJsonData),                             proplists:get_value(id, UserJsonData)                         );                         _Else -&gt; undefined                     end,                      Over = case movesAvailable(Grid) of                         true -&gt; false;                         fale -&gt; true % Game over!                     end,                     Removed = proplists:delete(grid, proplists:delete(over, NewJsonData)),                     {struct,[{ grid, addRandomTile(Grid) }, { over, Over } | Removed ]};                 true -&gt; %return state otherwise                     {struct,PreparedJsonData}             end     end. <\/code><\/pre>\n<p>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0447\u0442\u043e\u0431\u044b \u0443\u0437\u043d\u0430\u0442\u044c, \u0441\u043e\u0432\u0435\u0440\u0448\u0438\u043b\u0441\u044f \u043b\u0438 \u0445\u043e\u0434, \u044f \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u044e \u0441\u0442\u0430\u0440\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0438 \u043d\u043e\u0432\u043e\u0435. \u041d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432\u043d\u0435\u0448\u043d\u044f\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u043a\u0430\u043a \u0432 JS \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435. \u041d\u0435 \u0437\u043d\u0430\u044e, \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442 \u043b\u0438 \u044d\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c. \u0418 \u043f\u043e\u0442\u043e\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0441\u044f \u043b\u0438 \u0441\u0447\u0435\u0442, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c \u043b\u0438\u0448\u043d\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0411\u0414.<br \/>  \u0412\u043e\u043e\u0431\u0449\u0435, \u043f\u0440\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0435, \u0440\u0435\u0434\u043a\u043e \u043a\u043e\u0433\u0434\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e. \u0417\u0434\u0435\u0441\u044c \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435 \u043c\u0435\u043d\u044f \u0441\u043c\u0443\u0449\u0430\u0435\u0442 \u0442\u043e, \u0447\u0442\u043e \u044f \u043f\u0435\u0440\u0435\u0434\u0430\u044e TraversalsY, TraversalsX, Vector \u0432 process_travesals_y, \u0445\u043e\u0442\u044f TraversalsY \u0438 TraversalsX \u0438 \u0442\u0430\u043a \u0437\u0430\u0432\u0438\u0441\u044f\u0442 \u043e\u0442 Vector. \u041d\u043e \u0440\u0435\u0448\u0438\u043b \u043f\u043e\u043a\u0430 \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0442\u0430\u043a.<br \/>  \u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0442\u044c \u043e\u043f\u044b\u0442 \u00abavailableCells\u00bb \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u00abprocess_travesals_y\u00bb \u044f \u0440\u0430\u0441\u043f\u0438\u0441\u0430\u043b \u0431\u043e\u043b\u044c\u0448\u0435, \u043d\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043e\u043d\u0430 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0438\u0434\u0435\u0442 \u043f\u043e X \u0438 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u043f\u043e Y. \u0418 \u0432 \u0438\u0442\u043e\u0433\u0435 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u0435\u043d\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0433\u0440\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u00abmoveTile\u00bb. \u041a\u043e\u0442\u043e\u0440\u0430\u044f, \u0432 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 JS-\u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u0443.<\/p>\n<pre><code class=\"erlang\">moveTile(Cell, Vector, JsonData) -&gt;      Grid = proplists:get_value(grid, JsonData),     Tile = grid:cellContent(Cell, Grid),      case Tile =:= null of         true -&gt; JsonData;         false -&gt;             { Farthest, Next } = findFarthestPosition(Cell, Vector, Grid),              {struct, CurrJsonData} = Tile,             CurrValue = proplists:get_value(value, CurrJsonData),              NextTile = if                 Next =:= null -&gt; null;                 true -&gt;                     grid:cellContent(Next, Grid)             end,              {NextValue, NextMerged} = if                 NextTile =:= null -&gt; {null, null};                 true -&gt;                     NextJsonData = element(2, NextTile),                     {proplists:get_value(value, NextJsonData), proplists:get_value(mergedFrom, NextJsonData)}             end,              if  CurrValue =:= NextValue,                 NextMerged =:= null                 -&gt;                     MergedValue = CurrValue * 2,                     Merged = {                         struct,                         [                             {value, MergedValue},                             {mergedFrom, [Tile,NextTile]},                             {previousPosition, null}                         ]                     },                     NewGrid = grid:insertTile(Next, Merged, grid:removeTile(Cell, Grid)),                      % Update the score                     Score = proplists:get_value(score, JsonData) + MergedValue,                      % The mighty 2048 tile                     Won = if                         MergedValue =:= 2048 -&gt; true;                         true -&gt; false                     end,                      Removed = proplists:delete(score, proplists:delete(won, proplists:delete(grid, JsonData))),                      [                         {grid,NewGrid},                         {won,Won},                         {score,Score} |                         Removed                     ];                 true -&gt;                     [                         {                             grid,                             grid:moveTile(Cell, Farthest, proplists:get_value(grid, JsonData))                         }                         | proplists:delete(grid, JsonData)                     ]             end     end. <\/code><\/pre>\n<p>  \u041d\u0430 \u044d\u0442\u043e\u043c, \u0434\u0443\u043c\u0430\u044e, \u0440\u0430\u0441\u0441\u043a\u0430\u0437 \u043e\u0431 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 websocket \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u043e\u043c Erlang \u0437\u0430\u043a\u043e\u043d\u0447\u0435\u043d. \u0421 \u0443\u0434\u043e\u0432\u043e\u043b\u044c\u0441\u0442\u0432\u0438\u0435\u043c \u043e\u0442\u0432\u0435\u0447\u0443 \u043d\u0430 \u0432\u0441\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u044b.    \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/216517\/\"> http:\/\/habrahabr.ru\/post\/216517\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t<img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/371\/aaa\/1cf\/371aaa1cf74630ed981f46c07cf49682.png\" alt=\"image\" align=\"left\"\/>\u041d\u0430\u0432\u0435\u0440\u043d\u043e\u0435 \u043d\u0430 \u043d\u0435\u0434\u0435\u043b\u044e \u0438\u0433\u0440\u044b 2048 \u043d\u0430 \u0445\u0430\u0431\u0440\u0435 \u0443\u0436\u0435 \u043d\u0435 \u0443\u0441\u043f\u0435\u0432\u0430\u044e, \u043d\u043e \u0441\u0442\u0430\u0442\u044c\u044f \u043d\u0435 \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u043e \u0438\u0433\u0440\u0435 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e websocket \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u043d\u0430 Erlang. \u041d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f \u043f\u0440\u0435\u0434\u044b\u0441\u0442\u043e\u0440\u0438\u044f. \u041a\u043e\u0433\u0434\u0430 \u043d\u0430\u0447\u0430\u043b \u0438\u0433\u0440\u0430\u0442\u044c \u0432 2048, \u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0435 \u043c\u043e\u0433 \u043f\u0440\u0435\u043a\u0440\u0430\u0442\u0438\u0442\u044c. \u0412 \u0443\u0449\u0435\u0440\u0431 \u0440\u0430\u0431\u043e\u0442\u0435 \u0438 \u0441\u0435\u043c\u044c\u0435. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438\u043d\u044f\u043b \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u0447\u0442\u043e \u0438\u0433\u0440\u0430\u0442\u044c \u0437\u0430 \u043c\u0435\u043d\u044f \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u043e\u0442. \u041d\u043e \u0437\u0430\u0433\u0432\u043e\u0437\u0434\u043a\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0438\u0433\u0440\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0430\u044f, \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u043d\u0435 \u0432\u0435\u0434\u0435\u0442\u0441\u044f \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0439\u0442\u0438\u043d\u0433 \u0438 \u043d\u0435 \u0442\u0430\u043a \u0443\u0434\u043e\u0431\u043d\u043e \u0438\u0433\u0440\u0430\u0442\u044c \u0431\u0435\u0437 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0438 \u0440\u0435\u0448\u0438\u043b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c, \u0433\u0434\u0435 \u0431\u044b\u043b \u0431\u044b \u0440\u0435\u0439\u0442\u0438\u043d\u0433. \u0418 \u0433\u0434\u0435 \u043c\u043e\u0433 \u0431\u044b \u0438\u0433\u0440\u0430\u0442\u044c \u043c\u043e\u0439 \u0431\u043e\u0442 \u0431\u0435\u0437 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-216517","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/216517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=216517"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/216517\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=216517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=216517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=216517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}