private void Visit(CellState startCellState) { startCellState.Visited = true; count++; startCellState.DfsDepth = count; startCellState.DfsLow = count; foreach (CellState adjacentCellState in startCellState.GetAdjacentCellStates()) { if (adjacentCellState.ClosestPlayer == player && (adjacentCellState.OccupationStatus == OccupationStatus.Clear || adjacentCellState.OccupationStatus == playerOccupationStatus)) { if (!adjacentCellState.Visited) { Edge newEdge = new Edge { StartVertex = startCellState, EndVertex = adjacentCellState }; edgeStack.Push(newEdge); adjacentCellState.ParentCellState = startCellState; Visit(adjacentCellState); if (adjacentCellState.DfsLow >= startCellState.DfsDepth) { // Create a chamber Chamber chamber = new Chamber(nextChamberNumber); nextChamberNumber++; startCellState.GameState.AddChamber(chamber, player); Edge poppedEdge = null; do { poppedEdge = edgeStack.Pop(); chamber.AddCell(poppedEdge.StartVertex); chamber.AddCell(poppedEdge.EndVertex); poppedEdge.StartVertex.AddChamber(chamber, player); poppedEdge.EndVertex.AddChamber(chamber, player); }while (poppedEdge != newEdge); } if (adjacentCellState.DfsLow < startCellState.DfsLow) { startCellState.DfsLow = adjacentCellState.DfsLow; } } else if (!(adjacentCellState.ParentCellState == startCellState) && (adjacentCellState.DfsDepth < startCellState.DfsDepth)) { // The link from startCellState to adjacentCellState is a back edge to an ancestor of startCellState: Edge backEdge = new Edge { StartVertex = startCellState, EndVertex = adjacentCellState }; edgeStack.Push(backEdge); if (adjacentCellState.DfsDepth < startCellState.DfsLow) { startCellState.DfsLow = adjacentCellState.DfsDepth; } } } } }