Beispiel #1
0
 private static int GetAboveRowIndex(ReadOnlyGrid grid, int cellRowIndex)
 {
     if (cellRowIndex - 1 < 0)
     {
         return(grid.Rows.Count - 1);
     }
     return(cellRowIndex - 1);
 }
Beispiel #2
0
 private static int GetLeftColumnIndex(ReadOnlyGrid grid, CellPosition cellPosition)
 {
     if (cellPosition.Column - 1 < 0)
     {
         return(grid.Rows[cellPosition.Row].Count - 1);
     }
     return(cellPosition.Column - 1);
 }
Beispiel #3
0
 private static int GetBelowRowIndex(ReadOnlyGrid grid, int cellRowIndex)
 {
     if (cellRowIndex + 1 > grid.Rows.Count - 1)
     {
         return(0);
     }
     return(cellRowIndex + 1);
 }
Beispiel #4
0
 private static int GetRightColumnIndex(ReadOnlyGrid grid, CellPosition cellPosition)
 {
     if (cellPosition.Column + 1 > grid.Rows[cellPosition.Row].Count - 1)
     {
         return(0);
     }
     return(cellPosition.Column + 1);
 }
Beispiel #5
0
        public void Templates_Should_Be_Converted(string stringRepresentation, Grid expected)
        {
            var grid = StringTemplateToGridConverter.Convert(stringRepresentation);

            var readOnlyGrid     = new ReadOnlyGrid(grid);
            var readOnlyExpected = new ReadOnlyGrid(expected);

            Assert.True(readOnlyGrid.Equals(readOnlyExpected));
        }
Beispiel #6
0
        public void CanPlaceBasic()
        {
            Labrys.Generation.Grid rawGrid = new Labrys.Generation.Grid();

            rawGrid[0, 0] = Section.Default();

            // Wrap the raw grid in a readonly version
            ReadOnlyGrid grid = new ReadOnlyGrid(rawGrid);

            // Basic 2x2 feature
            Feature feature = new Feature();

            feature.Add(0, 0);
            feature.Add(1, 0);
            feature.Add(0, 1);
            feature.Add(1, 1);

            // Placing it in any configuration over the blocking tile should fail

            // This tests for placing the Feature over different grid positions
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 0), new Vector2Int(0, 0), 0));
            Assert.False(feature.CanPlace(grid, new Vector2Int(-1, 0), new Vector2Int(0, 0), 0));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, -1), new Vector2Int(0, 0), 0));
            Assert.False(feature.CanPlace(grid, new Vector2Int(-1, -1), new Vector2Int(0, 0), 0));

            // This tests placing the Feature with a different local position.
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 0), new Vector2Int(0, 0), 0));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 0), new Vector2Int(1, 0), 0));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 0), new Vector2Int(0, 1), 0));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 0), new Vector2Int(1, 1), 0));

            // This tests both at once.
            // This can be read as "I want to place this Feature so that its upper-right Section is
            // placed at the position (1, 1) in the grid". This should fail, because the bottom-left
            // corner of the Feature is over (0, 0), which already exists.
            Assert.False(feature.CanPlace(grid, new Vector2Int(1, 1), new Vector2Int(1, 1), 0));

            // Weird case, you can use offsets that don't exist but they can still work.
            Assert.False(feature.CanPlace(grid, new Vector2Int(100, 100), new Vector2Int(100, 100), 0));

            // The above types of cases are the only ones that should fail- most other placements
            // that don't follow those patterns should succeed. Here's a few that should work.

            // No local offset
            Assert.True(feature.CanPlace(grid, new Vector2Int(1, 0), new Vector2Int(0, 0), 0));
            Assert.True(feature.CanPlace(grid, new Vector2Int(-2, 0), new Vector2Int(0, 0), 0));
            Assert.True(feature.CanPlace(grid, new Vector2Int(-2, -2), new Vector2Int(0, 0), 0));
            Assert.True(feature.CanPlace(grid, new Vector2Int(10, 0), new Vector2Int(0, 0), 0));

            // Top-right local offset
            Assert.True(feature.CanPlace(grid, new Vector2Int(-1, 0), new Vector2Int(1, 1), 0));
            Assert.True(feature.CanPlace(grid, new Vector2Int(-1, -1), new Vector2Int(1, 1), 0));
            Assert.True(feature.CanPlace(grid, new Vector2Int(0, -1), new Vector2Int(1, 1), 0));

            // Just y local offset
            Assert.True(feature.CanPlace(grid, new Vector2Int(0, -2), new Vector2Int(0, 1), 0));
        }
