public void UpdateSegmentRange(SortedDictionary <Coordinate, Segment> segments) { foreach (var segment in segments) { if (!BattleshipExtensions.IsSegmentWithInGridRange(segment.Key.X, segment.Key.Y)) { throw new IndexOutOfRangeException(); } Segment item = segmentations.FirstOrDefault(q => q.Key.X == segment.Key.X && q.Key.Y == segment.Key.Y).Value; if (item != null) { if (item.IsEmpty & segment.Value.IsEmpty) { throw new ArgumentException(); } item.IsEmpty = false; if (segment.Value.Ship != null) { item.Ship = segment.Value.Ship; item.ShipDirection = segment.Value.ShipDirection; } } } }
public string GetUserInput(string userInput) { string message = string.Empty; if (userInput.Length > 3 || userInput.Length == 1 || string.IsNullOrEmpty(userInput)) { throw new ArgumentException(); } string userInputX = userInput.Substring(0, Index).ToUpper(); char.TryParse(userInputX, out char x); string userInputY = userInput.Substring(Index, userInput.Length - Index); int.TryParse(userInputY, out int y); if (!BattleshipExtensions.IsSegmentWithInGridRange(x, y)) { throw new ArgumentException(); } var segment = segmentation.GetSegment(x, y); if (segment.Character == Miss || segment.Character == Hit) { return(CoordinateTriedMessage); } Coordinate coordinate = new Coordinate(x, y); if (segment.Ship == null) { segment.IsEmpty = false; segment.Character = Miss; segmentation.UpdateSegment(coordinate, segment); message = MissMessage; playerStats.Miss++; } if (segment.Ship != null) { segment.IsEmpty = false; segment.Character = Hit; segment.Ship.ShipHit++; segmentation.UpdateSegment(coordinate, segment); message = HitMessage; playerStats.Hit++; if (segment.Ship.IsShipSunk) { playerStats.Sunk++; message = SunkMessage; } } return(message); }
public void AddSegment(Coordinate coordinate, Segment segment) { if (!BattleshipExtensions.IsSegmentWithInGridRange(coordinate.X, coordinate.Y)) { throw new IndexOutOfRangeException(); } this.segmentation.Add(coordinate, new Segment(segment.Character)); }
public void IsSegmentWithInGridRange_WhenYAxisIsWithinDimension_ReturnTrue() { // Arrange int x = XInitialPoint; int y = GridDimension; // Act bool result = BattleshipExtensions.IsSegmentWithInGridRange(x, y); // Assert Assert.IsTrue(result); }
public void IsSegmentWithInGridRange_WhenYAxisIsLessThanYAxisIndex_ReturnFalse() { // Arrange int x = XInitialPoint; int y = 0; // Act bool result = BattleshipExtensions.IsSegmentWithInGridRange(x, y); // Assert Assert.IsFalse(result); }
public void IsSegmentWithInGridRange_WhenXAxisIsGreaterThanDimension_ReturnFalse() { // Arrange int x = XInitialPoint + GridDimension + Index; int y = Index; // Act bool result = BattleshipExtensions.IsSegmentWithInGridRange(x, y); // Assert Assert.IsFalse(result); }
private bool AddToXAxis(int currentXPosition, int currentYPosition, IShip ship, ref SortedDictionary <Coordinate, Segment> temporarySegments) { bool result = false; // If the current segment position is valid add to the temporary list, otherwise clear the list and start again if (segments.IsSegmentAvailable(currentXPosition, currentYPosition) && BattleshipExtensions.IsSegmentWithInGridRange(currentXPosition, currentYPosition)) { temporarySegments.Add(new Coordinate(currentXPosition, currentYPosition), new Segment(ShipDirection.Horizontal, ship)); result = true; } else { this.Clear(temporarySegments); } return(result); }
public void UpdateSegment(Coordinate coordinate, Segment segment) { if (!BattleshipExtensions.IsSegmentWithInGridRange(coordinate.X, coordinate.Y)) { throw new IndexOutOfRangeException(); } Segment item = segmentations.FirstOrDefault(q => q.Key.X == coordinate.X && q.Key.Y == coordinate.Y).Value; if (item != null) { item.IsEmpty = false; item.Character = segment.Character; if (segment.Ship != null) { item.Ship = segment.Ship; item.ShipDirection = segment.ShipDirection; } } }