Пример #1
0
        /// <summary>
        /// Deserializing cleaning robot state
        /// </summary>
        public CleaningRobotState Deserialize(string inputFileName)
        {
            string jsonText = File.ReadAllText(inputFileName);

            JObject jObject = JObject.Parse(jsonText);
            var     jToken  = jObject["start"];

            // Location
            RoomMapPoint roomMapPoint = new RoomMapPoint();

            roomMapPoint.X  = (int)jToken["X"];
            roomMapPoint.Y  = (int)jToken["Y"];
            _state.Location = roomMapPoint;

            // Facing
            string facing = jToken["facing"].ToString();

            switch (facing)
            {
            case "N":
                _state.Facing = CleaningRobotFacing.North;
                break;

            case "S":
                _state.Facing = CleaningRobotFacing.South;
                break;

            case "E":
                _state.Facing = CleaningRobotFacing.East;
                break;

            case "W":
                _state.Facing = CleaningRobotFacing.West;
                break;

            default:
                _state.Facing = CleaningRobotFacing.North;
                break;
            }

            // Battery
            jToken         = jObject["battery"];
            _state.Battery = (int)jToken;

            // Lists initializing
            _state.MapPointsVisited.Clear();
            _state.MapPointsCleaned.Clear();
            _state.MapPointsVisited.Add(roomMapPoint); // Add start location to visited list

            return(_state);
        }
Пример #2
0
        /// <summary>
        /// Robot moves back
        /// </summary>
        public CleaningRobotActionResult Back()
        {
            // Checking battery
            const int batteryUnits = 3;

            if (_state.Battery < batteryUnits)
            {
                return(CleaningRobotActionResult.BatteryDrained);
            }

            // Movement by coordinates
            int xMove = 0;
            int yMove = 0;

            // Facing change
            switch (_state.Facing)
            {
            case CleaningRobotFacing.North:
                yMove++;
                break;

            case CleaningRobotFacing.East:
                xMove--;
                break;

            case CleaningRobotFacing.South:
                yMove--;
                break;

            case CleaningRobotFacing.West:
                xMove++;
                break;
            }

            // New robot location
            int xNew = _state.Location.X + xMove;
            int yNew = _state.Location.Y + yMove;

            // Map size
            int rowCount    = _roomMap.Cells.GetLength(0);
            int columnCount = _roomMap.Cells.GetLength(1);

            // Cell outside map
            bool obstacle = xNew > rowCount - 1 || xNew <0 || yNew> columnCount - 1 || yNew < 0;

            // Cell inside map not accessible
            if (!obstacle)
            {
                obstacle = _roomMap.Cells[xNew, yNew] == RoomMapCellState.NotCleanable ||
                           _roomMap.Cells[xNew, yNew] == RoomMapCellState.Empty;
            }

            // Battery draining
            _state.Battery = _state.Battery - batteryUnits;

            if (!obstacle)
            {
                // Successfull action

                // New location
                RoomMapPoint roomMapPoint = new RoomMapPoint();
                roomMapPoint.X = xNew;
                roomMapPoint.Y = yNew;
                State.Location = roomMapPoint;

                // Appending visited list (if not visited before)
                if (!_state.MapPointsVisited.Contains(roomMapPoint))
                {
                    _state.MapPointsVisited.Add(roomMapPoint);
                }

                return(CleaningRobotActionResult.Success);
            }
            else
            {
                // Obstacle
                return(CleaningRobotActionResult.Obstacle);
            }
        }