Board Game

Board Game

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.js"></script>

Babel + JSX

class GameBoard {
  constructor(height, width, tileSize = 0, filledCount = 0) {
		filledCount = (width / tileSize * height) * ((filledCount === 0) ? Math.ceil(tileSize/3) : filledCount);
    this.height = height;
    this.width = width;
    this.tileSize = tileSize;
    this.board = tileSize === 0 ? this.genEmptyBoard(height, width) :  this.genTiledBoard (height, width, tileSize, filledCount);
    this.tileDictionary = {};
  }
  
  genEmptyBoard (height, width) {
		return new Array(width).fill(new Array (height).fill(0))
	}
     
  log(board = '') {
  	board = board ? board : this.board;
    const width = board[0].length;
    let str = '	';
    for (let ctr = 0; ctr < width; ctr++){
      str+= '	' + ctr + '	';
    }
    console.log(str)
    for (let xIndex in board) {
        let row = xIndex + ':	|	'
        for (let y of board[xIndex]) {
          row += y + '	|	'
        }
        console.log(row)
    }
  }

  getSquare = (x, y) => this.board[x][y];
  
	getTileCoord = (x, y) => {
	  return { x: (x - (x % this.tileSize)), y: (y - (y % this.tileSize)) }
  };
    
  getLeftCoord({x, y}, steps = 1) {
  	const nextIndex = (x - steps > 0) ? x - steps : 0
    return {x: nextIndex, y}
  }
  getRightCoord({x, y}, steps = 1) {
  	const nextIndex = (x + steps < this.width) ? x + steps : this.width - 1
    return {x: nextIndex, y};

  }
  getUpCoord({x, y}, steps = 1) {
  	const nextIndex = (y - steps > 0) ? y - steps : 0;
    return {x, y: nextIndex}
  }
  getDownCoord({x, y}, steps = 1) {
  	const nextIndex = (y + steps < this.height) ? y + steps : this.height;
    return {x, y: nextIndex}
  }

  genTiledBoard (height, width, tileSize, filledCount) {
    let squares = _.shuffle(new Array (height * width - filledCount).fill(0).concat(new Array (filledCount).fill(1)));
    var board = [];
    while(squares.length) board.push(squares.splice(0, width));
    return board;
  }  
}

const board = new GameBoard(10, 10, 2);

const randomCoord = () => {
	return {x:...