document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * MAZE
  3.  */
  4. var MAZE = function(cols, rows){
  5.     this.cfg = {
  6.         width: 10,
  7.         height: 10,
  8.         wMargin: 5,
  9.         hMargin: 5,
  10.         maxLoop: 15000,
  11.         cols: 10,
  12.         rows: 10
  13.     };
  14.     this.cluster = {};
  15.     this.build(cols, rows);
  16. };
  17.  
  18. MAZE.prototype.build = function(cols, rows){
  19.     var _arr = [];
  20.     var _col = [];
  21.     var _row = [];
  22.     var _num = 0;
  23.     this.cfg.cols = cols;
  24.     this.cfg.rows = rows;
  25.     for(var i=0;i<rows;i++){
  26.         _arr[i] = [];
  27.         for(var j=0;j<cols;j++){
  28.             _arr[i][j] = _num++;
  29.         }
  30.     }
  31.     for(var i=0;i<rows;i++){
  32.         _col[i] = [];
  33.         for(var j=0;j<=cols;j++){
  34.             _col[i][j] = (j==0 || j==cols) ? 3 : 1 ;
  35.         }
  36.     }
  37.     for(var i=0;i<=rows;i++){
  38.         _row[i] = [];
  39.         for(var j=0;j<cols;j++){
  40.             _row[i][j] = (i==0 || i==rows) ? 3 : 1 ;
  41.         }
  42.     }
  43.     this.cluster =  {
  44.         el: _arr,
  45.         col: _col,
  46.         row: _row
  47.     };
  48.     this._breakWark();
  49. };
  50.  
  51. MAZE.prototype._fillNumber = function(tg, num){
  52.     var c = this.cluster.el;
  53.     for (var i = 0, max = c.length; i < max; i++) {
  54.         for (var j = 0, max2 = c[i].length; j < max2; j++) {
  55.             if(c[i][j] == tg){
  56.                 c[i][j] = num;
  57.             }
  58.         }
  59.     }
  60. };
  61. MAZE.prototype._isAllFill = function(){
  62.     var check = null;
  63.     var c = this.cluster.el;
  64.     for (var i = 0, max = c.length; i < max; i++) {
  65.         for (var j = 0, max2 = c[i].length; j < max2; j++) {
  66.             if(check == null){
  67.                 check = c[i][j];
  68.             }else if(check != c[i][j]){
  69.                 return false;
  70.             }
  71.         }
  72.     }
  73.     return true;
  74. };
  75. MAZE.prototype._borderBreak = function(){
  76.     var c = this.cluster;
  77.     var cfg = this.cfg;
  78.     var type = (Math.round(Math.random()) == 0) ? \'col\' : \'row\' ;
  79.     switch(type){
  80.         case \'row\':
  81.             var colNum = Math.floor(Math.random()*cfg.cols);
  82.             var rowNum = Math.floor(Math.random()*cfg.rows);
  83.             if(rowNum <= 0) rowNum = 1;
  84.             if(c[type][rowNum][colNum] == 1){
  85.                 var num1 = c.el[rowNum-1][colNum];
  86.                 var num2 = c.el[rowNum][colNum];
  87.                 if(num1 == num2){
  88.                     c[type][rowNum][colNum] = 2;
  89.                 }else{
  90.  
  91.                     this._fillNumber(Math.max(num1, num2), Math.min(num1, num2));
  92.                     c[type][rowNum][colNum] = 0;
  93.                 }
  94.             }
  95.             break;
  96.         case \'col\':
  97.             var colNum = Math.floor(Math.random()*cfg.cols);
  98.             var rowNum = Math.floor(Math.random()*cfg.rows);
  99.             if(colNum <= 0) colNum = 1;
  100.             if(c[type][rowNum][colNum] == 1){
  101.                 var num1 = c.el[rowNum][colNum-1];
  102.                 var num2 = c.el[rowNum][colNum];
  103.                 if(num1 == num2){
  104.                     c[type][rowNum][colNum] = 2;
  105.                 }else{
  106.  
  107.                     this._fillNumber(Math.max(num1, num2), Math.min(num1, num2));
  108.                     c[type][rowNum][colNum] = 0;
  109.                 }
  110.             }
  111.             break;
  112.         default:
  113.             break;
  114.     }
  115. };
  116. MAZE.prototype._breakWark = function(){
  117.     var i = 1;
  118.     while(!this._isAllFill() && (i%this.cfg.maxLoop != 0 || window.confirm(\'処理を継続しますか ?\'))){
  119.         this._borderBreak();
  120.         i++;
  121.     }
  122. };
  123. MAZE.prototype.draw = function(id){
  124.     var cfg = this.cfg;
  125.     var w = cfg.width;
  126.     var h = cfg.height;
  127.     var wm = cfg.wMargin;
  128.     var hm = cfg.wMargin;
  129.     var cols = cfg.cols;
  130.     var rows = cfg.rows;
  131.     var tg = (typeof(id)==\'string\') ? $(\'#\'+id) : id ;
  132.     var col = this.cluster.col;
  133.     var row = this.cluster.row;
  134.     var ctx, ww = (w * cols + wm * 2), hh = (h * rows + hm * 2);
  135.  
  136.     if(!tg || !tg.get(0).getContext){
  137.         return false;
  138.     }
  139.     ctx = tg.get(0).getContext("2d");
  140.  
  141.     tg.attr(\'width\', ww);
  142.     tg.attr(\'height\', hh);
  143.  
  144.     col[0][0] = 0;
  145.     col[rows-1][cols] = 0;
  146.  
  147.     ctx.clearRect(0, 0, ww, hh);
  148.     ctx.beginPath();
  149.     for (var i = 0, max = col.length; i < max; i++) {
  150.         for (var j = 0, max2 = col[i].length; j < max2; j++) {
  151.             if(col[i][j] > 0){
  152.                 //tg.drawLine(j*w+wm, i*h+hm, j*w+wm, i*h+h+hm);
  153.                 ctx.moveTo(j*w+wm, i*h+hm);
  154.                 ctx.lineTo(j*w+wm, i*h+h+hm);
  155.             }
  156.         }
  157.     }
  158.  
  159.     for (var i = 0, max = row.length; i < max; i++) {
  160.         for (var j = 0, max2 = row[i].length; j < max2; j++) {
  161.             if(row[i][j] > 0){
  162.                 //tg.drawLine(j*w+wm, i*h+hm, j*w+w+wm, i*h+hm);
  163.                 ctx.moveTo(j*w+wm, i*h+hm);
  164.                 ctx.lineTo(j*w+w+wm, i*h+hm);
  165.             }
  166.         }
  167.     }
  168.     ctx.stroke();
  169.  
  170.     col[0][0] = 3;
  171.     col[rows-1][cols] = 3;
  172. };
  173. MAZE.prototype.setWidth = function(_num){
  174.     this.cfg.width = _num;
  175. };
  176. MAZE.prototype.setHeight = function(_num){
  177.     this.cfg.height = _num;
  178. };
  179. MAZE.prototype.setMarginWidth = function(_num){
  180.     this.cfg.wMargin = _num;
  181. };
  182. MAZE.prototype.setMarginHeight = function(_num){
  183.     this.cfg.hMargin = _num;
  184. };
  185.  
  186. MAZE.prototype.getWidth = function(){
  187.     return this.cfg.width;
  188. };
  189. MAZE.prototype.getHeight = function(){
  190.     return this.cfg.height;
  191. };
  192. MAZE.prototype.getMarginWidth = function(){
  193.     return this.cfg.wMargin;
  194. };
  195. MAZE.prototype.getMarginHeight = function(){
  196.     return this.cfg.hMargin;
  197. };
  198. MAZE.prototype.getCols = function(){
  199.     return this.cfg.cols;
  200. };
  201. MAZE.prototype.getRows = function(){
  202.     return this.cfg.rows;
  203. };
  204.  
  205. MAZE.prototype.isTopWall = function(_x, _y){
  206.     var row = this.cluster.row;
  207.     if(row[_y][_x] > 0){
  208.         return true;
  209.     }
  210.     return false;
  211. };
  212. MAZE.prototype.isRightWall = function(_x, _y){
  213.     var col = this.cluster.col;
  214.     if(col[_y][_x+1] > 0){
  215.         return true;
  216.     }
  217.     return false;
  218. };
  219. MAZE.prototype.isBottomWall = function(_x, _y){
  220.     var row = this.cluster.row;
  221.     if(row[_y+1][_x] > 0){
  222.         return true;
  223.     }
  224.     return false;
  225. };
  226. MAZE.prototype.isLeftWall = function(_x, _y){
  227.     var col = this.cluster.col;
  228.     if(col[_y][_x] > 0){
  229.         return true;
  230.     }
  231.     return false;
  232. };
  233.  
  234.  
  235. /**
  236.  * MAZE_BOT
  237.  */
  238. var MAZE_BOT = function(maze){
  239.     this._maze = maze;
  240.     this._map = null;
  241.     this._pos = {x:0, y:0};
  242.     this._defpos = {x:0, y:0};
  243.     this._isStart = false;
  244.     this._name = \'MAZE_BOT_\' + (new Date()).getTime();
  245.     this._view = null;
  246.     this.duration = 10;
  247.     this.createMap();
  248. };
  249.  
  250. MAZE_BOT.prototype.createMap = function(){
  251.     var _arr = [];
  252.     var point;
  253.     var tgX = this._maze.getRows()-1;
  254.     var tgY = this._maze.getCols()-1;
  255.     var col = this._maze.getCols();
  256.     var row = this._maze.getRows();
  257.     for (var i = 0; i < row; i++) {
  258.         _arr[i] = [];
  259.         for (var j = 0; j < col; j++) {
  260.             point = Math.abs((tgX + tgY) - (i + j));
  261.             _arr[i][j] = point;
  262.         }
  263.     }
  264.     this._map = _arr;
  265. };
  266.  
  267. MAZE_BOT.prototype.createView = function(id){
  268.     if(this._view) return;
  269.     this._view = $("<div />");
  270.     this._view.width(this._maze.getWidth());
  271.     this._view.height(this._maze.getHeight());
  272.     this._view.css(\'position\', \'absolute\');
  273.     this._view.css(\'background-color\', \'#FF0000\');
  274.     var _parent = (typeof(id) == \'string\') ? $(\'#\'+id) : $(id) ;
  275.     _parent.append(this._view);
  276. };
  277.  
  278. MAZE_BOT.prototype.setPos = function(x, y){
  279.     var m = this._maze;
  280.     this._defpos.x = this._pos.x = x;
  281.     this._defpos.y = this._pos.y = y;
  282.     var x = m.getWidth() * x + m.getMarginWidth();
  283.     var y = m.getHeight() * y + m.getMarginHeight();
  284.     this._view.css(\'top\', y + \'px\');
  285.     this._view.css(\'left\', x + \'px\');
  286. };
  287. MAZE_BOT.prototype.moveTo = function(x, y){
  288.     var m = this._maze;
  289.     var x = m.getWidth() * x + m.getMarginWidth();
  290.     var y = m.getHeight() * y + m.getMarginHeight();
  291.     this._view.animate({
  292.         top: y + \'px\',
  293.         left: x + \'px\'
  294.     },
  295.     {
  296.         duration: this.duration,
  297.         complete: (function(o){
  298.             var obj = o;
  299.             return function(){
  300.                 obj.cycle();
  301.             };
  302.         })(this)
  303.     });
  304. };
  305. MAZE_BOT.prototype.reNumberMap = function(){
  306.     var x = this._pos.x;
  307.     var y = this._pos.y;
  308.     var maze = this._maze;
  309.     var map = this._map;
  310.     var max = maze.getCols() * maze.getRows();
  311.  
  312.     if(map[y][x] == 0) return false;
  313.  
  314.     var pointSum = 0;
  315.     var spaceCount = 0;
  316.     if(!(maze.isTopWall(x, y) || map[y-1][x] >= max)){
  317.         pointSum += map[y-1][x];
  318.         spaceCount++;
  319.     }
  320.     if(!(maze.isRightWall(x, y) || map[y][x+1] >= max)){
  321.         pointSum += map[y][x+1];
  322.         spaceCount++;
  323.     }
  324.     if(!(maze.isBottomWall(x, y) || map[y+1][x] >= max)){
  325.         pointSum += map[y+1][x];
  326.         spaceCount++;
  327.     }
  328.     if(!(maze.isLeftWall(x, y) || map[y][x-1] >= max)){
  329.         pointSum += map[y][x-1];
  330.         spaceCount++;
  331.     }
  332.  
  333.     if(spaceCount > 1){
  334.         map[y][x] = Math.min(
  335.             Math.floor(pointSum / spaceCount) + 1,
  336.             max
  337.         );
  338.     }else{
  339.         map[y][x] = max;
  340.     }
  341.  
  342.     return true;
  343. };
  344. MAZE_BOT.prototype.nextPos = function(){
  345.     var x = this._pos.x;
  346.     var y = this._pos.y;
  347.     var maze = this._maze;
  348.     var map = this._map;
  349.     var max = maze.getCols() * maze.getRows();
  350.     var min = maze.getCols() * maze.getRows();
  351.     var obj = {x:x, y:y};
  352.  
  353.     if(!maze.isTopWall(x, y) && map[y-1][x] < max){
  354.         if(map[y-1][x] < min){
  355.             min = map[y-1][x];
  356.             obj = {x:x, y:y-1};
  357.         }
  358.     }
  359.     if(!maze.isRightWall(x, y) && map[y][x+1] < max){
  360.         if(map[y][x+1] < min){
  361.             min = map[y][x+1];
  362.             obj = {x:x+1, y:y};
  363.         }
  364.     }
  365.     if(!maze.isBottomWall(x, y) && map[y+1][x] < max){
  366.         if(map[y+1][x] < min){
  367.             min = map[y+1][x];
  368.             obj = {x:x, y:y+1};
  369.         }
  370.     }
  371.     if(!maze.isLeftWall(x, y) && map[y][x-1] < max){
  372.         if(map[y][x-1] < min){
  373.             min = map[y][x-1];
  374.             obj = {x:x-1, y:y};
  375.         }
  376.     }
  377.     this._pos = obj;
  378. };
  379.  
  380. MAZE_BOT.prototype.cycle = function(){
  381.     if(this._isStart && this.reNumberMap()){
  382.         this.nextPos();
  383.         this.moveTo(this._pos.x, this._pos.y);
  384.     }else if(this._isStart){
  385.         this.kill();
  386.     }
  387. };
  388.  
  389. MAZE_BOT.prototype.start = function(){
  390.     this._isStart = true;
  391.     this.createMap();
  392.     this.setPos(this._defpos.x, this._defpos.y);
  393.     this.cycle();
  394. };
  395. MAZE_BOT.prototype.stop = function(){
  396.     this._isStart = false;
  397. };
  398. MAZE_BOT.prototype.kill = function(){
  399.     this.stop();
  400.     this._view.animate({
  401.         opacity: \'0\'
  402.     }, 500);
  403. };
  404. MAZE_BOT.prototype.restart = function(){
  405.     this.stop();
  406.     this.start();
  407. };
  408. MAZE_BOT.prototype.isStart = function(){
  409.     return this._isStart;
  410. };
  411. MAZE_BOT.prototype.name = function(){
  412.     return this._name;
  413. };
  414.  
  415.  
  416. /**
  417.  * Custom
  418.  */
  419. var mazeObj = null;
  420. var mazeBots = [];
  421. var isCtrl = false;
  422. function createMaze(){
  423.     if (isCtrl) {
  424.         var w = $(\'#mazeWidth\').val();
  425.         var h = $(\'#mazeHeight\').val();
  426.         for (var i in mazeBots) {
  427.             mazeBots[i].kill();
  428.         }
  429.         mazeObj = new MAZE(w, h);
  430.         $(\'#myCanvas\').empty();
  431.         mazeObj.draw(\'myCanvas\');
  432.     }
  433. };
  434. function createBot(){
  435.     if (isCtrl) {
  436.         var s = $(\'#botSpeed\').val();
  437.         var mb = new MAZE_BOT(mazeObj);
  438.         mb.createView(\'botStage\');
  439.         mb.setPos(0, 0);
  440.         mb.duration = Number(s);
  441.         mb.start();
  442.         mazeBots.push(mb);
  443.     }
  444. };
  445. $(function(){
  446.     isCtrl = true;
  447.     createMaze();
  448. });
');