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

ФТ-202 Володько, Мартынов #189

Closed
wants to merge 3 commits into from
Closed
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
135 changes: 118 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,28 @@ const ZERO = 'O';
const EMPTY = ' ';

const container = document.getElementById('fieldWrapper');
const GameDimension = 5;
const field = createEmptyField(GameDimension);
let currentSymbol = ZERO;
let isGameOver = false;

startGame();
addResetListener();

function startGame () {
renderGrid(3);
function createEmptyField(dimension) {
const field = [];
for (let i = 0; i < dimension; i++) {
const row = new Array(dimension).fill(EMPTY);
field.push(row);
}
return field;
}

function startGame() {
renderGrid(GameDimension);
}

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

for (let i = 0; i < dimension; i++) {
Expand All @@ -26,41 +39,129 @@ function renderGrid (dimension) {
}
}

function cellClickHandler (row, col) {
// Пиши код тут
console.log(`Clicked on cell: ${row}, ${col}`);
function checkRowState(row, symbol) {
return field[row].every(it => it === symbol);
}

function checkColState(col, symbol) {
for (const row of field) {
if (row[col] !== symbol)
return false;
}
return true;
}

/* Пользоваться методом для размещения символа в клетке так:
renderSymbolInCell(ZERO, row, col);
*/
function checkMainDiag(symbol) {
for (let i = 0; i < GameDimension; i++) {
if (field[i][i] !== symbol)
return false;
}
return true;
}

function renderSymbolInCell (symbol, row, col, color = '#333') {
function checkSideDiag(symbol) {
for (let i = 0; i < GameDimension; i++) {
if (field[i][GameDimension - 1 - i] !== symbol)
return false;
}
return true;
}

function checkGameState(currentSymbol) {
const currentSymbolRus = currentSymbol === ZERO ? 'нолик' : 'крестик';
for (let i = 0; i < GameDimension; i++) {
if (checkColState(i, currentSymbol)) {
alert(`Выиграл ${currentSymbolRus}!`);
for (let j = 0; j < GameDimension; j++) {
renderSymbolInCell(currentSymbol, j, i, '#ff0000');
}
isGameOver = true;
} else if (checkRowState(i, currentSymbol)) {
alert(`Выиграл ${currentSymbolRus}!`);
for (let j = 0; j < GameDimension; j++) {
renderSymbolInCell(currentSymbol, i, j, '#ff0000');
}
isGameOver = true;
}
}
if (checkMainDiag(currentSymbol)) {
alert(`Выиграл ${currentSymbolRus}!`);
for (let i = 0; i < GameDimension; i++) {
renderSymbolInCell(currentSymbol, i, i, '#ff0000');
}
isGameOver = true;
}
if (checkSideDiag(currentSymbol)) {
alert(`Выиграл ${currentSymbolRus}!`);
for (let i = 0; i < GameDimension; i++) {
renderSymbolInCell(currentSymbol, i, GameDimension - 1 - i, '#ff0000');
}
isGameOver = true;
}
if (getIndexesOfSymbol(EMPTY).length === 0) {
alert("Победила дружба");
}
}

function getIndexesOfSymbol(symbol) {
let currentIndex = 0;
const symbolIndexes = [];

for (const row of field) {
for (const element of row) {
if (element === symbol) {
symbolIndexes.push(currentIndex);
}
currentIndex++;
}
}

return symbolIndexes;
}

function cellClickHandler(row, col) {
console.log(`Clicked on cell: ${row}, ${col}`);
if (field[row][col] === EMPTY && !isGameOver) {
renderSymbolInCell(currentSymbol, row, col);
field[row][col] = currentSymbol;
checkGameState(currentSymbol);
currentSymbol = currentSymbol === ZERO ? CROSS : ZERO;
}
}

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

targetCell.textContent = symbol;
targetCell.style.color = color;
}

function findCell (row, col) {
function findCell(row, col) {
const targetRow = container.querySelectorAll('tr')[row];
return targetRow.querySelectorAll('td')[col];
}

function addResetListener () {
function addResetListener() {
const resetButton = document.getElementById('reset');
resetButton.addEventListener('click', resetClickHandler);
}

function resetClickHandler () {
console.log('reset!');
function resetClickHandler() {
for (let i = 0; i < GameDimension; i++) {
for (let j = 0; j < GameDimension; j++) {
renderSymbolInCell(EMPTY, i, j);
field[i][j] = EMPTY;
isGameOver = false;
}
}
startGame();
}


/* Test Function */

/* Победа первого игрока */
function testWin () {
function testWin() {
clickOnCell(0, 2);
clickOnCell(0, 0);
clickOnCell(2, 0);
Expand All @@ -71,7 +172,7 @@ function testWin () {
}

/* Ничья */
function testDraw () {
function testDraw() {
clickOnCell(2, 0);
clickOnCell(1, 0);
clickOnCell(1, 1);
Expand All @@ -84,6 +185,6 @@ function testDraw () {
clickOnCell(2, 2);
}

function clickOnCell (row, col) {
function clickOnCell(row, col) {
findCell(row, col).click();
}