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

gameTable алфавит
Сообщение от 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));
    }
    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() {
        this.index = 0;
        for (var i = 0; i < this.rows; i++) {
            let row = this.table.insertRow();
            for (var k = 0; k < this.colls; k++) {
                let 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]) {
            txt = this.contentTd[this.index + this.length] || '';
            target.textContent = txt;
            target.classList.toggle('collapsing', !txt);
            this.index++;
        }
        if (this.index == this.array.length) alert('End');
    }
}
let game = new gameTable('АБВГДЕЁЖЗИЙКЛМНОП'.split(''), 3, 4);
document.body.appendChild(game.table);
</script>
</body>
</html>

Последний раз редактировалось рони, 07.06.2020 в 23:08.
Ответить с цитированием