Beispiel #1
0
            internal bool IsRoomReachable(HallId hallId, RoomId roomId, out int distance)
            {
                HallId left  = HallId.L + (int)roomId;
                HallId right = left + 1;

                distance = 2;

                while (hallId < left)
                {
                    hallId++;
                    distance += (hallId == HallId.L) ? 1 : 2;

                    if (Positions[(int)hallId] != null)
                    {
                        distance = 0;
                        return(false);
                    }
                }

                while (hallId > right)
                {
                    hallId--;
                    distance += (hallId == HallId.R) ? 1 : 2;

                    if (Positions[(int)hallId] != null)
                    {
                        distance = 0;
                        return(false);
                    }
                }

                return(true);
            }
Beispiel #2
0
            internal IEnumerable <(HallId id, int distance)> HallsAvailableFrom(RoomId roomId)
            {
                int distance = 2;

                for (HallId left = HallId.L + (int)roomId; left >= HallId.LL; left--)
                {
                    if (Positions[(int)left] != null)
                    {
                        break;
                    }

                    yield return(left, distance);

                    distance += (left == HallId.L) ? 1 : 2;
                }

                distance = 2;

                for (HallId right = HallId.AB + (int)roomId; right <= HallId.RR; right++)
                {
                    if (Positions[(int)right] != null)
                    {
                        break;
                    }

                    yield return(right, distance);

                    distance += (right == HallId.R) ? 1 : 2;
                }
            }
Beispiel #3
0
            private IEnumerable <Move> GetAvailableMoves()
            {
                for (RoomId roomId = 0; roomId != RoomId.Count; roomId++)
                {
                    Room room = Rooms[(int)roomId];

                    if (room.CanMoveOut)
                    {
                        Amphipod occupant = room.Peek();

                        RoomId destinationRoomId = occupant.Home;
                        Room   destinationRoom   = Rooms[(int)destinationRoomId];

                        if (destinationRoom.CanMoveIn && Hallway.IsRoomReachable(roomId, destinationRoomId, out int hallDistance))
                        {
                            yield return(new Move()
                            {
                                FromRoom = roomId,
                                ToRoom = destinationRoomId,
                                Cost = (room.MoveOutDistance + destinationRoom.MoveInDistance + hallDistance) * occupant.MoveCost,
                            });
                        }
                        else
                        {
                            foreach ((HallId hallId, int distance) in Hallway.HallsAvailableFrom(roomId))
                            {
                                yield return(new Move()
                                {
                                    FromRoom = roomId,
                                    ToHall = hallId,
                                    Cost = (room.MoveOutDistance + distance) * occupant.MoveCost,
                                });
                            }
                        }
                    }
                }

                for (HallId hallId = 0; hallId < HallId.Count; hallId++)
                {
                    Amphipod?occupantNullable = Hallway.Positions[(int)hallId];

                    if (occupantNullable.HasValue)
                    {
                        Amphipod occupant = occupantNullable.Value;
                        RoomId   roomId   = occupant.Home;
                        Room     room     = Rooms[(int)roomId];

                        if (room.CanMoveIn && Hallway.IsRoomReachable(hallId, roomId, out int hallDistance))
                        {
                            yield return(new Move()
                            {
                                FromHall = hallId,
                                ToRoom = roomId,
                                Cost = (hallDistance + room.MoveInDistance) * occupant.MoveCost,
                            });
                        }
                    }
                }
            }
Beispiel #4
0
            internal bool IsRoomReachable(RoomId begin, RoomId end, out int distance)
            {
                if ((int)begin > (int)end)
                {
                    (begin, end) = (end, begin);
                }

                HallId hallBegin = HallId.AB + (int)begin;
                HallId hallEnd   = HallId.L + (int)end;

                distance = 2;

                for (; hallBegin <= hallEnd; hallBegin++, distance += 2)
                {
                    if (Positions[(int)hallBegin] != null)
                    {
                        distance = 0;
                        return(false);
                    }
                }

                return(true);
            }
Beispiel #5
0
 public override string ToString()
 {
     return("Seat_id: " + Id + " Number: " + Number + " Row: " + Row + " Is free: " + IsFree.ToString()
            + "\n    Hall: " + Hall != null?Hall.ToString() : HallId.ToString());
 }
Beispiel #6
0
        public async Task <Reservation> CreateAsync(EntityId id, CinemaId cinemaId, MovieId movieId, HallId hallId, CustomerId customerId,
                                                    DateTime dateTime, bool isPaymentUponArrival, IEnumerable <Seat> seats, Reservee reservee)
        {
            var areSeatsValid = await _validator.ValidateAsync(cinemaId, movieId, hallId, seats);

            if (!areSeatsValid)
            {
                throw new SeatsAlreadyReservedException(id);
            }

            if (!customerId.IsEmpty())
            {
                reservee = await _provider.GetAsync(customerId);
            }

            var status      = isPaymentUponArrival ? ReservationStatus.PaymentUponArrival : ReservationStatus.Pending;
            var reservation = Reservation.Create(id, cinemaId, movieId, hallId, dateTime, status, reservee, seats);

            return(reservation);
        }
Beispiel #7
0
 public Task <bool> ExistsAsync(HallId hallId)
 => _repository.ExistsAsync(h => h.Id == hallId);
Beispiel #8
0
 public Show(HallId hallId, DateTime date, Time time)
 {
     HallId = hallId;
     Date   = date;
     Time   = time;
 }