Example #1
0
        public void ReconstructPath(Dictionary<Position, UnitState> came_from, Position start, UnitState currentState)
        {
            LockResult result;
            var currentPosition = currentState.Position;

            if (!LockResults.TryGetValue(came_from[currentPosition].Position, out result))
            {
                result = new LockResult();
                var prevState = came_from[currentPosition];
                LockResults[prevState.Position] = result;
                result.MaxHeight = prevState.Cells.OrderByDescending(x => x.Y).First().Y;
                result.MinHeight = prevState.Cells.OrderBy(x => x.Y).First().Y;
                result.LinesRemoved = GameState.CheckRemovedLines(prevState.Cells, result.MinHeight, result.MaxHeight);
                result.MinDistanceFromCenter =
                    prevState.Cells.Select(x => Math.Abs((GameState.BoardState.Width/2) - x.X)).Min();
                result.NumberOfOpenNeighbors = GameState.CountNumberOfOpenNeighbors(prevState.Cells);
            }

            var movelist = new List<Direction>();

            while (currentPosition != start)
            {
                var prev = came_from[currentPosition].Position;

                if (currentPosition == prev.Translate(Direction.E))
                    movelist.Add(Direction.E);
                else if (currentPosition == prev.Translate(Direction.W))
                    movelist.Add(Direction.W);
                else if (currentPosition == prev.Translate(Direction.SE))
                    movelist.Add(Direction.SE);
                else if (currentPosition == prev.Translate(Direction.SW))
                    movelist.Add(Direction.SW);
                else if (currentPosition == prev.Translate(Direction.CW))
                    movelist.Add(Direction.CW);
                else if (currentPosition == prev.Translate(Direction.CCW))
                    movelist.Add(Direction.CCW);
                else
                {
                    throw new Exception();
                }

                currentPosition = prev;
            }

            movelist.Reverse();
            result.Directions.Add(movelist);
        }
Example #2
0
 protected bool Equals(Position other)
 {
     return Location.Equals(other.Location) && Orientation == other.Orientation;
 }
Example #3
0
 public UnitState(Position position, Point[] cells)
 {
     Position = position;
     Cells = cells;
 }
Example #4
0
        public UnitState CalcStartingPosition(Unit unit)
        {
            var max = new Cell();
            var min = new Cell(int.MaxValue, int.MaxValue);

            foreach (var cell in unit.Members)
            {
                max.X = Math.Max(cell.X, max.X);
                max.Y = Math.Max(cell.Y, max.Y);
                min.X = Math.Min(cell.X, min.X);
                min.Y = Math.Min(cell.Y, min.Y);
            }

            var unitWidth = 1 + (max.X - min.X);

            var x = (int) Math.Floor(BoardState.Width/2.0 - unitWidth/2.0) - min.X;
            var y = 0 - min.Y;
            var pos = new Point(x, y);

            var cells = new Point[unit.Members.Length];
            for (var i = 0; i < unit.Members.Length; i++)
                cells[i] = new Point(unit.Members[i].X + pos.X, unit.Members[i].Y + pos.Y);

            var position = new Position(new Point(CurrentUnit.Pivot.X + pos.X, CurrentUnit.Pivot.Y + pos.Y), 0);

            var state = new UnitState(position, cells);

            return state;
        }