How to use rotateMatrix method in wpt

Best JavaScript code snippet using wpt

logic.js

Source:logic.js Github

copy

Full Screen

1import {shiftRowGetAnimationOnePhase, shiftRowGetAnimationTwoPhase} from './animationLogic';2import {getScoreOfMatrixes} from "./getScoreOfMatrixes";3// основные элементы это 4 функции LEFT RIGHT UP DOWN4// отдаем стейт, получаем измененный стейт5export let logicLeft = (matrix) => {6 return null;7}8//матрица здесь- это объект из 4х строк(объект из 4х значений)9//получить матрицу из стейта10export let getMatrixOnState = (state) => {11 let matrix = {12 oneRaw: {13 one: state.oneRaw.one.value,14 two: state.oneRaw.two.value,15 three: state.oneRaw.three.value,16 four: state.oneRaw.four.value17 },18 twoRaw: {19 one: state.twoRaw.one.value,20 two: state.twoRaw.two.value,21 three: state.twoRaw.three.value,22 four: state.twoRaw.four.value23 },24 threeRaw:25 {26 one: state.threeRaw.one.value,27 two: state.threeRaw.two.value,28 three: state.threeRaw.three.value,29 four: state.threeRaw.four.value30 }31 , fourRaw:32 {33 one: state.fourRaw.one.value,34 two: state.fourRaw.two.value,35 three: state.fourRaw.three.value,36 four: state.fourRaw.four.value37 }38 }39 return matrix;4041}42//полчить матрицу анимаций из стейта43let getAnimationMatrixOnState = (state) => {44 let matrix = {45 oneRaw: {46 one: state.oneRaw.one.anime,47 two: state.oneRaw.two.anime,48 three: state.oneRaw.three.anime,49 four: state.oneRaw.four.anime50 },51 twoRaw: {52 one: state.twoRaw.one.anime,53 two: state.twoRaw.two.anime,54 three: state.twoRaw.three.anime,55 four: state.twoRaw.four.anime56 },57 threeRaw:58 {59 one: state.threeRaw.one.anime,60 two: state.threeRaw.two.anime,61 three: state.threeRaw.three.anime,62 four: state.threeRaw.four.anime63 }64 , fourRaw:65 {66 one: state.fourRaw.one.anime,67 two: state.fourRaw.two.anime,68 three: state.fourRaw.three.anime,69 four: state.fourRaw.four.anime70 }71 }72 return matrix73}74//сдвинуть строку75export let shiftRow = (row) => {76 let thisRow = {77 one: 0, two: 0, three: 0, four: 078 }79 //{x,x,0,0}80 if ((row.four === 0) && (row.three === 0)) {81 thisRow = {82 one: 0, two: 0, three: row.one, four: row.two83 }84 // {0,x,x,0}85 } else if ((row.two === 0) && (row.three === 0)) {86 thisRow = {87 one: 0, two: 0, three: row.one, four: row.four88 }89 // {0,x,0,x}90 } else if ((row.two === 0) && (row.four === 0)) {91 thisRow = {92 one: 0, two: 0, three: row.one, four: row.three93 }94 // {x,x,x,0}95 } else if (row.four === 0) {96 thisRow = {97 one: 0, two: row.one, three: row.two, four: row.three98 }99 // {x,x,0,x}100 } else if (row.three === 0) {101 thisRow = {102 one: 0, two: row.one, three: row.two, four: row.four103 }104 // {x,0,x,x}105 } else if (row.two === 0) {106 thisRow = {107 one: 0, two: row.one, three: row.three, four: row.four108 }109 } else {110 thisRow = {one: row.one, two: row.two, three: row.three, four: row.four}111 }112 return thisRow113}114export let rowSlide = (row) => {115 // console.log('input raw is')116 // console.log(row)117118//Сдвиг элементов, если есть нули119120 //Объявление исходного ВАРИАНТА(он же ОТВЕТ если никакие условия НЕ сработают)121 let thisRow = shiftRow(row)122 let returnedRow = {123 one: thisRow.one,124 two: thisRow.two,125 three: thisRow.three,126 four: thisRow.four127 }128 let compare2Num = (a, b) => {129 if (b === 0) {130 return {131 a: 0, b: a132 }133 }134 if (a === b) {135 return {a: 0, b: a * 2}136 } else137 return {a: a, b: b}138 }139 let oneAndTwo = compare2Num(thisRow.one, thisRow.two)140 let twoAndThree = compare2Num(thisRow.two, thisRow.three)141 let threeAndFour = compare2Num(thisRow.three, thisRow.four)142143144 //функция ПОПАРНОГО СРАВННЕНИЯ145 let pairwiseComparison = (oneAndTwo, threeAndFour) => {146 //Вариант когда результат содержит ноль посередине147 if (threeAndFour.a === 0) {148 returnedRow = {149 one: 0, two: oneAndTwo.a, three: oneAndTwo.b, four: threeAndFour.b150 }151 } else {152 //НЕ содежит НОЛЬ ПОСЕРЕДИНЕ153 returnedRow = {154 one: oneAndTwo.a, two: oneAndTwo.b, three: threeAndFour.a, four: threeAndFour.b155 }156 }157 // console.log('парное')158 // console.log(returnedRow)159 return returnedRow160 }161 //для исключения ситуаций: input row is {x:0;y:1;z:1;c:2}162 // output row is {x:0;y:1;z:1;c:2}163 let comparisonOfCentralMembers = (twoAndThree) => {164 // console.log('по центральным')165 if (twoAndThree.a === 0) {166 returnedRow = {one: 0, two: thisRow.one, three: twoAndThree.b, four: thisRow.four}167 } else {168 returnedRow = {one: thisRow.one, two: twoAndThree.a, three: twoAndThree.b, four: thisRow.four}169 }170 // console.log(returnedRow)171 return returnedRow172 }173 let result = ((thisRow.three !== thisRow.four) && thisRow.two === thisRow.three)174 ? comparisonOfCentralMembers(twoAndThree) : pairwiseComparison(oneAndTwo, threeAndFour)175176 return result177}178// функция для тестирования сдвига179export let testRowSlide = () => {180 for (let x = 0; x < 2; x++) {181 for (let y = 0; y < 2; y++) {182 for (let z = 0; z < 2; z++) {183 for (let c = 0; c < 2; c++) {184 let rowSlide1 = rowSlide({185 one: x,186 two: y,187 three: z,188 four: c189 })190 console.log('input row is {x:' + x + ';y:' + y + ';z:' + z + ';c:' + c + '} ' +191 ' output row is {x:' + rowSlide1.one + ';y:' + rowSlide1.two + ';z:'192 + rowSlide1.three + ';c:' + rowSlide1.four + '}')193 }194 }195 }196 }197 return "test is completed!"198}199//повороты матрицы для реализации нажатий кнопок200let getRightRowsOutMatrix = (matrix) => {201 return matrix202}203export let getLeftRowsOutMatrix = (matrix) => {204 // console.log(matrix)205 let leftMatrix = {206 oneRaw: {207 one: matrix.oneRaw.four,208 two: matrix.oneRaw.three,209 three: matrix.oneRaw.two,210 four: matrix.oneRaw.one211 },212 twoRaw: {213 one: matrix.twoRaw.four,214 two: matrix.twoRaw.three,215 three: matrix.twoRaw.two,216 four: matrix.twoRaw.one217 },218 threeRaw: {219 one: matrix.threeRaw.four,220 two: matrix.threeRaw.three,221 three: matrix.threeRaw.two,222 four: matrix.threeRaw.one223 },224 fourRaw: {225 one: matrix.fourRaw.four,226 two: matrix.fourRaw.three,227 three: matrix.fourRaw.two,228 four: matrix.fourRaw.one229230 }231 }232 return leftMatrix233}234export let getDownRowsOutMatrix = (matrix) => {235 let downMatrix = {236 oneRaw: {237 one: matrix.oneRaw.one,238 two: matrix.twoRaw.one,239 three: matrix.threeRaw.one,240 four: matrix.fourRaw.one241 },242 twoRaw: {243 one: matrix.oneRaw.two,244 two: matrix.twoRaw.two,245 three: matrix.threeRaw.two,246 four: matrix.fourRaw.two247 },248 threeRaw: {249 one: matrix.oneRaw.three,250 two: matrix.twoRaw.three,251 three: matrix.threeRaw.three,252 four: matrix.fourRaw.three253 },254 fourRaw: {255 one: matrix.oneRaw.four,256 two: matrix.twoRaw.four,257 three: matrix.threeRaw.four,258 four: matrix.fourRaw.four259260 }261 }262 return downMatrix263}264let getUnDownRowsOutMatrix = (matrix) => {265 let unDownMatrix = {266 oneRaw: {267 one: matrix.oneRaw.four,268 two: matrix.twoRaw.four,269 three: matrix.threeRaw.four,270 four: matrix.fourRaw.four271 },272 twoRaw: {273 one: matrix.oneRaw.three,274 two: matrix.twoRaw.three,275 three: matrix.threeRaw.three,276 four: matrix.fourRaw.three277 },278 threeRaw: {279 one: matrix.oneRaw.two,280 two: matrix.twoRaw.two,281 three: matrix.threeRaw.two,282 four: matrix.fourRaw.two283 },284 fourRaw: {285 one: matrix.oneRaw.one,286 two: matrix.twoRaw.one,287 three: matrix.threeRaw.one,288 four: matrix.fourRaw.one289 }290 }291 return unDownMatrix292}293export let getUpRowsOutMatrix = (matrix) => {294 let upMatrix = {295 oneRaw: {296 one: matrix.fourRaw.one,297 two: matrix.threeRaw.one,298 three: matrix.twoRaw.one,299 four: matrix.oneRaw.one300 },301 twoRaw: {302 one: matrix.fourRaw.two,303 two: matrix.threeRaw.two,304 three: matrix.twoRaw.two,305 four: matrix.oneRaw.two306 },307 threeRaw: {308 one: matrix.fourRaw.three,309 two: matrix.threeRaw.three,310 three: matrix.twoRaw.three,311 four: matrix.oneRaw.three312 },313 fourRaw: {314 one: matrix.fourRaw.four,315 two: matrix.threeRaw.four,316 three: matrix.twoRaw.four,317 four: matrix.oneRaw.four318319 }320 }321 return upMatrix322}323let getUnUpRowsOutMatrix = (matrix) => {324 let unUpMatrix = {325 oneRaw: {326 one: matrix.oneRaw.one,327 two: matrix.oneRaw.two,328 three: matrix.oneRaw.three,329 four: matrix.oneRaw.four330 },331 twoRaw: {332 one: matrix.twoRaw.one,333 two: matrix.twoRaw.two,334 three: matrix.twoRaw.three,335 four: matrix.twoRaw.four336 },337 threeRaw: {338 one: matrix.threeRaw.one,339 two: matrix.threeRaw.two,340 three: matrix.threeRaw.three,341 four: matrix.threeRaw.four342 },343 fourRaw: {344 one: matrix.fourRaw.one,345 two: matrix.fourRaw.two,346 three: matrix.fourRaw.three,347 four: matrix.fourRaw.four348349 }350 }351 return unUpMatrix352}353export let RIGHT = (state) => {354 //1)get matrix on state:355 let matrixOnState = getMatrixOnState(state);356 //2)rotate on key357 let rotateMatrix = getRightRowsOutMatrix(matrixOnState);358 //3)slide all rows in matrix359 //получить матрицу из анимаций текущей матрицы360 let oldAnimationMatrix = {361 oneRaw: shiftRowGetAnimationOnePhase(rotateMatrix.oneRaw),362 twoRaw: shiftRowGetAnimationOnePhase(rotateMatrix.twoRaw),363 threeRaw: shiftRowGetAnimationOnePhase(rotateMatrix.threeRaw),364 fourRaw: shiftRowGetAnimationOnePhase(rotateMatrix.fourRaw)365 }366 //получить матрицу из анимаций ВТОРАЯ ФАЗА!!!367 let newAnimationMatrix = {368 oneRaw: shiftRowGetAnimationTwoPhase(rotateMatrix.oneRaw),369 twoRaw: shiftRowGetAnimationTwoPhase(rotateMatrix.twoRaw),370 threeRaw: shiftRowGetAnimationTwoPhase(rotateMatrix.threeRaw),371 fourRaw: shiftRowGetAnimationTwoPhase(rotateMatrix.fourRaw)372 }373 //создать объект Текущая матрица + анимация ПЕРВЫЙ ОБЪЕКТ374 let oldMatrixOldAnimation = {375 oneRaw: {376 one: {value: rotateMatrix.oneRaw.one, anime: oldAnimationMatrix.oneRaw.one},377 two: {value: rotateMatrix.oneRaw.two, anime: oldAnimationMatrix.oneRaw.two},378 three: {value: rotateMatrix.oneRaw.three, anime: oldAnimationMatrix.oneRaw.three},379 four: {value: rotateMatrix.oneRaw.four, anime: oldAnimationMatrix.oneRaw.four}380 },381 twoRaw: {382 one: {value: rotateMatrix.twoRaw.one, anime: oldAnimationMatrix.twoRaw.one},383 two: {value: rotateMatrix.twoRaw.two, anime: oldAnimationMatrix.twoRaw.two},384 three: {value: rotateMatrix.twoRaw.three, anime: oldAnimationMatrix.twoRaw.three},385 four: {value: rotateMatrix.twoRaw.four, anime: oldAnimationMatrix.twoRaw.four}386 },387 threeRaw: {388 one: {value: rotateMatrix.threeRaw.one, anime: oldAnimationMatrix.threeRaw.one},389 two: {value: rotateMatrix.threeRaw.two, anime: oldAnimationMatrix.threeRaw.two},390 three: {value: rotateMatrix.threeRaw.three, anime: oldAnimationMatrix.threeRaw.three},391 four: {value: rotateMatrix.threeRaw.four, anime: oldAnimationMatrix.threeRaw.four}392 },393 fourRaw: {394 one: {value: rotateMatrix.fourRaw.one, anime: oldAnimationMatrix.fourRaw.one},395 two: {value: rotateMatrix.fourRaw.two, anime: oldAnimationMatrix.fourRaw.two},396 three: {value: rotateMatrix.fourRaw.three, anime: oldAnimationMatrix.fourRaw.three},397 four: {value: rotateMatrix.fourRaw.four, anime: oldAnimationMatrix.fourRaw.four}398 }399 }400401 //сдвинутая матрица402 let slideRotateMatrix = {403 oneRaw: rowSlide(rotateMatrix.oneRaw),404 twoRaw: rowSlide(rotateMatrix.twoRaw),405 threeRaw: rowSlide(rotateMatrix.threeRaw),406 fourRaw: rowSlide(rotateMatrix.fourRaw)407 }408 //4) unRotateMatrix (повернули матрицу обратно)409 let unRotateMatrix = slideRotateMatrix410 //5) return state in parent411 // создать объект новый стейт и новая анимация ВТОРОЙ ОБЪЕКТ412 //аналог gluingMatrix413 let newMatrixNewAnimation = {414 oneRaw: {415 one: {value: unRotateMatrix.oneRaw.one, anime: newAnimationMatrix.oneRaw.one},416 two: {value: unRotateMatrix.oneRaw.two, anime: newAnimationMatrix.oneRaw.two},417 three: {value: unRotateMatrix.oneRaw.three, anime: newAnimationMatrix.oneRaw.three},418 four: {value: unRotateMatrix.oneRaw.four, anime: newAnimationMatrix.oneRaw.four}419 },420 twoRaw: {421 one: {value: unRotateMatrix.twoRaw.one, anime: newAnimationMatrix.twoRaw.one},422 two: {value: unRotateMatrix.twoRaw.two, anime: newAnimationMatrix.twoRaw.two},423 three: {value: unRotateMatrix.twoRaw.three, anime: newAnimationMatrix.twoRaw.three},424 four: {value: unRotateMatrix.twoRaw.four, anime: newAnimationMatrix.twoRaw.four}425 },426 threeRaw: {427 one: {value: unRotateMatrix.threeRaw.one, anime: newAnimationMatrix.threeRaw.one},428 two: {value: unRotateMatrix.threeRaw.two, anime: newAnimationMatrix.threeRaw.two},429 three: {value: unRotateMatrix.threeRaw.three, anime: newAnimationMatrix.threeRaw.three},430 four: {value: unRotateMatrix.threeRaw.four, anime: newAnimationMatrix.threeRaw.four}431 },432 fourRaw: {433 one: {value: unRotateMatrix.fourRaw.one, anime: newAnimationMatrix.fourRaw.one},434 two: {value: unRotateMatrix.fourRaw.two, anime: newAnimationMatrix.fourRaw.two},435 three: {value: unRotateMatrix.fourRaw.three, anime: newAnimationMatrix.fourRaw.three},436 four: {value: unRotateMatrix.fourRaw.four, anime: newAnimationMatrix.fourRaw.four}437 }438 }439 // добавим третьим объектом score в возвращаемые объекты440 let score = getScoreOfMatrixes(newAnimationMatrix, matrixOnState)441 //442 let newMatrixNewAnimationPlusNewPlayingPiece = addNewPlayingPiece(newMatrixNewAnimation)443 //чтобы не добавлять лишнюю444 let notAnimations = isNullAnimation(oldAnimationMatrix)445 // console.log(oldAnimationMatrix)446 // console.log('notAnimations is = '+ notAnimations)447448 return {449 oneState: oldMatrixOldAnimation,450 twoState: notAnimations ? newMatrixNewAnimation : newMatrixNewAnimationPlusNewPlayingPiece,451 score: score452 }453}454export let LEFT = (state) => {455 //1)get matrix on state:456 let matrixOnState = getMatrixOnState(state);457 //2)rotate on key458 let rotateMatrix = getLeftRowsOutMatrix(matrixOnState);459 //получить матрицу анимаций СДВИГИ ВПРАВО460 let oldAnimationMatrixRight = getLeftRowsOutMatrix({461 oneRaw: shiftRowGetAnimationOnePhase(rotateMatrix.oneRaw),462 twoRaw: shiftRowGetAnimationOnePhase(rotateMatrix.twoRaw),463 threeRaw: shiftRowGetAnimationOnePhase(rotateMatrix.threeRaw),464 fourRaw: shiftRowGetAnimationOnePhase(rotateMatrix.fourRaw)465 })466 let x = 5;467 let oldAnimationMatrix = {468 oneRaw: {469 one: oldAnimationMatrixRight.oneRaw.one + x,470 two: oldAnimationMatrixRight.oneRaw.two + x,471 three: oldAnimationMatrixRight.oneRaw.three + x,472 four: oldAnimationMatrixRight.oneRaw.four + x473 },474 twoRaw: {475 one: oldAnimationMatrixRight.twoRaw.one + x,476 two: oldAnimationMatrixRight.twoRaw.two + x,477 three: oldAnimationMatrixRight.twoRaw.three + x,478 four: oldAnimationMatrixRight.twoRaw.four + x479 },480 threeRaw: {481 one: oldAnimationMatrixRight.threeRaw.one + x,482 two: oldAnimationMatrixRight.threeRaw.two + x,483 three: oldAnimationMatrixRight.threeRaw.three + x,484 four: oldAnimationMatrixRight.threeRaw.four + x485 },486 fourRaw: {487 one: oldAnimationMatrixRight.fourRaw.one + x,488 two: oldAnimationMatrixRight.fourRaw.two + x,489 three: oldAnimationMatrixRight.fourRaw.three + x,490 four: oldAnimationMatrixRight.fourRaw.four + x491 },492 }493 //анимация для второй фазы494 let newAnimationMatrix = getLeftRowsOutMatrix({495 oneRaw: shiftRowGetAnimationTwoPhase(rotateMatrix.oneRaw),496 twoRaw: shiftRowGetAnimationTwoPhase(rotateMatrix.twoRaw),497 threeRaw: shiftRowGetAnimationTwoPhase(rotateMatrix.threeRaw),498 fourRaw: shiftRowGetAnimationTwoPhase(rotateMatrix.fourRaw)499 })500 // получить стейт для ПЕРВОЙ ФАЗЫ501 let oldMatrixOldAnimation = gluingMatrix(matrixOnState, oldAnimationMatrix)502 //3)slide all rows in matrix503 //Сдвиг перевернутой матрицы504 let slideRotateMatrix = {505 oneRaw: rowSlide(rotateMatrix.oneRaw),506 twoRaw: rowSlide(rotateMatrix.twoRaw),507 threeRaw: rowSlide(rotateMatrix.threeRaw),508 fourRaw: rowSlide(rotateMatrix.fourRaw)509 }510 //4) unRotateMatrix511 // развернуть сдвинутую матрицу обратно 1для 2-ой фазы512 let unRotateMatrix = getLeftRowsOutMatrix(slideRotateMatrix)513 //5) return state in parent514 //получить стейт для ВТОРОЙ ФАЗЫ515 let newAnimationNewMatrix = gluingMatrix(unRotateMatrix, newAnimationMatrix)516517 let newMatrixNewAnimationPlusNewPlayingPiece = addNewPlayingPiece(newAnimationNewMatrix)518 //чтобы не добавлять лишнюю519 let notAnimations = isNullAnimation(oldAnimationMatrix)520 // console.log(oldAnimationMatrix)521 // console.log('notAnimations is = '+ notAnimations)522523 // добавим третьим объектом score в возвращаемые объекты524 let score = getScoreOfMatrixes(newAnimationMatrix, matrixOnState)525 //526 return {527 oneState: oldMatrixOldAnimation,528 twoState: notAnimations ? newAnimationNewMatrix : newMatrixNewAnimationPlusNewPlayingPiece,529 score: score530 }531}532export let UP = (state) => {533 //1)get matrix on state:534 let matrixOnState = getMatrixOnState(state);535 //2)rotate on key536 let rotateMatrix = getUpRowsOutMatrix(matrixOnState);537 // получим матрицу анимаций для первой фазы:538 let x = 9;539 let oldAnimationMatrix =540 getUnDownRowsOutMatrix({541 oneRaw: Object.fromEntries(542 Object.entries(shiftRowGetAnimationOnePhase(rotateMatrix.oneRaw))543 .map(([key, value]) => [key, value + x])544 ),545 twoRaw: Object.fromEntries(546 Object.entries(shiftRowGetAnimationOnePhase(rotateMatrix.twoRaw))547 .map(([key, value]) => [key, value + x])548 ),549 threeRaw: Object.fromEntries(550 Object.entries(shiftRowGetAnimationOnePhase(rotateMatrix.threeRaw))551 .map(([key, value]) => [key, value + x])552 ),553 fourRaw: Object.fromEntries(554 Object.entries(shiftRowGetAnimationOnePhase(rotateMatrix.fourRaw))555 .map(([key, value]) => [key, value + x])556 )557 })558 //готовый стейт для ПЕРВОЙ ФАЗЫ559 let oldMatrixOldAnimation = gluingMatrix(matrixOnState, oldAnimationMatrix)560 //3)slide all rows in matrix561 // Получим матрицу новых анимаций562 let newAnimationMatrix =563 getUnDownRowsOutMatrix({564 oneRaw: shiftRowGetAnimationTwoPhase(rotateMatrix.oneRaw),565 twoRaw: shiftRowGetAnimationTwoPhase(rotateMatrix.twoRaw),566 threeRaw: shiftRowGetAnimationTwoPhase(rotateMatrix.threeRaw),567 fourRaw: shiftRowGetAnimationTwoPhase(rotateMatrix.fourRaw)568 })569 let slideRotateMatrix = {570 oneRaw: rowSlide(rotateMatrix.oneRaw),571 twoRaw: rowSlide(rotateMatrix.twoRaw),572 threeRaw: rowSlide(rotateMatrix.threeRaw),573 fourRaw: rowSlide(rotateMatrix.fourRaw)574 }575 //получим матрицу новых значений:576 let newValuesMatrix = getUnDownRowsOutMatrix(slideRotateMatrix)577 //Получим стейт для ВТОРОЙ ФАЗЫ578 let newMatrixNewAnimation = gluingMatrix(newValuesMatrix, newAnimationMatrix)579580 let newMatrixNewAnimationPlusNewPlayingPiece = addNewPlayingPiece(newMatrixNewAnimation)581 //чтобы не добавлять лишнюю582 let notAnimations = isNullAnimation(oldAnimationMatrix)583 // console.log(oldAnimationMatrix)584 // console.log('notAnimations is = '+ notAnimations)585586 // добавим третьим объектом score в возвращаемые объекты587 let score = getScoreOfMatrixes(newAnimationMatrix, matrixOnState)588 //589590 return {591 oneState: oldMatrixOldAnimation,592 twoState: notAnimations ? newMatrixNewAnimation : newMatrixNewAnimationPlusNewPlayingPiece,593 score: score594 }595}596export let DOWN = (state) => {597 //1)get matrix on state:598 let matrixOnState = getMatrixOnState(state);599 //2)rotate on key600 let rotateMatrix = getDownRowsOutMatrix(matrixOnState);601 //3)slide all rows in matrix602 //получим матрицу анимаций сдвигов603 let x = 13604 let oldAnimationMatrix = getUnUpRowsOutMatrix(getDownRowsOutMatrix({605 oneRaw: Object.fromEntries(606 Object.entries(shiftRowGetAnimationOnePhase(rotateMatrix.oneRaw))607 .map(([key, value]) => [key, value + x])608 ),609 twoRaw: Object.fromEntries(610 Object.entries(shiftRowGetAnimationOnePhase(rotateMatrix.twoRaw))611 .map(([key, value]) => [key, value + x])612 ),613 threeRaw: Object.fromEntries(614 Object.entries(shiftRowGetAnimationOnePhase(rotateMatrix.threeRaw))615 .map(([key, value]) => [key, value + x])616 ),617 fourRaw: Object.fromEntries(618 Object.entries(shiftRowGetAnimationOnePhase(rotateMatrix.fourRaw))619 .map(([key, value]) => [key, value + x])620 )621 }))622 //получим стейт ПЕРВОЙ ФАЗЫ623 let oldMatrixOldAnimation = gluingMatrix(matrixOnState, oldAnimationMatrix)624 let slideRotateMatrix = {625 oneRaw: rowSlide(rotateMatrix.oneRaw),626 twoRaw: rowSlide(rotateMatrix.twoRaw),627 threeRaw: rowSlide(rotateMatrix.threeRaw),628 fourRaw: rowSlide(rotateMatrix.fourRaw)629 }630 //4) unRotateMatrix631 let unRotateMatrix = getDownRowsOutMatrix(slideRotateMatrix)632 //5) return state in parent633 // получим матрицу значений после преобразовния634 let newValuesMatrix = getUnUpRowsOutMatrix(unRotateMatrix)635 // получим матрицу анимаций после преобразования636 let newAnimationMatrix = getUnUpRowsOutMatrix(getDownRowsOutMatrix(637 {638 oneRaw: shiftRowGetAnimationTwoPhase(rotateMatrix.oneRaw),639 twoRaw: shiftRowGetAnimationTwoPhase(rotateMatrix.twoRaw),640 threeRaw: shiftRowGetAnimationTwoPhase(rotateMatrix.threeRaw),641 fourRaw: shiftRowGetAnimationTwoPhase(rotateMatrix.fourRaw)642 }643 ))644 //получим стей ВТОРОЙ ФАЗЫ645 let newMatrixNewAnimation = gluingMatrix(newValuesMatrix, newAnimationMatrix)646647 let newMatrixNewAnimationPlusNewPlayingPiece = addNewPlayingPiece(newMatrixNewAnimation)648 // чтобы не добалять лишнюю649 let notAnimations = isNullAnimation(oldAnimationMatrix)650 // console.log(oldAnimationMatrix)651 // console.log('notAnimations is = '+ notAnimations)652653 // добавим третьим объектом score в возвращаемые объекты654 let score = getScoreOfMatrixes(newAnimationMatrix, matrixOnState)655 //656657 return {658 oneState: oldMatrixOldAnimation,659 twoState: notAnimations ? newMatrixNewAnimation : newMatrixNewAnimationPlusNewPlayingPiece,660 score: score661 }662}663//склеиватель матрицы данных и матрицы анимаций664let gluingMatrix = (valMatrix, animeMatrix) => {665 return {666 oneRaw: {667 one: {value: valMatrix.oneRaw.one, anime: animeMatrix.oneRaw.one},668 two: {value: valMatrix.oneRaw.two, anime: animeMatrix.oneRaw.two},669 three: {value: valMatrix.oneRaw.three, anime: animeMatrix.oneRaw.three},670 four: {value: valMatrix.oneRaw.four, anime: animeMatrix.oneRaw.four}671 },672 twoRaw: {673 one: {value: valMatrix.twoRaw.one, anime: animeMatrix.twoRaw.one},674 two: {value: valMatrix.twoRaw.two, anime: animeMatrix.twoRaw.two},675 three: {value: valMatrix.twoRaw.three, anime: animeMatrix.twoRaw.three},676 four: {value: valMatrix.twoRaw.four, anime: animeMatrix.twoRaw.four}677 },678 threeRaw: {679 one: {value: valMatrix.threeRaw.one, anime: animeMatrix.threeRaw.one},680 two: {value: valMatrix.threeRaw.two, anime: animeMatrix.threeRaw.two},681 three: {value: valMatrix.threeRaw.three, anime: animeMatrix.threeRaw.three},682 four: {value: valMatrix.threeRaw.four, anime: animeMatrix.threeRaw.four}683 },684 fourRaw: {685 one: {value: valMatrix.fourRaw.one, anime: animeMatrix.fourRaw.one},686 two: {value: valMatrix.fourRaw.two, anime: animeMatrix.fourRaw.two},687 three: {value: valMatrix.fourRaw.three, anime: animeMatrix.fourRaw.three},688 four: {value: valMatrix.fourRaw.four, anime: animeMatrix.fourRaw.four}689 }690 }691}692//Для одобавлления новых плиток в процессе игры693// возвращает измененный стейт694export let addNewPlayingPiece = (state) => {695 //получить текущую матрицу696 let currentMatrix = getMatrixOnState(state);697 let getMatrixMassive = getMassiveOnMatrix(currentMatrix)698 let matrixMassive = getMatrixMassive699 //посчитаем все нули700 let counter = 0;701 let nullsIndexMassive = []702 matrixMassive.forEach((currentValue, index) => {703 if (currentValue === 0) {704 nullsIndexMassive.push(index)705 counter = counter + 1706 }707 })708 //выберем случайное число от из нулей709 let rndNull = Math.floor(Math.random() * Math.floor(counter))710 //получим индекс случайной клетки которую будем увеличивать711 let nullIndex = nullsIndexMassive[rndNull]712 // для того чтобы плитки были не только 2 но и 4 сделаем функуию которая н оснеове rnd вернет713 // 2 или 4 в пропорции 1 к 9714 let newX = () => {715 let rand = Math.floor(1 + Math.random() * 10);716 if (rand === 1) {717 return 4718 } else {719 return 2720 }721 }722 let x = newX()723 //724 // с учетом случайного числа создадим матрицу значений725 matrixMassive[nullIndex] = matrixMassive[nullIndex] + x726 let newValuesMatrix = getMatrixOnMassive(matrixMassive)727 //создание измененной матрицы анимаций объекта728 let oldAnimations = getAnimationMatrixOnState(state)729 //полчим массив из матрицы анимаций730 let animationsMatrixMassive = getMassiveOnMatrix(oldAnimations)731 //заменим в массиве анимаций необходимое значение для чего732 animationsMatrixMassive[nullIndex] = 44733// соберем обратно матрицу из массива734 let newAnimationMatrix = getMatrixOnMassive(animationsMatrixMassive)735 let returnedState = gluingMatrix(newValuesMatrix, newAnimationMatrix);736 return returnedState737}738//общий метод получения массива из матрицы739export let getMassiveOnMatrix = (matrix) => {740 let massive = [];741 Object.entries(matrix.oneRaw)742 .map(([key, value]) => massive.push(value))743 Object.entries(matrix.twoRaw)744 .map(([key, value]) => massive.push(value))745 Object.entries(matrix.threeRaw)746 .map(([key, value]) => massive.push(value))747 Object.entries(matrix.fourRaw)748 .map(([key, value]) => massive.push(value))749 return massive750}751// общий метод для получения матрицы из массива752let getMatrixOnMassive = (massive) => {753 let oneRaw = [];754 let twoRaw = [];755 let threeRaw = [];756 let fourRaw = []757 massive.forEach(758 (cV, index) => {759 if (index <= 3) {760 oneRaw.push(cV)761 } else if (index <= 7) {762 twoRaw.push(cV)763 } else if (index <= 11) {764 threeRaw.push(cV)765 } else {766 fourRaw.push(cV)767 }768 })769 let shift = (massive) => {770 let obj = {771 one: massive[0],772 two: massive[1],773 three: massive[2],774 four: massive[3],775 }776 return obj;777 }778 let returnedValuesMatrix = {779 oneRaw: shift(oneRaw),780 twoRaw: shift(twoRaw),781 threeRaw: shift(threeRaw),782 fourRaw: shift(fourRaw)783 }784 return returnedValuesMatrix785}786// вспомогательная матрица для проверки на отсутствие анимации787let isNullAnimation = (animationMatrix) => {788 // let rights = {one: 0, two: 0, three: 0, four: 0};789 // let downs = {one: , two: 0, three: 0, four: 0};790 let massive = []791 Object.entries(animationMatrix.oneRaw).map(([key, value]) =>792 massive.push(value))793 Object.entries(animationMatrix.twoRaw).map(([key, value]) =>794 massive.push(value))795 Object.entries(animationMatrix.threeRaw).map(([key, value]) =>796 massive.push(value))797 Object.entries(animationMatrix.fourRaw).map(([key, value]) =>798 massive.push(value))799 // console.log(massive)800 let result = true801 massive.forEach(x => {802 if (!((x === 9) || (x === 0) || (x === 5) || (x === 13))) {803 // console.log('не равно')804 result = false805 }806 })807 return result ...

