Exemplo n.º 1
0
 private static PointDTO OffsetModulo(PointDTO p, Offset offset, SizeDTO boardSize)
 {
     return new PointDTO
     {
         X = (p.X + offset.DX + boardSize.Width)%boardSize.Width,
         Y = (p.Y + offset.DY + boardSize.Height)%boardSize.Height
     };
 }
Exemplo n.º 2
0
        public int Distance(PointDTO head, PointDTO target, SizeDTO size)
        {
            var trans = new[] {
                            new {X = 0, Y = 0},
                            new {X = 1, Y = 0},
                            new {X = -1, Y = 0},
                            new {X = 0, Y = 1},
                            new {X = 0, Y = -1}
            };

            var heads = trans.Select(t => new PointDTO { X = t.X * size.Width + head.X, Y = t.Y * size.Height + head.Y });

            return heads.Min(h => Distance(h, target));
        }
Exemplo n.º 3
0
        private IEnumerable<SnakeDTO> GetHeadsNearPoint(GameBoardState state, PointDTO point)
        {
            var surrounding = Move.Offsets.Select(x => Move.OffsetModulo(point, x.Offset, state.BoardSize())).ToArray();

            return state.GetSnakes()
                .Where(x => x.IsAlive)
                .Where(x => surrounding.Contains(x.Head));
        }
Exemplo n.º 4
0
 public int Distance(PointDTO a, PointDTO b)
 {
     return (int)(Math.Abs(a.X - b.X) + Math.Abs(a.Y - b.Y));
 }
Exemplo n.º 5
0
        public int NumOfNeigh(IEnumerable<PointDTO> food, PointDTO f)
        {
            var neigh = new[] {
                new {X = -2, Y = -2},new {X = -1, Y = -2},new {X = 0, Y = -2},new {X = 1, Y = -2},new {X = 2, Y = -2},
            new {X = -2, Y = -1},new {X = -1, Y = -1},new {X = 0, Y = -1},new {X = 1, Y = -1},new {X = 2, Y = -1},
            new {X = -2, Y = 0},new {X = -1, Y = 0},new {X = 0, Y = 0},new {X = 1, Y = 0},new {X = 2, Y = 0},
            new {X = -2, Y = 1},new {X = -1, Y = 1},new {X = 0, Y = 1},new {X = 1, Y = 1},new {X = 2, Y = 1},
            new {X = -2, Y = 2},new {X = -1, Y = 2},new {X = 0, Y = 2},new {X = 1, Y = 2},new {X = 2, Y = 2}
            };

            return neigh.Where(n => food.Contains(new PointDTO { X = f.X + n.X, Y = f.Y + n.Y })).Count();
        }
Exemplo n.º 6
0
        public bool IsPotentiallyClosed(int depth, PointDTO mySnakeHead, IEnumerable<PointDTO> taken)
        {
            var neigh = new[] {
                new {X = -1, Y = -1},new {X = 0, Y = -1},new {X = 1, Y = -1},
                new {X = -1, Y = 0},new {X = 0, Y = 0},new {X = 1, Y = 0},
                new {X = -1, Y = 1},new {X = 0, Y = 1},new {X = 1, Y = 1}
            };

            var free = neigh.Select(n => new PointDTO { X = n.X + mySnakeHead.X, Y = n.Y + mySnakeHead.Y }).Except(taken);

            var newTaken = taken.Concat(free).Distinct();

            if (depth == 0) {

                return free.Any();
            }

            return free.Select(f => IsPotentiallyClosed(--depth, f, newTaken)).Any();
        }