Пример #1
0
        private string HandleMoveRequest(BattleSnakeRequest moveRequest)
        {
            // Save the current board and our snake so we know where everything is.
            _currentBoard = moveRequest.Board;
            _ourSnake     = moveRequest.You;

            var direction = _snakeBrain.decideMoveDirection(_currentBoard, _ourSnake);

            return("{ \"move\":" + "\"" + direction + "\"" + " }");
        }
Пример #2
0
        // Handles a Start request. Saves current board and sends desired color.
        private string HandleStartRequest(BattleSnakeRequest startRequest)
        {
            // Save the starting board so we know where everything is.
            _currentBoard = startRequest.Board;

            // Turn the color into a hex string.
            var color = $"#{_snakeColor.R:X}{_snakeColor.G:X}{_snakeColor.B:X}";

            // Th response to this request is the color we want to be.
            return("{ \"color\":" + "\"" + color + "\"" + " }");
        }
Пример #3
0
 // Runs the handler for the given endpoint and request from the game engine.
 protected string HandleRequest(BattleSnakeRequest request, EndPoints endPoint)
 {
     if (endPoint == EndPoints.Start)
     {
         return(HandleStartRequest(request));
     }
     else if (endPoint == EndPoints.Move)
     {
         return(HandleMoveRequest(request));
     }
     else if (endPoint == EndPoints.End)
     {
         return("");
     }
     else
     {
         return($"<HTML><BODY>My web page.<br>{DateTime.Now}</BODY></HTML>");
     }
 }
Пример #4
0
        // Parses a JSON string and converts it to a BattleSnakeRequest object.
        public BattleSnakeRequest Parse(string json)
        {
            if (json == null)
            {
                throw new ArgumentNullException(nameof(json));
            }

            var request = new BattleSnakeRequest();

            try
            {
                var jObject = JObject.Parse(json);
                // If the json is empty, exit.
                if (!jObject.HasValues)
                {
                    throw new JsonException("Empty JSON");
                }

                // Parse the Game object.
                var game = jObject["game"];
                request.Game.Id = (string)game["id"];

                // Parse the Turn object.
                request.Turn = (long)jObject["turn"];

                // Parse the Board object.
                var board = jObject["board"];
                request.Board = ParseBoard(board);

                var snake = jObject["you"];
                request.You = ParseSnake(snake);
            }
            catch
            {
                Console.WriteLine("Failed to parse JSON string.");
            }

            return(request);
        }