private bool AddToTable(Ship ship, out string error) { var currentItem = _shipConfig.Peek(); if (!ShipValidator.IsValid(_currentPoints, currentItem.Item2, ship, out error)) { return false; } var shipActor = Context.ActorOf(Props.Create(() => new ShipActor(ship, _gameToken))); _ships.Add(ship); foreach (var point in ship.Points) { _pointActors[point] = shipActor; _currentPoints[_currentPoints.IndexOf(point)] = point; } error = null; return true; }
public ShipActor(Ship ship, Guid gameToken) { _gameToken = gameToken; _points = new HashSet<Point>(ship.Points); Receive<Message.Missile>(message => message.GameToken == _gameToken, message => { _points.Remove(message.Point); var point = PointHasHit(message.Point); if (_points.Count == 0) { Context.Parent.Tell(new Message.ShipDestroyed(Guid.Empty, _gameToken, point), Self); Become(Destroyed); } else { Context.Parent.Tell(new Message.PartOfTheShipDestroyed(Guid.Empty, _gameToken, point), Self); } }); }
public static bool IsValid(IReadOnlyList<Point> allPoints, int currentShipLength, Ship ship, out string error) { if (ship.Length != currentShipLength) { error = "The given ship length is not " + currentShipLength; return false; } if (allPoints.Where(d => d.HasShip).Intersect(ship.Points).Any()) { error = "The given ship overlaps with the existing ship."; return false; // point already exists } if (ship.Points.Any(point => HasShipNextDoor(allPoints, point))) { error = "The ship is next to another ship."; return false; } error = null; return true; }
public ShipPosition(Guid token, Guid gameToken, Ship ship) : base(token, gameToken) { Ship = ship; }