tetris-fumen
Version:
Fumen parser for tetris
155 lines (154 loc) • 5.22 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Mino = exports.Field = void 0;
var inner_field_1 = require("./inner_field");
var defines_1 = require("./defines");
function toMino(operationOrMino) {
return operationOrMino instanceof Mino ? operationOrMino.copy() : Mino.from(operationOrMino);
}
var Field = /** @class */ (function () {
function Field(field) {
this.field = field;
}
Field.create = function (field, garbage) {
return new Field(new inner_field_1.InnerField({
field: field !== undefined ? inner_field_1.PlayField.load(field) : undefined,
garbage: garbage !== undefined ? inner_field_1.PlayField.loadMinify(garbage) : undefined,
}));
};
Field.prototype.canFill = function (operation) {
if (operation === undefined) {
return true;
}
var mino = toMino(operation);
return this.field.canFillAll(mino.positions());
};
Field.prototype.canLock = function (operation) {
if (operation === undefined) {
return true;
}
if (!this.canFill(operation)) {
return false;
}
// Check on the ground
return !this.canFill(__assign(__assign({}, operation), { y: operation.y - 1 }));
};
Field.prototype.fill = function (operation, force) {
if (force === void 0) { force = false; }
if (operation === undefined) {
return undefined;
}
var mino = toMino(operation);
if (!force && !this.canFill(mino)) {
throw Error('Cannot fill piece on field');
}
this.field.fillAll(mino.positions(), (0, defines_1.parsePiece)(mino.type));
return mino;
};
Field.prototype.put = function (operation) {
if (operation === undefined) {
return undefined;
}
var mino = toMino(operation);
for (; 0 <= mino.y; mino.y -= 1) {
if (!this.canLock(mino)) {
continue;
}
this.fill(mino);
return mino;
}
throw Error('Cannot put piece on field');
};
Field.prototype.clearLine = function () {
this.field.clearLine();
};
Field.prototype.at = function (x, y) {
return (0, defines_1.parsePieceName)(this.field.getNumberAt(x, y));
};
Field.prototype.set = function (x, y, type) {
this.field.setNumberAt(x, y, (0, defines_1.parsePiece)(type));
};
Field.prototype.copy = function () {
return new Field(this.field.copy());
};
Field.prototype.str = function (option) {
if (option === void 0) { option = {}; }
var skip = option.reduced !== undefined ? option.reduced : true;
var separator = option.separator !== undefined ? option.separator : '\n';
var minY = option.garbage === undefined || option.garbage ? -1 : 0;
var output = '';
for (var y = 22; minY <= y; y -= 1) {
var line = '';
for (var x = 0; x < 10; x += 1) {
line += this.at(x, y);
}
if (skip && line === '__________') {
continue;
}
skip = false;
output += line;
if (y !== minY) {
output += separator;
}
}
return output;
};
return Field;
}());
exports.Field = Field;
var Mino = /** @class */ (function () {
function Mino(type, rotation, x, y) {
this.type = type;
this.rotation = rotation;
this.x = x;
this.y = y;
}
Mino.from = function (operation) {
return new Mino(operation.type, operation.rotation, operation.x, operation.y);
};
Mino.prototype.positions = function () {
return (0, inner_field_1.getBlockXYs)((0, defines_1.parsePiece)(this.type), (0, defines_1.parseRotation)(this.rotation), this.x, this.y).sort(function (a, b) {
if (a.y === b.y) {
return a.x - b.x;
}
return a.y - b.y;
});
};
Mino.prototype.operation = function () {
return {
type: this.type,
rotation: this.rotation,
x: this.x,
y: this.y,
};
};
Mino.prototype.isValid = function () {
try {
(0, defines_1.parsePiece)(this.type);
(0, defines_1.parseRotation)(this.rotation);
}
catch (e) {
return false;
}
return this.positions().every(function (_a) {
var x = _a.x, y = _a.y;
return 0 <= x && x < 10 && 0 <= y && y < 23;
});
};
Mino.prototype.copy = function () {
return new Mino(this.type, this.rotation, this.x, this.y);
};
return Mino;
}());
exports.Mino = Mino;