-
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
Щукина Ирина #22
base: gh-pages
Are you sure you want to change the base?
Щукина Ирина #22
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,35 @@ | ||
/* Стили для пятнашек опишите в этом файле */ | ||
body, html { | ||
height: 100%; | ||
} | ||
|
||
#playground { | ||
padding: 12px 64px; | ||
} | ||
|
||
#win { | ||
margin: 0; | ||
font-size: 50px; | ||
display: inline-block; | ||
text-align: center; | ||
width: 100%; | ||
color: #0B614B; | ||
visibility: hidden; | ||
} | ||
|
||
.button { | ||
background-color: #81F7BE; | ||
color: black; | ||
text-align: center; | ||
display: inline-block; | ||
margin: 2px 2px; | ||
height: 60px; | ||
width: 60px; | ||
font-size: 20px; | ||
cursor: pointer; | ||
|
||
} | ||
|
||
.button:hover { | ||
background-color: #0B614B; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,135 @@ | ||
// Логику пятнашек нужно описать в этом файле | ||
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 randomSelector() { | ||
return Math.random() - 0.5; | ||
} | ||
|
||
function startGame(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){ | ||
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.
|
||
board = []; | ||
var number = []; | ||
for (i = 1; i< n*n; i++){ | ||
number.push(i) | ||
} | ||
number.sort(randomSelector); | ||
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 <n; j++){ | ||
var button = document.createElement('input'); | ||
button.id = i * n +j; | ||
button.type = 'button'; | ||
button.className = 'button'; | ||
button.value = board[i][j]; | ||
if (board[i][j] === -1){ | ||
button.style.visibility = 'hidden'; | ||
} | ||
else { | ||
button.style.visibility = 'visible'; | ||
} | ||
buttons[i][j] = button; | ||
playGround.appendChild(button); | ||
if ((i*4 + j + 1) % n === 0) { | ||
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. CodeStyle немного страдает |
||
playGround.appendChild(document.createElement('br')); | ||
} | ||
} | ||
} | ||
|
||
if (playGround.addEventListener) { | ||
playGround.addEventListener('click', swap); | ||
} else { | ||
playGround.attachEvent('onclick', swap); | ||
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. 👍 |
||
} | ||
} | ||
|
||
|
||
|
||
document.body.onload = startGame(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,105 @@ | ||
describe('Tag', function() { | ||
it('should ...'); | ||
}); | ||
var expect = chai.expect; | ||
|
||
|
||
function clearPlayground() { | ||
var playground = document.getElementById('playground'); | ||
playground.innerHTML = ''; | ||
} | ||
|
||
describe('Game', function() { | ||
var clickEvent = document.createEvent('Events'); | ||
clickEvent.initEvent('click', true, false); | ||
var cells = []; | ||
var hiddenCell = null; | ||
var cellsWithoutEmpty = null; | ||
clearPlayground(); | ||
beforeEach(function () { | ||
startGame(); | ||
cells = [].slice.call(document.getElementsByClassName('button')); | ||
hiddenCell = cells.filter(function(item) { | ||
return parseInt(item.value) === -1; | ||
})[0]; | ||
cellsWithoutEmpty = cells.filter(function(item) { | ||
return item.value !== -1; | ||
}); | ||
}); | ||
afterEach(function () { | ||
clearPlayground(); | ||
}); | ||
after(function () { | ||
startGame(); | ||
}); | ||
|
||
it('should 1 hidden and 15 visible cells', function() { | ||
cells = [].slice.call(document.getElementsByClassName('button')); | ||
chai.expect(cells.filter(function(item) { | ||
return item.style.visibility === 'hidden'; | ||
})).to.have.length(1); | ||
chai.expect(cells.filter(function(item) { | ||
return item.style.visibility === 'visible'; | ||
})).to.have.length(15); | ||
}); | ||
|
||
it('should move the cell to a neighboring empty cell', function() { | ||
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. Здесь хорошо бы 4 теста, т.к бывают клетки снизу, сверху, справа и слева. |
||
var neighboursOfHiddenCell = cells.filter(function(item) { | ||
return getEmptyCellId(item.id, cells) !== -1; | ||
}); | ||
|
||
var hiddenCellValue = hiddenCell.value; | ||
neighboursOfHiddenCell.forEach(function (neighbour) { | ||
var neighbourValue = neighbour.value; | ||
|
||
neighbour.dispatchEvent(clickEvent); | ||
expect(neighbour.value).to.eql(hiddenCellValue); | ||
expect(hiddenCell.style.visibility).to.equal('visible'); | ||
expect(hiddenCell.value).to.eql(neighbourValue); | ||
expect(neighbour.style.visibility).to.equal('hidden'); | ||
hiddenCell.dispatchEvent(clickEvent); | ||
}); | ||
}); | ||
|
||
|
||
|
||
it('shouldn\'t move empty cell by click', function() { | ||
var hiddenCell = cells.filter(function(item) { | ||
return parseInt(item.value) === -1; | ||
})[0]; | ||
var expectedHiddenCellValue = hiddenCell.value; | ||
|
||
hiddenCell.dispatchEvent(clickEvent); | ||
expect(hiddenCell.style.visibility).to.equal('hidden'); | ||
expect(hiddenCell.value).to.eql(expectedHiddenCellValue); | ||
}); | ||
|
||
it('shouldn\'t move cells, which have no empty neighboring cells', function() { | ||
var cellsWithoutEmptyNeighbour = cells.filter(function(item) { | ||
return getEmptyCellId(item.id, cells) === -1 && parseInt(item.value) !== -1; | ||
}); | ||
var HiddenCellValue = hiddenCell.value; | ||
|
||
cellsWithoutEmptyNeighbour.forEach(function(cell) { | ||
cell.dispatchEvent(clickEvent); | ||
var CellValue = cell.value; | ||
expect(cell.value).to.eql(CellValue); | ||
expect(hiddenCell.value).to.eql(HiddenCellValue); | ||
}); | ||
|
||
}); | ||
|
||
it('should show win message', function() { | ||
var board = [ | ||
[1, 2, 3, 4], | ||
[5, 6, 7, 8], | ||
[9, 10, 11, -1], | ||
[13, 14, 15, 12] | ||
]; | ||
clearPlayground(); | ||
startGame(board); | ||
cells = [].slice.call(document.getElementsByClassName('button')); | ||
var winMessage = document.getElementById('win'); | ||
expect(winMessage.style.visibility).to.equal('hidden'); | ||
cells[cells.length - 1].dispatchEvent(clickEvent); | ||
expect(winMessage.style.visibility).to.equal('visible'); | ||
winMessage.style.visibility = 'hidden'; | ||
}); | ||
}); |
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.
Почему не объединила в одном if?