Exemple #1
0
        public Move GetNextMove(int[,] playground)
        {
            _roundCounter++;

            EvaluateGame(playground);

            SafeMap safePath = new SafeMap(10, _actualPosition, _direction, _possibilities, _size);

            safePath.Start();

            if (safePath.Steps.Count > 0)
            {
                var next = safePath.Steps.Pop();
                _actualPosition = next.FinalPosition;
                _direction      = Service.UpdateDirection(_direction, next.Move);
                // MessageBox.Show("Jdu:" + next.Move);
                return(next.Move);
            }

            DangerMap dangerPath = new DangerMap(4, _actualPosition, _direction, _possibilities, _size);

            dangerPath.Start();

            if (dangerPath.Steps.Count > 0)
            {
                var next = dangerPath.Steps.Pop();
                _actualPosition = next.FinalPosition;
                _direction      = Service.UpdateDirection(_direction, next.Move);
                //MessageBox.Show("Jdu:" + next.Move);
                return(next.Move);
            }

            return(Move.Straight);
        }
Exemple #2
0
        private bool Count(GamePoint point, Direction direction, int depth, List <GamePoint> occupied)
        {
            if (depth == _depth)
            {
                return(true);
            }

            var steps = PossibleSteps(direction, point);

            foreach (var step in steps)
            {
                if (occupied.Contains(step.FinalPosition))
                {
                    continue;
                }

                List <GamePoint> copy = occupied.Select(x => new GamePoint(x.X, x.Y)).ToList();
                copy.Add(step.FinalPosition);

                if (Count(step.FinalPosition, step.FinalDirection, depth + 1, copy))
                {
                    Steps.Push(step);
                    return(true);
                }
            }

            return(false);
        }
Exemple #3
0
 public Step(Move move, Direction finalDirection, GamePoint finalPosition, bool isSafe)
 {
     Move           = move;
     FinalDirection = finalDirection;
     FinalPosition  = finalPosition;
     IsSafe         = isSafe;
 }
Exemple #4
0
 public DangerMap(int depth, GamePoint start, Direction origin, byte[,] possibilities, int size)
 {
     _depth         = depth;
     _size          = size;
     _possibilities = possibilities;
     _start         = start;
     _origin        = origin;
     Steps          = new Stack <Step>();
 }
Exemple #5
0
 public void Init(int playerId, int playgroundSize, int x, int y, Direction direction)
 {
     _size           = playgroundSize;
     _direction      = direction;
     _identificator  = playerId;
     _actualPosition = new GamePoint(x, y);
     _possibilities  = new byte[_size, _size];
     _roundCounter   = 0;
 }
Exemple #6
0
        private bool IsViable(GamePoint gamePoint)
        {
            //overit jeslti vubec bod existuje
            if (gamePoint.X < 0 || gamePoint.X >= _size || gamePoint.Y < 0 || gamePoint.Y >= _size)
            {
                return(false);
            }

            //overit jestli je bod dostupny
            return(_possibilities[gamePoint.X, gamePoint.Y] == 0);
        }
