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

ФТ203 Образцов Владимир Султанов Вячеслав #184

Closed
wants to merge 2 commits into from
Closed
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
62 changes: 56 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
const CROSS = 'X';
const ZERO = 'O';
const EMPTY = ' ';
const xo = [ZERO, CROSS];
const emptyMap = [[EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY]];
let curr_step;
let map;
let isEnd;

const container = document.getElementById('fieldWrapper');

startGame();
addResetListener();

function startGame () {
isEnd = false;
renderGrid(3);
curr_step = 1;
map = emptyMap;
}

function renderGrid (dimension) {
container.innerHTML = '';

for (let i = 0; i < dimension; i++) {
const row = document.createElement('tr');
for (let j = 0; j < dimension; j++) {
Expand All @@ -22,18 +29,60 @@ function renderGrid (dimension) {
cell.addEventListener('click', () => cellClickHandler(i, j));
row.appendChild(cell);
}

container.appendChild(row);
}
}

function cellClickHandler (row, col) {
// Пиши код тут
if (map[row][col] !== EMPTY || isEnd)
return;
console.log(`Clicked on cell: ${row}, ${col}`);

if (curr_step >= 3 * 3) {
alert(`Победила дружба`);
isEnd = true;
return;
}

const curr_symbol = xo[curr_step % 2];
renderSymbolInCell(curr_symbol, row, col);
map[row][col] = curr_symbol;
curr_step++;
checkWinner(curr_symbol);
}

function checkDiagonalWinner(diagonal) {
let diag = [];

for (let i = 0; i < map.length; i++) {
diag[i] = diagonal ? map[i][i] : map[map.length - i - 1][i];
}
return diag.every(element => element === diag[0] && element !== EMPTY);
}

function checkWinner(cell){
const isWinRow = map.some(row => row.every(element => element === row[0] && element !== EMPTY));
let isWinCol = false;
for (let j = 0; j < map.length; j++) {
let col = [];
for (let i = 0; i < map.length; i++) {
col[i] = [map[i][j], [i, j]];
}
isWinCol = col.every(element => element === col[0] && element !== EMPTY);
if (isWinCol) {
break;
}

}

const isWinDiag1 = checkDiagonalWinner(false);
const isWinDiag2 = checkDiagonalWinner(true);

/* Пользоваться методом для размещения символа в клетке так:
renderSymbolInCell(ZERO, row, col);
*/
if (isWinRow || isWinCol || isWinDiag1 || isWinDiag2){
alert(`Победил ${cell}!`)
isEnd = true;
}
}

function renderSymbolInCell (symbol, row, col, color = '#333') {
Expand All @@ -54,7 +103,8 @@ function addResetListener () {
}

function resetClickHandler () {
console.log('reset!');
startGame();
console.log(`reset!, ${curr_step}`);
}


Expand Down