public Car(int xPos, int yPos, City city, Passenger passenger) { XPos = xPos; YPos = yPos; City = city; Passenger = passenger; }
public override void MoveLeft(Passenger prospectivePassenger) { if (MoveInXDirection(prospectivePassenger, MaxSpacesCanMove, false)) { WritePositionToConsole(); } }
public override void MoveUp(Passenger prospectivePassenger) { if (MoveInYDirection(prospectivePassenger, MaxSpacesCanMove, true)) { WritePositionToConsole(); } }
/// <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()); } }
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; }
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; }
public abstract void MoveDown(Passenger prospectivePassenger);
public int GetTargetYPos(Passenger prospectivePassenger) { return HasPassenger ? Passenger.DestinationYPos : prospectivePassenger.StartingYPos; }
public void PickupPassenger(Passenger passenger) { Passenger = passenger; }
public abstract void MoveRight(Passenger prospectivePassenger);
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; }
public Sedan(int xPos, int yPos, City city, Passenger passenger) : base(xPos, yPos, city, passenger) { }
public RaceCar(int xPos, int yPos, City city, Passenger passenger) : base(xPos, yPos, city, passenger) { }
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; }
public Motorcycle(int xPos, int yPos, City city, Passenger passenger) : base(xPos, yPos, city, passenger) { }
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; }