Exemple #7
0
        private List <Step> PossibleSteps(Direction direction, GamePoint gamePoint)
        {
            var steps = new List <Step>();

            switch (direction)
            {
            case Direction.Top:
                if (!IsTaken(gamePoint.X - 1, gamePoint.Y - 1))
                {
                    var move = Move.Left;
                    var dir  = Service.UpdateDirection(direction, Move.Left);
                    var safe = IsViable(gamePoint.X - 1, gamePoint.Y - 1);

                    var point = new GamePoint(gamePoint.X - 1, gamePoint.Y - 1);

                    steps.Add(new Step(move, dir, point, safe));
                }
                if (!IsTaken(gamePoint.X, gamePoint.Y - 1))
                {
                    var move  = Move.Straight;
                    var dir   = Service.UpdateDirection(direction, Move.Straight);
                    var point = new GamePoint(gamePoint.X, gamePoint.Y - 1);
                    var safe  = IsViable(gamePoint.X, gamePoint.Y - 1);

                    steps.Add(new Step(move, dir, point, safe));
                }
                if (!IsTaken(gamePoint.X + 1, gamePoint.Y - 1))
                {
                    var move  = Move.Right;
                    var dir   = Service.UpdateDirection(direction, Move.Right);
                    var point = new GamePoint(gamePoint.X + 1, gamePoint.Y - 1);
                    var safe  = IsViable(gamePoint.X + 1, gamePoint.Y - 1);

                    steps.Add(new Step(move, dir, point, safe));
                }
                break;

            case Direction.TopRight:
                if (!IsTaken(gamePoint.X, gamePoint.Y - 1))
                {
                    var move  = Move.Left;
                    var dir   = Service.UpdateDirection(direction, Move.Left);
                    var point = new GamePoint(gamePoint.X, gamePoint.Y - 1);
                    var safe  = IsViable(gamePoint.X, gamePoint.Y - 1);

                    steps.Add(new Step(move, dir, point, safe));
                }
                if (!IsTaken(gamePoint.X + 1, gamePoint.Y - 1))
                {
                    var move  = Move.Straight;
                    var dir   = Service.UpdateDirection(direction, Move.Straight);
                    var point = new GamePoint(gamePoint.X + 1, gamePoint.Y - 1);
                    var safe  = IsViable(gamePoint.X - 1, gamePoint.Y - 1);

                    steps.Add(new Step(move, dir, point, safe));
                }
                if (!IsTaken(gamePoint.X + 1, gamePoint.Y))
                {
                    var move  = Move.Right;
                    var dir   = Service.UpdateDirection(direction, Move.Right);
                    var point = new GamePoint(gamePoint.X + 1, gamePoint.Y);
                    var safe  = IsViable(gamePoint.X + 1, gamePoint.Y);

                    steps.Add(new Step(move, dir, point, safe));
                }
                break;

            case Direction.Right:
                if (!IsTaken(gamePoint.X + 1, gamePoint.Y - 1))
                {
                    var move  = Move.Left;
                    var dir   = Service.UpdateDirection(direction, Move.Left);
                    var point = new GamePoint(gamePoint.X + 1, gamePoint.Y - 1);
                    var safe  = IsViable(gamePoint.X + 1, gamePoint.Y - 1);

                    steps.Add(new Step(move, dir, point, safe));
                }
                if (!IsTaken(gamePoint.X + 1, gamePoint.Y))
                {
                    var move  = Move.Straight;
                    var dir   = Service.UpdateDirection(direction, Move.Straight);
                    var point = new GamePoint(gamePoint.X + 1, gamePoint.Y);
                    var safe  = IsViable(gamePoint.X + 1, gamePoint.Y);

                    steps.Add(new Step(move, dir, point, safe));
                }
                if (!IsTaken(gamePoint.X + 1, gamePoint.Y + 1))
                {
                    var move  = Move.Right;
                    var dir   = Service.UpdateDirection(direction, Move.Right);
                    var point = new GamePoint(gamePoint.X + 1, gamePoint.Y + 1);
                    var safe  = IsViable(gamePoint.X + 1, gamePoint.Y + 1);

                    steps.Add(new Step(move, dir, point, safe));
                }
                break;

            case Direction.BottomRight:
                if (!IsTaken(gamePoint.X + 1, gamePoint.Y))
                {
                    var move  = Move.Left;
                    var dir   = Service.UpdateDirection(direction, Move.Left);
                    var point = new GamePoint(gamePoint.X + 1, gamePoint.Y);
                    var safe  = IsViable(gamePoint.X + 1, gamePoint.Y);

                    steps.Add(new Step(move, dir, point, safe));
                }
                if (!IsTaken(gamePoint.X + 1, gamePoint.Y + 1))
                {
                    var move  = Move.Straight;
                    var dir   = Service.UpdateDirection(direction, Move.Straight);
                    var point = new GamePoint(gamePoint.X + 1, gamePoint.Y + 1);
                    var safe  = IsViable(gamePoint.X + 1, gamePoint.Y + 1);

                    steps.Add(new Step(move, dir, point, safe));
                }
                if (!IsTaken(gamePoint.X, gamePoint.Y + 1))
                {
                    var move  = Move.Right;
                    var dir   = Service.UpdateDirection(direction, Move.Right);
                    var point = new GamePoint(gamePoint.X, gamePoint.Y + 1);
                    var safe  = IsViable(gamePoint.X, gamePoint.Y + 1);

                    steps.Add(new Step(move, dir, point, safe));
                }
                break;

            case Direction.Bottom:
                if (!IsTaken(gamePoint.X + 1, gamePoint.Y + 1))
                {
                    var move  = Move.Left;
                    var dir   = Service.UpdateDirection(direction, Move.Left);
                    var point = new GamePoint(gamePoint.X + 1, gamePoint.Y + 1);
                    var safe  = IsViable(gamePoint.X + 1, gamePoint.Y + 1);

                    steps.Add(new Step(move, dir, point, safe));
                }
                if (!IsTaken(gamePoint.X, gamePoint.Y + 1))
                {
                    var move  = Move.Straight;
                    var dir   = Service.UpdateDirection(direction, Move.Straight);
                    var point = new GamePoint(gamePoint.X, gamePoint.Y + 1);
                    var safe  = IsViable(gamePoint.X, gamePoint.Y + 1);

                    steps.Add(new Step(move, dir, point, safe));
                }
                if (!IsTaken(gamePoint.X - 1, gamePoint.Y + 1))
                {
                    var move  = Move.Right;
                    var dir   = Service.UpdateDirection(direction, Move.Right);
                    var point = new GamePoint(gamePoint.X - 1, gamePoint.Y + 1);
                    var safe  = IsViable(gamePoint.X - 1, gamePoint.Y + 1);

                    steps.Add(new Step(move, dir, point, safe));
                }
                break;

            case Direction.BottomLeft:
                if (!IsTaken(gamePoint.X, gamePoint.Y + 1))
                {
                    var move  = Move.Left;
                    var dir   = Service.UpdateDirection(direction, Move.Left);
                    var point = new GamePoint(gamePoint.X, gamePoint.Y + 1);
                    var safe  = IsViable(gamePoint.X, gamePoint.Y + 1);

                    steps.Add(new Step(move, dir, point, safe));
                }
                if (!IsTaken(gamePoint.X - 1, gamePoint.Y + 1))
                {
                    var move  = Move.Straight;
                    var dir   = Service.UpdateDirection(direction, Move.Straight);
                    var point = new GamePoint(gamePoint.X - 1, gamePoint.Y + 1);
                    var safe  = IsViable(gamePoint.X - 1, gamePoint.Y + 1);

                    steps.Add(new Step(move, dir, point, safe));
                }
                if (!IsTaken(gamePoint.X - 1, gamePoint.Y))
                {
                    var move  = Move.Right;
                    var dir   = Service.UpdateDirection(direction, Move.Right);
                    var point = new GamePoint(gamePoint.X - 1, gamePoint.Y);
                    var safe  = IsViable(gamePoint.X - 1, gamePoint.Y);

                    steps.Add(new Step(move, dir, point, safe));
                }
                break;

            case Direction.Left:
                if (!IsTaken(gamePoint.X - 1, gamePoint.Y + 1))
                {
                    var move  = Move.Left;
                    var dir   = Service.UpdateDirection(direction, Move.Left);
                    var point = new GamePoint(gamePoint.X - 1, gamePoint.Y + 1);
                    var safe  = IsViable(gamePoint.X - 1, gamePoint.Y + 1);

                    steps.Add(new Step(move, dir, point, safe));
                }
                if (!IsTaken(gamePoint.X - 1, gamePoint.Y))
                {
                    var move  = Move.Straight;
                    var dir   = Service.UpdateDirection(direction, Move.Straight);
                    var point = new GamePoint(gamePoint.X - 1, gamePoint.Y);
                    var safe  = IsViable(gamePoint.X - 1, gamePoint.Y);

                    steps.Add(new Step(move, dir, point, safe));
                }
                if (!IsTaken(gamePoint.X - 1, gamePoint.Y - 1))
                {
                    var move  = Move.Right;
                    var dir   = Service.UpdateDirection(direction, Move.Right);
                    var point = new GamePoint(gamePoint.X - 1, gamePoint.Y - 1);
                    var safe  = IsViable(gamePoint.X - 1, gamePoint.Y - 1);

                    steps.Add(new Step(move, dir, point, safe));
                }
                break;

            case Direction.TopLeft:
                if (!IsTaken(gamePoint.X - 1, gamePoint.Y))
                {
                    var move  = Move.Left;
                    var dir   = Service.UpdateDirection(direction, Move.Left);
                    var point = new GamePoint(gamePoint.X - 1, gamePoint.Y);
                    var safe  = IsViable(gamePoint.X - 1, gamePoint.Y);

                    steps.Add(new Step(move, dir, point, safe));
                }
                if (!IsTaken(gamePoint.X - 1, gamePoint.Y - 1))
                {
                    var move  = Move.Straight;
                    var dir   = Service.UpdateDirection(direction, Move.Straight);
                    var point = new GamePoint(gamePoint.X - 1, gamePoint.Y - 1);
                    var safe  = IsViable(gamePoint.X - 1, gamePoint.Y - 1);

                    steps.Add(new Step(move, dir, point, safe));
                }
                if (!IsTaken(gamePoint.X, gamePoint.Y - 1))
                {
                    var move  = Move.Right;
                    var dir   = Service.UpdateDirection(direction, Move.Right);
                    var point = new GamePoint(gamePoint.X, gamePoint.Y - 1);
                    var safe  = IsViable(gamePoint.X, gamePoint.Y - 1);

                    steps.Add(new Step(move, dir, point, safe));
                }
                break;
            }

            return(steps);
        }