Beispiel #7
0
 public IndexSet GetIndexSet(ReadOnlyGrid grid, CellPosition cellPosition)
 {
     return(new IndexSet(
                GetAboveRowIndex(grid, cellPosition.Row),
                cellPosition.Row,
                GetBelowRowIndex(grid, cellPosition.Row),
                GetLeftColumnIndex(grid, cellPosition),
                cellPosition.Column,
                GetRightColumnIndex(grid, cellPosition)
                ));
 }
Beispiel #8
0
        public void Board_Successfully_Updates_From_Frame_To_Frame(int startingFrame, int expectedFrame)
        {
            var grid = BasicGliderFrameCreator.GetFrame(startingFrame);

            var board = new Board(_basicRuleSet, grid);

            board.UpdateToNextGeneration();
            var expectedGrid = new ReadOnlyGrid(BasicGliderFrameCreator.GetFrame(expectedFrame));
            var actualGrid   = board.GetGrid();

            Assert.True(expectedGrid.Equals(actualGrid));
        }
        public CellState GetNextCellState(ReadOnlyGrid neighbours)
        {
            var prioritisedRuleKeys = GetRuleKeysOrderedInReversePriority();

            var currentState = neighbours.GetCellState(new CellPosition(1, 1));

            foreach (var ruleKey in prioritisedRuleKeys)
            {
                currentState = _rules[ruleKey].GetNextCellState(neighbours, currentState);
            }

            return(currentState);
        }
Beispiel #10
0
        public void It_Should_Successfully_Add_The_Template_To_The_Grid(Grid grid, ReadOnlyGrid expected)
        {
            var template = new Grid(new List <List <Cell> >
            {
                new List <Cell> {
                    new Cell(CellState.Alive), new Cell(CellState.Dead)
                },
                new List <Cell> {
                    new Cell(CellState.Dead), new Cell(CellState.Alive)
                }
            });

            grid.AddTemplateToCenter(template);

            var readOnlyGrid = new ReadOnlyGrid(grid);

            Assert.True(readOnlyGrid.Equals(expected));
        }
Beispiel #11
0
        public void CanConnectBasic()
        {
            Labrys.Generation.Grid rawGrid = new Labrys.Generation.Grid();
            rawGrid[0, 0] = Section.Default();

            // Wrap the raw grid in a readonly version
            ReadOnlyGrid grid = new ReadOnlyGrid(rawGrid);

            // 3x1 feature
            Feature feature = new Feature();

            feature.Add(0, 0);
            feature.Add(1, 0);
            feature.Add(2, 0);

            //Assert.True(feature.CanConnect(grid, Vector2Int.zero));
            List <Feature.Configuration> configurations = feature.CanConnect(grid, Vector2Int.zero);
        }
Beispiel #12
0
        private static List <CellState> GetNeighbouringCellStates(ReadOnlyGrid concernedCells)
        {
            var neighbouringCells = new List <CellState>();

            for (var rowIndex = 0; rowIndex < 3; rowIndex++)
            {
                for (var columnIndex = 0; columnIndex < 3; columnIndex++)
                {
                    if (!IsCenterCell(rowIndex, columnIndex))
                    {
                        var cellPosition = new CellPosition(rowIndex, columnIndex);
                        neighbouringCells.Add(concernedCells.GetCellState(cellPosition));
                    }
                }
            }

            return(neighbouringCells);
        }
