fix overlay property bugs

This commit is contained in:
Sorrel Bri 2019-08-03 19:21:02 -07:00
parent 4ab3edbe78
commit c5aa20029a

View file

@ -64,7 +64,8 @@ class Point {
constructor(x, y) { constructor(x, y) {
this.pos = [ x, y ] this.pos = [ x, y ]
this.stone = 0; // this is where move placement will go 0, 1, -1 'k' this.stone = 0; // this is where move placement will go 0, 1, -1 'k'
this.overlay = 0; // this is where 'chk', 'l' this.legal;
this.chk = false; // this is where 'chk', 'l'
this.neighbors = { this.neighbors = {
top: {}, top: {},
btm: {}, btm: {},
@ -81,6 +82,7 @@ class Point {
let neighborsArr = []; let neighborsArr = [];
for ( let neighbor in this.neighbors ) { for ( let neighbor in this.neighbors ) {
let nbr = this.neighbors[neighbor]; let nbr = this.neighbors[neighbor];
console.log(nbr);
// neighbor exists it's point is stored as { rPos, cPos} // neighbor exists it's point is stored as { rPos, cPos}
if ( nbr !== null ) { if ( nbr !== null ) {
neighborsArr.push(boardState.find( val => val.pos[0] === nbr[0] && val.pos[1] === nbr[1] )) neighborsArr.push(boardState.find( val => val.pos[0] === nbr[0] && val.pos[1] === nbr[1] ))
@ -91,26 +93,22 @@ class Point {
} }
emptyNeighbor = () => { emptyNeighbor = () => {
let neighborsArr = this.checkNeighbors(); let neighborsArr = this.checkNeighbors();
console.log(neighborsArr); return !!neighborsArr.find(val => val.stone === 0 && !val.chk ); //checked
return !!neighborsArr.find(val => val.stone === 0 && val.overlay !== 'chk' );
// return true if neighboring point is empty; // return true if neighboring point is empty;
} }
checkCapture = () => { checkCapture = () => {
console.log(this.findGroup(gameState.turn * -1)) console.log(this)
console.log(!this.findGroup(gameState.turn * -1).some(val => val.emptyNeighbor())) return !this.findStone(gameState.turn * -1).some(val => val.emptyNeighbor());
return !this.findGroup(gameState.turn * -1).some(val => val.emptyNeighbor());
} }
// returns
checkGroup = () => { checkGroup = () => {
console.log(this.findGroup(gameState.turn)) return this.findStone(gameState.turn).some(val => val.emptyNeighbor());
console.log(this.findGroup(gameState.turn).some(val => val.emptyNeighbor()))
return this.findGroup(gameState.turn).some(val => val.emptyNeighbor());
// returns first neighbor of turn color that has an empty neighbor // returns first neighbor of turn color that has an empty neighbor
} }
findGroup = (stone) => { findStone = (stone) => {
return this.checkNeighbors().filter(val => { return this.checkNeighbors().filter(val => {
if ( val.stone === stone ) return val; if ( val.stone === (stone) ) return val;
}); });
//returns an array of neighbors for the value of stone //returns an array of neighbors for the value of stone
} }
@ -152,27 +150,40 @@ function hoverPreview(evt) {
let hover = [ parseInt(evt.target.closest('td').id[0]), parseInt(evt.target.closest('td').id[2]) ]; let hover = [ parseInt(evt.target.closest('td').id[0]), parseInt(evt.target.closest('td').id[2]) ];
let point = findPointFromIdx(hover); let point = findPointFromIdx(hover);
if (checkLegal(point)) { if (checkLegal(point)) {
point.overlay = 'l'; point.legal = true; // legal
render(point); render(point);
} }
} }
function checkLegal(point) { function checkLegal(point) {
clearOverlay();
// first step in logic: is point occupied, or in ko // first step in logic: is point occupied, or in ko
point.overlay = 'chk'; point.chk = true; //check
if (point.stone) return false; if (point.stone) return false;
console.log('getting here')
// if point is not empty check if neighboring point is empty // if point is not empty check if neighboring point is empty
if (!point.emptyNeighbor()) { if (!point.emptyNeighbor()) {
console.log('getting here')
//if neighboring point is not empty check if enemy group is captured //if neighboring point is not empty check if enemy group is captured
if (point.checkCapture()) return true; if ( point.findStone(gameState.turn * -1).length && point.checkCapture()) return true;
console.log('getting here')
//if neighboring point is not empty check if friendly group is alive //if neighboring point is not empty check if friendly group is alive
if (!point.checkGroup()) return false; if ( point.findStone(gameState.turn).length && point.checkGroup()) return true;
return true; console.log('getting here')
return false;
} }
console.log('getting here')
render(point); render(point);
return true; return true;
} }
function clearOverlay() { //legal and check
for (let point in boardState) {
boardState[point].chk = false;
boardState[point].legal = false;
}
}
function placeStone(evt) { function placeStone(evt) {
// checks for placement and pushes to cell // checks for placement and pushes to cell
let placement = [ parseInt(evt.target.closest('td').id[0]), parseInt(evt.target.closest('td').id[2]) ]; let placement = [ parseInt(evt.target.closest('td').id[0]), parseInt(evt.target.closest('td').id[2]) ];
@ -181,9 +192,6 @@ function placeStone(evt) {
if ( !checkLegal(point) ) return; if ( !checkLegal(point) ) return;
point.stone = gameState.turn; point.stone = gameState.turn;
gameState.turn*= -1; gameState.turn*= -1;
for (let point in boardState) {
point.overlay = '';
}
render(); render();
} }
function init() { function init() {
@ -231,7 +239,7 @@ function renderBoard() {
function renderPreview(hoverPoint) { function renderPreview(hoverPoint) {
boardState.forEach(val => { boardState.forEach(val => {
let dot = document.getElementById(`${val.pos[0]},${val.pos[1]}`).childNodes[1].childNodes[0]; let dot = document.getElementById(`${val.pos[0]},${val.pos[1]}`).childNodes[1].childNodes[0];
dot.setAttribute("data-dot", val.overlay === 'l' && val.pos[0] === hoverPoint.pos[0] && val.pos[1] === hoverPoint.pos[1] ? DOTS_DATA[gameState.turn] : DOTS_DATA[0]); dot.setAttribute("data-dot", val.legal === true && val.pos[0] === hoverPoint.pos[0] && val.pos[1] === hoverPoint.pos[1] ? DOTS_DATA[gameState.turn] : DOTS_DATA[0]);
}) })
} }