Показать сообщение отдельно
  #8 (permalink)  
Старый 08.06.2020, 09:03
Аватар для рони
Профессор
Отправить личное сообщение для рони Посмотреть профиль Найти все сообщения от рони
 
Регистрация: 27.05.2010
Сообщений: 33,068

игра алфавит
s4meone,
<!doctype html>
<html lang="ru">
<head>
        <meta charset="UTF-8">
        <title>Document</title>
  <style type="text/css">
 td{
     border: 1px solid hsla(240, 100%, 40%, 1);
     height: 45px;
     width: 45px;
     text-align: center;
     font-size: 28px;
     transition: 1s;
 }
 td.collapsing{
     transform: scale(0);
 }
 </style>
</head>
<body>
<script>
class gameTable {
    constructor(arr, colls, rows) {
        this.colls = colls;
        this.rows = rows;
        this.length = this.colls * this.rows;
        this.index = 0;
        this.array = arr;
        this.contentTd = this.createContent(this.array, this.length);
        this.table = document.createElement('table');
        this.createTableHtml();
        this.table.addEventListener("click", this.clickHandler.bind(this));
        this.time = 0;
    }
    shuffleArray(array) {
        for (let i = array.length - 1; i > 0; i--) {
            let j = Math.trunc(Math.random() * i);
            [array[i], array[j]] = [array[j], array[i]];
        }
        return array;
    }
    createContent(array, length) {
        let content = [];
        for (let i = 0; i < array.length;) {
            let temp = array.slice(i, i += length);
            temp = this.shuffleArray(temp);
            content = content.concat(temp);
        }
        return content
    }
    createTableHtml() {
        const tHead = this.table.createTHead();
        let row = tHead.insertRow();
        let cell = row.insertCell();
        cell.colSpan = this.colls
        cell.textContent = '0.0';
        this.timeTd = cell;
        this.index = 0;
        const tBody = this.table.createTBody();
        for (var i = 0; i < this.rows; i++) {
            row = tBody.insertRow();
            for (var k = 0; k < this.colls; k++) {
                cell = row.insertCell();
                cell.textContent = this.contentTd[this.index++] || '';
            }
        }
        this.index = 0;
    }
    clickHandler({target}) {
        target = target.closest('td');
        if (!target) return;
        let txt = target.textContent;
        if (txt == this.array[this.index]) {
            if (txt == this.array[0]) {
                this.interval = setInterval(() => {
                    this.time += 0.01;
                    this.timeTd.textContent = (Math.floor(this.time * 100) / 100).toFixed(1);
                }, 10);
            }
            if (txt == this.array[this.array.length - 1]) {
                clearInterval(this.interval)

            }
            txt = this.contentTd[this.index + this.length] || '';
            target.textContent = txt;
            target.classList.toggle('collapsing', !txt);
            this.index++;
        }
    }
}
let game = new gameTable('АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'.split(''), 3, 4);
document.body.appendChild(game.table);
</script>
</body>
</html>

Последний раз редактировалось рони, 08.06.2020 в 09:56.
Ответить с цитированием