Beispiel #13
0
        public void CanPlaceWithRotationAndOffset()
        {
            Labrys.Generation.Grid rawGrid = new Labrys.Generation.Grid();
            rawGrid[0, 0] = Section.Default();

            // Wrap the raw grid in a readonly version
            ReadOnlyGrid grid = new ReadOnlyGrid(rawGrid);

            // 3x1 feature, but offset.
            // With normalization, should be same as CanPlaceWithRotation; the only difference is
            // that the local offset needs to be different to align with the input coordinates.
            Feature feature = new Feature();

            feature.Add(5, 3);
            feature.Add(6, 3);
            feature.Add(7, 3);

            // Basic tests for placement
            Assert.False(feature.CanPlace(grid, new Vector2Int(-2, 0), new Vector2Int(5, 3), 0));
            Assert.False(feature.CanPlace(grid, new Vector2Int(-1, 0), new Vector2Int(5, 3), 0));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 0), new Vector2Int(5, 3), 0));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 0), new Vector2Int(6, 3), 0));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 0), new Vector2Int(6, 3), 0));

            // Rotated cases
            Assert.True(feature.CanPlace(grid, new Vector2Int(0, -2), new Vector2Int(5, 3), 1));
            Assert.True(feature.CanPlace(grid, new Vector2Int(0, -1), new Vector2Int(5, 3), 1));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 1), new Vector2Int(5, 3), 1));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 2), new Vector2Int(5, 3), 1));
            Assert.True(feature.CanPlace(grid, new Vector2Int(0, 3), new Vector2Int(5, 3), 1));

            Assert.False(feature.CanPlace(grid, new Vector2Int(2, 0), new Vector2Int(5, 3), 2));
            Assert.False(feature.CanPlace(grid, new Vector2Int(1, 0), new Vector2Int(5, 3), 2));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 0), new Vector2Int(5, 3), 2));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 0), new Vector2Int(6, 3), 2));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 0), new Vector2Int(7, 3), 2));

            Assert.True(feature.CanPlace(grid, new Vector2Int(0, 2), new Vector2Int(5, 3), 3));
            Assert.True(feature.CanPlace(grid, new Vector2Int(0, 1), new Vector2Int(5, 3), 3));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, -1), new Vector2Int(5, 3), 3));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, -2), new Vector2Int(5, 3), 3));
            Assert.True(feature.CanPlace(grid, new Vector2Int(0, -3), new Vector2Int(5, 3), 3));
        }
        public void GetNextCellState_Should_Get_Next_State_For_Given_Grid(ReadOnlyGrid grid, CellState expectedState)
        {
            var initialState = grid.GetCellState(new CellPosition(1, 1));

            Assert.AreEqual(expectedState, _overcrowdingRule.GetNextCellState(grid, initialState));
        }
Beispiel #15
0
        public void CanPlaceWithRotation()
        {
            Labrys.Generation.Grid rawGrid = new Labrys.Generation.Grid();
            rawGrid[0, 0] = Section.Default();

            // Wrap the raw grid in a readonly version
            ReadOnlyGrid grid = new ReadOnlyGrid(rawGrid);

            // 3x1 feature
            Feature feature = new Feature();

            feature.Add(0, 0);
            feature.Add(1, 0);
            feature.Add(2, 0);

            // Basic tests for placement
            Assert.False(feature.CanPlace(grid, new Vector2Int(-2, 0), new Vector2Int(0, 0), 0));
            Assert.False(feature.CanPlace(grid, new Vector2Int(-1, 0), new Vector2Int(0, 0), 0));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 0), new Vector2Int(0, 0), 0));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 0), new Vector2Int(1, 0), 0));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 0), new Vector2Int(2, 0), 0));


            /*
             * Every Feature can be initially thought of as normalized. (This might
             * need to be enforced at a later point- TODO test this)
             *
             * Rotating is followed by a normalization step, which places the
             * new bottom-left-most Section at the coordinate (0,0). You can
             * specify which position in the Feature you want to connect using,
             * which essentially shifts the normalized Feature so that the desired
             * coordinate is now at (0,0). These are referred to using the original
             * coordinate (i.e., (1,0) for the middle element to be placed at (0,0)).
             *
             * A diagram is provided below for the non-shifted case:
             *
             *  |               |               |X
             *  |               |               |X
             *  |XXX            |X              |X
             * -+------   ->   -+------   ->   -+------
             *  |       rot 90  |X       norm   |
             *                  |X
             */

            // Rotating 90 degrees clockwise should keep everything centered.
            // Because of this centering property, rotating 270 degrees should be the same.
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 0), new Vector2Int(0, 0), 1));

            // Assuming the rotated Feature is normalized (it should be!)-
            // We should be able to place the (0, 0) point anywhere below 0. At y=1 or 2, we overlap.
            // At 3+, there is no overlap anymore.
            Assert.True(feature.CanPlace(grid, new Vector2Int(0, -2), new Vector2Int(0, 0), 1));
            Assert.True(feature.CanPlace(grid, new Vector2Int(0, -1), new Vector2Int(0, 0), 1));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 1), new Vector2Int(0, 0), 1));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 2), new Vector2Int(0, 0), 1));
            Assert.True(feature.CanPlace(grid, new Vector2Int(0, 3), new Vector2Int(0, 0), 1));

            // We can place the (1, 0) point at the exact same positions, but offset 1 unit up.
            Assert.True(feature.CanPlace(grid, new Vector2Int(0, -2), new Vector2Int(1, 0), 1));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, -1), new Vector2Int(1, 0), 1));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 0), new Vector2Int(1, 0), 1));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 1), new Vector2Int(1, 0), 1));
            Assert.True(feature.CanPlace(grid, new Vector2Int(0, 2), new Vector2Int(1, 0), 1));
            Assert.True(feature.CanPlace(grid, new Vector2Int(0, 3), new Vector2Int(1, 0), 1));

            // We can place the (2, 0) point at the same positions, but 2 units up.
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, -2), new Vector2Int(2, 0), 1));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, -1), new Vector2Int(2, 0), 1));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 0), new Vector2Int(2, 0), 1));
            Assert.True(feature.CanPlace(grid, new Vector2Int(0, 1), new Vector2Int(2, 0), 1));
            Assert.True(feature.CanPlace(grid, new Vector2Int(0, 2), new Vector2Int(2, 0), 1));
            Assert.True(feature.CanPlace(grid, new Vector2Int(0, 3), new Vector2Int(2, 0), 1));

            // The pattern should hold for (3, 0), even though that's not a Section in the Feature.
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, -3), new Vector2Int(3, 0), 1));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, -2), new Vector2Int(3, 0), 1));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, -1), new Vector2Int(3, 0), 1));
            Assert.True(feature.CanPlace(grid, new Vector2Int(0, 0), new Vector2Int(3, 0), 1));
            Assert.True(feature.CanPlace(grid, new Vector2Int(0, 1), new Vector2Int(3, 0), 1));
            Assert.True(feature.CanPlace(grid, new Vector2Int(0, 2), new Vector2Int(3, 0), 1));

            // Finally, check for the other rotations. Rotation 2 should == 0 (minus the local offsets),
            // and rotation 3 should == 1 (minus the same).
            Assert.False(feature.CanPlace(grid, new Vector2Int(2, 0), new Vector2Int(0, 0), 2));
            Assert.False(feature.CanPlace(grid, new Vector2Int(1, 0), new Vector2Int(0, 0), 2));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 0), new Vector2Int(0, 0), 2));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 0), new Vector2Int(1, 0), 2));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, 0), new Vector2Int(2, 0), 2));

            Assert.True(feature.CanPlace(grid, new Vector2Int(0, 2), new Vector2Int(0, 0), 3));
            Assert.True(feature.CanPlace(grid, new Vector2Int(0, 1), new Vector2Int(0, 0), 3));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, -1), new Vector2Int(0, 0), 3));
            Assert.False(feature.CanPlace(grid, new Vector2Int(0, -2), new Vector2Int(0, 0), 3));
            Assert.True(feature.CanPlace(grid, new Vector2Int(0, -3), new Vector2Int(0, 0), 3));
        }
