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 Гречухин Владимир, Адылов Муротжон #228

Open
wants to merge 7 commits 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
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<div>
<button class="resetButton" id="reset">Сначала</button>
</div>
<input type="text" id="textInput" style="width: 500px; margin: 0 auto;" placeholder="Введите текст здесь...">
<button id="submitButton" style="width: 500px; margin: 0 auto;">Отправить</button>
<script src="index.js"></script>
</body>
</html>
200 changes: 194 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,118 @@
const CROSS = 'X';
const ZERO = 'O';
const EMPTY = ' ';
let playerNow = CROSS;
let fieldSize = 3;
let field = [];
let movesMade = 0;
let endGame = false;

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

const submitButton = document.getElementById('submitButton');
const textInput = document.getElementById('textInput');
submitButton.addEventListener('click', function() {
fieldSize = +textInput.value;
if (isNaN(fieldSize) || fieldSize < 3) {
return;
}
console.log(fieldSize);
startGame();
});

startGame();
addResetListener();



function startGame () {
renderGrid(3);
renderGrid(fieldSize);
playerNow = CROSS;
makeEmptyField();
movesMade = 0;
endGame = false;
}

function makeEmptyField() {
field = [];
for (let i = 0; i < fieldSize; ++i) {
field.push(new Array(fieldSize).fill(EMPTY));
}
}

function expandField() {
++fieldSize;
for (let row of field){
row.push(EMPTY);
}
field.push(new Array(fieldSize).fill(EMPTY));
renderGrid(fieldSize);
for (let row = 0; row < fieldSize; ++row) {
for (let col = 0; col < fieldSize; ++col) {
renderSymbolInCell(field[row][col], row, col);
}
}
}

function checkWinner() {
for (let row = 0; row < fieldSize; row++) {
for (let col = 0; col <= fieldSize - fieldSize; col++) {
const symbol = field[row][col];
if (symbol !== EMPTY && checkLine(field, symbol, row, col, 0, 1, fieldSize)) {
renderWinningCells(symbol, row, col, 0, 1, fieldSize);
return symbol;
}
}
}

for (let col = 0; col < fieldSize; col++) {
for (let row = 0; row <= fieldSize - fieldSize; row++) {
const symbol = field[row][col];
if (symbol !== EMPTY && checkLine(field, symbol, row, col, 1, 0, fieldSize)) {
renderWinningCells(symbol, row, col, 1, 0, fieldSize);
return symbol;
}
}
}

for (let row = 0; row <= fieldSize - fieldSize; row++) {
for (let col = 0; col <= fieldSize - fieldSize; col++) {
const symbol = field[row][col];
if (symbol !== EMPTY && checkLine(field, symbol, row, col, 1, 1, fieldSize)) {
renderWinningCells(symbol, row, col, 1, 1, fieldSize);
return symbol;
}
}
}

for (let row = 0; row <= fieldSize - fieldSize; row++) {
for (let col = fieldSize - 1; col >= fieldSize - fieldSize; col--) {
const symbol = field[row][col];
if (symbol !== EMPTY && checkLine(field, symbol, row, col, 1, -1, fieldSize)) {
renderWinningCells(symbol, row, col, 1, -1, fieldSize);
return symbol;
}
}
}

return false;
}

function checkLine(field, symbol, startRow, startCol, deltaRow, deltaCol, fieldSize) {
for (let i = 0; i < fieldSize; i++) {
if (field[startRow + i * deltaRow][startCol + i * deltaCol] !== symbol) {
return false;
}
}
return true;
}

function renderWinningCells(symbol, startRow, startCol, deltaRow, deltaCol, fieldSize) {
for (let i = 0; i < fieldSize; i++) {
const row = startRow + i * deltaRow;
const col = startCol + i * deltaCol;
renderSymbolInCell(symbol, row, col, '#ff0000');
}
}

function renderGrid (dimension) {
Expand All @@ -27,13 +131,30 @@ function renderGrid (dimension) {
}

function cellClickHandler (row, col) {
// Пиши код тут
console.log(`Clicked on cell: ${row}, ${col}`);
if (endGame) {
return;
}
if (field[row][col] === EMPTY) {
renderSymbolInCell(playerNow, row, col);
field[row][col] = playerNow;
playerNow = playerNow == CROSS ? ZERO : CROSS;
++movesMade;
}
updateGameState();
}


/* Пользоваться методом для размещения символа в клетке так:
renderSymbolInCell(ZERO, row, col);
*/
function updateGameState() {
let winner = checkWinner();
if (winner) {
endGame = true;
console.log(`Победил ${winner}`);
} else if (movesMade == fieldSize * fieldSize) {
endGame = true;
console.log("Победила дружба");
} else if (movesMade > fieldSize * fieldSize / 2) {
expandField();
}
}

function renderSymbolInCell (symbol, row, col, color = '#333') {
Expand All @@ -55,6 +176,73 @@ function addResetListener () {

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

function findWinningMove(field, playerNow) {
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (field[i][j] === EMPTY) {
field[i][j] = playerNow;
if (checkWinner(field, playerNow)) {
return [i, j];
}
field[i][j] = EMPTY;
}
}
}
return null;
}

function makeSmartMove(field) {
let winningMove = findWinningMove(field, ZERO);
if (winningMove) {
field[winningMove[0]][winningMove[1]] = ZERO;
return field;
}

let blockMove = findWinningMove(field, CROSS);
if (blockMove) {
field[blockMove[0]][blockMove[1]] = ZERO;
return field;
}

let emptyCells = [];
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (field[i][j] === EMPTY) {
emptyCells.push([i, j]);
}
}
}

let randomMove = emptyCells[Math.floor(Math.random() * emptyCells.length)];
field[randomMove[0]][randomMove[1]] = ZERO;

return field;

}


function makeAssistentMove (field) {
let emptyCells = [];

for (let i = 0; i < field.length; i++) {
for (let j = 0; j < field[i].length; j++) {
if (field[i][j] == EMPTY)
emptyCells.push([i, j]);
}
if (emptyCells.length === 0) {
return field;
}

let [row, col] = emptyCells[Math.floor(Math.random() * emptyCells.length)];

field[row][col] = ZERO;

return field
}

}


Expand Down