コード例 #1
0
ファイル: ProcessorTests.cs プロジェクト: CatSkald/Roguelike
        public void Process_PainterDrawMapIsCalledWithCorrectMapImage()
        {
            var parameters = new DungeonParameters();
            var dungeon = new FakeDungeon(5, 4);
            var mapBuilder = Substitute.For<IMapBuilder>();
            mapBuilder.Build(parameters).Returns(dungeon);
            var populator = Substitute.For<IDungeonPopulator>();
            var character = new Character
            {
                Location = new Point(1, 1)
            };
            populator.WhenForAnyArgs(it => it.Fill(Arg.Any<IGameDungeon>()))
                .Do(d =>
                {
                    d.Arg<IGameDungeon>().Character = character;
                });
            var painter = Substitute.For<IMapPainter>();

            var processor = new Processor(
                mapBuilder,
                populator,
                painter);
            processor.Initialize(parameters);
            processor.Process(GameAction.None);

            painter.Received(1).DrawMap(Arg.Is<MapImage>(d =>
                d.Width == dungeon.Width
                && d.Height == dungeon.Height
                && d[1, 1].Type == XType.Character));
        }
コード例 #2
0
        protected override void ExecuteCommand(IMap map, DungeonParameters parameters)
        {
            var sparseFactor = parameters.CellSparseFactor;
            var expectedNumberOfRemovedCells =
                   (int)Math.Ceiling(map.Size * (sparseFactor / 100m)) - 1;

            var removedCellsCount = 0;
            var nonWalls = map.Where(c => !c.IsWall).ToList();
            if (!nonWalls.Any())
            {
                throw new InvalidOperationException("All cells are walls.");
            }

            while (removedCellsCount < expectedNumberOfRemovedCells)
            {
                foreach (var cell in nonWalls.Where(c => c.IsDeadEnd).ToList())
                {
                    if (!cell.IsDeadEnd)
                        continue;

                    var emptySide = cell.Sides
                        .Single(s => s.Value != Side.Wall)
                        .Key;

                    map.CreateWall(cell, emptySide);
                    cell.IsCorridor = false;
                    nonWalls.Remove(cell);
                    removedCellsCount++;
                }
            }
        }
コード例 #3
0
        public void Execute_ShouldThrow_IfMapNull()
        {
            Map map = null;
            DungeonParameters parameters = new DungeonParameters();
            var command = new FakeMapBuilderCommand();

            Assert.That(() => command.Execute(map, parameters),
                Throws.ArgumentNullException);
        }
コード例 #4
0
        public void Execute(IMap map, DungeonParameters parameters)
        {
            Throw.IfNull(map, nameof(map));
            Throw.IfNull(parameters, nameof(parameters));
            Throw.IfNull(parameters.RoomParameters, nameof(parameters.RoomParameters));
            ValidateParameters(parameters);

            ExecuteCommand(map, parameters);
        }
コード例 #5
0
ファイル: Processor.cs プロジェクト: CatSkald/Roguelike
        public void Initialize(DungeonParameters parameters)
        {
            var dungeonMap = builder.Build(parameters);
            var dungeon = Convert(dungeonMap);

            populator.Fill(dungeon);
            Dungeon = dungeon;
            Log.Debug("Dungeon initialized.");
        }
コード例 #6
0
 protected override void ExecuteCommand(
     IMap map, DungeonParameters parameters)
 {
     var rooms = CreateRooms(parameters.RoomParameters);
     foreach (var room in rooms)
     {
         PlaceRoom(map, room);
     }
 }
コード例 #7
0
        public void Execute_ShouldCallValidateParameters()
        {
            var map = new Map(3, 4);
            DungeonParameters parameters = new DungeonParameters();
            var command = new FakeMapBuilderCommand();

            command.Execute(map, parameters);

            Assert.That(command.ValidateParametersCalls, Is.EqualTo(1));
        }
コード例 #8
0
        public void Execute_SetsAllCells_ToVisited()
        {
            var map = new Map(2, 3);
            var parameters = new DungeonParameters();
            var command = new CorridorBuilderCommand(new DirectionPicker(50));

            command.Execute(map, parameters);

            Assert.That(map, Has.All.With.Property(nameof(MapCell.IsVisited)).True);
        }
コード例 #9
0
 protected override void ExecuteCommand(IMap map, DungeonParameters parameters)
 {
     var _sparseFactor = parameters.DeadEndSparseFactor;
     foreach (var cell in map)
     {
         if (cell.IsDeadEnd && _sparseFactor > StaticRandom.Next(0, 100))
         {
             BuildCorridor(map, cell);
         }
     }
 }
コード例 #10
0
        public void Execute_ShouldThrow_IfMapContainsVisitedCell(int x, int y)
        {
            var map = new Map(2, 3);
            var parameters = new DungeonParameters();
            var command = new CorridorBuilderCommand(new DirectionPicker(50));

            map.Visit(map[x, y]);

            Assert.That(() => command.Execute(map, parameters),
                Throws.InvalidOperationException);
        }
コード例 #11
0
        public void Execute_ShouldThrow_IfTwistFactorIsInvalid(int factor)
        {
            var map = new Map(2, 3);
            var parameters = new DungeonParameters
            {
                TwistFactor = factor
            };
            var command = new CorridorBuilderCommand(new DirectionPicker(50));

            Assert.That(() => command.Execute(map, parameters),
                Throws.InstanceOf<ArgumentOutOfRangeException>());
        }
コード例 #12
0
        public void Execute_ShouldThrow_IfRoomParametersNull()
        {
            var map = new Map(3, 4);
            DungeonParameters parameters = new DungeonParameters
            {
                RoomParameters = null
            };
            var command = new FakeMapBuilderCommand();

            Assert.That(() => command.Execute(map, parameters),
                Throws.ArgumentNullException);
        }
コード例 #13
0
ファイル: ProcessorTests.cs プロジェクト: CatSkald/Roguelike
        public void Initialize_BuilderIsCalled()
        {
            var parameters = new DungeonParameters();
            var mapBuilder = Substitute.For<IMapBuilder>();

            var processor = new Processor(
                mapBuilder,
                Substitute.For<IDungeonPopulator>(),
                Substitute.For<IMapPainter>());
            processor.Initialize(parameters);

            mapBuilder.Received(1).Build(parameters);
        }
コード例 #14
0
ファイル: MapBuilder.cs プロジェクト: CatSkald/Roguelike
        public IDungeon Build(DungeonParameters parameters)
        {
            Throw.IfNull(parameters, nameof(parameters));
            Throw.IfLess(1, parameters.Width, nameof(parameters.Width));
            Throw.IfLess(1, parameters.Height, nameof(parameters.Height));

            var map = new Map(parameters.Width, parameters.Height);

            foreach (var command in commands)
            {
                command.Execute(map, parameters);
            }

            return converter.ConvertToDungeon(map);
        }
コード例 #15
0
ファイル: ProcessorTests.cs プロジェクト: CatSkald/Roguelike
        public void Initialize_PopulatorIsCalled()
        {
            var parameters = new DungeonParameters();
            var dungeon = new FakeDungeon();
            var mapBuilder = Substitute.For<IMapBuilder>();
            mapBuilder.Build(parameters).Returns(dungeon);
            var populator = Substitute.For<IDungeonPopulator>();

            var processor = new Processor(
                mapBuilder,
                populator,
                Substitute.For<IMapPainter>());
            processor.Initialize(parameters);

            populator.Received(1).Fill(
                Arg.Is<IGameDungeon>(d => d.Size == dungeon.Size));
        }
コード例 #16
0
        public void Constructor_ValidateParameters_Throws_IfMaxHeightLessMin(
            int min, int max)
        {
            var map = new Map(5, 5);
            var parameters = new DungeonParameters
            {
                RoomParameters =
                {
                    MinHeight = min,
                    MaxHeight = max
                }
            };

            Assert.That(() => _command.Execute(map, parameters),
                Throws.InstanceOf<ArgumentOutOfRangeException>()
                    .With.Property(nameof(ArgumentOutOfRangeException.ParamName))
                    .EqualTo(nameof(RoomParameters.MaxHeight)));
        }
コード例 #17
0
 public void Constructor_ValidateParameters_Succeeds_IfParametersOK()
 {
     var map = new Map(5, 5);
     var parameters = new DungeonParameters
     {
         CellSparseFactor = 50,
         DeadEndSparseFactor = 60,
         TwistFactor = 45,
         Height = 11,
         Width = 15,
         RoomParameters =
         {
             Count = 2,
             MaxHeight = 5,
             MaxWidth = 5,
             MinHeight = 5,
             MinWidth = 4
         }
     };
     Assert.That(() => _command.Execute(map, parameters), Throws.Nothing);
 }
コード例 #18
0
        protected override void ValidateParameters(DungeonParameters parameters)
        {
            var roomParameters = parameters.RoomParameters;

            Throw.IfNull(roomParameters, nameof(roomParameters));

            Throw.IfLess(0, roomParameters.Count, nameof(roomParameters.Count));

            Throw.IfLess(0, roomParameters.MinWidth, nameof(roomParameters.MinWidth));
            Throw.IfLess(0, roomParameters.MinHeight, nameof(roomParameters.MinHeight));

            Throw.IfLess(roomParameters.MinWidth, roomParameters.MaxWidth,
                nameof(roomParameters.MaxWidth));
            Throw.IfLess(roomParameters.MinHeight, roomParameters.MaxHeight,
                nameof(roomParameters.MaxHeight));

            Throw.IfGreater(parameters.Width, roomParameters.MaxWidth,
                nameof(roomParameters.MaxWidth));
            Throw.IfGreater(parameters.Height, roomParameters.MaxHeight,
                nameof(roomParameters.MaxHeight));
        }
コード例 #19
0
 protected override void ExecuteCommand(IMap map, DungeonParameters parameters)
 {
     BuildCorridors(map);
 }
コード例 #20
0
 protected abstract void ExecuteCommand(IMap map, DungeonParameters parameters);
コード例 #21
0
        public void Constructor_ValidateParameters_Throws_IfMaxWidthGreaterThanWidth(
            int maxW, int w)
        {
            var map = new Map(5, 5);
            var parameters = new DungeonParameters
            {
                Width = w,
                RoomParameters =
                {
                    MaxWidth = maxW
                }
            };

            Assert.That(() => _command.Execute(map, parameters),
                Throws.InstanceOf<ArgumentOutOfRangeException>()
                    .With.Property(nameof(ArgumentOutOfRangeException.ParamName))
                    .EqualTo(nameof(RoomParameters.MaxWidth)));
        }
コード例 #22
0
        public void SetUp()
        {
            _map = new Map(15, 20);
            _parameters = new DungeonParameters
            {
                CellSparseFactor = 50,
                DeadEndSparseFactor = 60,
                TwistFactor = 45,
                Height = 20,
                Width = 15,
                RoomParameters =
                {
                    Count = 2,
                    MaxHeight = 5,
                    MaxWidth = 5,
                    MinHeight = 5,
                    MinWidth = 4
                }
            };
            _command = new PlaceRoomsCommand();

            var corridorBuilderCommand = new CorridorBuilderCommand(
                new DirectionPicker(_parameters.TwistFactor));
            corridorBuilderCommand.Execute(_map, _parameters);
        }
コード例 #23
0
        public void Constructor_ValidateParameters_Throws_IfRoomCountNegative(int count)
        {
            var map = new Map(5, 5);
            var parameters = new DungeonParameters
            {
                RoomParameters =
                {
                    Count = count
                }
            };

            Assert.That(() => _command.Execute(map, parameters),
                Throws.InstanceOf<ArgumentOutOfRangeException>()
                    .With.Property(nameof(ArgumentOutOfRangeException.ParamName))
                    .EqualTo(nameof(RoomParameters.Count)));
        }
コード例 #24
0
 protected abstract void ValidateParameters(DungeonParameters parameters);
コード例 #25
0
        public void Constructor_RoomParameters_AreNotNull()
        {
            var parameters = new DungeonParameters();

            Assert.That(parameters.RoomParameters, Is.Not.Null);
        }
コード例 #26
0
ファイル: ProcessorTests.cs プロジェクト: CatSkald/Roguelike
        public void Process_PainterDrawMessageIsCalled()
        {
            var parameters = new DungeonParameters();
            var dungeon = new FakeDungeon(5, 4);
            var mapBuilder = Substitute.For<IMapBuilder>();
            mapBuilder.Build(parameters).Returns(dungeon);
            var populator = Substitute.For<IDungeonPopulator>();
            var character = new Character
            {
                Location = new Point(1, 1)
            };
            populator.WhenForAnyArgs(it => it.Fill(Arg.Any<IGameDungeon>()))
                .Do(d =>
                {
                    d.Arg<IGameDungeon>().Character = character;
                });
            var painter = Substitute.For<IMapPainter>();

            var processor = new Processor(
                mapBuilder,
                populator,
                painter);
            processor.Initialize(parameters);
            processor.Process(GameAction.None);

            ////TODO test precise message
            painter.Received(1).DrawMessage(Arg.Any<GameMessage>());
        }
コード例 #27
0
 protected override void ValidateParameters(DungeonParameters parameters)
 {
     Throw.IfNotInRange(0, 100, parameters.DeadEndSparseFactor,
         nameof(parameters.DeadEndSparseFactor));
 }
コード例 #28
0
ファイル: MapBuilderTests.cs プロジェクト: CatSkald/Roguelike
 public void SetUp()
 {
     _params = new DungeonParameters
     {
         Width = 10,
         Height = 10,
         RoomParameters = new RoomParameters()
     };
     _builder = new MapBuilder(new List<IMapBuilderCommand>(),
         Substitute.For<IMapConverter>());
 }
コード例 #29
0
 public void SetUp()
 {
     _map = new Map(12, 16);
     _parameters = new DungeonParameters
     {
         TwistFactor = 50,
         DeadEndSparseFactor = 50
     };
     _command = new SparsifyDeadEndsCommand(new DirectionPicker(0));
 }
コード例 #30
0
 public void SetUp()
 {
     _map = new Map(8, 6);
     _parameters = new DungeonParameters
     {
         TwistFactor = 50,
         CellSparseFactor = 50
     };
     _command = new SparsifyCellsCommand();
 }