Beispiel #16
0
 public GenerationViewModel(ReadOnlyGrid grid, int count)
 {
     Grid  = grid;
     Count = count;
 }
Beispiel #17
0
 public void Initialize(ReadOnlyGrid grid)
 {
 }
Beispiel #18
0
 public Vector2Int Select(ReadOnlyGrid currentGrid)
 {
     Vector2Int[] positions = currentGrid.GetBoundary();
     return(positions[Random.Range(0, positions.Length)]);
 }
Beispiel #19
0
        public void GetNextCellState_Should_Return_Next_State_Given_Different_Grids(ReadOnlyGrid grid, CellState expectedState)
        {
            var nextState = _ruleSet.GetNextCellState(grid);

            Assert.AreEqual(expectedState, nextState);
        }
Beispiel #20
0
 public CellState GetNextCellState(ReadOnlyGrid concernedCells, CellState initialState)
 {
     return(_requirements.All(requirement => requirement.HasMet(concernedCells)) ? _resultantState : initialState);
 }
 public NeighbourFinder(ReadOnlyGrid grid, IIndexFinder indexFinder)
 {
     _indexFinder = indexFinder;
     _grid        = grid;
 }
        public void GetNextCellState_Should_Return_Next_State_Given_Different_Grids(ReadOnlyGrid grid, bool expected)
        {
            var allAliveGrid = GridCreator.GetAllAliveGrid();

            Assert.AreEqual(expected, allAliveGrid.Equals(grid));
        }
        public bool HasMet(ReadOnlyGrid concernedCells)
        {
            var centerCellPosition = new CellPosition(1, 1);

            return(_initialStates.Contains(concernedCells.GetCellState(centerCellPosition)));
        }
Beispiel #24
0
        private static int CalculateActiveNeighbours(ReadOnlyGrid concernedCells)
        {
            var neighbouringCellStates = GetNeighbouringCellStates(concernedCells);

            return(neighbouringCellStates.Count(state => state == CellState.Alive));
        }
Beispiel #25
0
        public bool HasMet(ReadOnlyGrid concernedCells)
        {
            var activeNeighbourCount = CalculateActiveNeighbours(concernedCells);

            return(_activeNeighbourCounts.Contains(activeNeighbourCount));
        }