public static bool IsIntersection(IContains inter, int move) { if (inter.GetSize() <= 2) { return(false); } int lWidth = inter.GetWidth(); int ActiveMice = 0; int[] lMousePosition = new int[4]; int[] lMouseDirection = new int[4]; int[] lMouseNextDirection = new int[4]; int[] lMouseByDirection = new int[4]; CoordinateSystem lCoord = CoordinateSystem.GetCoordinateSystem(lWidth); for (int i = 0; i < 4; i++) { int lNeightbor = lCoord.GetNeighbor(move, i); if (lCoord.OnBoard(lNeightbor) && inter.Contains(lNeightbor)) { lMousePosition[ActiveMice] = lNeightbor; lMouseDirection[ActiveMice] = i; lMouseByDirection[i] = ActiveMice; if (ActiveMice > 0) { lMouseNextDirection[ActiveMice - 1] = i; } ActiveMice++; } else { lMouseByDirection[i] = -1; // no mouse } } if (ActiveMice == 1) { return(false); } lMouseNextDirection[ActiveMice - 1] = 0; // it's never used! for (int CurrentMouse = 0; CurrentMouse < ActiveMice - 1; CurrentMouse++) { int CurrentMousePosition = lMousePosition[CurrentMouse]; int CurrentMouseDirection = lMouseDirection[CurrentMouse]; int CurrentMouseOriginalDirection = CurrentMouseDirection; while (CurrentMousePosition != move) { int lRightDirection = CoordinateSystem.TurnClockWise(CurrentMouseDirection); int lRightPosition = lCoord.GetNeighbor(CurrentMousePosition, lRightDirection); // if (can turn right & move) if (CoordinateSystem.OnBoard(lRightPosition, lWidth) && inter.Contains(lRightPosition)) { // yes, turn turn and move CurrentMousePosition = lRightPosition; CurrentMouseDirection = lRightDirection; } else { while (true) { int lStraightPosition = lCoord.GetNeighbor(CurrentMousePosition, CurrentMouseDirection); // if (can go straight) ( if (CoordinateSystem.OnBoard(lStraightPosition, lWidth) && inter.Contains(lStraightPosition)) { // yes, go straight CurrentMousePosition = lStraightPosition; break; } else { // else, change direction clockwise and try-again (loop) CurrentMouseDirection = CoordinateSystem.TurnCounterClockWise(CurrentMouseDirection); } } } } // right back where we started int FromDirection = CoordinateSystem.TurnAround(CurrentMouseDirection); // if mice returned from a direction other than the next direction, then there is a split if (lMouseNextDirection[CurrentMouse] != FromDirection) { return(true); } // if mouse came back from original direction then there is a split if (CurrentMouseOriginalDirection == FromDirection) { return(true); // should never reach here } // by now we know that the next mouse must merge, so no split if (CurrentMouse == ActiveMice - 1) { return(false); } } return(false); }