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

Biktasheva Altyngul, Kalinina Sophia, FT-201, tasks 1-9 #164

Open
wants to merge 3 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
174 changes: 165 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
const CROSS = 'X';
const ZERO = 'O';
const EMPTY = ' ';
let DIMENSION = 3;

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

let grid;
let currSymbol = CROSS;
let stepsCount = DIMENSION * DIMENSION;
let winner = EMPTY;
let winnerLoc = [];


startGame();
addResetListener();

function startGame () {
renderGrid(3);
DIMENSION = parseInt(prompt("Введи размер:", '3'), 10);
stepsCount = DIMENSION * DIMENSION;
createGrid();
renderGrid();
}

function renderGrid (dimension) {
function createGrid () {
grid = [];
for (let i = 0; i < DIMENSION; i++) {
grid[i] = [];
for (let j = 0; j < DIMENSION; j++) {
grid[i][j] = EMPTY;
}
}
}

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

for (let i = 0; i < dimension; i++) {
for (let i = 0; i < DIMENSION; i++) {
const row = document.createElement('tr');
for (let j = 0; j < dimension; j++) {
for (let j = 0; j < DIMENSION; j++) {
const cell = document.createElement('td');
cell.textContent = EMPTY;
cell.textContent = grid[i][j];
cell.addEventListener('click', () => cellClickHandler(i, j));
row.appendChild(cell);
}
Expand All @@ -27,13 +48,141 @@ function renderGrid (dimension) {
}

function cellClickHandler (row, col) {
// Пиши код тут

console.log(`Clicked on cell: ${row}, ${col}`);
if (winner != EMPTY) {
return;
}

if (grid[row][col] != EMPTY) {
return;
}

renderSymbol(row, col);

stepsCount -= 1;

setWinner();

/* Пользоваться методом для размещения символа в клетке так:
renderSymbolInCell(ZERO, row, col);
*/
console.log(winner);

if (winner != EMPTY) {
console.log(winnerLoc);
for(let loc of winnerLoc) {
renderSymbolInCell(winner, loc[0], loc[1], '#FF0000');
}
alert(`Победили ${winner}!!`);
return;
}

if (stepsCount == 0) {
alert('Победила дружба!!!');
return;
}
}

function renderSymbol(row, col) {
renderSymbolInCell(currSymbol, row, col);
grid[row][col] = currSymbol;

switch (currSymbol) {
case ZERO:
currSymbol = CROSS;
break;
case CROSS:
currSymbol = ZERO;
break;
}
}

function setWinner() {
setWinnerHorizontal();
setWinnerVertical();
setWinnerDiagonal();
setWinnerDiagonalReverse();
}

function setWinnerHorizontal () {
next: for (let i = 0; i < DIMENSION; i++) {
locations = [ [i, 0] ];
checkValue = grid[i][0];

if (checkValue == EMPTY){
continue next;
}

for (let j = 1; j < DIMENSION; j++) {
if (grid[i][j] != checkValue) {
continue next;
}
locations.push([i, j]);
}

winner = checkValue;
// for (let loc of )
winnerLoc = winnerLoc.concat(locations);
break;
}
}

function setWinnerVertical () {
next: for (let i = 0; i < DIMENSION; i++) {
locations = [ [0, i] ];
checkValue = grid[0][i];

if (checkValue == EMPTY){
continue next;
}

for (let j = 1; j < DIMENSION; j++) {
if (grid[j][i] != checkValue) {
continue next;
}
locations.push([j, i]);
}

winner = checkValue;
winnerLoc = winnerLoc.concat(locations);
break;
}
}

function setWinnerDiagonal () {
locations = [ [0, 0] ];
checkValue = grid[0][0];

if (checkValue == EMPTY){
return;
}

for (let i = 1; i < DIMENSION; i++) {
if (grid[i][i] != checkValue) {
return;
}
locations.push([i, i]);
}

winner = checkValue;
winnerLoc = winnerLoc.concat(locations);
}

function setWinnerDiagonalReverse () {
locations = [ [0, DIMENSION - 1] ];
checkValue = grid[0][DIMENSION - 1];

if (checkValue == EMPTY){
return;
}

for (let i = 1; i < DIMENSION; i++) {
if (grid[i][DIMENSION - i - 1] != checkValue) {
return;
}
locations.push([i, DIMENSION - i - 1]);
}

winner = checkValue;
winnerLoc = winnerLoc.concat(locations);
}

function renderSymbolInCell (symbol, row, col, color = '#333') {
Expand All @@ -55,6 +204,13 @@ function addResetListener () {

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

startGame();
currSymbol = CROSS;
stepsCount = DIMENSION * DIMENSION;
winner = EMPTY;
winnerLoc = [];

}


Expand Down