Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Russian.MineSweeper</title>
  5. </head>
  6. <body>
  7. <canvas height='320' width='480' id='renderField'>Обновите браузер</canvas>
  8.     <div id="kekOut"></div>
  9.  
  10. <script>
  11. "use strict";
  12.  
  13. var renderField = document.getElementById("renderField"),
  14. ctx  = renderField.getContext('2d');
  15.  
  16. var Settings = {
  17. cellSize: 15
  18. };
  19.  
  20. // Общая структура для приложения
  21. var Common = {
  22. cellSize: 35,
  23.     // возвращает рандомное [0;1]
  24.     randomCoin: function() {
  25.         return Math.floor(Math.random() * 2)
  26.     },
  27.    
  28.     // Меняет внутреннее содержимое у html-элемента
  29.     // name: имя html-элемента
  30.     // value: новое значение
  31.     // add: добавлять или переписывать значение [ = false ]
  32.     dataToHTML: function(name, value, add = false) {
  33.    
  34.         if(add) {
  35.             document.getElementById(name).innerHTML += value;
  36.         }
  37.         else {
  38.             document.getElementById(name).innerHTML = value;
  39.         }  
  40.     },
  41.    
  42.     // возвращает рандомное число
  43.     getRandomInt: function (min, max)
  44.     {
  45.       return Math.floor(Math.random() * (max - min + 1)) + min;
  46.     }
  47.  
  48. };
  49.  
  50.  
  51. // Игровое поле
  52. var Field = function() {
  53.  
  54.     // клетки поля
  55.     this.cells = [];
  56.     this.minesProcent = function(proc)
  57.     {
  58.         this.updateMinesCount(proc);
  59.         return proc;
  60.     };
  61.     this.minesCount = 0;
  62.    
  63.     // обновляет кол-во мин на основе процентов
  64.     this.updateMinesCount = function(minesProcent) {
  65.     this.minesCount = Math.floor((this.size.x * this.size.y) * (minesProcent / 100)+0.5);
  66.     };
  67.    
  68.     // возвращает кол-во клеток в поле
  69.     this.getCellsCount = function() {
  70.     return this.size.x * this.size.y;
  71.     };
  72.    
  73.     // получает кол-во оставшихся мин (префискное условие!)
  74.     this.getCellsCountToFrom = function(x,y) {
  75.     return this.getCellsCount() - x * this.size.y - y;
  76.     };
  77.    
  78.     // Размер поля
  79.     // x: координата X
  80.     // y: координата Y
  81.     var Size = function(x, y) {
  82.         // Задает новый размер
  83.         this.Resize = function (x, y) {
  84.             this.x = x;
  85.             this.y = y;
  86.             renderField.width = x*Settings.cellSize;
  87.             renderField.height = y*Settings.cellSize;
  88.         }
  89.        
  90.     };
  91.  
  92.         this.size = new Size();
  93.    
  94.  
  95.    
  96.     // Объект координат клетки
  97.     // x: координата X
  98.     // y: координата Y
  99.     var Position = function(x, y) {
  100.        
  101.         this.x = x; // координата X
  102.         this.y = y; // координата Y
  103.  
  104.         // Задает новую позицию
  105.         // x: координата X
  106.         // y: координата Y
  107.         this.newPosition = function (x, y) {
  108.             this.x = x;
  109.             this.y = y;
  110.         }
  111.        
  112.     };
  113.  
  114.     // Объект клетки поля
  115.     var Cell = function() {
  116.    
  117.         //позиция клетки
  118.         this.position = null;
  119.        
  120.         //заминирована ли клетка
  121.         this.mined = false;
  122.        
  123.         // Возвращает кол-во мин вокруг клетки
  124.         this.getMinesAround = function () {
  125.             return this.position.x + this.position.y;
  126.         }
  127.        
  128.         // Показывает debug-инфорамцию о клетке
  129.         this.showInformation = function () {
  130.             return("X: " + this.position.x + "<br>"
  131.             + "Y: " + this.position.y + "<br>"
  132.             + "" + this.getMinesAround());
  133.         }
  134.        
  135.     };
  136.  
  137.    
  138.    
  139.    
  140.     //установить мины рандомно
  141.     this.setRandomMines = function()
  142.     {
  143.         var minesRandomizer = new Set();                   
  144.         for(var i=0; i < this.size.x * this.size.y; ++i)
  145.         {
  146.             minesRandomizer.add(i);
  147.         }
  148.        
  149.         for(var i=0; i<this.minesCount; ++i)
  150.         {
  151.             var randArr = Array.from(minesRandomizer);
  152.             var randCell = randArr[Common.getRandomInt(0,randArr.length-1)]; // поле 100*5, получили 356
  153.             minesRandomizer.delete(randCell);
  154.            
  155.             var posX = Math.floor(randCell/this.size.y);
  156.             var posY = randCell - posX*this.size.y;
  157.             this.cells[posX][posY].mined = true;
  158.         }
  159.     };
  160.    
  161.    
  162.     //инициализация поля
  163.     this.createNullField = function()
  164.     {
  165.         for(var i=0; i<this.size.x; ++i)
  166.             {  
  167.                 this.cells[i] = [];
  168.                 for(var j=0; j<this.size.y; ++j)
  169.                 {
  170.                     this.cells[i][j] = new Cell();
  171.                     this.cells[i][j].position = new Position(i, j);
  172.                 }
  173.             }
  174.     };
  175.    
  176.     //показать дебаг поле
  177.     this.showField = function()
  178.     {
  179.         for(var i=0; i<this.size.x; ++i)
  180.         {  
  181.             for(var j=0; j<this.size.y; ++j)
  182.             {  
  183.             ctx.fillStyle = this.cells[i][j].mined?"#000":"#fff";
  184.             ctx.fillRect(Settings.cellSize * i, Settings.cellSize * j, Settings.cellSize, Settings.cellSize);              
  185.             }
  186.         }      
  187.     };
  188.    
  189.    
  190.     // Создает поле
  191.     this.createField = function() {
  192.     this.createNullField();
  193.     this.setRandomMines(); 
  194.     this.showField();
  195.     }
  196.        
  197. };
  198.  
  199.  
  200.  
  201. var now = new Field();
  202. now.size.Resize(35, 20);
  203. now.minesProcent(30);
  204. now.createField();
  205.  
  206. </script>
  207. </body>
  208. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement