-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Брагин Егор #18
base: gh-pages
Are you sure you want to change the base?
Брагин Егор #18
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,48 @@ | ||
/* Стили для пятнашек опишите в этом файле */ | ||
.game-board | ||
{ | ||
border: solid 1px; | ||
width: 400px; | ||
height: 400px; | ||
-moz-user-select: none; | ||
-webkit-user-select: none; | ||
-ms-user-select: none; | ||
user-select: none; | ||
} | ||
|
||
.game-cell | ||
{ | ||
border: solid 1px; | ||
width: 100px; | ||
height: 100px; | ||
text-align: center; | ||
font-size: 24px; | ||
cursor: pointer; | ||
background-color: #000; | ||
color: #fff; | ||
display: inline-block; | ||
box-sizing: border-box; | ||
line-height: 100px; | ||
} | ||
|
||
.game-cell.empty | ||
{ | ||
background: #fff; | ||
color: #fff; | ||
} | ||
|
||
.game-victory | ||
{ | ||
display: none; | ||
position: fixed; | ||
background: rgba(0, 0, 0, .6); | ||
z-index: 100; | ||
width: 100%; | ||
height: 100%; | ||
} | ||
|
||
.game-victory_content | ||
{ | ||
color: #fff; | ||
font-size: 40px; | ||
text-align: center; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,83 @@ | ||
// Логику пятнашек нужно описать в этом файле | ||
var gameBoard; | ||
var gameBoardElement = document.getElementById('game-board'); | ||
|
||
gameBoardElement.addEventListener('click', function (event) { | ||
move(event.target.id); | ||
if (checkWin()) { | ||
document.querySelector('.game-victory').style.display = 'block'; | ||
} | ||
}); | ||
|
||
startNewGame(); | ||
|
||
function startNewGame() { | ||
document.querySelector('.game-victory').style.display = 'none'; | ||
gameBoard = createBoard(); | ||
printBoard(); | ||
} | ||
|
||
function createBoard() { | ||
var gameBoard = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].sort(function () { | ||
return Math.random() - 0.5; | ||
}); | ||
|
||
gameBoard.unshift(0); | ||
|
||
return gameBoard; | ||
} | ||
|
||
function printBoard() { | ||
while (gameBoardElement.firstChild) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. можно, например, сделать gameBoardElement.innerHtml = '' |
||
gameBoardElement.removeChild(gameBoardElement.firstChild); | ||
} | ||
for (var i = 0; i < 16; i++) { | ||
var gameCell = document.createElement('div'); | ||
gameCell.id = i; | ||
gameCell.innerHTML = gameBoard[i]; | ||
gameCell.className = gameBoard[i] === 0 ? 'game-cell empty' : 'game-cell'; | ||
gameBoardElement.appendChild(gameCell); | ||
} | ||
} | ||
|
||
function move(id) { | ||
id = parseInt(id); | ||
if (gameBoard[id] === 0) { | ||
return; | ||
} | ||
|
||
if (gameBoard[id + 4] === 0) { | ||
gameBoard[id + 4] = gameBoard[id]; | ||
gameBoard[id] = 0; | ||
printBoard(); | ||
|
||
return; | ||
} | ||
|
||
if (gameBoard[id - 4] === 0) { | ||
gameBoard[id - 4] = gameBoard[id]; | ||
gameBoard[id] = 0; | ||
printBoard(); | ||
|
||
return; | ||
} | ||
|
||
if (gameBoard[id - 1] === 0 && Math.floor(id / 4) === Math.floor((id - 1) / 4)) { | ||
gameBoard[id - 1] = gameBoard[id]; | ||
gameBoard[id] = 0; | ||
printBoard(); | ||
|
||
return; | ||
} | ||
|
||
if (gameBoard[id + 1] === 0 && Math.floor(id / 4) === Math.floor((id + 1) / 4)) { | ||
gameBoard[id + 1] = gameBoard[id]; | ||
gameBoard[id] = 0; | ||
printBoard(); | ||
} | ||
} | ||
|
||
function checkWin() { | ||
return !gameBoard.some(function (item, index) { | ||
return item !== 0 && (item - 1) !== index; | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,61 @@ | ||
describe('Tag', function() { | ||
it('should ...'); | ||
after(function() { | ||
gameBoard = createBoard(); | ||
printBoard(); | ||
}); | ||
|
||
it('should initialize a game field', function () { | ||
var gameCells = document.getElementsByClassName('game-cell'); | ||
chai.assert.equal(gameCells.length, 16); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. можно еще проверить, что числа разные и от 1 до 15 |
||
}); | ||
|
||
it('should be an empty cell', function () { | ||
var emptyCells = document.getElementsByClassName('empty'); | ||
chai.assert.equal(emptyCells.length, 1); | ||
}); | ||
|
||
it('should move cell up', function () { | ||
var gameBoardCopy = gameBoard.slice(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лезть в переменную, используемую в код плохо (если хотим поменять название, то надо менять и в тестах) |
||
gameBoardCopy[0] = gameBoardCopy[4]; | ||
gameBoardCopy[4] = 0; | ||
var cell = document.getElementById('4'); | ||
cell.click(); | ||
chai.assert.deepEqual(gameBoardCopy, gameBoard); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. мы не это должны проверять, не то, что в памяти поменялось |
||
}); | ||
|
||
it('should move cell down', function () { | ||
var gameBoardCopy = gameBoard.slice(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. у тебя тесты сейчас зависят друг от друга, а надо чтобы были независимы, чтобы можно было отдельно запустить любой |
||
gameBoardCopy[4] = gameBoardCopy[0]; | ||
gameBoardCopy[0] = 0; | ||
var cell = document.getElementById('0'); | ||
cell.click(); | ||
chai.assert.deepEqual(gameBoardCopy, gameBoard); | ||
}); | ||
|
||
it('should move cell left', function () { | ||
var gameBoardCopy = gameBoard.slice(); | ||
gameBoardCopy[0] = gameBoardCopy[1]; | ||
gameBoardCopy[1] = 0; | ||
var cell = document.getElementById('1'); | ||
cell.click(); | ||
chai.assert.deepEqual(gameBoardCopy, gameBoard); | ||
}); | ||
|
||
it('should move cell right', function () { | ||
var gameBoardCopy = gameBoard.slice(); | ||
gameBoardCopy[1] = gameBoardCopy[0]; | ||
gameBoardCopy[0] = 0; | ||
var cell = document.getElementById('0'); | ||
cell.click(); | ||
chai.assert.deepEqual(gameBoardCopy, gameBoard); | ||
}); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. еще нужна проверка на то, что если мы кликнем по ячейке далеко от пустой, ничего не произойдет |
||
it('should take message if player win', function () { | ||
gameBoard = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0, 15]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. опять лезем во внутренности кода игры, не надо так делать, надо инициализировать поле заранее подготовленными числами |
||
var cell = document.getElementById('15'); | ||
cell.click(); | ||
var modal = document.querySelector('.game-victory'); | ||
chai.assert.equal(modal.style.display, 'block'); | ||
modal.style.display = 'none'; | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
лучше сделать через навешивание класса, у которого будет соотвествующее свойство