public void AddFullShipsCorrectly() { var ships = new[] { new { Type = ShipType.Battleship, Start = new CellPosition(0, 0), Vertical = true }, new { Type = ShipType.Cruiser, Start = new CellPosition(5, 2), Vertical = false }, new { Type = ShipType.Submarine, Start = new CellPosition(9, 4), Vertical = true } }; var cells = new[] { new CellPosition(0, 0), new CellPosition(1, 0), new CellPosition(2, 0), new CellPosition(3, 0), new CellPosition(5, 2), new CellPosition(5, 3), new CellPosition(5, 4), new CellPosition(9, 4) }; foreach (var ship in ships) { builder.TryAddFullShip(ship.Type, ship.Start, ship.Vertical).Should().BeTrue(); } foreach (var position in builder.EnumeratePositions()) { builder[position].Should().Be(cells.Contains(position)); } }
protected IGameField GenerateNewPrediction() { var builder = new GameFieldBuilder(); foreach (var position in OpponentFieldKnowledge.EnumeratePositions()) { if (OpponentFieldKnowledge[position] == true) { builder.TryAddShipCell(position); } } var generator = new RandomFieldGenerator(builder); var damagedShip = FindDamagedShip().ToList(); if (damagedShip.Any()) { foreach (var cell in damagedShip) { builder.TryRemoveShipCell(cell); } var variants = new[] { 4, 3, 2, 1 }.SelectMany(x => new[] { new { Ship = (ShipType)x, Vertical = true }, new { Ship = (ShipType)x, Vertical = false } }).SelectMany(x => GenerateContinuesForDamagedShip(damagedShip, builder, x.Vertical, x.Ship)); foreach (var variant in variants) { builder.TryAddFullShip(variant.Item1, variant.Item2, variant.Item3); var prediction = generator.Generate(x => OpponentFieldKnowledge[x] != false); if (prediction != null) { return(prediction); } builder.TryRemoveFullShip(variant.Item1, variant.Item2, variant.Item3); } } return(generator.Generate(x => OpponentFieldKnowledge[x] != false)); }