Skip to content

Тихонов Александр #53

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
140 changes: 133 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,39 @@ const EMPTY = ' ';

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

let currentPlayer;
let currentTurn;
let field;
let maxTurns;
let dimension;
let winCombination;
let isGameOver;

getDimensionCount();
startGame();
addResetListener();

function startGame () {
renderGrid(3);
isGameOver = false;
maxTurns = dimension ** 2;
field = [];
renderGrid(dimension);
currentPlayer = CROSS;
currentTurn = 1;
}

function getDimensionCount() {
dimension = parseInt(prompt('Введите количество измерений', '3'));
}

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

for (let i = 0; i < dimension; i++) {
field[i] = [];
const row = document.createElement('tr');
for (let j = 0; j < dimension; j++) {
field[i][j] = EMPTY;
const cell = document.createElement('td');
cell.textContent = EMPTY;
cell.addEventListener('click', () => cellClickHandler(i, j));
Expand All @@ -27,17 +47,113 @@ function renderGrid (dimension) {
}

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

function cellClick (row, col) {
if (field[row][col] != EMPTY || isGameOver) {
return;
}
renderSymbolInCell(currentPlayer, row, col);
checkWinner(currentPlayer, row, col);
currentTurn++;
currentPlayer = (currentPlayer == CROSS) ? ZERO : CROSS;
}

function checkWinner(player, row, col) {
if (currentTurn < dimension * 2 - 1) return;
if (isWinCombination(rowWinnerCombination(player, row))
|| isWinCombination(colWinnerCombination(player, col))
|| isWinCombination(mainDiagWinnerCombination(player))
|| isWinCombination(secondDiagWinnerCombination(player))) {
alertWinner(player);
}
if (currentTurn == maxTurns){
alert('Победила дружба!');
isGameOver = true;
return;
}
}

function isWinCombination(combination) {
if (combination.length == dimension) {
winCombination = combination;
return true;
}
return false;
}

/* Пользоваться методом для размещения символа в клетке так:
renderSymbolInCell(ZERO, row, col);
*/
function mainDiagWinnerCombination(player) {
combination = [];
for (let i = 0; i < dimension; i++){
if (field[i][i] == player){
combination.push([i, i]);
}
else return [];
}
return combination;
}

function secondDiagWinnerCombination(player) {
combination = [];
for (let i = 0; i < dimension; i++){
if (field[dimension - 1 - i][i] == player){
combination.push([dimension - 1 - i, i]);
}
else return [];
}
return combination;
}

function rowWinnerCombination(player, row) {
combination = [];
for (let i = 0; i < dimension; i++){
if (field[row][i] == player){
combination.push([row, i]);
}
else return [];
}
return combination;
}

function colWinnerCombination(player, col) {
combination = [];
for (let i = 0; i < dimension; i++){
if (field[i][col] == player){
combination.push([i, col]);
}
else return [];
}
return combination;
}

function alertWinner(player) {
switch(player) {
case CROSS:
alert('Победили крестики!');
break;
case ZERO:
alert('Победили нолики!');
break;
}
isGameOver = true;
colorWinner(winCombination);
}

function colorWinner(winComb) {
winComb.forEach(winCell => {
const cell = findCell(winCell[0], winCell[1]);
cell.style.backgroundColor = "red";
});
}

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

targetCell.textContent = symbol;
targetCell.style.color = color;
Expand All @@ -54,9 +170,19 @@ function addResetListener () {
}

function resetClickHandler () {
console.log('reset!');
startGame();
}

function botTurn() {
while(!isGameOver){
x = Math.floor(Math.random() * dimension);
y = Math.floor(Math.random() * dimension);
if (field[x][y] == EMPTY) {
cellClick(x, y);
break;
}
}
}

/* Test Function */
/* Победа первого игрока */
Expand Down