/// <summary> /// Attempt to add a boat to the <see cref="Port" />. /// </summary> /// <param name="boat">The <see cref="Boat" /> to be added.</param> /// <returns><see langword="true" /> if the <see cref="Boat" /> was accepted, otherwise <see langword="false" /></returns> /// <exception cref="ArgumentException">Thrown if the <see cref="Boat" /> ID is already in the port.</exception> public bool TryAdd(Boat boat) { if (Boats.Contains(boat)) { throw new ArgumentException("The current boat id is already in port.", nameof(boat)); } var result = false; foreach (Dock dock in algorithm(boat, docks)) { result = dock.TryAdd(boat); if (result) { break; } } return(result); }
public void GenerateBoat(int amount) { for (int boat = 0; boat < amount; boat++) { int avaliableSpace = 0; switch (Rand.Next(1, 5 + 1)) { case 1: SailBoat s = new SailBoat(); for (int i = 0; i < DockTwo.Length; i += 2) { if (DockTwo[i] == null) { avaliableSpace += 2; if (avaliableSpace == s.Slots) { Array.Fill(DockTwo, s, i + 2 - s.Slots, s.Slots); Boats.Add(s); s.Docked = true; break; } } else { avaliableSpace = 0; } } avaliableSpace = 0; if (s.Docked == false) { for (int i = 0; i < DockOne.Length; i += 2) { if (DockOne[i] == null) { avaliableSpace += 2; if (avaliableSpace == s.Slots) { Array.Fill(DockOne, s, i + 2 - s.Slots, s.Slots); Boats.Add(s); s.Docked = true; break; } } else { avaliableSpace = 0; } } } _ = Boats.Contains(s) ? AddedBoats++ : RejectedBoats++; break; case 2: RowingBoat r = new RowingBoat(); for (int i = 0; i < DockOne.Length; i++) { if (DockOne[i] == null) { Array.Fill(DockOne, r, i, r.Slots); Boats.Add(r); r.Docked = true; break; } else { avaliableSpace = 0; } } avaliableSpace = 0; if (r.Docked == false) { for (int i = 0; i < DockTwo.Length; i++) { if (DockTwo[i] == null) { Array.Fill(DockTwo, r, i, r.Slots); Boats.Add(r); r.Docked = true; break; } else { avaliableSpace = 0; } } } _ = Boats.Contains(r) ? AddedBoats++ : RejectedBoats++; break; case 3: PowerBoat p = new PowerBoat(); for (int i = 0; i < DockOne.Length; i += 2) { if (DockOne[i] == null) { avaliableSpace += 2; if (avaliableSpace > p.Slots) { Array.Fill(DockOne, p, i - p.Slots, p.Slots); Boats.Add(p); p.Docked = true; break; } } else { avaliableSpace = 0; } } avaliableSpace = 0; if (p.Docked == false) { for (int i = 0; i < DockTwo.Length; i += 2) { if (DockTwo[i] == null) { avaliableSpace += 2; if (avaliableSpace > p.Slots) { Array.Fill(DockTwo, p, i - p.Slots, p.Slots); Boats.Add(p); p.Docked = true; break; } } else { avaliableSpace = 0; } } } _ = Boats.Contains(p) ? AddedBoats++ : RejectedBoats++; break; case 4: Catamaran k = new Catamaran(); for (int i = 0; i < DockOne.Length; i += 2) { if (DockOne[i] == null) { avaliableSpace += 2; if (avaliableSpace == k.Slots) { Array.Fill(DockOne, k, i + 2 - k.Slots, k.Slots); Boats.Add(k); k.Docked = true; break; } } else { avaliableSpace = 0; } } if (k.Docked == false) { avaliableSpace = 0; for (int i = 0; i < DockTwo.Length; i += 2) { if (DockTwo[i] == null) { avaliableSpace += 2; if (avaliableSpace == k.Slots) { Array.Fill(DockTwo, k, i + 2 - k.Slots, k.Slots); Boats.Add(k); k.Docked = true; break; } } else { avaliableSpace = 0; } } } _ = Boats.Contains(k) ? AddedBoats++ : RejectedBoats++; break; case 5: CargoShip c = new CargoShip(); for (int i = DockTwo.Length - 1; i > 0; i -= 2) { if (DockTwo[i] == null) { avaliableSpace += 2; if (avaliableSpace == c.Slots) { Array.Fill(DockTwo, c, i - 2 + 1, c.Slots); Boats.Add(c); c.Docked = true; break; } } else { avaliableSpace = 0; } } if (c.Docked == false) { avaliableSpace = 0; for (int i = DockOne.Length - 1; i > 0; i -= 2) { if (DockOne[i] == null) { avaliableSpace += 2; if (avaliableSpace == c.Slots) { Array.Fill(DockOne, c, i - 2 + 1, c.Slots); Boats.Add(c); c.Docked = true; break; } } else { avaliableSpace = 0; } } } _ = Boats.Contains(c) ? AddedBoats++ : RejectedBoats++; break; } } }
public void GenerateBoat(int amount) { for (int boat = 0; boat < amount; boat++) { int avaliableSpace = 0; switch (Rand.Next(1, 5 + 1)) { case 1: SailBoat s = new SailBoat(); for (int i = 0; i < Docks.Length; i += 2) { if (Docks[i] == null) { avaliableSpace += 2; if (avaliableSpace > s.Slots) { TempInterval = i - s.Slots; Array.Fill(Docks, s, TempInterval, s.Slots); Boats.Add(s); s.Docked = true; break; } } else { avaliableSpace = 0; } } _ = Boats.Contains(s) ? AddedBoats++ : RejectedBoats++; break; case 2: RowingBoat r = new RowingBoat(); for (int i = 0; i < Docks.Length; i++) { if (Docks[i] == null) { Array.Fill(Docks, r, i, r.Slots); Boats.Add(r); r.Docked = true; break; } else { avaliableSpace = 0; } } _ = Boats.Contains(r) ? AddedBoats++ : RejectedBoats++; break; case 3: PowerBoat p = new PowerBoat(); for (int i = 0; i < Docks.Length; i += 2) { if (Docks[i] == null) { avaliableSpace += 2; if (avaliableSpace > p.Slots) { TempInterval = i - p.Slots; Array.Fill(Docks, p, TempInterval, p.Slots); Boats.Add(p); p.Docked = true; break; } } else { avaliableSpace = 0; } } _ = Boats.Contains(p) ? AddedBoats++ : RejectedBoats++; break; case 4: Catamaran k = new Catamaran(); for (int i = 0; i < Docks.Length; i += 2) { if (Docks[i] == null) { avaliableSpace += 2; if (avaliableSpace > k.Slots) { TempInterval = i - k.Slots; Array.Fill(Docks, k, TempInterval, k.Slots); Boats.Add(k); k.Docked = true; break; } } else { avaliableSpace = 0; } } _ = Boats.Contains(k) ? AddedBoats++ : RejectedBoats++; break; case 5: CargoShip c = new CargoShip(); for (int i = Docks.Length - 1; i > 0; i -= 2) { if (Docks[i] == null) { avaliableSpace += 2; if (avaliableSpace > c.Slots) { TempInterval = i + 1; Array.Fill(Docks, c, TempInterval, c.Slots); Boats.Add(c); c.Docked = true; break; } } else { avaliableSpace = 0; } } _ = Boats.Contains(c) ? AddedBoats++ : RejectedBoats++; break; } } }