refactor all move logic to account for group tracking
This commit is contained in:
parent
48c061919f
commit
ac1c595b70
1 changed files with 117 additions and 100 deletions
217
js/main.js
217
js/main.js
|
@ -67,6 +67,7 @@ class Point {
|
||||||
this.legal;
|
this.legal;
|
||||||
this.chk = false; // this is where 'chk', 'l'
|
this.chk = false; // this is where 'chk', 'l'
|
||||||
this.capturing = [];
|
this.capturing = [];
|
||||||
|
this.groupMembers = [];
|
||||||
this.captureChecked = false;
|
this.captureChecked = false;
|
||||||
this.neighbors = {
|
this.neighbors = {
|
||||||
top: {},
|
top: {},
|
||||||
|
@ -85,75 +86,51 @@ class Point {
|
||||||
let nbr = this.neighbors[neighbor];
|
let nbr = this.neighbors[neighbor];
|
||||||
// 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( pt => pt.pos[0] === nbr[0] && pt.pos[1] === nbr[1] ))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// returns array of existing neighbors to calling function
|
// returns array of existing neighbors to calling function
|
||||||
return neighborsArr;
|
return neighborsArr;
|
||||||
}
|
}
|
||||||
getLiberties = () => {
|
getLiberties = () => {
|
||||||
let neighborsArr = this.checkNeighbors().filter(val => val.stone === 0);
|
let neighborsArr = this.checkNeighbors().filter(pt => pt.stone === 0);
|
||||||
return neighborsArr; //checked
|
return neighborsArr; //checked
|
||||||
// return true if neighboring point is empty;
|
// return all liberties;
|
||||||
|
}
|
||||||
|
joinGroup = () => {
|
||||||
|
this.groupMembers.push(this);
|
||||||
|
let frns = this.checkNeighbors().filter(nbr => nbr.stone === this.stone);
|
||||||
|
for (let frn of frns) {
|
||||||
|
this.groupMembers.push(frn);
|
||||||
|
}
|
||||||
|
// this.groupMembers = Array.from(new Set(this.groupMembers));
|
||||||
|
for (let grpMem in this.groupMembers) {
|
||||||
|
this.groupMembers = Array.from(new Set(this.groupMembers.concat(this.groupMembers[grpMem].groupMembers)));
|
||||||
|
}
|
||||||
|
for (let grpMem in this.groupMembers) {
|
||||||
|
this.groupMembers[grpMem].groupMembers = Array.from(new Set(this.groupMembers[grpMem].groupMembers.concat(this.groupMembers)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
checkCapture = () => {
|
checkCapture = () => {
|
||||||
function checkCaptureNeighbors(opp) {
|
let tempCaptures = [];
|
||||||
let opps2 = opp.checkNeighbors().filter(nbr2 => nbr2.stone === gameState.turn * -1);
|
let opps = this.checkNeighbors().filter(nbr => nbr.stone === gameState.turn * -1
|
||||||
for (let opp2 of opps2) {
|
&& nbr.getLiberties().every(liberty => liberty === this));
|
||||||
if (opp2.chk === true) continue;
|
|
||||||
opp2.chk = true;
|
|
||||||
if (opp2.getLiberties().length > 0) {
|
|
||||||
opp.chk = false;
|
|
||||||
tempCaps = [];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
tempCaps.push(opp2);
|
|
||||||
checkCaptureNeighbors(opp2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let opps = this.checkNeighbors().filter(nbr => nbr.stone === gameState.turn * -1);
|
|
||||||
let tempCaps;
|
|
||||||
for (let opp of opps) {
|
for (let opp of opps) {
|
||||||
opp.chk = true;
|
if (opp.groupMembers.every(stone => stone.getLiberties().filter(liberty => liberty !== this).length === 0)) {
|
||||||
tempCaps = [];
|
console.log(opp);
|
||||||
if (opp.getLiberties().length > 1) {
|
console.log(opp.groupMembers);
|
||||||
tempCaps = [];
|
this.capturing = this.capturing.concat(opp.groupMembers);
|
||||||
opp.chk = false;
|
};
|
||||||
continue;
|
|
||||||
}
|
|
||||||
console.log(opp);
|
|
||||||
console.log(tempCaps);
|
|
||||||
tempCaps.push(opp);
|
|
||||||
checkCaptureNeighbors(opp);
|
|
||||||
this.capturing = this.capturing.length ? this.capturing.concat(tempCaps) : tempCaps;
|
|
||||||
}
|
}
|
||||||
console.log(this);
|
|
||||||
this.captureChecked = true;
|
|
||||||
console.log(this);
|
|
||||||
// filters duplicate points
|
|
||||||
this.capturing = Array.from(new Set(this.capturing));
|
|
||||||
return this.capturing;
|
return this.capturing;
|
||||||
}
|
}
|
||||||
|
|
||||||
checkGroup = () => { // liberty is true when called by move false when called by check Capture
|
checkGroup = () => { // liberty is true when called by move false when called by check Capture
|
||||||
function checkGroupNeighbors(frn) {
|
|
||||||
let frns2 = frn.checkNeighbors().filter(nbr2 => nbr2.stone === gameState.turn && nbr2.chk === false);
|
|
||||||
if(!frns2.length) return false;
|
|
||||||
frns2.filter(frn2 => {
|
|
||||||
if (frn2.chk === true) return false;
|
|
||||||
frn2.chk = true;
|
|
||||||
return (frn2.getLiberties().length) ? frn2 : checkGroupNeighbors(frn2);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
let frns = this.checkNeighbors().filter(nbr => nbr.stone === gameState.turn);
|
let frns = this.checkNeighbors().filter(nbr => nbr.stone === gameState.turn);
|
||||||
return frns.filter(frn => {
|
for (let frn in frns) {
|
||||||
if(frn.chk === true) return false;
|
if (frns[frn].getLiberties().filter(liberty => liberty !== this).length) return true;
|
||||||
frn.check = true;
|
return false;
|
||||||
if (frn.getLiberties().length > 1) return frn;
|
}
|
||||||
checkGroupNeighbors(frn);
|
}
|
||||||
})
|
|
||||||
// returns all friendly neighbors that have an empty neighbor, recursively
|
|
||||||
}
|
|
||||||
findStone = (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;
|
||||||
|
@ -252,11 +229,11 @@ function checkLegal(point) {
|
||||||
point.chk = true; //check
|
point.chk = true; //check
|
||||||
if (point.stone) return false;
|
if (point.stone) return false;
|
||||||
// if point is not empty check if liberties
|
// if point is not empty check if liberties
|
||||||
if (point.getLiberties().length < 2) {
|
if (point.getLiberties().length < 1) {
|
||||||
//if no liberties check if enemy group has liberties
|
//if no liberties check if enemy group has liberties
|
||||||
if ( point.checkCapture() ) return true;
|
if ( point.checkCapture().length ) return true;
|
||||||
//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().length ) return true;
|
if (point.checkGroup()) return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -272,12 +249,12 @@ function clearOverlay() { //legal and check
|
||||||
|
|
||||||
function resolveCaptures(point) {
|
function resolveCaptures(point) {
|
||||||
if(!point.capturing.length && !point.captureChecked) {
|
if(!point.capturing.length && !point.captureChecked) {
|
||||||
console.log('check fail');
|
|
||||||
point.checkCapture();
|
point.checkCapture();
|
||||||
}
|
}
|
||||||
if(point.capturing.length) {
|
if(point.capturing.length) {
|
||||||
point.capturing.forEach(cap => {
|
point.capturing.forEach(cap => {
|
||||||
gameState.playerState[gameState.turn > 0 ? 'bCaptures' : 'wCaptures']++;
|
gameState.playerState[gameState.turn > 0 ? 'bCaptures' : 'wCaptures']++;
|
||||||
|
cap.groupMembers = [];
|
||||||
cap.stone = 0;
|
cap.stone = 0;
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -298,6 +275,7 @@ function clickPlaceStone(evt) {
|
||||||
clearPass();
|
clearPass();
|
||||||
resolveCaptures(point);
|
resolveCaptures(point);
|
||||||
point.stone = gameState.turn;
|
point.stone = gameState.turn;
|
||||||
|
point.joinGroup();
|
||||||
clearCaptures();
|
clearCaptures();
|
||||||
gameState.gameRecord.push(`${STONES_DATA[gameState.turn]}: ${point.pos}`)
|
gameState.gameRecord.push(`${STONES_DATA[gameState.turn]}: ${point.pos}`)
|
||||||
gameState.turn*= -1;
|
gameState.turn*= -1;
|
||||||
|
@ -353,48 +331,87 @@ function init() {
|
||||||
// testing board state for moves at [32]
|
// testing board state for moves at [32]
|
||||||
gameState.turn = 1;
|
gameState.turn = 1;
|
||||||
|
|
||||||
boardState[1].stone = 1;
|
// boardState[1].stone = 1;
|
||||||
boardState[4].stone = 1;
|
// boardState[1].joinGroup();
|
||||||
boardState[5].stone = 1;
|
// boardState[4].stone = 1;
|
||||||
boardState[6].stone = 1;
|
// boardState[4].joinGroup();
|
||||||
boardState[9].stone = 1;
|
// boardState[5].stone = 1;
|
||||||
boardState[10].stone = -1;
|
// boardState[5].joinGroup();
|
||||||
boardState[11].stone = 1;
|
// boardState[6].stone = 1;
|
||||||
boardState[13].stone = -1;
|
// boardState[6].joinGroup();
|
||||||
boardState[14].stone = -1;
|
// boardState[9].stone = 1;
|
||||||
boardState[15].stone = -1;
|
// boardState[9].joinGroup();
|
||||||
boardState[16].stone = 1;
|
// boardState[10].stone = -1;
|
||||||
boardState[18].stone = 1;
|
// boardState[10].joinGroup();
|
||||||
boardState[19].stone = -1;
|
// boardState[11].stone = 1;
|
||||||
boardState[20].stone = 1;
|
// boardState[11].joinGroup();
|
||||||
boardState[21].stone = 1;
|
// boardState[13].stone = -1;
|
||||||
boardState[22].stone = 1;
|
// boardState[13].joinGroup();
|
||||||
boardState[23].stone = -1;
|
// boardState[14].stone = -1;
|
||||||
boardState[24].stone = 1;
|
// boardState[14].joinGroup();
|
||||||
boardState[25].stone = 1;
|
// boardState[15].stone = -1;
|
||||||
boardState[27].stone = 1;
|
// boardState[15].joinGroup();
|
||||||
boardState[28].stone = -1;
|
// boardState[16].stone = 1;
|
||||||
boardState[29].stone = -1;
|
// boardState[16].joinGroup();
|
||||||
boardState[30].stone = -1;
|
// boardState[18].stone = 1;
|
||||||
boardState[31].stone = -1;
|
// boardState[18].joinGroup();
|
||||||
boardState[33].stone = -1;
|
// boardState[19].stone = -1;
|
||||||
boardState[34].stone = 1;
|
// boardState[19].joinGroup();
|
||||||
boardState[36].stone = 1;
|
// boardState[20].stone = 1;
|
||||||
boardState[37].stone = -1;
|
// boardState[20].joinGroup();
|
||||||
boardState[38].stone = 1;
|
// boardState[21].stone = 1;
|
||||||
boardState[39].stone = 1;
|
// boardState[21].joinGroup();
|
||||||
boardState[40].stone = 1;
|
// boardState[22].stone = 1;
|
||||||
boardState[41].stone = -1;
|
// boardState[22].joinGroup();
|
||||||
boardState[42].stone = 1;
|
// boardState[23].stone = -1;
|
||||||
boardState[46].stone = 1;
|
// boardState[23].joinGroup();
|
||||||
boardState[56].stone = 1;
|
// boardState[24].stone = 1;
|
||||||
boardState[57].stone = 1;
|
// boardState[24].joinGroup();
|
||||||
boardState[65].stone = -1;
|
// boardState[25].stone = 1;
|
||||||
boardState[66].stone = -1;
|
// boardState[25].joinGroup();
|
||||||
boardState[67].stone = 1;
|
// boardState[27].stone = 1;
|
||||||
boardState[74].stone = -1;
|
// boardState[27].joinGroup();
|
||||||
boardState[75].stone = -1;
|
// boardState[28].stone = -1;
|
||||||
boardState[76].stone = 1;
|
// boardState[28].joinGroup();
|
||||||
|
// boardState[29].stone = -1;
|
||||||
|
// boardState[29].joinGroup();
|
||||||
|
// boardState[30].stone = -1;
|
||||||
|
// boardState[30].joinGroup();
|
||||||
|
// boardState[31].stone = -1;
|
||||||
|
// boardState[31].joinGroup();
|
||||||
|
// boardState[33].stone = -1;
|
||||||
|
// boardState[33].joinGroup();
|
||||||
|
// boardState[34].stone = 1;
|
||||||
|
// boardState[34].joinGroup();
|
||||||
|
// boardState[36].stone = 1;
|
||||||
|
// boardState[36].joinGroup();
|
||||||
|
// boardState[37].stone = -1;
|
||||||
|
// boardState[37].joinGroup();
|
||||||
|
// boardState[38].stone = 1;
|
||||||
|
// boardState[38].joinGroup();
|
||||||
|
// boardState[39].stone = 1;
|
||||||
|
// boardState[39].joinGroup();
|
||||||
|
// boardState[40].stone = 1;
|
||||||
|
// boardState[40].joinGroup();
|
||||||
|
// boardState[41].stone = -1;
|
||||||
|
// boardState[41].joinGroup();
|
||||||
|
// boardState[42].stone = 1;
|
||||||
|
// boardState[42].joinGroup();
|
||||||
|
// boardState[46].stone = 1;
|
||||||
|
// boardState[46].joinGroup();
|
||||||
|
// boardState[56].stone = 1;
|
||||||
|
// boardState[56].joinGroup();
|
||||||
|
// boardState[57].stone = 1;
|
||||||
|
// boardState[57].joinGroup();
|
||||||
|
// boardState[65].stone = -1;
|
||||||
|
// boardState[65].joinGroup();
|
||||||
|
// boardState[66].stone = -1;
|
||||||
|
// boardState[66].joinGroup();
|
||||||
|
// boardState[67].stone = 1;
|
||||||
|
// boardState[67].joinGroup();
|
||||||
|
// boardState[74].stone = -1;
|
||||||
|
// boardState[75].stone = -1;
|
||||||
|
// boardState[76].stone = 1;
|
||||||
|
|
||||||
|
|
||||||
clearCaptures();
|
clearCaptures();
|
||||||
|
|
Loading…
Reference in a new issue