protected override ValidationResult IsValid(object value, ValidationContext validationContext) { var setupShips = (SetupShips)validationContext.ObjectInstance; var setupBoard = new SetupBoard(); Ship thisShip = null; foreach (var ship in setupShips.GetValidStructuralShips()) { if (ship.propertyName == validationContext.MemberName) { thisShip = ship.ship; continue; } // Add all other ships setupBoard.AddShip(ship.ship); } if (thisShip == null) // Isn't valid enough to test - TODO: Rework this to avoid the foreach, just exit { return(null); } // Now add target ship, to get error for this one specifically var result = setupBoard.AddShip(thisShip); if (!result.Success) { return(new ValidationResult(result.Error, new[] { validationContext.MemberName })); } return(null); }
public bool RunSetup(PlayerGrid playerGrid) { SetupBoard = new SetupBoard(); var error = string.Empty; while (!SetupBoard.IsValid) { var errorDisplay = error.Length > 0 ? $" [{error}]" : ""; var input = m_CommandInput.WaitForInput($"Enter coords of Ship length {SetupBoard.NextShip}, e.g. A0 A4{errorDisplay}"); // TODO: Note that you can enter in any order, so why ask? if (input.ToLowerInvariant() == "exit") { return(false); } if (!s_Regex.IsMatch(input)) { error = $"Input not recognised as Ship Coordinates '{input}'"; continue; } var coords = input.Split(' '); Ship ship; try { ship = new Ship(coords[0], coords[1]); } catch (ArgumentException ex) { error = ex.Message; continue; } error = string.Empty; var result = SetupBoard.AddShip(ship); if (!result.Success) { error = result.Error; } else { playerGrid.DrawShip(ship); } } return(true); }
public void AddShipFailsIfTooManyShipsForLength(int length) { // Given m_SetupBoard.AddShip(("E0", "E4")); Assert.True(m_SetupBoard.IsValid); var excessShip = new Ship("F0", $"F{length-1}"); Assert.Equal(length, excessShip.Length); // When var result = m_SetupBoard.AddShip(excessShip); // Then Assert.False(result.Success); Assert.Contains($"already enough ships of length {length}", result.Error); }