fix capture group bug

This commit is contained in:
Sorrel Bri 2019-08-05 13:56:54 -07:00
parent d23eb24c84
commit 75b2feca43

View file

@ -98,27 +98,32 @@ class Point {
checkCapture = () => { checkCapture = () => {
function checkCaptureNeighbors(opp) { function checkCaptureNeighbors(opp) {
let opps2 = opp.checkNeighbors().filter(nbr2 => nbr2.stone === gameState.turn * -1); let opps2 = opp.checkNeighbors().filter(nbr2 => nbr2.stone === gameState.turn * -1);
opps2.forEach(opp2 => { for (let opp2 of opps2) {
if (opp2.chk === true) return false; if (opp2.chk === true) continue;
opp2.chk = true; opp2.chk = true;
if (opp2.getLiberties().length > 0) {
tempCaps = [];
continue;
}
tempCaps.push(opp2); tempCaps.push(opp2);
if (opp2.getLiberties().length) return false;
checkCaptureNeighbors(opp2); checkCaptureNeighbors(opp2);
}) }
} }
let opps = this.checkNeighbors().filter(nbr => nbr.stone === gameState.turn * -1); let opps = this.checkNeighbors().filter(nbr => nbr.stone === gameState.turn * -1);
let tempCaps; let tempCaps;
opps.forEach(opp => { for (let opp of opps) {
opp.chk = true;
tempCaps = []; tempCaps = [];
if (opp.chk === true) return; if (opp.getLiberties().length > 1) {
opp.check = true; tempCaps = [];
continue;
}
console.log(opp);
console.log(tempCaps);
tempCaps.push(opp); tempCaps.push(opp);
if (opp.getLiberties().length > 1) return;
checkCaptureNeighbors(opp); checkCaptureNeighbors(opp);
console.log(this.capturing); this.capturing = this.capturing.length ? this.capturing.concat(tempCaps) : tempCaps;
}) }
this.capturing = this.capturing ? this.capturing.concat(tempCaps)
: tempCaps;
// filters duplicate points // filters duplicate points
this.capturing = Array.from(new Set(this.capturing)); this.capturing = Array.from(new Set(this.capturing));
return this.capturing; return this.capturing;
@ -126,9 +131,7 @@ class Point {
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) { function checkGroupNeighbors(frn) {
console.log(frn);
let frns2 = frn.checkNeighbors().filter(nbr2 => nbr2.stone === gameState.turn && nbr2.chk === false); let frns2 = frn.checkNeighbors().filter(nbr2 => nbr2.stone === gameState.turn && nbr2.chk === false);
console.log(frns2);
if(!frns2.length) return false; if(!frns2.length) return false;
frns2.filter(frn2 => { frns2.filter(frn2 => {
if (frn2.chk === true) return false; if (frn2.chk === true) return false;
@ -136,12 +139,10 @@ class Point {
return (frn2.getLiberties().length) ? frn2 : checkGroupNeighbors(frn2); return (frn2.getLiberties().length) ? frn2 : checkGroupNeighbors(frn2);
}); });
} }
console.log('checking group')
let frns = this.checkNeighbors().filter(nbr => nbr.stone === gameState.turn); let frns = this.checkNeighbors().filter(nbr => nbr.stone === gameState.turn);
return frns.filter(frn => { return frns.filter(frn => {
if(frn.chk === true) return false; if(frn.chk === true) return false;
frn.check = true; frn.check = true;
console.log(frn);
if (frn.getLiberties().length > 1) return frn; if (frn.getLiberties().length > 1) return frn;
checkGroupNeighbors(frn); checkGroupNeighbors(frn);
}) })
@ -176,7 +177,7 @@ const blackCaps = document.getElementById("black-caps");
// ::hover-over on board to preview move (with legal move logic) // ::hover-over on board to preview move (with legal move logic)
document.getElementById('board').addEventListener('mousemove', hoverPreview); document.getElementById('board').addEventListener('mousemove', hoverPreview);
// click on board to play move // click on board to play move
document.getElementById('board').addEventListener('click', placeStone); document.getElementById('board').addEventListener('click', clickPlaceStone);
// ::hover-over on either bowl for pass, one-level undo options (CSS implementation) // ::hover-over on either bowl for pass, one-level undo options (CSS implementation)
// click on menu items // click on menu items
// click on kifu to display game menu // click on kifu to display game menu
@ -202,21 +203,15 @@ function checkLegal(point) {
clearOverlay(); clearOverlay();
// first step in logic: is point occupied, or in ko // first step in logic: is point occupied, or in ko
point.chk = true; //check point.chk = true; //check
console.log(!!point.stone);
if (point.stone) return false; if (point.stone) return false;
console.log('getting here')
// 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 < 2) {
console.log('getting here')
//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() ) return true;
console.log(point.checkGroup())
//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().length ) return true;
console.log('getting here')
return false; return false;
} }
// console.log('getting here')
return true; return true;
} }
@ -229,8 +224,6 @@ function clearOverlay() { //legal and check
} }
function resolveCaptures(point) { function resolveCaptures(point) {
console.log('getting here');
point.checkCapture();
if(point.capturing.filter(cap => cap).length ) { if(point.capturing.filter(cap => cap).length ) {
point.capturing.forEach(cap => { point.capturing.forEach(cap => {
gameState.playerState[point.stone > 0 ? 'bCaptures' : 'wCaptures']++; gameState.playerState[point.stone > 0 ? 'bCaptures' : 'wCaptures']++;
@ -240,11 +233,10 @@ function resolveCaptures(point) {
} }
function checkKo(cap, point) { function checkKo(cap, point) {
console.log(cap);
return cap.findStone(gameState.turn).length === 4 && point.getLiberties().length;//determines ponnuki return cap.findStone(gameState.turn).length === 4 && point.getLiberties().length;//determines ponnuki
} }
function placeStone(evt) { function clickPlaceStone(evt) {
evt.stopPropagation(); evt.stopPropagation();
// 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]) ];
@ -345,7 +337,6 @@ function init() {
}; };
function render(hoverPoint) { function render(hoverPoint) {
console.log(gameState.gameRecord)
gameState.gameRecord.length? renderTurn() : renderFirstTurn(); gameState.gameRecord.length? renderTurn() : renderFirstTurn();
renderBoard(); renderBoard();
renderCaps(); renderCaps();