示例#1
0
        public Move GetNextMove(GameBoardState gameBoardState)
        {
            //===========================
            // Your snake logic goes here
            //===========================

            var mySnake = gameBoardState.GetSnake(_mySnakeId);

            Console.Title = $"Alive: {mySnake.IsAlive} Direction: {mySnake.Direction}";

            if (mySnake.IsAlive)
            {
                var occupiedCells = gameBoardState.GetOccupiedCells();

                // Check possible moves in random order.

                var nearestFood = gameBoardState.GetFood()
                    .OrderBy(x => x.DistanceTo(mySnake.Head, gameBoardState.BoardSize()))
                    .Select(x => (PointDTO?)x)
                    .FirstOrDefault();

                if (nearestFood != null)
                {
                    Console.WriteLine("Food at {0}, {1}", nearestFood?.X, nearestFood?.Y);

                    var bestMove = moves
                        .Select(x => new { Move = x, NewHead = gameBoardState.GetSnakeNewHeadPosition(_mySnakeId, x) })
                        .Where(x => !occupiedCells.Contains(x.NewHead))
                        .Where(x => GetHeadsNearPoint(gameBoardState, x.NewHead).Count() == 1)
                        .OrderBy(x => x.NewHead.DistanceTo(nearestFood.Value, gameBoardState.BoardSize()))
                        .FirstOrDefault();

                    if (bestMove != null)
                    {
                        return bestMove.Move;
                    }
                    else
                    {
                        Console.WriteLine("DUPA nie ma gdzie iϾ");
                    }
                }

                var moves = new List<Move>
                {
                    Move.Left,
                    Move.Right,
                    Move.Straight
                };

                while (moves.Any())
                {
                    // Select random move.
                    var move = moves[_random.Next(moves.Count)];
                    moves.Remove(move);

                    var newHead = gameBoardState.GetSnakeNewHeadPosition(_mySnakeId, move);

                    var nearHeads = GetHeadsNearPoint(gameBoardState, newHead);

                    if (nearHeads.Count() == 1)
                    {
                        if (!occupiedCells.Contains(newHead))
                        {
                            return move;
                        }
                    }
                }
                moves = new List<Move>
                {
                    Move.Left,
                    Move.Right,
                    Move.Straight
                };

                while (moves.Any())
                {
                    // Select random move.
                    var move = moves[_random.Next(moves.Count)];
                    moves.Remove(move);

                    var newHead = gameBoardState.GetSnakeNewHeadPosition(_mySnakeId, move);

                    if (!occupiedCells.Contains(newHead))
                    {
                        return move;
                    }
                }
            }
            return Move.None;
        }
示例#2
0
        public Move GetNextMove(GameBoardState gameBoardState)
        {
            //===========================
            // Your snake logic goes here
            //===========================

            var mySnake = gameBoardState.GetSnake(_mySnakeId);
            if (mySnake.IsAlive)
            {
                var occupiedCells = gameBoardState.GetOccupiedCells();

                // Check possible moves in random order.
                var moves = new List<Move>
                {
                    Move.Left,
                    Move.Right,
                    Move.Straight
                };

                var potentialSneakHeads = gameBoardState._gameState.Snakes.Where(s => s.IsAlive).Where(s => s.Id != _mySnakeId).SelectMany(x => moves.Select(m => m.GetSnakeNewHead(x, gameBoardState._gameState.BoardSize))).Distinct();

                var potentialConflicts = new List<Move>();

                occupiedCells = new HashSet<PointDTO>(occupiedCells.Concat(potentialSneakHeads));

                while (moves.Any())
                {

                    // Select random move.
                    var move = GetPrefferedDirection(gameBoardState._gameState.Food, moves, mySnake, gameBoardState._gameState.BoardSize);

                    if (move == Move.None)
                    {
                        Console.WriteLine("Fallback to Random!");
                        move = moves[_random.Next(moves.Count)];
                    }

                    moves.Remove(move);

                    var newHead = gameBoardState.GetSnakeNewHeadPosition(_mySnakeId, move);
                    if (!occupiedCells.Contains(newHead))
                    {
                        //if (IsPotentiallyClosed(2, newHead, gameBoardState.GetOccupiedCells()))
                        //{
                        //    potentialConflicts.Add(move);
                        //    continue;
                        //}

                        return move;
                    }

                    if (potentialSneakHeads.Contains(newHead))
                    {
                        potentialConflicts.Add(move);
                    }
                }

                if (potentialConflicts.Any())
                {
                    return potentialConflicts.First();
                }
            }
            return Move.None;
        }