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

сдаюсь #58

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.

12 changes: 12 additions & 0 deletions .idea/1-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/misc.xml

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.

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.

68 changes: 59 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
const CROSS = 'X';
const ZERO = 'O';
const EMPTY = ' ';

const container = document.getElementById('fieldWrapper');
let map;
let flag;
let startPos;
let dimension;
let count;
let fieldArr = [];
let gameOver = false;
let sizeField = 3;

startGame();
addResetListener();

function startGame () {
function startGame() {
renderGrid(3);
}

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

for (let i = 0; i < dimension; i++) {
Expand All @@ -26,16 +33,59 @@ function renderGrid (dimension) {
}
}


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

if (!(map.has(`${row}${col}`) || flag)) {
startPos = (startPos + 1) % 2;
let symbol = startPos === 0 ? CROSS : ZERO;
map.set(`${row}${col}`, symbol);
renderSymbolInCell(symbol, row, col);
//count++;
flag = map.size === dimension ** 2;
if (checkWin(symbol, row, col)) {
alert(`${symbol} WIN`);
flag = true;
} else if (flag) {
alert("Победила дружба");
}
console.log(`Clicked on cell: ${row}, ${col}`);
}
}

/* Пользоваться методом для размещения символа в клетке так:
renderSymbolInCell(ZERO, row, col);
*/
function checkWin (symbol, row, col) {
for (let i = 0; i < dimension; i++) {
if (map.get(`${i}${col}`) !== symbol) break;
if (i === dimension - 1) {
for (let x = 0; x < dimension; x++) renderSymbolInCell(symbol, x, col, '#FF0000');
return true;
}
}
for (let j = 0; j < dimension; j++) {
if (map.get(`${row}${j}`) !== symbol) break;
if (j === dimension - 1) {
for (let x = 0; x < dimension; x++) renderSymbolInCell(symbol, row, x, '#FF0000');
return true;
}
}
for (let ij = 0; ij < dimension; ij++) {
if (map.get(`${ij}${ij}`) !== symbol) break;
if (ij === dimension - 1) {
for (let x = 0; x < dimension; x++) renderSymbolInCell(symbol, x, x, '#ff0000');
return true;
}
}
for (let ij = 0; ij < dimension; ij++) {
if (map.get(`${dimension - ij - 1}${ij}`) !== symbol) break;
if (ij === dimension - 1) {
for (let x = 0; x < dimension; x++) renderSymbolInCell(symbol, dimension - 1 - x, x, '#FF0000');
return true;
}
}
return false
}


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

Expand All @@ -54,10 +104,10 @@ function addResetListener () {
}

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


/* Test Function */
/* Победа первого игрока */
function testWin () {
Expand Down