Skip to content

Мехряков Матвей #63

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 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
68 changes: 61 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const CROSS = 'X';
const ZERO = 'O';
const EMPTY = ' ';
let field = [
[null,null,null],
[null,null,null],
[null,null,null]
];
let move = 0;
let isWin = false;

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

Expand All @@ -26,17 +33,56 @@ function renderGrid (dimension) {
}
}

function cellClickHandler (row, col) {
// Пиши код тут
function cellClickHandler (row, col) {
if (!isWin) {
if (field[row][col] === null) {
move % 2 === 0 ? (renderSymbolInCell(CROSS, row, col), field[row][col] = CROSS) : (renderSymbolInCell(ZERO, row, col), field[row][col] = ZERO);
move++;;
}

checkWinner();

if (move == 9 && !isWin) {
alert('Победила дружба');
}
}

//console.log(field);
console.log(`Clicked on cell: ${row}, ${col}`);
}

function checkWinner () {
for (let i = 0; i <= 3; i = i + 2) {
if (field[0][i] == field[1][1] && field[1][1] == field[2][2-i] && field[0][i] != null) {
renderSymbolInCell(field[i][0], 0, i, 'rgb(240, 17, 17)');
renderSymbolInCell(field[i][0], 1, 1, 'rgb(240, 17, 17)');
renderSymbolInCell(field[i][0], 2, 2-i, 'rgb(240, 17, 17)');
isWin = true;
alert(`Выиграл игрок использующий ${field[0][i]}`);

}
}

for (let i = 0; i < 3; i++) {
if (field[i][0] == field[i][1] && field[i][1] == field[i][2] && field[i][0] != null) {
renderSymbolInCell(field[i][0], i, 0, 'rgb(240, 17, 17)');
renderSymbolInCell(field[i][0], i, 1, 'rgb(240, 17, 17)');
renderSymbolInCell(field[i][0], i, 2, 'rgb(240, 17, 17)');
isWin = true;
alert(`Выиграл игрок использующий ${field[i][0]}`);
}

/* Пользоваться методом для размещения символа в клетке так:
renderSymbolInCell(ZERO, row, col);
*/
if (field[0][i] == field[1][i] && field[1][i] == field[2][i] && field[0][i] != null) {
renderSymbolInCell(field[1][i], 0, i, 'rgb(240, 17, 17)');
renderSymbolInCell(field[1][i], 1, i, 'rgb(240, 17, 17)');
renderSymbolInCell(field[1][i], 2, i, 'rgb(240, 17, 17)');
isWin = true;
alert(`Выиграл игрок использующий ${field[1][i]}`);
}
}
}

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

targetCell.textContent = symbol;
Expand All @@ -54,7 +100,14 @@ function addResetListener () {
}

function resetClickHandler () {
console.log('reset!');
startGame(3);
move = 0;
isWin = false;
field = [
[null,null,null],
[null,null,null],
[null,null,null]
];
}


Expand Down Expand Up @@ -87,3 +140,4 @@ function testDraw () {
function clickOnCell (row, col) {
findCell(row, col).click();
}

19 changes: 11 additions & 8 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ body {
margin: 40px auto;
/*display: inline-block;*/
width: auto;
background-color: lightgoldenrodyellow;
padding: 10px;
background-color: rgb(43, 43, 42);
padding: 1px;
border-radius: 10px;
}

.table {
Expand All @@ -22,7 +23,7 @@ body {
font-size: 24px;
font-family: "Consolas", monospace;
text-align: center;
border: 1px solid #333;
border: 1px solid rgb(255, 255, 255);
cursor: pointer;
}

Expand All @@ -44,10 +45,12 @@ body {

.resetButton {
display: block;
margin: 20px auto;
margin: 5px auto;
padding: 10px;
background: none;
font-size: 16px;
border: 3px solid orange;
border-radius: 5px;
}
background-color: rgb(43, 43, 42);
font-size: 20px;
border: 2px solid orange;
border-radius: 10px;
color: orange;
}