All Downloads are FREE. Search and download functionalities are using the official Maven repository.

zebra4j.PuzzleGenerator Maven / Gradle / Ivy

/*-
 * #%L
 * zebra4j
 * %%
 * Copyright (C) 2020 Marin Nozhchev
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */
package zebra4j;

import java.util.Collection;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import java.util.stream.Stream;

import javax.annotation.concurrent.ThreadSafe;

import zebra4j.fact.Fact;

/**
 * A generator for {@link Puzzle}
 * 
 * 

* You can use the same generator to create multiple different puzzles by * calling the {@link #generate()} method multiple times. All generated puzzles * will have the same attributes and solution but will have different facts * (clues). */ @ThreadSafe public class PuzzleGenerator extends AbstractPuzzleGenerator { /** * Generate a new random puzzle using defaults * * @param numPeople number of people in the puzzle * @return */ public static Puzzle randomPuzzle(int numPeople) { PuzzleGenerator generator = new PuzzleGenerator(new SolutionGenerator(numPeople).generate(), DEFAULT_FACT_TYPES); return generator.generate(); } public PuzzleGenerator(PuzzleSolution solution, Set factTypes) { this(new Random(), solution, factTypes); } public PuzzleGenerator(Random rnd, PuzzleSolution solution, Set factTypes) { super(rnd, solution, factTypes); } @Override protected Puzzle toPuzzle(Collection facts) { return new Puzzle(solution.getAttributeSets(), facts); } @Override protected int countSolutions(Puzzle puzzle) { return new PuzzleSolver(puzzle, getChocoSettings()).solve().size(); } @Override protected boolean uniqueSolution(Puzzle puzzle) { Iterator iter = solveToStream(puzzle).iterator(); if (!iter.hasNext()) { return false; } PuzzleSolution first = iter.next(); while (iter.hasNext()) { if (!first.equals(iter.next())) { return false; } } return true; } private Stream solveToStream(Puzzle puzzle) { return new PuzzleSolver(puzzle, getChocoSettings()).solveToStream(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy