Skip to content
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

ФТ-201 Солин Коуров #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/tic-tac-toe.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 78 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const CROSS = 'X';
const ZERO = 'O';
const EMPTY = ' ';

let winnerExist = false;
const container = document.getElementById('fieldWrapper');
let field = [[EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY],[EMPTY, EMPTY, EMPTY]];
let currentMove = CROSS;

startGame();
addResetListener();
Expand All @@ -28,19 +30,85 @@ function renderGrid (dimension) {

function cellClickHandler (row, col) {
// Пиши код тут
console.log(`Clicked on cell: ${row}, ${col}`);


if (field[row][col] === EMPTY && !winnerExist) {
renderSymbolInCell(currentMove, row, col);
winnerExist = checkWinner(row, col);
currentMove = currentMove === CROSS ? ZERO : CROSS;

console.log(`Clicked on cell: ${row}, ${col}`);
if (!winnerExist) {
let flag = false;
for (let row of field) {
for (let col of row) {
if (col === EMPTY) {
flag = true;
}
}
}
if (!flag) {
alert('Победила дружба')
}
} else {
alert(field[row][col] + ' wins');
}
}
/* Пользоваться методом для размещения символа в клетке так:
renderSymbolInCell(ZERO, row, col);
*/
}

function renderSymbolInCell (symbol, row, col, color = '#333') {
const targetCell = findCell(row, col);


function checkWinner(row, col) {

if (field[row][0] === field[row][1] && field[row][1] === field[row][2]){
for (let i = 0; i < 3; i++)
renderSymbolInCell(field[row][i], row, i, '#FF0000');
return true;
}

if (field[0][col] === field[1][col] && field[1][col] === field[2][col]){
for (let i = 0; i < 3; i++)
renderSymbolInCell(field[i][col], i, col, '#FF0000');
return true;
}
if ((row + col) % 2 === 0){
if ( row === 1 && col === 1 ){
return checkMainDiagonal() || checkSecondDiagonal();
} else {
if (row === col) {
return checkMainDiagonal();
} else {
return checkSecondDiagonal();
}
}
}
return false;
}

function checkMainDiagonal(){
if (field[0][0] === field[1][1] && field[1][1] === field[2][2]) {
for (let i = 0; i < 3; i++) {
renderSymbolInCell(field[i][i], i, i, '#FF0000');
}
}

return (field[0][0] === field[1][1] && field[1][1] === field[2][2]);
}

function checkSecondDiagonal() {
if (field[2][0] === field[1][1] && field[1][1] === field[0][2]) {
for (let i = 0; i < 3; i++) {
renderSymbolInCell(field[i][2-i], i, 2-i, '#FF0000');
}
}
return (field[2][0] === field[1][1] && field[1][1] === field[0][2]);
}
function renderSymbolInCell (symbol, row, col, color = '#333') {
const targetCell = findCell(row, col)
targetCell.textContent = symbol;
targetCell.style.color = color;
field[row][col] = symbol;
}

function findCell (row, col) {
Expand All @@ -54,6 +122,10 @@ function addResetListener () {
}

function resetClickHandler () {
startGame();
field = [[EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY],[EMPTY, EMPTY, EMPTY]];
winnerExist = false;
currentMove = CROSS;
console.log('reset!');
}

Expand Down