
com.barrybecker4.puzzle.hiq.HiQController Maven / Gradle / Ivy
/** Copyright by Barry G. Becker, 2000-2011. Licensed under MIT License: http://www.opensource.org/licenses/MIT */
package com.barrybecker4.puzzle.hiq;
import com.barrybecker4.puzzle.common.Refreshable;
import com.barrybecker4.puzzle.common.ui.AbstractPuzzleController;
import com.barrybecker4.puzzle.hiq.model.MoveGenerator;
import com.barrybecker4.puzzle.hiq.model.PegBoard;
import com.barrybecker4.puzzle.hiq.model.PegMove;
import java.util.List;
import java.util.Set;
/**
* HiQ Puzzle Controller.
*See puzzle.common for puzzle framework classes.
*
* @author Barry Becker
*/
public class HiQController extends AbstractPuzzleController {
/**
* @param ui shows the current state on the screen.
*/
public HiQController(Refreshable ui) {
super(ui);
// set default
algorithm_ = Algorithm.CONCURRENT_OPTIMUM;
}
@Override
public PegBoard initialPosition() {
return PegBoard.INITIAL_BOARD_POSITION;
}
@Override
public boolean isGoal(PegBoard position) {
return position.isSolved();
}
@Override
public List legalMoves(PegBoard position) {
return new MoveGenerator(position).generateMoves();
}
@Override
public PegBoard move(PegBoard position, PegMove move) {
return position.doMove(move, false);
}
/**
* Check all board symmetries to be sure it has or has not been seen.
* If it was never seen before add it.
* Must be synchronized because some solvers use concurrency.
*/
@Override
public synchronized boolean alreadySeen(PegBoard position, Set seen) {
boolean visited = false;
for (int i = 0; i < PegBoard.SYMMETRIES; i++) {
if (seen.contains(position.symmetry(i))) {
visited = true;
break;
}
}
if (!visited) {
seen.add(position);
}
return visited;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy