icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java
author indvdum (gotoindvdum[at]gmail[dot]com)
Thu, 02 Aug 2012 15:12:33 +0400
changeset 54 8a248d789ec7
parent 51 b431cec961d7
permissions -rw-r--r--
Passing maps in solver from standard input instead of command line
     1 package ru.bosony.solvers;
     2 
     3 import java.text.DecimalFormat;
     4 import java.util.HashSet;
     5 import java.util.Set;
     6 
     7 import ru.bosony.model.game.Game;
     8 import ru.bosony.model.mine.Mine;
     9 
    10 /**
    11  * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    12  *         16.07.2012 4:30:47
    13  * 
    14  */
    15 public abstract class AbstractSolver {
    16 
    17 	protected Mine				initialMine		= null;
    18 	protected SolverListener	listener		= null;
    19 	protected Set<Game>			games			= new HashSet<Game>();
    20 	protected long				attemptsCount	= 0;
    21 	protected long				startTime		= System.currentTimeMillis();
    22 
    23 	public AbstractSolver(Mine mine, SolverListener listener) {
    24 		this.initialMine = mine;
    25 		this.listener = listener;
    26 	}
    27 
    28 	protected void addNewRoute(Game game) {
    29 		Game maxScoreGame = getMaxFoundScoreGame();
    30 		if (maxScoreGame == null || game.getScore() > maxScoreGame.getScore()
    31 				|| game.getScore() == maxScoreGame.getScore()
    32 				&& game.getRoute().size() < maxScoreGame.getRoute().size()) {
    33 			listener.foundNextRoute(game.getStringRoute());
    34 			// TODO delete
    35 //			System.out.println(new DecimalFormat("0.00").format((((System.currentTimeMillis() - startTime) / 1000d)))
    36 //					+ " seconds, State = " + game.getState() + ", Score = " + game.getScore() + ", Route("
    37 //					+ game.getRoute().size() + ") = " + game.getStringRoute());
    38 		}
    39 		games.add(game);
    40 	}
    41 
    42 	protected Game getMaxFoundScoreGame() {
    43 		Game game = null;
    44 		for (Game foundGame : games) {
    45 			if (game == null || foundGame.getScore() > game.getScore())
    46 				game = foundGame;
    47 		}
    48 		return game;
    49 	}
    50 
    51 	public abstract void solve();
    52 }