Beispiel #1
0
        static public bool Move(this iBoard b, MoveType moveType)
        {
            int dx = 0, dy = 0;

            moveType.Move(ref dx, ref dy);

            int bx = b.BuddyX, by = b.BuddyY;
            int nx = bx + dx, ny = by + dy;

            // on border check
            if (dx < 0 && nx <= 0 || dx > 0 && nx >= b.Width - 1)
            {
                return(false);
            }
            if (dy < 0 && ny <= 0 || dy > 0 && ny >= b.Height - 1)
            {
                return(false);
            }

            if (IsEmpty(b[nx, ny]))
            {
                string boardInfo = b.GetBoardInfo(true);
                b[bx, by] &= ~CellType.Buddy;
                b[nx, ny] |= CellType.Buddy;
                b.BuddyX   = nx;
                b.BuddyY   = ny;
                b.PushStep(new UndoStep(moveType, false, boardInfo));
                return(true);
            }
            if (Is(b[nx, ny], CellType.Box))
            {
                int nbx = nx + dx, nby = ny + dy;
                // on border check
                if (dx < 0 && nbx <= 0 || dx > 0 && nbx >= b.Width - 1)
                {
                    return(false);
                }
                if (dy < 0 && nby <= 0 || dy > 0 && nby >= b.Height - 1)
                {
                    return(false);
                }
                if (IsEmpty(b[nbx, nby]))
                {
                    string boardInfo = b.GetBoardInfo(true);
                    b[nx, ny]   &= ~CellType.Box;
                    b[nbx, nby] |= CellType.Box;
                    b[bx, by]   &= ~CellType.Buddy;
                    b[nx, ny]   |= CellType.Buddy;
                    b.BuddyX     = nx;
                    b.BuddyY     = ny;
                    b.PushStep(new UndoStep(moveType, true, boardInfo));
                    return(true);
                }
            }
            return(false);
        }
Beispiel #2
0
        static public bool IsInDeadLock(this iBoard b)
        {
            string boardInfo = b.GetBoardInfo(true);

            if (b.Steps.Any(us => us._boardInfo == boardInfo))
            {
                return(true);
            }
            for (int y = 1; y < b.Height - 1; y++)
            {
                for (int x = 1; x < b.Width - 1; x++)
                {
                    if (IsBoxNotInPlace(b[x, y]))
                    {
                        bool u = b.CanNotBeMoved(x, y, MoveType.Up);
                        bool d = b.CanNotBeMoved(x, y, MoveType.Down);
                        bool l = b.CanNotBeMoved(x, y, MoveType.Left);
                        bool r = b.CanNotBeMoved(x, y, MoveType.Right);
                        if ((u || d) && (l || r))
                        {
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }