Example #1
0
        public void NotBuildField_WhenThereAreTooManyShips()
        {
            shipsCount = new Dictionary <ShipType, int> {
                { ShipType.Submarine, 1 }, { ShipType.Destroyer, 2 }
            };
            rules   = new GameRules(fieldSize, shipsCount);
            builder = new GameFieldBuilder(rules);

            var cells = new[]
            {
                new CellPosition(0, 0),
                new CellPosition(7, 2),
                new CellPosition(9, 7),

                new CellPosition(6, 4),
                new CellPosition(6, 5),

                new CellPosition(2, 1),
                new CellPosition(2, 2)
            };

            foreach (var cell in cells)
            {
                builder.TryAddShipCell(cell);
            }

            foreach (var shipType in Enum.GetValues(typeof(ShipType)).Cast <ShipType>())
            {
                shipsCount[shipType] = 0;
            }

            builder.Build().Should().BeNull();
        }
Example #2
0
        public void BuildField_WhenDataCorrect()
        {
            shipsCount = new Dictionary <ShipType, int> {
                { ShipType.Submarine, 3 }, { ShipType.Destroyer, 2 }
            };
            rules   = new GameRules(fieldSize, shipsCount);
            builder = new GameFieldBuilder(rules);

            var cells = new[]
            {
                new CellPosition(0, 0),
                new CellPosition(7, 2),
                new CellPosition(9, 7),

                new CellPosition(6, 4),
                new CellPosition(6, 5),

                new CellPosition(2, 1),
                new CellPosition(2, 2)
            };

            foreach (var cell in cells)
            {
                builder.TryAddShipCell(cell);
            }

            builder.Build().Should().NotBeNull();
        }
Example #3
0
        public void ReturnCorrectShipsLeftCounter_WhenAllShipsUsed()
        {
            shipsCount = new Dictionary <ShipType, int> {
                { ShipType.Submarine, 3 }, { ShipType.Destroyer, 2 }
            };
            rules   = new GameRules(fieldSize, shipsCount);
            builder = new GameFieldBuilder(rules);

            var cells = new[]
            {
                new CellPosition(0, 0),
                new CellPosition(7, 2),
                new CellPosition(9, 7),

                new CellPosition(6, 4),
                new CellPosition(6, 5),

                new CellPosition(2, 1),
                new CellPosition(2, 2)
            };

            foreach (var cell in cells)
            {
                builder.TryAddShipCell(cell);
            }

            foreach (var shipType in Enum.GetValues(typeof(ShipType)).Cast <ShipType>())
            {
                shipsCount[shipType] = 0;
            }

            builder.ShipsLeft.Should().BeEquivalentTo(shipsCount);
        }
Example #4
0
        private static IGameField FromLines(GameRules rules, string[] lines)
        {
            var builder = new GameFieldBuilder(rules);

            for (var row = 0; row < rules.FieldSize.Height; row++)
            {
                for (var column = 0; column < rules.FieldSize.Width; column++)
                {
                    if (lines[row][column] == 'X')
                    {
                        builder.TryAddShipCell(new CellPosition(row, column));
                    }
                }
            }
            return(builder.Build());
        }
Example #5
0
        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));
        }
Example #6
0
        public void BuildCorrectField()
        {
            shipsCount = new Dictionary <ShipType, int> {
                { ShipType.Submarine, 3 }, { ShipType.Destroyer, 2 }
            };
            rules   = new GameRules(fieldSize, shipsCount);
            builder = new GameFieldBuilder(rules);

            var cells = new[]
            {
                new CellPosition(0, 0),
                new CellPosition(7, 2),
                new CellPosition(9, 7),

                new CellPosition(6, 4),
                new CellPosition(6, 5),

                new CellPosition(2, 1),
                new CellPosition(2, 2)
            };

            foreach (var cell in cells)
            {
                builder.TryAddShipCell(cell);
            }

            foreach (var shipType in Enum.GetValues(typeof(ShipType)).Cast <ShipType>())
            {
                shipsCount[shipType] = 0;
            }

            var field = builder.Build();

            foreach (var cell in field.EnumeratePositions())
            {
                cells.Contains(cell).Should().Be(field[cell] is IShipCell);
            }
        }
Example #7
0
 public void ClearFieldCorrectly()
 {
     for (var i = 0; i < 30; i++)
     {
         builder.TryAddShipCell(CellPosition.Random(fieldSize));
     }
     builder.Clear();
     builder.EnumeratePositions().Select(x => builder[x]).ShouldAllBeEquivalentTo(false);
 }