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 #136

Open
wants to merge 8 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
164 changes: 149 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
const CROSS = 'X';
const ZERO = 'O';
const EMPTY = ' ';
let DIMENSION = 3;

const container = document.getElementById('fieldWrapper');
let grid = [];
let ended = false;

startGame();
addResetListener();

function startGame () {
renderGrid(3);
function startGame() {
DIMENSION = +prompt("Введите размер поля");
createGrid(DIMENSION);
renderGrid(DIMENSION);
}

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

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

function cellClickHandler (row, col) {
// Пиши код тут
function checkWinCondition() {
for (let i = 0; i < DIMENSION; i++) {

let arr = [];
let countZero = 0;
let countCross = 0;
for (let j = 0; j < DIMENSION; j++) {
arr.push([i, j]);
if (grid[i][j] === ZERO)
countZero++;
if (grid[i][j] === CROSS)
countCross++;
}
console.log(countZero);
if (DIMENSION === countZero || DIMENSION === countCross)
return arr
}
for (let i = 0; i < DIMENSION; i++) {
arr = [];
let countZero = 0;
let countCross = 0;
for (let j = 0; j < DIMENSION; j++) {
arr.push([j, i]);
if (grid[j][i] === ZERO)
countZero++;
if (grid[j][i] === CROSS)
countCross++;
}
console.log(countZero);
if (DIMENSION === countZero || DIMENSION === countCross)
return arr
}
arr = [];
let countZero = 0;
let countCross = 0;

for (let i = 0; i < DIMENSION; i++) {
arr.push([i, i]);
if (grid[i][i] === ZERO)
countZero++;
if (grid[i][i] === CROSS)
countCross++;
}

if (DIMENSION === countZero || DIMENSION === countCross)
return arr


countZero = 0;
countCross = 0;
arr = [];
for (let i = 0; i < DIMENSION; i++) {
arr.push([DIMENSION - i - 1, i]);
if (grid[DIMENSION - i - 1][i] === ZERO)
countZero++;
if (grid[DIMENSION - i - 1][i] === CROSS)
countCross++;
}

if (DIMENSION === countZero || DIMENSION === countCross)
return arr

return false;
}

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

let parity = false;

function checkEndGame() {
const line = checkWinCondition();
if (line) {
let win = grid[line[0][0]][line[0][1]]
switch (win) {
case CROSS:
alert("CROSS");
ended = true;
renderWinner(line)
break;
case ZERO:
alert("ZERO");
ended = true;
renderWinner(line)
break;
}
} else {
if (checkDraw(DIMENSION))
alert("Победила дружба!")
}
}

function renderWinner(line) {
for (let coord of line) {
const symbol = grid[coord[0]][coord[1]]
renderSymbolInCell(symbol, coord[0], coord[1], "#c00")
}
}

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

if (ended) {
return;
}
if (grid[row][col] === EMPTY) {
if (parity) {
grid[row][col] = CROSS;
renderSymbolInCell(CROSS, row, col)
} else {
grid[row][col] = ZERO;
renderSymbolInCell(ZERO, row, col)
}
}
parity = !parity;
checkEndGame();
}

/* Пользоваться методом для размещения символа в клетке так:
renderSymbolInCell(ZERO, row, col);
*/
function checkDraw(dimension) {
let empty = 0;
for (let i = 0; i < dimension; i++) {
for (let j = 0; j < dimension; j++) {
if (grid[i][j] === EMPTY)
empty++;
}
}
return empty === 0;
}

function renderSymbolInCell (symbol, row, col, color = '#333') {
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 () {
function resetClickHandler() {
console.log('reset!');
parity = false;
ended = false;
startGame();
}


/* Test Function */

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

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

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