|
'use strict'; |
|
|
|
const constants = require('./constants'); |
|
|
|
module.exports = class Point { |
|
constructor(x, y, game){ |
|
this.x = x || 0; |
|
this.y = y || 0; |
|
this.player = 0; |
|
this.game = game; |
|
|
|
// initialising direction the checks and getters |
|
constants.directions.forEach(dirString => { |
|
Object.defineProperty(this, 'check_' + dirString, { |
|
get: function(){ |
|
const dir = this['_' + dirString]; |
|
if(this.player && dir.allowed){ |
|
const neighbour = this.game.boardPoint(dir.x, dir.y); |
|
if(neighbour.player && neighbour.player == this.player) |
|
return [neighbour].concat(neighbour['check_' + dirString]); |
|
else |
|
return []; |
|
} else { |
|
return []; |
|
} |
|
} |
|
}); |
|
Object.defineProperty(this, dirString, { |
|
get: function(){ |
|
const dir = this['_' + dirString]; |
|
return dir.allowed ? this.game.boardPoint(dir.x, dir.y) : null; |
|
} |
|
}); |
|
}); |
|
} |
|
|
|
get logger(){ |
|
return `pt: (${this.x}, ${this.y}) - ${this.player}`; |
|
} |
|
|
|
get potential(){ |
|
const vertical = Array.prototype.concat(this.check_n, this, this.check_s), |
|
horizontal = Array.prototype.concat(this.check_e, this, this.check_w), |
|
diagonal_nw = Array.prototype.concat(this.check_nw, this, this.check_se), |
|
diagonal_ne = Array.prototype.concat(this.check_ne, this, this.check_sw), |
|
max = [vertical, horizontal, diagonal_nw, diagonal_ne] |
|
.reduce((mem, dir) => mem.length > dir.length ? mem : dir); |
|
|
|
return { |
|
vertical, |
|
horizontal, |
|
diagonal_ne, |
|
diagonal_nw, |
|
max, |
|
}; |
|
} |
|
|
|
posEquals(point){ |
|
return point.x == this.x && point.y == this.y; |
|
} |
|
|
|
get _n(){ const x = this.x, y = this.y; return { allowed: y - 1 >= 0, x: x, y: y - 1}; } // north |
|
get _e(){ const x = this.x, y = this.y; return { allowed: x - 1 >= 0, x: x - 1, y: y}; } // east |
|
get _w(){ const x = this.x, y = this.y; return { allowed: x + 1 < this.game.length, x: x + 1, y: y}; } // west |
|
get _s(){ const x = this.x, y = this.y; return { allowed: y + 1 < this.game.height, x: x, y: y + 1}; } // south |
|
|
|
get _ne(){ // north-east |
|
const x = this.x, y = this.y; |
|
const north = this._n, east = this._e; |
|
return {allowed: north.allowed && east.allowed, x: east.x, y: north.y}; |
|
} |
|
get _nw(){ // north-west |
|
const x = this.x, y = this.y; |
|
const north = this._n, west = this._w; |
|
return { allowed: north.allowed && west.allowed, x: west.x, y: north.y}; |
|
} |
|
get _se(){ // south-east |
|
const x = this.x, y = this.y; |
|
const south = this._s, east = this._e; |
|
return { allowed: south.allowed && east.allowed, x: east.x, y: south.y}; |
|
} |
|
get _sw(){ // south-west |
|
const x = this.x, y = this.y; |
|
const south = this._s, west = this._w; |
|
return { allowed: south.allowed && west.allowed, x: west.x, y: south.y}; |
|
} |
|
} |