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

Куксовский Алехин ФТ-201 #172

Open
wants to merge 1 commit 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
137 changes: 133 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const CROSS = 'X';
const ZERO = 'O';
const PLAYER1 = 'X';
const PLAYER2 = 'O';
const EMPTY = ' ';

let counter = 0;
let winner = null;
let player = PLAYER1
let field = [EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY];


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

startGame();
Expand All @@ -27,15 +33,130 @@ function renderGrid (dimension) {
}

function cellClickHandler (row, col) {
// Пиши код тут
console.log(`Clicked on cell: ${row}, ${col}`);
if (winner === null) {
let cellCymbol = field[row * 3+ col];

if (cellCymbol === EMPTY && (winner === null || counter == 0)){
let newCymbol = (player === PLAYER1) ? PLAYER1 : PLAYER2;
field[row * 3+ col] = player;
player = (player === PLAYER1) ? PLAYER2 : PLAYER1;
counter++;
renderSymbolInCell(newCymbol, row, col)
}
//printFieldAlert()
winner = checkWinner();

if (winner !== null){
if (winner === PLAYER1){
comb = findWinnerCombination()
alert(comb)
renderSymbolInCell (field[comb[0]], Math.trunc(comb[0] / 3), comb[0] % 3, '#990000');
renderSymbolInCell (field[comb[1]], Math.trunc(comb[1] / 3), comb[1] % 3, '#990000');
renderSymbolInCell (field[comb[2]], Math.trunc(comb[2] / 3), comb[2] % 3, '#990000');
alert ("Победил Player 1", comb);
}
else if (winner === PLAYER2){
comb = findWinnerCombination()
renderSymbolInCell (field[comb[0]], Math.trunc(comb[0] / 3), comb[0] % 3, '#990000');
renderSymbolInCell (field[comb[1]], Math.trunc(comb[1] / 3), comb[1] % 3, '#990000');
renderSymbolInCell (field[comb[2]], Math.trunc(comb[2] / 3), comb[2] % 3, '#990000');
alert ("Победил Player 2", comb);
}
}
else if (checkDraw()){
alert ("Победила дружба");
}

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



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



}

function checkDraw () {
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (field[i * 3 + j] === EMPTY) {
return false;
}
}
}
return true;
}

function printFieldAlert() {
res = '';
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (field[i * 3 + j] === EMPTY) {
res += '_';
} else {
res += field[i * 3 + j];
}
}

}
alert(res);
}

function findWinnerCombination () {
let combs = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];

for (let i = 0; i < 8; i++) {
if (
field[combs[i][0]] === field[combs[i][1]] &&
field[combs[i][1]] === field[combs[i][2]] &&
field[combs[i][0]] != EMPTY
) {
return combs[i];
}
}

return [];
}


function checkWinner () {
if ((field[0] === field[1] && field[1] === field[2] && field[0] === PLAYER1)||
(field[3] === field[4] && field[4] === field[5] && field[3] === PLAYER1) ||
(field[6] === field[7] && field[7] === field[8] && field[6] === PLAYER1) ||
(field[0] === field[3] && field[3] === field[6] && field[0] === PLAYER1) ||
(field[1] === field[4] && field[4] === field[7] && field[1] === PLAYER1) ||
(field[2] === field[5] && field[5] === field[8] && field[2] === PLAYER1) ||
(field[0] === field[4] && field[4] === field[8] && field[0] === PLAYER1) ||
(field[2] === field[4] && field[4] === field[6] && field[6] === PLAYER1)) {
return PLAYER1
}
else if ((field[0] === field[1] && field[1] === field[2] && field[0] === PLAYER2)||
(field[3] === field[4] && field[4] === field[5] && field[3] === PLAYER2) ||
(field[6] === field[7] && field[7] === field[8] && field[6] === PLAYER2) ||
(field[0] === field[3] && field[3] === field[6] && field[0] === PLAYER2) ||
(field[1] === field[4] && field[4] === field[7] && field[1] === PLAYER2) ||
(field[2] === field[5] && field[5] === field[8] && field[2] === PLAYER2) ||
(field[0] === field[4] && field[4] === field[8] && field[0] === PLAYER2) ||
(field[2] === field[4] && field[4] === field[6] && field[6] === PLAYER2)) {
return PLAYER2
}
else return null
}


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

Expand All @@ -54,6 +175,14 @@ function addResetListener () {
}

function resetClickHandler () {
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
field[i * 3 + j] = EMPTY;
renderSymbolInCell(EMPTY, i, j)

}
}
winner = null
console.log('reset!');
}

Expand Down