Exemple #1
0
 public Car(int xPos, int yPos, City city, Passenger passenger)
 {
     XPos = xPos;
     YPos = yPos;
     City = city;
     Passenger = passenger;
 }
Exemple #2
0
 public override void MoveLeft(Passenger prospectivePassenger)
 {
     if (MoveInXDirection(prospectivePassenger, MaxSpacesCanMove, false))
     {
         WritePositionToConsole();
     }
 }
Exemple #3
0
 public override void MoveUp(Passenger prospectivePassenger)
 {
     if (MoveInYDirection(prospectivePassenger, MaxSpacesCanMove, true))
     {
         WritePositionToConsole();
     }
 }
Exemple #4
0
        /// <summary>
        /// Takes one action (move the car one spot or pick up the passenger).
        /// </summary>
        /// <param name="car">The car to move</param>
        /// <param name="passenger">The passenger to pick up</param>
        private static async Task TickAsync(Car car, Passenger passenger)
        {
            var hasCarMoved = false;

            DirectionToMove directionToMove = GetDirectionToMove(car, passenger);

            switch (directionToMove)
            {
                case DirectionToMove.DoNothing:
                    break;
                case DirectionToMove.Left:
                    car.MoveLeft(passenger);
                    hasCarMoved = true;
                    break;
                case DirectionToMove.Right:
                    car.MoveRight(passenger);
                    hasCarMoved = true;
                    break;
                case DirectionToMove.Up:
                    car.MoveUp(passenger);
                    hasCarMoved = true;
                    break;
                case DirectionToMove.Down:
                    car.MoveDown(passenger);
                    hasCarMoved = true;
                    break;
                case DirectionToMove.Arrived:
                    if (!car.HasPassenger)
                    {
                        passenger.GetInCar(car);
                    }
                    else
                    {
                        passenger.GetOutOfCar();
                    }
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            if (hasCarMoved)
            {
                Console.WriteLine("\tCar moved, downloading 2PointB Website");
                var downloadedBytes = await Task.Run(() => Get2PointBWebsiteInBytesAsync());
            }
        }
Exemple #5
0
        public Car Create(CarType type, int xPos, int yPos, City city, Passenger passenger)
        {
            Car car;

            switch (type)
            {
                case CarType.Sedan:
                    car = new Sedan(xPos, yPos, city, passenger);
                    break;
                case CarType.RaceCar:
                    car = new RaceCar(xPos, yPos, city, passenger);
                    break;
                case CarType.Motorcycle:
                    car = new Motorcycle(xPos, yPos, city, passenger);
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }

            return car;
        }
Exemple #6
0
        public bool MoveInXDirection(Passenger prospectivePassenger, int numOfSpaces, bool isRight)
        {
            if (numOfSpaces > 0)
            {
                var oldXPos = XPos;

                if (isRight)
                {

                    var possibleIndex = YPos * City.YMax + (XPos + numOfSpaces);
                    if (XPos + numOfSpaces <= City.XMax
                        && XPos + numOfSpaces <= GetTargetXPos(prospectivePassenger))
                    {
                        XPos += numOfSpaces;

                        UpdateCityPosition(oldXPos, YPos);
                        return true;
                    }
                }
                else
                {
                    var possibleIndex = YPos * City.YMax + (XPos - numOfSpaces);
                    if (XPos - numOfSpaces >= 0
                        && XPos - numOfSpaces >= GetTargetXPos(prospectivePassenger))
                    {
                        XPos -= numOfSpaces;

                        UpdateCityPosition(oldXPos, YPos);
                        return true;
                    }
                }

                return MoveInXDirection(prospectivePassenger, numOfSpaces - 1, isRight);
            }

            return false;
        }
Exemple #7
0
 public abstract void MoveDown(Passenger prospectivePassenger);
Exemple #8
0
 public int GetTargetYPos(Passenger prospectivePassenger)
 {
     return HasPassenger
         ? Passenger.DestinationYPos
         : prospectivePassenger.StartingYPos;
 }
Exemple #9
0
 public void PickupPassenger(Passenger passenger)
 {
     Passenger = passenger;
 }
Exemple #10
0
 public abstract void MoveRight(Passenger prospectivePassenger);
Exemple #11
0
        public bool MoveInYDirection(Passenger prospectivePassenger, int numOfSpaces, bool isDown)
        {
            if (numOfSpaces > 0)
            {
                var oldYPos = YPos;

                if (isDown)
                {
                    var possibleIndex = (YPos + numOfSpaces) * City.YMax + XPos;
                    if (YPos + numOfSpaces <= City.YMax
                        && YPos + numOfSpaces <= GetTargetYPos(prospectivePassenger))
                    {
                        YPos += numOfSpaces;

                        UpdateCityPosition(XPos, oldYPos);
                        return true;
                    }
                }
                else
                {

                    var possibleIndex = (YPos - numOfSpaces) * City.YMax + XPos;
                    if (YPos - numOfSpaces >= 0
                        && YPos - numOfSpaces >= GetTargetYPos(prospectivePassenger))
                    {
                        YPos -= numOfSpaces;

                        UpdateCityPosition(XPos, oldYPos);
                        return true;
                    }
                }

                return MoveInYDirection(prospectivePassenger, numOfSpaces - 1, isDown);
            }

            return false;
        }
Exemple #12
0
 public Sedan(int xPos, int yPos, City city, Passenger passenger)
     : base(xPos, yPos, city, passenger)
 {
 }
Exemple #13
0
 public RaceCar(int xPos, int yPos, City city, Passenger passenger)
     : base(xPos, yPos, city, passenger)
 {
 }
Exemple #14
0
        private static DirectionToMove GetDirectionToMove(Car car, Passenger passenger)
        {
            int targetXPos = car.GetTargetXPos(passenger);
            int targetYPos = car.GetTargetYPos(passenger);

            int distanceXPos = Math.Abs(targetXPos - car.XPos);
            int distanceYPos = Math.Abs(targetYPos - car.YPos);

            if (distanceXPos == 0 && distanceYPos == 0)
            {
                return DirectionToMove.Arrived;
            }
            if (distanceYPos > distanceXPos)
            {
                return car.YPos > targetYPos
                    ? DirectionToMove.Down
                    : DirectionToMove.Up;
            }

            return car.XPos > targetXPos
                ? DirectionToMove.Left
                : DirectionToMove.Right;
        }
Exemple #15
0
 public Motorcycle(int xPos, int yPos, City city, Passenger passenger)
     : base(xPos, yPos, city, passenger)
 {
 }
Exemple #16
0
        public Passenger AddPassengerToCity(int startXPos, int startYPos, int destXPos, int destYPos)
        {
            Passenger passenger = new Passenger(startXPos, startYPos, destXPos, destYPos, this);

            CityPositions[startYPos * YMax + startXPos].Passenger = passenger;
            CityPositions[destYPos * YMax + destXPos].IsDestinationPosition = true;

            Passenger = passenger;

            return passenger;
        }