Full Screen

Full Screen

test.js

Source:test.js Github

copy

Full Screen

...27 [12, 11, 10, 9],28 [8, 7, 6, 5],29 [4, 3, 2, 1]30 ];31 expect('rotateMatrix clockwise360deg test', matrix, rotateMatrix(matrix, 'clockwise', 360))32 expect('rotateMatrix counterClockwise360deg test', matrix, rotateMatrix(matrix, 'counterClockwise', 360))33 expect('rotateMatrix clockwise180deg test', deg180, rotateMatrix(matrix, 'clockwise', 180))34 expect('rotateMatrix counterClockwise180deg test', deg180, rotateMatrix(matrix, 'counterClockwise', 180))35 expect('rotateMatrix clockwise90deg test', clockwise90deg, rotateMatrix(matrix))36 expect('rotateMatrix counterClockwise90deg test', counterClockwise90deg, rotateMatrix(matrix, 'counterClockwise'))37 }38 )()39}...

Full Screen

Full Screen

rotate-matrix.js

Source:rotate-matrix.js Github

copy

Full Screen

2const rotateMatrix = require('../rotate-matrix');3test('rotateMatrix', function (t) {4 'use strict';5 t.plan(12);6 t.deepEqual(rotateMatrix(), []);7 t.deepEqual(rotateMatrix(''), []);8 t.deepEqual(rotateMatrix([]), []);9 t.deepEqual(rotateMatrix([10 [1,2,3],11 [5,6],12 [7,8,9]13 ]), []);14 t.deepEqual(rotateMatrix([15 [1,2,3],16 [4,5,6],17 [7,8,9]18 ]), [19 [7,4,1],20 [8,5,2],21 [9,6,3]22 ]);23 t.deepEqual(rotateMatrix([24 [1,2,3],25 [4,5,6],26 [7,8,9]27 ], 1), [28 [7,4,1],29 [8,5,2],30 [9,6,3]31 ]);32 t.deepEqual(rotateMatrix([33 [1,2,3],34 [4,5,6],35 [7,8,9]36 ], 2), [37 [9,8,7],38 [6,5,4],39 [3,2,1]40 ]);41 t.deepEqual(rotateMatrix([42 [1,2,3],43 [4,5,6],44 [7,8,9]45 ], 3), [46 [3,6,9],47 [2,5,8],48 [1,4,7]49 ]);50 t.deepEqual(rotateMatrix([51 [1,2,3],52 [4,5,6],53 [7,8,9]54 ], -3), [55 [7,4,1],56 [8,5,2],57 [9,6,3]58 ]);59 t.deepEqual(rotateMatrix([60 [1,2,3],61 [4,5,6],62 [7,8,9]63 ], 4), [64 [1,2,3],65 [4,5,6],66 [7,8,9]67 ]);68 t.deepEqual(rotateMatrix([69 [1,2,3],70 [4,5,6],71 [7,8,9]72 ], -1), [73 [3,6,9],74 [2,5,8],75 [1,4,7]76 ]);77 t.deepEqual(rotateMatrix([78 [1,2,3],79 [4,5,6],80 [7,8,9]81 ], -2), [82 [9,8,7],83 [6,5,4],84 [3,2,1]85 ]);...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('./wpt.js');2];3var rotatedMatrix = wpt.rotateMatrix(matrix);4console.log(rotatedMatrix);5function rotateMatrix(matrix) {6var rotatedMatrix = [];7for (var i = 0; i < matrix[0].length; i++) {8var row = [];9for (var j = matrix.length - 1; j >= 0; j--) {10row.push(matrix[j][i]);11}12rotatedMatrix.push(row);13}14return rotatedMatrix;15}16module.exports.rotateMatrix = rotateMatrix;17{“errorMessage”:“Cannot find module ‘./wpt.js’”,”errorType”:“Error”,”stackTrace”:[“Object.Module._extensions…js:584:10”,”Object.Module._extensions…js:596:32”,”Module.load (module.js:498:32)”,”tryModuleLoad (module.js:457:12)”,”Function.Module._load (module.js:449:3)”,”Module.require (module.js:498:17)”,”require (internal/module.js:20:19)”,”Object. (/var/task/test.js:1:15)”,”Module._compile (module.js:571:32)”,”Object.Module._extensions…js:584:10”]}

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptoolkit = require('wptoolkit');2var matrix = [[1,2,3],[4,5,6],[7,8,9]];3var result = wptoolkit.rotateMatrix(matrix,1);4var wptoolkit = require('wptoolkit');5var matrix = [[1,2,3],[4,5,6],[7,8,9]];6var result = wptoolkit.rotateMatrix(matrix,2);7var wptoolkit = require('wptoolkit');8var matrix = [[1,2,3],[4,5,6],[7,8,9]];9var result = wptoolkit.rotateMatrix(matrix,3);10var wptoolkit = require('wptoolkit');11var matrix = [[1,2,3],[4,5,6],[7,8,9]];12var result = wptoolkit.rotateMatrix(matrix,4);13var wptoolkit = require('wptoolkit');14var matrix = [[1,2,3],[4,5,6],[7,8,9]];15var result = wptoolkit.rotateMatrix(matrix,5);16var wptoolkit = require('wptoolkit');17var matrix = [[1,2,3],[4,5,6],[7,8,9]];

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2];3wpt.rotateMatrix(matrix);4var wpt = require('wpt');5];6wpt.rotateMatrix(matrix);7module.exports = function() {8 console.log('Hello from exports.js');9};10var exports = require('./exports');11exports();

Full Screen

Using AI Code Generation

copy

Full Screen

1function rotateMatrix(matrix) {2 var rotatedMatrix = [];3 for (var i = 0; i < matrix.length; i++) {4 rotatedMatrix[i] = [];5 for (var j = 0; j < matrix.length; j++) {6 rotatedMatrix[i][j] = matrix[matrix.length - 1 - j][i];7 }8 }9 return rotatedMatrix;10}11module.exports = rotateMatrix;12var wpt = require('./wpt');13var matrix = [[1,2,3],[4,5,6],[7,8,9]];14var rotatedMatrix = wpt(matrix);15console.log(rotatedMatrix);16function rotateMatrix(matrix) {17 var rotatedMatrix = [];18 for (var i = 0; i < matrix.length; i++) {19 rotatedMatrix[i] = [];20 for (var j = 0; j < matrix.length; j++) {21 rotatedMatrix[i][j] = matrix[matrix.length - 1 - j][i];22 }23 }24 return rotatedMatrix;25}26module.exports = {27};28var wpt = require('./wpt');

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2wpt.rotateMatrix(3,3,90);3var wpt = require('wpt');4wpt.rotateMatrix(3,3,180);5var wpt = require('wpt');6wpt.rotateMatrix(3,3,270);7var wpt = require('wpt');8wpt.rotateMatrix(4,4,90);9var wpt = require('wpt');10wpt.rotateMatrix(4,4,180);11var wpt = require('wpt');12wpt.rotateMatrix(4,4,270);13var wpt = require('wpt');14wpt.rotateMatrix(5,5,90);15var wpt = require('wpt');16wpt.rotateMatrix(5,5,180);17var wpt = require('wpt');18wpt.rotateMatrix(5,5,270);19var wpt = require('wpt');20wpt.rotateMatrix(6,6,90);21var wpt = require('wpt');22wpt.rotateMatrix(6,6,180);23var wpt = require('wpt');24wpt.rotateMatrix(6,6,270);25var wpt = require('wpt');26wpt.rotateMatrix(7,7,90);

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2var matrix = new wpt.Matrix();3var matrix2 = new wpt.Matrix();4var matrix3 = new wpt.Matrix();5var matrix4 = new wpt.Matrix();6var matrix5 = new wpt.Matrix();7var matrix6 = new wpt.Matrix();8var matrix7 = new wpt.Matrix();9var matrix8 = new wpt.Matrix();10var matrix9 = new wpt.Matrix();11var matrix10 = new wpt.Matrix();12var matrix11 = new wpt.Matrix();13var matrix12 = new wpt.Matrix();14var matrix13 = new wpt.Matrix();15var matrix14 = new wpt.Matrix();16var matrix15 = new wpt.Matrix();17var matrix16 = new wpt.Matrix();18var matrix17 = new wpt.Matrix();19var matrix18 = new wpt.Matrix();20var matrix19 = new wpt.Matrix();21var matrix20 = new wpt.Matrix();22var matrix21 = new wpt.Matrix();23var matrix22 = new wpt.Matrix();24var matrix23 = new wpt.Matrix();25var matrix24 = new wpt.Matrix();26var matrix25 = new wpt.Matrix();27var matrix26 = new wpt.Matrix();28var matrix27 = new wpt.Matrix();29var matrix28 = new wpt.Matrix();30var matrix29 = new wpt.Matrix();31var matrix30 = new wpt.Matrix();32var matrix31 = new wpt.Matrix();33var matrix32 = new wpt.Matrix();34var matrix33 = new wpt.Matrix();35var matrix34 = new wpt.Matrix();36var matrix35 = new wpt.Matrix();37var matrix36 = new wpt.Matrix();38var matrix37 = new wpt.Matrix();39var matrix38 = new wpt.Matrix();40var matrix39 = new wpt.Matrix();41var matrix40 = new wpt.Matrix();42var matrix41 = new wpt.Matrix();43var matrix42 = new wpt.Matrix();44var matrix43 = new wpt.Matrix();45var matrix44 = new wpt.Matrix();46var matrix45 = new wpt.Matrix();47var matrix46 = new wpt.Matrix();48var matrix47 = new wpt.Matrix();49var matrix48 = new wpt.Matrix();50var matrix49 = new wpt.Matrix();51var matrix50 = new wpt.Matrix();52var matrix51 = new wpt.Matrix();53var matrix52 = new wpt.Matrix();54var matrix53 = new wpt.Matrix();55var matrix54 = new wpt.Matrix();

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.rotateMatrix('www.webpagetest.org', function(err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 }9});10var wpt = require('webpagetest');11var wpt = new WebPageTest('www.webpagetest.org');12wpt.getLocations(function(err, data) {13 if (err) {14 console.log(err);15 } else {16 console.log(data);17 }18});19var wpt = require('webpagetest');20var wpt = new WebPageTest('www.webpagetest.org');21wpt.getTesters(function(err, data) {22 if (err) {23 console.log(err);24 } else {25 console.log(data);26 }27});28var wpt = require('webpagetest');29var wpt = new WebPageTest('www.webpagetest.org');30var options = {

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptRotateImage = require('wpt-rotate-image');2var path = require('path');3var fs = require('fs');4var image = fs.readFileSync(path.join(__dirname, 'image.jpg'));5var rotatedImage = wptRotateImage.rotateMatrix(image, 90);6fs.writeFileSync(path.join(__dirname, 'rotatedImage.jpg'), rotatedImage);

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful