Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class StrategyAdviser {
- private final YahtzeeGame yahtzeeGame;
- public StrategyAdviser(YahtzeeGame yahtzeeGame) {
- this.yahtzeeGame = yahtzeeGame;
- }
- public Strategy getAdvise() {
- Roll roll = yahtzeeGame.getRoll();
- Board board = yahtzeeGame.getBoard();
- YahtzeePlayer currentPlayer = yahtzeeGame.getCurrentPlayer();
- StrategyFactory factory = new StrategyFactory();
- if (!roll.hasRoll()) {
- return factory.createStartRollStrategy();
- }
- if (!yahtzeeGame.canRoll()) {
- return factory.createWriteToBoardStrategy();
- }
- RollAnalyze rollAnalyze = new RollAnalyze(roll);
- BoardAnalyze boardAnalyze = new BoardAnalyze(board, currentPlayer);
- int amountOfIdenticals = rollAnalyze.getAmountOfIdenticals();
- int highestEyeOfIdenticals = rollAnalyze.getHighestEyeOfIdenticals();
- boolean has6 = rollAnalyze.hasSix();
- boolean has5 = rollAnalyze.hasFive();
- boolean is6Empty = boardAnalyze.isTopRowEmpty(6);
- boolean is5Empty = boardAnalyze.isTopRowEmpty(5);
- boolean has3InRow = rollAnalyze.hasThreeInRow();
- boolean has4InRow = rollAnalyze.hasFourInRow();
- boolean hasBottomVariables = boardAnalyze.hasBottomRowsWithVariableCounter();
- if (amountOfIdenticals == 5) {
- return factory.createWriteToBoardStrategy();
- }
- if ((amountOfIdenticals >= 3 && boardAnalyze.isTopRowEmpty(highestEyeOfIdenticals))) {
- return factory.createStrategyRollForThreeOrMoreIdentical(highestEyeOfIdenticals, rollAnalyze);
- }
- if (amountOfIdenticals >= 2 && highestEyeOfIdenticals >= 4) {
- return factory.createStrategyRollForThreeOrMoreIdentical(highestEyeOfIdenticals, rollAnalyze);
- }
- if (has4InRow) {
- return factory.createStrategyRollForMajorStreet();
- }
- if (has6 && is6Empty) {
- return factory.createStrategyRollForSix(rollAnalyze);
- }
- if (((!has6) && (has5 && is5Empty))) {
- return factory.createStrategyRollForSix(rollAnalyze);
- }
- if (hasBottomVariables) {
- if (has6) {
- return factory.createStrategyRollForSix(rollAnalyze);
- }
- if (has5) {
- return factory.createStrategyRollForFive(rollAnalyze);
- }
- }
- if (has3InRow) {
- if (has5) {
- return factory.createStrategyRollForMinorStreet(3, 4, 5);
- } else {
- return factory.createStrategyRollForMinorStreet(2, 3, 4);
- }
- }
- return factory.createStrategyReRollAll();
- }
- public interface Strategy {
- boolean adviseToContinueRolling();
- void apply(YahtzeeGame game);
- }
- public class StrategyFactory {
- private static final Logger LOGGER = LoggerFactory.getLogger(Strategy.class);
- public Strategy createStrategyRollForThreeOrMoreIdentical(int highestEyeOfIdenticals, RollAnalyze rollAnalyze) {
- return new ContinueStrategy() {
- @Override
- public void apply(YahtzeeGame game) {
- LOGGER.debug("apply strategy: roll, try to get as much identicals of {} as possible", +rollAnalyze.getHighestEyeOfIdenticals());
- LOGGER.debug("based on roll: {}", rollAnalyze.getRoll());
- roll(game, rollAnalyze.getKeepingFor(highestEyeOfIdenticals));
- }
- @Override
- public String toString() {
- return "roll for identicals of " + highestEyeOfIdenticals;
- }
- };
- }
- public Strategy createStartRollStrategy() {
- return new ContinueStrategy() {
- @Override
- public void apply(YahtzeeGame game) {
- LOGGER.debug("apply strategy: roll for the first time...");
- roll(game);
- }
- @Override
- public String toString() {
- return "roll for the first time...";
- }
- };
- }
- public Strategy createWriteToBoardStrategy() {
- return new StopStrategy() {
- @Override
- public void apply(YahtzeeGame game) {
- LOGGER.debug("apply strategy: write to board... do NOT roll anymore");
- }
- @Override
- public String toString() {
- return "do not roll - write it into the board!";
- }
- };
- }
- public Strategy createStrategyRollForMajorStreet() {
- return new ContinueStrategy() {
- @Override
- public void apply(YahtzeeGame game) {
- LOGGER.debug("apply strategy: roll, try to get as much different since we have alreadey minor straight");
- roll(game, new Keeping(new HashSet<>(Arrays.asList(2, 3, 4, 5))));
- }
- @Override
- public String toString() {
- return "roll for a major street";
- }
- };
- }
- public Strategy createStrategyRollForSix(RollAnalyze rollAnalyze) {
- return createStartRollForEye(6, rollAnalyze);
- }
- public Strategy createStrategyRollForFive(RollAnalyze rollAnalyze) {
- return createStartRollForEye(5, rollAnalyze);
- }
- private Strategy createStartRollForEye(int eye, RollAnalyze rollAnalyze) {
- return new ContinueStrategy() {
- @Override
- public void apply(YahtzeeGame game) {
- Keeping keeping = rollAnalyze.getKeepingFor(eye);
- LOGGER.debug("apply strategy: roll, try to get as much identicals of {} as possible", +eye);
- LOGGER.debug("based on roll: {}", keeping);
- roll(game, keeping);
- }
- @Override
- public String toString() {
- return "roll one certain eye: " + eye;
- }
- };
- }
- public Strategy createStrategyRollForMinorStreet(Integer... eyes) {
- return new ContinueStrategy() {
- @Override
- public void apply(YahtzeeGame game) {
- LOGGER.debug("apply strategy: roll, try to get as much different since we have alreadey 3/4 of minor straight");
- roll(game, new Keeping(new HashSet<>(Arrays.asList(eyes))));
- }
- @Override
- public String toString() {
- return "roll for a minor street";
- }
- };
- }
- public Strategy createStrategyReRollAll() {
- return new ContinueStrategy() {
- @Override
- public void apply(YahtzeeGame game) {
- LOGGER.debug("apply strategy: reRoll all...");
- roll(game);
- }
- @Override
- public String toString() {
- return "reRoll all... ";
- }
- };
- }
- private abstract class TemplateStrategy implements Strategy {
- private final boolean adviseToContinue;
- private TemplateStrategy(final boolean adviseToContinue) {
- this.adviseToContinue = adviseToContinue;
- }
- @Override
- public boolean adviseToContinueRolling() {
- return adviseToContinue;
- }
- void roll(YahtzeeGame yahtzeeGame, Keeping keeping) {
- LOGGER.debug("set keeping: {}", keeping);
- yahtzeeGame.setKeepings(keeping);
- yahtzeeGame.roll();
- LOGGER.debug("...done with set Keeping and rolling...");
- }
- void roll(YahtzeeGame yahtzeeGame) {
- yahtzeeGame.roll();
- LOGGER.debug("...done, rolling...");
- }
- }
- private abstract class StopStrategy extends TemplateStrategy {
- private StopStrategy() {
- super(false);
- }
- }
- private abstract class ContinueStrategy extends TemplateStrategy {
- private ContinueStrategy() {
- super(true);
- }
- }
- }
- public class YahtzeePlayer extends BasePlayer<YahtzeeGame> {
- private static final Logger LOGGER = LoggerFactory.getLogger(YahtzeePlayer.class);
- YahtzeePlayer(String name, int color, boolean isHuman) {
- super(name, color, isHuman);
- }
- @Override
- public void performAiTurn() {
- YahtzeeGame yahtzeeGame = getBoardGame();//weg damit
- StrategyAdviser strategyAdviser = new StrategyAdviser(yahtzeeGame);
- Strategy currentStrategy = strategyAdviser.getAdvise();
- while (currentStrategy.adviseToContinueRolling()) {
- currentStrategy.apply(yahtzeeGame);
- currentStrategy = strategyAdviser.getAdvise();
- }
- WriteAdviser writeAdviser = new WriteAdviser(
- yahtzeeGame.getRoll(), yahtzeeGame.getBoard(), this, new RollAnalyze(yahtzeeGame.getRoll()));
- RowType rowType = writeAdviser.getOptimalRow();
- if (!yahtzeeGame.getBoard().getRow(rowType, this).isEmpty()) {
- throw new IllegalStateException("error - writing into an already filled row");
- }
- LOGGER.debug("write roll {} into rowType {}", yahtzeeGame.getRoll(), rowType);
- yahtzeeGame.write(rowType);
- LOGGER.debug("done Ai turn");
- yahtzeeGame.endPlayersTurn();
- yahtzeeGame.startPlayersTurn();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement