001package ball.game.sudoku;
002/*-
003 * ##########################################################################
004 * Game Applications and Utilities
005 * $Id: Rule.java 5285 2020-02-05 04:23:21Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-game/trunk/src/main/java/ball/game/sudoku/Rule.java $
007 * %%
008 * Copyright (C) 2010 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import lombok.NoArgsConstructor;
024
025import static lombok.AccessLevel.PROTECTED;
026
027/**
028 * Sudoku {@link Puzzle} solution {@link Rule}.
029 *
030 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
031 * @version $Revision: 5285 $
032 */
033@NoArgsConstructor(access = PROTECTED)
034public abstract class Rule {
035
036    /**
037     * Method to apply {@link.this} {@link Rule} to the argument
038     * {@link Puzzle}.
039     *
040     * @param   puzzle          The {@link Puzzle} to solve.
041     *
042     * @return  {@code true} if {@link.this} {@link Puzzle} is modified;
043     *          {@code false} otherwise.
044     */
045    public abstract boolean applyTo(Puzzle puzzle);
046
047    /**
048     * Method to get the count of solved {@link Cell}s.
049     *
050     * @param   iterable        The {@link Iterable} of {@link Cell}s.
051     *
052     * @return  The count of solved {@link Cell}s.
053     */
054    protected int count(Iterable<Cell> iterable) {
055        int count = 0;
056
057        for (Cell cell : iterable) {
058            if (cell.isSolved()) {
059                count += 1;
060            }
061        }
062
063        return count;
064    }
065
066    /**
067     * Method to get the sum of solved {@link Cell}s.
068     *
069     * @param   iterable        The {@link Iterable} of {@link Cell}s.
070     *
071     * @return  The sum of solved {@link Cell}s.
072     */
073    protected int sum(Iterable<Cell> iterable) {
074        int sum = 0;
075
076        for (Cell cell : iterable) {
077            if (cell.isSolved()) {
078                sum += cell.solution();
079            }
080        }
081
082        return sum;
083    }
084
085    /**
086     * Method to get the solved {@link Digits}.
087     *
088     * @param   iterable        The {@link Iterable} of {@link Cell}s.
089     *
090     * @return  The solved {@link Digits}.
091     */
092    protected Digits solved(Iterable<Cell> iterable) {
093        Digits digits = new Digits();
094
095        digits.clear();
096
097        for (Cell cell : iterable) {
098            if (cell.isSolved()) {
099                digits.addAll(cell);
100            }
101        }
102
103        return digits;
104    }
105}