-
Notifications
You must be signed in to change notification settings - Fork 25
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
Ильиных Анна #16
Open
AnnaIlinykh
wants to merge
1
commit into
urfu-2017:master
Choose a base branch
from
AnnaIlinykh:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Ильиных Анна #16
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,85 @@ | ||
// Реализуй логику выбора победителя в этом файле | ||
const playingField = document.getElementById('playing-field'); | ||
let currentPlayer = 'x'; | ||
let canChangePlayer = true; | ||
/* Инпуты игрового поля */ | ||
let playingFieldInputs = new Array(9); | ||
for (var x = 0; x < 3; x++) { | ||
for (var y = 0; y < 3; y++) { | ||
playingFieldInputs[x * 3 + y] = document.getElementById(x + "-" + y) | ||
} | ||
} | ||
|
||
function getCellValue(row, column) { | ||
const input = document.getElementById(row + '-' + column); | ||
return input.value; | ||
} | ||
|
||
/* Очищаем игровое поле, устанавливаем текущего игрока и очищаем запись о победители*/ | ||
function startNewGame(fieldInputs) { | ||
playingFieldInputs.forEach(input => { | ||
input.removeAttribute('value'); | ||
}); | ||
currentPlayer = 'x' | ||
playingField.setAttribute('class', 'current_' + currentPlayer); | ||
document.getElementById("winmsg").innerText = "" | ||
canChangePlayer = true | ||
} | ||
|
||
// Логика определения победителя!! | ||
/* Возвращает символ победителя либо пустую строку либо ничью */ | ||
function getWinMsg() { | ||
let winMsg = undefined; | ||
|
||
/* Возвращает элемент, содержащийся во всех ячейках, иначе пустую строку*/ | ||
function getSomeValue(valuesToCheck) { | ||
const checkedValue = valuesToCheck[0]; | ||
if (valuesToCheck.every(valueToCheck => valueToCheck == checkedValue)){ | ||
return checkedValue; | ||
} | ||
else{ | ||
return ""; | ||
} | ||
} | ||
// Текущие состояние доски | ||
const gameValues = playingFieldInputs.map(inp => inp.value); | ||
// проверка горизонтальных линий | ||
winMsg = getSomeValue(gameValues.slice(0, 3)) + getSomeValue(gameValues.slice(3, 6)) + getSomeValue(gameValues.slice(6, 9)) | ||
// проверка вертикальных линий | ||
+ getSomeValue([gameValues[0], gameValues[3], gameValues[6]]) | ||
+ getSomeValue([gameValues[1], gameValues[4], gameValues[7]]) | ||
+ getSomeValue([gameValues[2], gameValues[5], gameValues[8]]) | ||
// проверка диагоналей | ||
+ getSomeValue([gameValues[0], gameValues[4], gameValues[8]]) | ||
+ getSomeValue([gameValues[2], gameValues[4], gameValues[6]]) | ||
|
||
// если победитель не определён и все ячейки заполнены | ||
if (winMsg.length == 0 && !gameValues.some(value => value == "")){ | ||
winMsg = "ничья" | ||
} | ||
return winMsg; | ||
} | ||
|
||
playingField.addEventListener('click', event => { | ||
if(!canChangePlayer){ | ||
return; | ||
} | ||
canChangePlayer = false; | ||
|
||
const cell = event.target; | ||
const hiddenInput = cell.getElementsByTagName('input')[0]; | ||
if (!hiddenInput.value) { | ||
hiddenInput.setAttribute('value', currentPlayer); | ||
currentPlayer = currentPlayer === 'x' ? 'o' : 'x'; | ||
playingField.setAttribute('class', 'current_' + currentPlayer); | ||
} | ||
|
||
canChangePlayer = true; | ||
|
||
const winMsg = getWinMsg() | ||
if (winMsg.length > 0) { | ||
const winMsgDiv = document.getElementById("winmsg") | ||
winMsgDiv.innerText = winMsg | ||
canChangePlayer = false | ||
} | ||
}) | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,80 @@ | ||
describe('cross-zero', () => { | ||
// Этот тест можно уалить, он нужен для проверки сборки | ||
it('should sum digits', () => chai.assert(1 + 1, 2)); | ||
|
||
function clickCell(id) { | ||
document.getElementById(id).parentNode.click(); | ||
} | ||
function getWinnerMsg() { | ||
return document.getElementById("winmsg").innerText | ||
} | ||
function startNewGameByButtonClicking() { | ||
return document.getElementById("startGameButton").click() | ||
} | ||
// две линии по горизонтале, x просто первее закончит | ||
it('should win x - horizontal', () => { | ||
startNewGameByButtonClicking() | ||
clickCell("0-0"); | ||
clickCell("1-0"); | ||
clickCell("0-1"); | ||
clickCell("1-1"); | ||
clickCell("0-2"); | ||
clickCell("1-2"); | ||
chai.assert.equal(getWinnerMsg(), 'x'); | ||
}); | ||
// две линии по горизонтале, x на 2ом шаге ставит на другую линию, отдавая победу o | ||
it('should win o horizontal', () => { | ||
startNewGameByButtonClicking() | ||
clickCell("0-0"); | ||
clickCell("1-0"); | ||
clickCell("2-1"); | ||
clickCell("1-1"); | ||
clickCell("0-2"); | ||
clickCell("1-2"); | ||
chai.assert.equal(getWinnerMsg(), 'o'); | ||
}); | ||
|
||
it('should win x vertical', () => { | ||
startNewGameByButtonClicking() | ||
clickCell("0-0"); | ||
clickCell("1-1"); | ||
clickCell("2-0"); | ||
clickCell("0-1"); | ||
clickCell("1-0"); | ||
clickCell("2-2"); | ||
chai.assert.equal(getWinnerMsg(), 'x'); | ||
}); | ||
|
||
it('should win o vertical', () => { | ||
startNewGameByButtonClicking() | ||
clickCell("0-0"); | ||
clickCell("1-0"); | ||
clickCell("2-1"); | ||
clickCell("1-1"); | ||
clickCell("2-2"); | ||
clickCell("1-2"); | ||
chai.assert.equal(getWinnerMsg(), 'o'); | ||
}); | ||
|
||
it('should win x diagonal', () => { | ||
startNewGameByButtonClicking() | ||
clickCell("0-0"); | ||
clickCell("0-1"); | ||
clickCell("1-0"); | ||
clickCell("1-1"); | ||
clickCell("2-0"); | ||
chai.assert.equal(getWinnerMsg(), 'x'); | ||
}); | ||
|
||
it('without winners', () => { | ||
startNewGameByButtonClicking() | ||
clickCell("0-0"); | ||
clickCell("0-1"); | ||
clickCell("0-2"); | ||
clickCell("1-1"); | ||
clickCell("1-0"); | ||
clickCell("2-0"); | ||
clickCell("2-1"); | ||
clickCell("2-2"); | ||
clickCell("1-2"); | ||
chai.assert.equal(getWinnerMsg(), 'ничья'); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.