Beispiel #1
0
 private void ChangeDirection(GetResponse.Directions desiredDirection)
 {
     while (currentDirection != desiredDirection)
     {
         GetPostResponse("L");
     }
 }
 public void Death()
 {
     currentX         = 1;
     currentY         = 1;
     currentScent     = false;
     currentDirection = GetResponse.Directions.North;
 }
Beispiel #3
0
        private void GetPostResponse(string commands = "")
        {
            var response = client.PostRequest(commands);

            currentDirection = response.Direction;
            currentX         = response.X;
            currentY         = response.Y;
            var directionSymbol = GetDirectionSymbol(currentDirection);

            DrawMap(response.X, response.Y, directionSymbol);
            AddToLogs(response.ToString());

            if (client.GetMapRequest(currentX, currentY) == "W")
            {
                AddToLogs("You found a treasure! You win!");
            }
        }
Beispiel #4
0
        private string GetDirectionSymbol(GetResponse.Directions dir)
        {
            switch (dir)
            {
            case GetResponse.Directions.North:
                return("^");

            case GetResponse.Directions.East:
                return(">");

            case GetResponse.Directions.West:
                return("<");

            case GetResponse.Directions.South:
                return("∨");
            }
            throw new ArgumentException("Direction is not recognized");
        }
Beispiel #5
0
        public void Test_060_StartPositionIsValid()
        {
            //Starting values from RobotController
            var x     = 1;
            var y     = 1;
            var scent = false;

            GetResponse.Directions dirs = GetResponse.Directions.North;

            //Prepare
            var postRequest = new RestRequest("/api/values", Method.POST);

            postRequest.AddJsonBody("");
            //Act
            var postResponse             = client.Execute(postRequest);
            var postResponseDeserialized = JsonConvert.DeserializeObject <GetResponse>(postResponse.Content);

            //Assert
            Assert.AreEqual(x, postResponseDeserialized.X);
            Assert.AreEqual(y, postResponseDeserialized.Y);
            Assert.AreEqual(scent, postResponseDeserialized.Scent);
            Assert.AreEqual(GetResponse.Directions.North, postResponseDeserialized.Direction);
        }
        public GetResponse ProcessRobotInput(string inputs, MartianMap map)
        {
            var response = new GetResponse();

            foreach (var input in inputs.ToUpperInvariant())
            {
                if (input == nameof(GetResponse.Turns.L)[0])
                {
                    if ((int)currentDirection > 0)
                    {
                        currentDirection = currentDirection - 1;
                    }
                    else
                    {
                        currentDirection = GetResponse.Directions.West;
                    }
                }

                if (input == nameof(GetResponse.Turns.R)[0])
                {
                    if ((int)currentDirection < 3)
                    {
                        currentDirection = currentDirection + 1;
                    }
                    else
                    {
                        currentDirection = GetResponse.Directions.North;
                    }
                }

                if (input == nameof(GetResponse.Turns.F)[0])
                {
                    var previousCell = new int[]
                    {
                        currentX, currentY
                    };

                    switch (currentDirection)
                    {
                    case GetResponse.Directions.North:
                        if (!HasScent(currentX, currentY, map))
                        {
                            currentY++;
                        }
                        else if (map.GetMapPosition(currentX, currentY + 1) != deathCell)
                        {
                            currentY++;
                        }
                        else
                        {
                            response.Message = $"Suicide at {currentX}, {currentY + 1} is forbidden";
                        }

                        break;

                    case GetResponse.Directions.East:
                        if (!HasScent(currentX, currentY, map))
                        {
                            currentX++;
                        }
                        else if (map.GetMapPosition(currentX + 1, currentY) != deathCell)
                        {
                            currentX++;
                        }
                        else
                        {
                            response.Message = $"Suicide at {currentX + 1}, {currentY} is forbidden";
                        }
                        break;

                    case GetResponse.Directions.South:
                        if (!HasScent(currentX, currentY, map))
                        {
                            currentY--;
                        }
                        else if (map.GetMapPosition(currentX, currentY - 1) != deathCell)
                        {
                            currentY--;
                        }
                        else
                        {
                            response.Message = $"Suicide at {currentX}, {currentY - 1} is forbidden";
                        }
                        break;

                    case GetResponse.Directions.West:
                        if (!HasScent(currentX, currentY, map))
                        {
                            currentX--;
                        }
                        else if (map.GetMapPosition(currentX - 1, currentY) != deathCell)
                        {
                            currentX--;
                        }
                        else
                        {
                            response.Message = $"Suicide at {currentX - 1}, {currentY} is forbidden";
                        }
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }

                    currentScent = HasScent(currentX, currentY, map);

                    if (map.GetMapPosition(currentX, currentY)
                        .Equals(deathCell, StringComparison.InvariantCultureIgnoreCase))
                    {
                        map.MarkAsScent(previousCell[0], previousCell[1]);
                        Death();
                        response.Message = $"Robot died at {previousCell[0]}, {previousCell[1]}, cell marked as {scentCell} Dangerous";
                    }
                }
            }

            response.X         = currentX;
            response.Y         = currentY;
            response.Scent     = currentScent;
            response.Direction = currentDirection;

            return(response);
        }