Пример #1
0
            // Return the number of moves that were avaiable on this pipe segment.
            // If the return value is 1, the move has been made.
            private int tryPipe(Pipe p, bool tryHead)
            {
                if (p.IsConnected)
                {
                    return(0);
                }

                List <XY> a = p.HeadPath;
                List <XY> b = p.TailPath;

                XY aPos, bPos;

                if (tryHead) // Swap roles of a and b
                {
                    aPos = p.HeadPos;
                    bPos = p.TailPos;
                }
                else
                {
                    aPos = p.TailPos;
                    bPos = p.HeadPos;
                }

                // See if we can advance deterministically from a, or meet b
                List <XY> possibles = findPossibleMoves(aPos, bPos);

                if (possibles.Count != 1)
                {
                    return(possibles.Count);
                }

                // There is one and only one move, so make it *on the current board*
                // without cloning a new state.
                XY newPos = possibles[0];

                if (TheBoard[newPos.Y, newPos.X] == 0)
                {
                    // Propagate this pipe's color into the new position
                    TheBoard[newPos.Y, newPos.X] = TheBoard[aPos.Y, aPos.X];
                    // And extend the pipe.
                    if (tryHead)
                    {
                        p.AdvanceHead(newPos);
                    }
                    else
                    {
                        p.AdvanceTail(newPos);
                    }
                }
                else
                {
                    p.IsConnected = true;
                }
                MovesToGo--;
                return(1);
            }