Beispiel #1
0
        private IEnumerable <SokobanPathItem> GetEntryPoints(CellStaticInfo locationCell)
        {
            if (!locationCell.Left.IsWall && !locationCell.Left.IsLocation && !locationCell.Left.Left.IsWall && !locationCell.Left.Left.IsLocation)
            {
                yield return(new SokobanPathItem()
                {
                    Key = Key.Right, Position = GetPosition(Key.Left, locationCell.Position).Value
                });
            }

            if (!locationCell.Top.IsWall && !locationCell.Top.IsLocation && !locationCell.Top.Top.IsWall && !locationCell.Top.Top.IsLocation)
            {
                yield return(new SokobanPathItem()
                {
                    Key = Key.Down, Position = GetPosition(Key.Up, locationCell.Position).Value
                });
            }

            if (!locationCell.Right.IsWall && !locationCell.Right.IsLocation && !locationCell.Right.Right.IsWall && !locationCell.Right.Right.IsLocation)
            {
                yield return(new SokobanPathItem()
                {
                    Key = Key.Left, Position = GetPosition(Key.Right, locationCell.Position).Value
                });
            }

            if (!locationCell.Bottom.IsWall && !locationCell.Bottom.IsLocation && !locationCell.Bottom.Bottom.IsWall && !locationCell.Bottom.Bottom.IsLocation)
            {
                yield return(new SokobanPathItem()
                {
                    Key = Key.Up, Position = GetPosition(Key.Down, locationCell.Position).Value
                });
            }
        }
Beispiel #2
0
        private void InitializeStaticGraph(int pos)
        {
            var  point      = Convert(pos);
            bool isWall     = map[pos] == Solver.Sokoban.WALL;
            bool isLocation = map[pos] == Solver.Sokoban.LOCATION ||
                              map[pos] == Solver.Sokoban.KEEPER_ON_LOCATION ||
                              map[pos] == Solver.Sokoban.BOX_ON_LOCATION;

            var cell = new CellStaticInfo(pos, (int)point.X, (int)point.Y, isLocation, isWall);

            staticGraph.Add(pos, cell);

            var leftPos = GetPosition(Key.Left, pos);

            if (leftPos.HasValue && staticGraph.ContainsKey(leftPos.Value))
            {
                var leftCell = staticGraph[leftPos.Value];
                leftCell.Right = cell;
                cell.Left      = leftCell;
            }

            var topPos = GetPosition(Key.Up, pos);

            if (topPos.HasValue && staticGraph.ContainsKey(topPos.Value))
            {
                var topCell = staticGraph[topPos.Value];
                topCell.Bottom = cell;
                cell.Top       = topCell;
            }
        }
Beispiel #3
0
 private IEnumerable <CellStaticInfo> GetNeighbours(CellStaticInfo cell)
 {
     if (cell.Left != null)
     {
         yield return(cell.Left);
     }
     if (cell.Top != null)
     {
         yield return(cell.Top);
     }
     if (cell.Right != null)
     {
         yield return(cell.Right);
     }
     if (cell.Bottom != null)
     {
         yield return(cell.Bottom);
     }
 }
Beispiel #4
0
        //private void BuildDragGraph(int dragBoxFrom, int dragBoxTo)
        //{
        //    var finalPathes = new List<Stack<Tuple<CellStaticInfo, CellStaticInfo>>>();

        //    var currentPos = new Tuple<CellStaticInfo, CellStaticInfo>(staticGraph[KeeperPosition], staticGraph[dragBoxFrom]);

        //    // visited Tuple<KeeperPos, BoxPos>
        //    var visited = new HashSet<Tuple<CellStaticInfo, CellStaticInfo>>();
        //    visited.Add(currentPos);

        //    var scope = new Queue<Tuple<CellStaticInfo, CellStaticInfo>>();
        //    scope.Enqueue(currentPos);

        //    var currentPath = new Stack<Tuple<CellStaticInfo, CellStaticInfo>>();
        //    currentPath.Push(currentPos);

        //    var pathes = new Queue<Stack<Tuple<CellStaticInfo, CellStaticInfo>>>();
        //    pathes.Enqueue(currentPath);

        //    do
        //    {
        //        currentPos = scope.Dequeue();
        //        currentPath = pathes.Dequeue();

        //        var currentKeeperPos = currentPos.Item1;
        //        var currentBoxPos = currentPos.Item2;

        //        // teleport box to currentBoxPos and keeper to currentKeeperPos
        //        dynamicGraph[dragBoxFrom].HoldsBox = false;
        //        dynamicGraph[currentBoxPos.Position].HoldsBox = true;
        //        KeeperPosition = currentKeeperPos.Position;

        //        if (currentBoxPos.Position == dragBoxTo)
        //        {
        //            // found new path
        //            finalPathes.Add(currentPath);
        //            continue;
        //        }

        //        var targetKeeperCell = GetKeeperDragTarget(currentKeeperPos, currentBoxPos);
        //        if (targetKeeperCell == null || targetKeeperCell.IsWall)
        //        {
        //            continue;
        //        }

        //        // Move box
        //        dynamicGraph[currentBoxPos.Position].HoldsBox = false;
        //        dynamicGraph[currentKeeperPos.Position].HoldsBox = true;
        //        KeeperPosition = targetKeeperCell.Position;
        //        DetectAreas();

        //        var keeperArea = areaGraph[dynamicGraph[KeeperPosition].AreaId];
        //        foreach (var neighbor in new[] { currentKeeperPos.Left, currentKeeperPos.Top, currentKeeperPos.Right, currentKeeperPos.Bottom })
        //        {
        //            if (keeperArea.Contains(neighbor.Position))
        //            {
        //                var nextPosition = new Tuple<CellStaticInfo, CellStaticInfo>(neighbor, currentKeeperPos);
        //                if (!visited.Contains(nextPosition))
        //                {
        //                    visited.Add(nextPosition);
        //                    scope.Enqueue(nextPosition);

        //                    var nextPath = new Stack<Tuple<CellStaticInfo, CellStaticInfo>>(currentPath.Reverse());
        //                    nextPath.Push(nextPosition);
        //                    pathes.Enqueue(nextPath);
        //                }
        //            }
        //        }
        //    }
        //    while (scope.Count > 0);
        //}

        private CellStaticInfo GetKeeperDragTarget(CellStaticInfo keeperPos, CellStaticInfo boxPos)
        {
            if (boxPos.Left == keeperPos)
            {
                return(keeperPos.Left);
            }
            if (boxPos.Top == keeperPos)
            {
                return(keeperPos.Top);
            }
            if (boxPos.Right == keeperPos)
            {
                return(keeperPos.Right);
            }
            if (boxPos.Bottom == keeperPos)
            {
                return(keeperPos.Bottom);
            }

            return(null);
        }