From deb98ccc4d4c600c6186329c0094f03416a45415 Mon Sep 17 00:00:00 2001 From: "georgii1605@gmail.com" Date: Tue, 9 May 2017 18:47:22 +0500 Subject: [PATCH 1/2] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=BE?= =?UTF-8?q?=D0=BD=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE=D1=81=D1=82=D1=8C=20+=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.css | 35 ++++++++- index.html | 29 ++++---- index.js | 133 ++++++++++++++++++++++++++++++++++- tests/index-test.js | 168 +++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 347 insertions(+), 18 deletions(-) diff --git a/index.css b/index.css index 30ce6fb..4b665cd 100644 --- a/index.css +++ b/index.css @@ -1 +1,34 @@ -/* Стили для пятнашек опишите в этом файле */ +body, html { + height: 100%; +} + +#playground { + padding: 32px 32px; +} + +#win { + margin: 0; + font-size: 50px; + display: inline-block; + text-align: center; + width: 100%; + color: black; + visibility: hidden; +} + +.button { + background-color: white; + color: black; + text-align: center; + display: inline-block; + margin: 2px 2px; + height: 100px; + width: 100px; + font-size: 20px; + cursor: pointer; + +} + +.button:hover { + background-color: aliceblue; +} \ No newline at end of file diff --git a/index.html b/index.html index 1cca5fe..8f4640e 100644 --- a/index.html +++ b/index.html @@ -9,21 +9,22 @@ - + +
+

ура!

+ +
- -
+ + + - - - + + - - - - - - - + + + + - + \ No newline at end of file diff --git a/index.js b/index.js index 8c84dd3..d48b1b6 100644 --- a/index.js +++ b/index.js @@ -1 +1,132 @@ -// Логику пятнашек нужно описать в этом файле +var n = 4; +function getEmptyCellId(cellId, cells) { + cellId = parseInt(cellId); + if (cellId - 1 >= 0 && cellId % n !== 0) { + if (parseInt(cells[cellId - 1].value) === -1){ + return cellId - 1; + } + } + if (cellId + 1 < 16 && cellId % n !== n-1) { + if (parseInt(cells[cellId + 1].value) === -1){ + return cellId + 1; + } + } + if (cellId - n >= 0) { + if (parseInt(cells[cellId - n].value) === -1){ + return cellId - n; + } + } + if (cellId + n < 16) { + + if (parseInt(cells[cellId + n].value) === -1) { + return cellId + n; + } + } + return -1 +} + +function isWin(cells) { + for (var i = 0; i < cells.length - 1; i++) { + if (parseInt(cells[i].value) !== i + 1) { + return false; + } + } + return true; +} + +function swapCells(nextCell, targetCell) { + nextCell.style.visibility = 'visible'; + nextCell.value = targetCell.value; + targetCell.style.visibility = 'hidden'; + targetCell.value = -1; +} + +function swap(event) { + var e = event || window.event; + + var targetCell = e.target || e.srcElement; + if (targetCell.tagName !== 'INPUT') { + return; + } + var playGround = document.getElementById('playground'); + var cells = playGround.getElementsByClassName('button'); + var winMessage = document.getElementById('win'); + var nextCellId = getEmptyCellId(targetCell.id, cells); + if (nextCellId !== -1) { + swapCells(cells[nextCellId], targetCell); + } + if (isWin(cells)) { + winMessage.style.visibility = 'visible'; + } else { + winMessage.style.visibility = 'hidden'; + } +} + + +function compareRandom() { + return Math.random() - 0.5; +} + +function setupGame(board) { + var playGround = document.getElementById('playground'); + var winMessage = document.getElementById('win'); + winMessage.style.visibility = 'hidden'; + + var buttons = []; + for (var i = 0; i < n; i++){ + buttons[i] = []; + for (var j = 0; j < n; j++) + { + buttons[i][j] = null; + } + } + + if (board === undefined){ + board = []; + var number = []; + for (i = 1; i< n*n; i++){ + number.push(i) + } + number.sort(compareRandom); + number.unshift(-1); + var count = 0; + for (i = 0; i < n; i++) { + board[i] = []; + for(j = 0; j < n; j++) + { + board[i][j] = number[count]; + count++ + } + } + } + + for (i = 0; i < n; i++) { + for (j = 0; j Date: Tue, 9 May 2017 19:33:13 +0500 Subject: [PATCH 2/2] =?UTF-8?q?=D1=80=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 8 +++----- tests/index-test.js | 10 +++++----- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index d48b1b6..11d9556 100644 --- a/index.js +++ b/index.js @@ -62,12 +62,11 @@ function swap(event) { } } - function compareRandom() { return Math.random() - 0.5; } -function setupGame(board) { +function startGame(board) { var playGround = document.getElementById('playground'); var winMessage = document.getElementById('win'); winMessage.style.visibility = 'hidden'; @@ -115,11 +114,10 @@ function setupGame(board) { } buttons[i][j] = button; playGround.appendChild(button); - if ((i*4 +j + 1) % n === 0) { + if ((i*4 + j + 1) % n === 0) { playGround.appendChild(document.createElement('br')); } } - } if (playGround.addEventListener) { @@ -129,4 +127,4 @@ function setupGame(board) { } } -document.body.onload = setupGame(); \ No newline at end of file +document.body.onload = startGame(); \ No newline at end of file diff --git a/tests/index-test.js b/tests/index-test.js index 0de5c4c..89ce87c 100644 --- a/tests/index-test.js +++ b/tests/index-test.js @@ -14,7 +14,7 @@ describe('Tag', function() { var cellsWithoutEmpty = null; clearPlayground(); beforeEach(function () { - setupGame(); + startGame(); cells = [].slice.call(document.getElementsByClassName('button')); hiddenCell = cells.filter(function(item) { return parseInt(item.value) === -1; @@ -27,7 +27,7 @@ describe('Tag', function() { clearPlayground(); }); after(function () { - setupGame(); + startGame(); }); it('should generate 15 visible numbered cells', function() { @@ -66,7 +66,7 @@ describe('Tag', function() { [12, 13, 14, 15] ]; clearPlayground(); - setupGame(board); + startGame(board); cells = [].slice.call(document.getElementsByClassName('button')); var neighboursOfHiddenCell = cells.filter(function(item) { @@ -97,7 +97,7 @@ describe('Tag', function() { [12, 13, 14, -1] ]; clearPlayground(); - setupGame(board); + startGame(board); cells = [].slice.call(document.getElementsByClassName('button')); var neighboursOfHiddenCell = cells.filter(function(item) { @@ -155,7 +155,7 @@ describe('Tag', function() { [13, 14, -1, 15] ]; clearPlayground(); - setupGame(board); + startGame(board); cells = [].slice.call(document.getElementsByClassName('button')); var winMessage = document.getElementById('win');