/** * Returns a string with the stone this strategy would place on the given board with the given * stones. * * @param board * @param stones * @return */ public String getHint(Board board, List<Stone> stones) { String res = "I suggest you "; List<Stone> stonesplaced = getMove(board, stones); if (stonesplaced.isEmpty()) { res += "trade"; } else { Stone s = stonesplaced.get(0); res += "place " + s + " at " + s.getPosition(); } return res; }
/** * Gets the move the strategy wants to make. * * <p>It does so by creating a deepcopy of the board and trying a lot of random moves until the * time is over then it chooses the best one and returns that. * * @param board * @param stones * @return a list of stones that are to be placed on the board. or an empty list if the strategy * can not place any stones (indicating that it wants to swap). */ public List<Stone> getMove(Board board, List<Stone> stones) { List<Stone> move = new ArrayList<Stone>(); int movePoints = 0; long start = System.currentTimeMillis(); long end = start + time * 100; while (System.currentTimeMillis() < end) { List<Stone> stonesBackup = new ArrayList<Stone>(); for (Stone s : stones) { Stone newStone = new Stone(s.getShape(), s.getColor()); stonesBackup.add(newStone); } Board b = board.deepCopy(); List<PossibleMove> possibleMoves = new ArrayList<PossibleMove>(b.getPossibleMoves().values()); List<Stone> stonesplaced = new ArrayList<Stone>(); possibleMoves = Player.adaptPossibleMoves(possibleMoves, stonesBackup, stonesplaced, b); while (!possibleMoves.isEmpty()) { int choice = (int) Math.floor(Math.random() * possibleMoves.size()); Stone placed = placeStone(b, possibleMoves.get(choice), stonesBackup); stonesplaced.add(placed); stonesBackup.remove(placed); possibleMoves = new ArrayList<PossibleMove>(b.getPossibleMoves().values()); possibleMoves = Player.adaptPossibleMoves(possibleMoves, stonesBackup, stonesplaced, b); } List<Position> positions = new ArrayList<Position>(); for (int i = 0; i < stonesplaced.size(); i++) { positions.add(stonesplaced.get(i).getPosition()); } if (!stonesplaced.isEmpty()) { int points = b.calculatePoints(stonesplaced, positions); if (points > movePoints) { move = stonesplaced; movePoints = points; } } else { return stonesplaced; } } return move; }