private void VerticalCheck(IBoard board, HashSet <BoardChange> changes)
        {
            for (int x = 0; x < board.Width; x++)
            {
                var ruleLine      = board.TopRules[x];
                var sum           = ruleLine.Sum();
                var numberOfRules = ruleLine.Count;
                var sizeNeeded    = sum + numberOfRules - 1;
                if (sizeNeeded == board.Height)
                {
                    int y = 0;
                    foreach (var rule in ruleLine)
                    {
                        for (int i = 0; i < rule; i++)
                        {
                            if (board[x, y] == CellState.None)
                            {
                                changes.Add(BoardChange.Filled(x, y));
                            }
                            y += 1;
                        }

                        if (y < board.Height)
                        {
                            if (board[x, y] == CellState.None)
                            {
                                changes.Add(BoardChange.Blocked(x, y));
                            }
                            y += 1;
                        }
                    }
                }
            }
        }
        private void VerticalCheck(IBoard board, HashSet <BoardChange> changes)
        {
            for (int x = 0; x < board.Width; x++)
            {
                var groups   = SimpleGrouper.GroupVertical(board, x);
                var ruleLine = board.TopRules[x];
                if (ruleLine.Count == 0)
                {
                    continue;
                }
                var highestRuleNumber = ruleLine.Max();
                if (!groups.ContainsNones || highestRuleNumber == 0)
                {
                    continue;
                }
                foreach (var group in groups.Groups)
                {
                    if (group.State == CellState.Filled && group.Count == highestRuleNumber)
                    {
                        // Block off before the start of the group.
                        if (group.StartIndex > 0 && board[x, group.StartIndex - 1] == CellState.None)
                        {
                            changes.Add(BoardChange.Blocked(x, group.StartIndex - 1));
                        }

                        var lastIndex = group.StartIndex + group.Count;
                        if (lastIndex < board.Height && board[x, lastIndex] == CellState.None)
                        {
                            changes.Add(BoardChange.Blocked(x, lastIndex));
                        }
                    }
                }
            }
        }
        private void VerticalCheck(IBoard board, HashSet <BoardChange> changes)
        {
            bool lineIsCompleted;

            for (int x = 0; x < board.Width; x++)
            {
                lineIsCompleted = false;
                IRuleLine?ruleLine  = board.TopRules[x];
                int?      firstRule = ruleLine?.FirstOrDefault();
                if (firstRule == null)
                {
                    continue;
                }
                GroupCollection?groups = SimpleGrouper.GroupVertical(board, x);
                if (groups == null)
                {
                    continue;
                }

                Group?firstEmptyGroup = default;
                int   totalSpace      = 0;
                foreach (Group group in groups.Groups)
                {
                    switch (group.State)
                    {
                    case CellState.None:
                        if (firstEmptyGroup == null)
                        {
                            firstEmptyGroup = group;
                        }
                        totalSpace += group.Count;
                        break;

                    case CellState.Filled:
                        totalSpace += group.Count;
                        break;

                    case CellState.Blocked:
                        if (totalSpace < firstRule.Value && firstEmptyGroup != null)
                        {
                            for (int i = 0; i < firstEmptyGroup.Value.Count; i++)
                            {
                                changes.Add(BoardChange.Blocked(x, i + firstEmptyGroup.Value.StartIndex));
                            }
                            lineIsCompleted = true;
                        }
                        break;

                    default:
                        break;
                    }

                    // Jump out of the group loop
                    if (lineIsCompleted)
                    {
                        break;
                    }
                }
            }
        }
        public void BoardWithTwoToFillHorizontal_Flipped_MarkOneAsBlocked()
        {
            var board = BoardSamples.Board7_ManualFlipped
                        .ApplyChanges(
                BoardChange.Blocked(3, 4)
                );
            var solver = new FirstGroupTooSmall();

            var actualChanges = solver.Solve(board);
            var expected      = new List <BoardChange>
            {
                BoardChange.Blocked(4, 4)
            };

            Assert.Equal(expected, actualChanges);
        }
        public void BoardWithTwoToFillVertical_MarkOneAsBlocked()
        {
            var board = BoardSamples.Board7
                        .ApplyChanges(
                BoardChange.Blocked(0, 1)
                );
            var solver = new FirstGroupTooSmall();

            var actualChanges = solver.Solve(board);
            var expected      = new List <BoardChange>
            {
                BoardChange.Blocked(0, 0)
            };

            Assert.Equal(expected, actualChanges);
        }
Пример #6
0
 private static void HorizontalRulesCheck(IBoard board, HashSet <BoardChange> changes)
 {
     for (int y = 0; y < board.Height; y++)
     {
         GroupCollection?groups   = SimpleGrouper.GroupHorizontal(board, y);
         IRuleLine?      ruleLine = board.LeftRules[y];
         if (groups.ContainsNones && groups.SatisfiesRuleLine(ruleLine))
         {
             for (int x = 0; x < board.Width; x++)
             {
                 CellState state = board[x, y];
                 if (state == CellState.None)
                 {
                     changes.Add(BoardChange.Blocked(x, y));
                 }
             }
         }
     }
 }