@vivekimsit/

PaleIdolizedHypotenuse

Nodejs

No description

fork
loading
Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Tile {
  constructor(shape='👽', width = Tile.Width, height = Tile.Height) {
    this.shape = shape;
    this.width = width;
    this.height = height;
  }

  render() {
    return this.shape || ' ';
  }
}
Tile.Width = 50;
Tile.Height = 50;

class Grid {
  constructor(rows, cols) {
    this.cells = [];
    this.R = rows;
    this.C = cols;
    for (let row = 0; row < rows; row++) {
      const cells = [];
      for (let col = 0; col < cols; col++) {
        cells.push({
          x: col * Tile.Width,
          y: row * Tile.Height,
          tile: new Tile()
        });
      }
      this.cells.push(cells);
    }
  }

  setTile(shape, {row, col}) {
    if (row < 0 || row >= this.R || col < 0 || col >= this.C) {
      throw new Error("Invalid tile position");
    }
    this.cells[row][col] = {
      x: col * Tile.Width,
      y: row * Tile.Height,
      tile: new Tile(shape)
    };
  }

  render() {
    const output = this.cells.map(row => this.renderRow(row));
    console.log(output.join('\n'));
  }

  renderRow(cells) {
    const output = cells.map(({ tile }) => tile.render());
    return output.join(' ');
  }
}

class Renderer {
  constructor(grid) {
    this.grid    = grid;
    this.canvas  = document.createElement('canvas');
    this.canvas.width  = '250px';
    this.canvas.height = '250px';
    this.context = this.canvas.getContext('2d');  
  }
}

const grid = new Grid(2, 2);
grid.setTile('👾', {row: 0, col: 1});
grid.setTile('💩', {row: 1, col: 0});
grid.setTile('👻', {row: 0, col: 1});
grid.setTile('🎃', {row: 1, col: 1});
//grid.render();
const renderer = new Renderer(grid);
node v10.16.0