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 Ярослав Белозеров, Андрей Батин #224

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
141 changes: 136 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,84 @@ const EMPTY = ' ';

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

let n = Number(prompt("Введите размер матрицы, например: 3"));
let matrix = Array.from({ length: n }, () => Array(n).fill(EMPTY));


let step_counter = 0;
let gameEnded = true;


function getMaxStepCount(){
return n*n;
}

function getAvailableMoves(){
let moves = [];
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++){
if (matrix[i][j] === EMPTY)
moves.push([i, j]);
}
}

return moves;
}

function hardCodeMove(bot_ = CROSS){
moves = getAvailableMoves();
console.log(moves);

for (let pair of moves){
i = pair[0];
j = pair[1];
matrix[i][j] = bot_;
if (checkWin(matrix))
return (i,j);
matrix[i][j] = EMPTY;
}
return getRandomPoint(moves);
}

function getRandomPoint(moves){
const rand = Math.floor(Math.random() * (moves.length));

return moves[rand];
}

function checkWin(matrix) {
const n = matrix.length;

for (let i = 0; i < n; i++) {
if (matrix[i][0] !== EMPTY && matrix[i].every(cell => cell === matrix[i][0])) {
return matrix[i].map((cell, index) => [i, index]);
}
}

for (let j = 0; j < n; j++) {
if (matrix[0][j] !== EMPTY && matrix.every(row => row[j] === matrix[0][j])) {
return matrix.map((row, index) => [index, j]);
}
}

if (matrix[0][0] !== EMPTY && matrix.every((row, index) => row[index] === matrix[0][0])) {
return matrix.map((row, index) => [index, index]);
}

if (matrix[0][n - 1] !== EMPTY && matrix.every((row, index) => row[n - 1 - index] === matrix[0][n - 1])) {
return matrix.map((row, index) => [index, n - 1 - index]);
}

return null;
}
startGame();
addResetListener();

function startGame () {
renderGrid(3);
renderGrid(n);
step_counter = 0;
matrix = Array.from({ length: n }, () => Array(n).fill(EMPTY));
gameEnded = false;
}

function renderGrid (dimension) {
Expand All @@ -27,13 +100,70 @@ function renderGrid (dimension) {
}

function cellClickHandler (row, col) {
// Пиши код тут
console.log(`Clicked on cell: ${row}, ${col}`);

if (gameEnded){
alert("Начните новую игру!");
return;
}

if (step_counter == getMaxStepCount()){
console.log("Game already ended!")
return;
}

if (matrix[row][col] !== EMPTY){
return;
}


let current_step = step_counter % 2 === 0 ? ZERO : CROSS;
matrix[row][col] = current_step;
renderSymbolInCell(current_step, row, col);
step_counter += 1;
let winner = checkWin(matrix);
if (winner !== null){
gameEnded = true;
switch (matrix[winner[0][0]][winner[0][1]]){
case CROSS:
alert("Победили крестики");
break;
case ZERO:
alert("Победили нолики");
break;
}
for (let coords of winner){
renderSymbolInCell(matrix[winner[0][0]][winner[0][1]], coords[0], coords[1], "red")
}
return;
}
/*if (step_counter > (n * n) / 2){
increaseMatrixSize();
}*/
let point = hardCodeMove();
if (step_counter % 2 === 1){
cellClickHandler(point[0], point[1]);
}
if (step_counter === getMaxStepCount()){
console.log("Game End!")
alert("Победила Дружба!")
}
}

/* Пользоваться методом для размещения символа в клетке так:
renderSymbolInCell(ZERO, row, col);
*/
function increaseMatrixSize(){
for (let row of matrix){
row.unshift(EMPTY);
row.push(EMPTY);
}
matrix.push(Array(n + 2).fill(EMPTY));
matrix.unshift(Array(n + 2).fill(EMPTY));
n = n + 2;
renderGrid(n);
for (let row = 0; row < matrix.length; row++){
for (let column = 0; column < matrix.length; column++){
renderSymbolInCell(matrix[row][column], row, column);
}
}
}

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

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


Expand Down