예제 #1
0
 public ThingChangeEvent(Position pOld, Position pNew)
 {
     Old = pOld;
     New = pNew;
 }
예제 #2
0
파일: Game.cs 프로젝트: Whazor/Sokoban
 public virtual IThing Neighbour(Position pos, Direction direction)
 {
     return Get(pos.Move(direction));
 }
예제 #3
0
파일: Game.cs 프로젝트: Whazor/Sokoban
 public virtual IThing Get(Position pos)
 {
     return _map.Get(pos);
 }
예제 #4
0
파일: Map.cs 프로젝트: Whazor/Sokoban
 private void Recognize(char letter, Position pos)
 {
     SetMap map = (target, cor, obj) => target[cor.X, cor.Y] = obj;
     Destination destination;
     Coffin coffin;
     switch (letter)
     {
         case '#':
             map(_staticMap, pos, new Wall());
             break;
         case 'o':
             coffin = new Coffin();
             Coffins.Add(coffin);
             map(_dynamicMap, pos, coffin);
             break;
         case 'O':
             coffin = new Coffin {OnDestination = true};
             Coffins.Add(coffin);
             map(_dynamicMap, pos, coffin);
             destination = new Destination();
             Destinations.Add(destination);
             map(_staticMap, pos, destination);
             break;
         case 'x':
             destination = new Destination();
             Destinations.Add(destination);
             map(_staticMap, pos, destination);
             break;
         case '@':
             map(_dynamicMap, pos, new Forklift());
             Player = pos;
             break;
     }
 }
예제 #5
0
파일: Map.cs 프로젝트: Whazor/Sokoban
        private bool CheckForPercolation()
        {
            var isOpen = new Func<Position, bool>(thing => !(_staticMap[thing.X, thing.Y] is Wall));
            var getUnionId = new Func<Position, int>(thing =>
                {
                    if (Width >= Height)
                    {
                        return thing.Y % Height + thing.X * Height;
                    }
                    else
                    {
                        return thing.X % Width + thing.Y * Width;
                    }
                });
            var uf = new UnionFind(Width * Height);
            for (var i = 0; i < Width; i++)
            {
                for (var j = 0; j < Height; j++)
                {
                    var pos = new Position(i, j);
                    if (!isOpen(pos)) continue;

                    Direction[] directions = {Direction.Down, Direction.Up, Direction.Left, Direction.Right};
                    foreach (var tmp in directions
                        .Select(pos.Move)
                        .Where(tmp => InBounds(tmp) && isOpen(tmp)))
                    {
                        uf.Union(getUnionId(pos), getUnionId(tmp));
                    }
                }
            }
            var percolates = false;
            var playerId = getUnionId(Player);
            for (var i = 0; i < Width; i++)
            {
                var top = new Position(i, 0);
                var bottom = new Position(i, Height - 1);
                if (uf.IsConnected(playerId, getUnionId(top)))
                    percolates = true;
                if (uf.IsConnected(playerId, getUnionId(bottom)))
                    percolates = true;
            }
            for (var j = 0; j < Height; j++)
            {
                var left = new Position(0, j);
                var right = new Position(Width - 1, j);
                if (uf.IsConnected(playerId, getUnionId(left)))
                    percolates = true;

                if (uf.IsConnected(playerId, getUnionId(right)))
                    percolates = true;
            }
            return percolates;
        }
예제 #6
0
파일: Map.cs 프로젝트: Whazor/Sokoban
        public void Move(Position o, Position n, EventHandler<ThingChangeEvent> moved)
        {
            if (!InBounds(o)) return;
            if (!InBounds(n)) return;

            if (_dynamicMap[o.X, o.Y] == null) return;
            _dynamicMap[n.X, n.Y] = _dynamicMap[o.X, o.Y];
            _dynamicMap[o.X, o.Y] = null;

            if (moved == null) return;
            moved(this, new ThingChangeEvent(o, n));
        }
예제 #7
0
파일: Map.cs 프로젝트: Whazor/Sokoban
 public bool InBounds(Position pos)
 {
     return (pos.X >= 0 && pos.Y >= 0 && pos.Y < Height && pos.X < Width);
 }
예제 #8
0
파일: Map.cs 프로젝트: Whazor/Sokoban
        public virtual IThing Get(Position pos)
        {
            // check for out of bounds
            if (!InBounds(pos)) return null;

            return _dynamicMap[pos.X, pos.Y] ?? _staticMap[pos.X, pos.Y];
        }