import * as fs from "fs"; function load(filename) { let data = fs.readFileSync(filename, "utf8").trim(); const l = data.length; let i = 0; return { next: function () { const c = data[i]; i = (i + 1) % l; return c; } }; } const shapes = [ [0b0011110], [0b0001000, 0b0011100, 0b0001000], [0b0000100, 0b0000100, 0b0011100], [0b0010000, 0b0010000, 0b0010000, 0b0010000], [0b0011000, 0b0011000], ]; const shapes_len = shapes.length; class Shape { constructor(shape) { this.shape = shape; this.length = shape.length; } fall(field) { // returns true if the shape fell without colliding for (let i=0; i": tmp = this.shape.map(line => { if (line & 0b1) { // we touch the right edge already collision = true; } return line >>> 1; }); break; case "<": tmp = this.shape.map(line => { if (line & 0b1000000) { // we touch the left edge already collision = true; } return line << 1; }); break; default: throw "invalid direction character in shape shift: " + direction; } if (collision) { return; } for (let i=0; i { console.log( line & 0b1000000 ? "#" : ".", line & 0b0100000 ? "#" : ".", line & 0b0010000 ? "#" : ".", line & 0b0001000 ? "#" : ".", line & 0b0000100 ? "#" : ".", line & 0b0000010 ? "#" : ".", line & 0b0000001 ? "#" : ".", ); }); console.log("======="); } } class Shaper { constructor() { this.index = 0; } next(field) { const s = new Shape(shapes[this.index]); field.accomodate(shapes[this.index].length + 3); this.index = (this.index + 1) % shapes_len; return s; } } class Field { constructor() { this.data = []; this.offset = 0; } accomodate(n) { this.offset = 0; for(let i=0; i { console.log( line & 0b1000000 ? "#" : ".", line & 0b0100000 ? "#" : ".", line & 0b0010000 ? "#" : ".", line & 0b0001000 ? "#" : ".", line & 0b0000100 ? "#" : ".", line & 0b0000010 ? "#" : ".", line & 0b0000001 ? "#" : ".", ); }); console.log("-------"); } solidify(shape) { for(let i=0; i