##### How to detect if a group of pieces is surrounded in GO game?

Question: How to check if chess pieces are being surrounded in Go?
Rule: If the local group of chess pieces does not have any Liberty(The connecting space that has not been occupied by opponent’s piece), this one or this group of chess pieces would be removed.
This is the rule of Gconnect which uses the same capture method as Go. Could anyone please shed some light on what to do capture detection in JavaScript? Thanks!
This is my code, which does not work:
```javascript
let grid;
let rows;
let cols;
//Add a state machine;
let whosTurn;
let winner;
let bKoList;
let wKoList;
//Testing
let wStones;
let wDead;
function setup() {
restart();
}
function mouseClicked() {
if(!winner){
let x = findNearestIndex(mouseX);
let y = findNearestIndex(mouseY);
//Check ko list
if(whosTurn == "b"){
for (let i = 0; i < bKoList.length; i++) {
if(x == bKoList[i].x && y == bKoList[i].y){
showWarnning();
return;
}
}
}
//Track which intersections are occupied
if(grid[x][y] == "empty") {
//SetStoneFaction
grid[x][y] = whosTurn;
//findLiberty();
//clearGroup();
//removeDead();
//Testing
findStoneGroup();
clearAndDrawBoard();
drawStone();
checkWin();
//refresh ko List
if(whosTurn=="b"){
}
//Track whose turn it is
//Set next player
if(whosTurn == "b")
whosTurn = "w";
else whosTurn = "b";
} else {
console.log(grid[x][y]);
}
} else {
restart();
}
showHint();
}
//Detect a vertical or horizontal win
function checkWin() {
let bStones;
let wStones;
//Check vertical
for (let i = 0; i < grid.length; i++) {
bStones = 0;
wStones = 0;
for (let j = 0; j < grid.length; j++) {
if(grid[i][j] == "b"){
bStones ++;
if(bStones == rows){
console.log("Black Won");
winner = "black";
}
} else if(grid[i][j] == "w"){
wStones ++;
if(wStones == rows){
console.log("White Won");
winner = "white";
}
}
}
}
//Check horizontal
for (let i = 0; i < grid.length; i++) {
bStones = 0;
wStones = 0;
for (let j = 0; j < grid.length; j++) {
if(grid[j][i] == "b"){
bStones ++;
if(bStones == cols){
console.log("Black Won");
winner = "black";
}
} else if(grid[j][i] == "w"){
wStones ++;
if(wStones == cols){
console.log("White Won");
winner = "white";
}
}
}
}
}
//Add "game over" and "game restart" states
function restart() {
rows = 6;
cols = 6;
grid = [];
whosTurn = "b";
winner = null;
bKoList = [];
wKoList = [];
for (let i = 0; i < cols; i++) {
grid[i] = [];
for (let j = 0; j < rows; j++) {
grid[i][j] = "empty";
}
}
createCanvas(600, 700);
clearAndDrawBoard();
showHint();
}
function stone(x, y, f) {
this.x = x;
this.y = y;
this.faction = f;
}
function findStoneGroup() {
wStones = [];
wDead = [];
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
if(grid[i][j] == "w"){
wStones.push(new stone(i,j, "w"));
wDead.push(new stone(i,j, "w"));
}
}
}
for (let i = 0; i < wStones.length; i++) {
if(grid[wStones[i].x][wStones[i].y - 1] == "empty"
|| grid[wStones[i].x][wStones[i].y + 1] == "empty"
|| grid[wStones[i].x - 1][wStones[i].y] == "empty"
|| grid[wStones[i].x + 1][wStones[i].y] == "empty" ){
wDead.pop(wStones[i]);
}
}
console.log(wDead.length);
}
function findLiberty() {
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
if(grid[i][j] == "w" || grid[i][j] == "wGroup" ) {
if(j>0) {
if(grid[i][j-1] == "w") {
grid[i][j-1] = "wGroup";
grid[i][j] = "wGroup";
}
}
if(j<5) {
if(grid[i][j+1] == "w") {
grid[i][j+1] = "wGroup";
grid[i][j] = "wGroup";
}
}
if(i>0) {
if(grid[i-1][j] == "w") {
grid[i-1][j] = "wGroup";
grid[i][j] = "wGroup";
}
}
if(i<5) {
if(grid[i+1][j] == "w") {
grid[i+1][j] = "wGroup";
grid[i][j] = "wGroup";
}
}
}
}
}
}
function clearGroup(){
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
if(grid[i][j] != "empty"){
if(i > 0 && i < cols - 1 && j > 0 && j < rows - 1) {
if(grid[i][j-1] == "empty"
|| grid[i][j+1] == "empty"
|| grid[i-1][j] == "empty"
|| grid[i+1][j] == "empty" ){
if(grid[i][j] == "wGroup" || grid[i][j] == "w"){
if(j>0)
if(grid[i][j-1] == "wGroup" || grid[i][j-1] == "w") {
grid[i][j-1] = "w";
grid[i][j] = "w";
}
if(j<5)
if(grid[i][j+1] == "wGroup" || grid[i][j+1] == "w") {
grid[i][j+1] = "w";
grid[i][j] = "w";
}
if(i>0)
if(grid[i-1][j] == "wGroup" || grid[i-1][j] == "w") {
grid[i-1][j] = "w";
grid[i][j] = "w";
}
if(i<5)
if(grid[i+1][j] == "wGroup" || grid[i+1][j] == "w") {
grid[i+1][j] = "w";
grid[i][j] = "w";
}
}
}
}
}
}
}
}
function removeDead(){
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
if(grid[i][j] == "wGroup") {
grid[i][j] = "empty";
}
}
}
}
function checkLiberty(x, y) {
}
function koStone(x, y) {
this.x = x;
this.y = y;
}
function findNearestIndex(pt) {
let nearest = Math.round(pt / 100 - 0.5);
return nearest;
}
function drawStone() {
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
if(grid[i][j] != "empty"){
let stoneColor;
//Check whos turn
if(grid[i][j] == "b"){
stoneColor = 20;
}
else if(grid[i][j] == "w") {
stoneColor = 230;
}
fill(120, 100, 30, 100);
ellipse(((i + .5) * 100) - 5, ((j + .5) * 100) + 5, 65, 65);
fill(stoneColor);
ellipse((i + .5) * 100, (j + .5) * 100, 65, 65);
}
}
}
}
function clearAndDrawBoard() {
background(180, 130, 70);
stroke(100, 100, 80, 160);
strokeWeight(3);
for (let i = 0; i < 6; i++) {
line((i + .5) * 100, 50, (i + .5) * 100, 550);
line(50, (i + .5) * 100, 550, (i + .5) * 100);
}
noStroke();
}
function showWarnning() {
fill(150,60,10);
textSize(18);
text("* KO! Invalid Move! \n Can't Place on Where Your Chess Was Just Being Captured. \n Please Place on Another Intersection. *", 55,285);
}
function showHint() {
fill(255);
textSize(18);
if(whosTurn == "b" && !winner)
text("Black's turn. Click a grid intersection.", 50,610);
else if(whosTurn == "w" && !winner)
text("White's turn. Click a grid intersection.", 50,610);
if(winner == "black")
text("Black Won! Click to restart...", 50,610);
else if(winner == "white")
text("White Won! Click to restart...", 50,610);
}
```