Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>Russian.MineSweeper</title>
- </head>
- <body>
- <canvas height='320' width='480' id='renderField'>Обновите браузер</canvas>
- <div id="kekOut"></div>
- <script>
- "use strict";
- var renderField = document.getElementById("renderField"),
- ctx = renderField.getContext('2d');
- var Settings = {
- cellSize: 15
- };
- // Общая структура для приложения
- var Common = {
- cellSize: 35,
- // возвращает рандомное [0;1]
- randomCoin: function() {
- return Math.floor(Math.random() * 2)
- },
- // Меняет внутреннее содержимое у html-элемента
- // name: имя html-элемента
- // value: новое значение
- // add: добавлять или переписывать значение [ = false ]
- dataToHTML: function(name, value, add = false) {
- if(add) {
- document.getElementById(name).innerHTML += value;
- }
- else {
- document.getElementById(name).innerHTML = value;
- }
- },
- // возвращает рандомное число
- getRandomInt: function (min, max)
- {
- return Math.floor(Math.random() * (max - min + 1)) + min;
- }
- };
- // Игровое поле
- var Field = function() {
- // клетки поля
- this.cells = [];
- this.minesProcent = function(proc)
- {
- this.updateMinesCount(proc);
- return proc;
- };
- this.minesCount = 0;
- // обновляет кол-во мин на основе процентов
- this.updateMinesCount = function(minesProcent) {
- this.minesCount = Math.floor((this.size.x * this.size.y) * (minesProcent / 100)+0.5);
- };
- // возвращает кол-во клеток в поле
- this.getCellsCount = function() {
- return this.size.x * this.size.y;
- };
- // получает кол-во оставшихся мин (префискное условие!)
- this.getCellsCountToFrom = function(x,y) {
- return this.getCellsCount() - x * this.size.y - y;
- };
- // Размер поля
- // x: координата X
- // y: координата Y
- var Size = function(x, y) {
- // Задает новый размер
- this.Resize = function (x, y) {
- this.x = x;
- this.y = y;
- renderField.width = x*Settings.cellSize;
- renderField.height = y*Settings.cellSize;
- }
- };
- this.size = new Size();
- // Объект координат клетки
- // x: координата X
- // y: координата Y
- var Position = function(x, y) {
- this.x = x; // координата X
- this.y = y; // координата Y
- // Задает новую позицию
- // x: координата X
- // y: координата Y
- this.newPosition = function (x, y) {
- this.x = x;
- this.y = y;
- }
- };
- // Объект клетки поля
- var Cell = function() {
- //позиция клетки
- this.position = null;
- //заминирована ли клетка
- this.mined = false;
- // Возвращает кол-во мин вокруг клетки
- this.getMinesAround = function () {
- return this.position.x + this.position.y;
- }
- // Показывает debug-инфорамцию о клетке
- this.showInformation = function () {
- return("X: " + this.position.x + "<br>"
- + "Y: " + this.position.y + "<br>"
- + "" + this.getMinesAround());
- }
- };
- //установить мины рандомно
- this.setRandomMines = function()
- {
- var minesRandomizer = new Set();
- for(var i=0; i < this.size.x * this.size.y; ++i)
- {
- minesRandomizer.add(i);
- }
- for(var i=0; i<this.minesCount; ++i)
- {
- var randArr = Array.from(minesRandomizer);
- var randCell = randArr[Common.getRandomInt(0,randArr.length-1)]; // поле 100*5, получили 356
- minesRandomizer.delete(randCell);
- var posX = Math.floor(randCell/this.size.y);
- var posY = randCell - posX*this.size.y;
- this.cells[posX][posY].mined = true;
- }
- };
- //инициализация поля
- this.createNullField = function()
- {
- for(var i=0; i<this.size.x; ++i)
- {
- this.cells[i] = [];
- for(var j=0; j<this.size.y; ++j)
- {
- this.cells[i][j] = new Cell();
- this.cells[i][j].position = new Position(i, j);
- }
- }
- };
- //показать дебаг поле
- this.showField = function()
- {
- for(var i=0; i<this.size.x; ++i)
- {
- for(var j=0; j<this.size.y; ++j)
- {
- ctx.fillStyle = this.cells[i][j].mined?"#000":"#fff";
- ctx.fillRect(Settings.cellSize * i, Settings.cellSize * j, Settings.cellSize, Settings.cellSize);
- }
- }
- };
- // Создает поле
- this.createField = function() {
- this.createNullField();
- this.setRandomMines();
- this.showField();
- }
- };
- var now = new Field();
- now.size.Resize(35, 20);
- now.minesProcent(30);
- now.createField();
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement