1/jekyll_site/js/tetris-figures.js
2023-12-17 07:55:25 +03:00

42 lines
1.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// © Головин Г.Г., Набор фигур, 2023
'use strict';
// фигуры тетрамино
const FIGURE=[
[[1,1],[1,0],[1,0]],
[[2,2],[0,2],[0,2]],
[[0,3],[3,3],[3,0]],
[[4,0],[4,4],[0,4]],
[[5,5],[5,5]],
[[6],[6],[6],[6]],
[[7,0],[7,7],[7,0]]];
// полный набор фигур
FIGURE.set = function() {
let set = [];
for (let i=0; i<FIGURE.length; i++)
set.push(new Figure(i));
return set;
}
// класс-фабрика для фигур
class Figure {
// создать новый объект
constructor(num) {
// num - номер в диапазоне [0..6]
if (num<0||num>6) return undefined;
this.type=num+1;
this.shape=FIGURE[num];
}
// копия текущего объекта
clone() {
return new Figure(this.type-1);
}
// поворот по часовой стрелке
rotate() {
let nShape = [], shape = this.shape;
for (let y = 0; y < shape.length; y++)
for (let x = 0; x < shape[y].length; x++) {
if (nShape[x]==undefined) nShape[x] = [];
nShape[x][shape.length-y-1] = shape[y][x];
}
this.shape=nShape;
}
};