untitled notebook

node v10.24.1
version: 4.0.1
endpointsharetweet
this.currentQuestion = 0; this.currentLetter = 0; this.positionLetters = [ { cells: [ [6, 0], [7, 0], [8, 0], ], keyboard: ['a', 'a', 'z', 'm', 'c', 'n', 'c', 'd'], question: 'Not a Woman', }, { cells: [ [0, 1], [0, 0], ], keyboard: ['D', 'A', 'T', 'M', 'C', 'N', 'C', 'D'], question: 'Father', }, { cells: [ [0, 0], [0, 1], [0, 2], ], keyboard: ['D', 'O', 'N', 'M', 'O', 'W', 'C', 'Y'], question: 'Not up', }, { cells: [ [1, 8] ], keyboard: ['D', 'O', 'N', 'M', 'O', 'A', 'C', 'Y'], question: 'Sun', }, { cells: [ [8, 0], [8, 1], [8, 2], [8, 3], ], keyboard: ['D', 'E', 'N', 'M', 'O', 'A', 'C', 'R'], question: 'Oppsite of far', }, ]; this.startingField = [ [' ', ' ', 'd', '.', '.', 's', 'e', 'n', 'd'], [' ', '.', 'e', 'a', 's', 't', '.', '.', ' '], [' ', '.', 'a', '.', '.', 'i', 't', 's', 'y'], ['n', 'e', 'r', 'f', '.', 'n', '.', 't', '.'], ['.', 'a', '.', 'a', 'r', 'k', '.', 'u', '.'], ['.', 's', '.', 't', '.', 's', 'y', 'n', 'c'], ['m', 'e', 's', 'h', '.', '.', 'a', '.', 'a'], [' ', '.', '.', 'e', 'v', 'e', 'r', '.', 'r'], [' ', ' ', ' ', ' ', '.', '.', 'd', 'i', 'd'], ]; this.checkWord = [ ['d', 'a', 0, 0, 0, 0, 0, 0, 0], ['o', 0, 0, 0, 0, 0, 0, 0, 'a'], ['w', 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], ['a', 0, 0, 0, 0, 0, 0, 0, 0], ['n', 'e', 'a', 'r', 0, 0, 0, 0, 0], ]; this.gotoNextCell = () => { const questionObj = this.positionLetters[this.currentQuestion]; console.log('questionObj = ', questionObj); // Клетки текущего слова — questionObj.cells. // Т.к. мы можем начинать и не с первой клетки, берем слайс нашего массива. const slicedCells = questionObj.cells.slice(this.currentLetter); const nextIndex = slicedCells // Люблю я функциональный стиль :) // Так посмотрим на буквы которые есть на поле в наших клетках. .map(([i, j]) => this.startingField[i][j]) .findIndex((e) => e === ' '); if (nextIndex >= 0) { // Индекс следующей клетки. this.currentLetter += nextIndex; return true; } else { this.currentLetter = 0; return false; } };
this.gotoNextCell();
this.currentLetter; // После вызова функции выше this.currentLetter равно 1.
this.handleCorrectInput = () => { const [i, j] = this.positionLetters[this.currentQuestion].cells[ this.currentLetter ]; // Показываем букву, подсвечиваем и т.д. this.arr[i][j].cellText.text = e.toUpperCase(); // Переходим к следующей клетке if (this.gotoNextCell()) { // Следующая клетка есть. // this.currentLetter содержит в себе индекс следующей клетки. } else { // Следующей клетки нет. // Нам нужно переходить к следующему слову. this.currentQuestion += 1; if (this.currentQuestion <= this.positionLetters.length - 1) { // Следующее слово есть. // И мы ищем следующую подходящую клетку в этом слове. this.gotoNextCell(); } else { // Слов больше не осталось, конец игры! } } }; this.handleIncorrectInput = () => { // Обнуляем прогресс. this.currentLetter = 0; // А еще нам нужно удалить все введенные буквы и все такое. // И мы ищем следующую подходящую клетку в этом слове, для ввода пользователя. this.gotoNextCell(); }; this.handleClick = (letter) => { const [i, j] = this.positionLetters[this.currentQuestion].cells[ this.currentLetter ]; if (this.checkWord[i][j] === letter) { // Верная буква. this.handleCorrectInput(); } else { // Игрок ошибся this.handleIncorrectInput(); } }
Loading…

no comments

    sign in to comment