Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.87 KB | None | 0 0
  1. public class StrategyAdviser {
  2.  
  3. private final YahtzeeGame yahtzeeGame;
  4.  
  5. public StrategyAdviser(YahtzeeGame yahtzeeGame) {
  6. this.yahtzeeGame = yahtzeeGame;
  7. }
  8.  
  9. public Strategy getAdvise() {
  10. Roll roll = yahtzeeGame.getRoll();
  11. Board board = yahtzeeGame.getBoard();
  12. YahtzeePlayer currentPlayer = yahtzeeGame.getCurrentPlayer();
  13.  
  14. StrategyFactory factory = new StrategyFactory();
  15. if (!roll.hasRoll()) {
  16. return factory.createStartRollStrategy();
  17. }
  18.  
  19. if (!yahtzeeGame.canRoll()) {
  20. return factory.createWriteToBoardStrategy();
  21. }
  22.  
  23. RollAnalyze rollAnalyze = new RollAnalyze(roll);
  24. BoardAnalyze boardAnalyze = new BoardAnalyze(board, currentPlayer);
  25. int amountOfIdenticals = rollAnalyze.getAmountOfIdenticals();
  26. int highestEyeOfIdenticals = rollAnalyze.getHighestEyeOfIdenticals();
  27. boolean has6 = rollAnalyze.hasSix();
  28. boolean has5 = rollAnalyze.hasFive();
  29. boolean is6Empty = boardAnalyze.isTopRowEmpty(6);
  30. boolean is5Empty = boardAnalyze.isTopRowEmpty(5);
  31. boolean has3InRow = rollAnalyze.hasThreeInRow();
  32. boolean has4InRow = rollAnalyze.hasFourInRow();
  33. boolean hasBottomVariables = boardAnalyze.hasBottomRowsWithVariableCounter();
  34.  
  35. if (amountOfIdenticals == 5) {
  36. return factory.createWriteToBoardStrategy();
  37. }
  38.  
  39. if ((amountOfIdenticals >= 3 && boardAnalyze.isTopRowEmpty(highestEyeOfIdenticals))) {
  40. return factory.createStrategyRollForThreeOrMoreIdentical(highestEyeOfIdenticals, rollAnalyze);
  41. }
  42. if (amountOfIdenticals >= 2 && highestEyeOfIdenticals >= 4) {
  43. return factory.createStrategyRollForThreeOrMoreIdentical(highestEyeOfIdenticals, rollAnalyze);
  44. }
  45.  
  46. if (has4InRow) {
  47. return factory.createStrategyRollForMajorStreet();
  48. }
  49.  
  50. if (has6 && is6Empty) {
  51. return factory.createStrategyRollForSix(rollAnalyze);
  52. }
  53.  
  54. if (((!has6) && (has5 && is5Empty))) {
  55. return factory.createStrategyRollForSix(rollAnalyze);
  56. }
  57.  
  58. if (hasBottomVariables) {
  59. if (has6) {
  60. return factory.createStrategyRollForSix(rollAnalyze);
  61. }
  62. if (has5) {
  63. return factory.createStrategyRollForFive(rollAnalyze);
  64. }
  65. }
  66.  
  67. if (has3InRow) {
  68. if (has5) {
  69. return factory.createStrategyRollForMinorStreet(3, 4, 5);
  70. } else {
  71. return factory.createStrategyRollForMinorStreet(2, 3, 4);
  72. }
  73. }
  74.  
  75. return factory.createStrategyReRollAll();
  76. }
  77.  
  78. public interface Strategy {
  79.  
  80. boolean adviseToContinueRolling();
  81.  
  82. void apply(YahtzeeGame game);
  83.  
  84. }
  85.  
  86. public class StrategyFactory {
  87.  
  88. private static final Logger LOGGER = LoggerFactory.getLogger(Strategy.class);
  89.  
  90. public Strategy createStrategyRollForThreeOrMoreIdentical(int highestEyeOfIdenticals, RollAnalyze rollAnalyze) {
  91.  
  92. return new ContinueStrategy() {
  93.  
  94. @Override
  95. public void apply(YahtzeeGame game) {
  96. LOGGER.debug("apply strategy: roll, try to get as much identicals of {} as possible", +rollAnalyze.getHighestEyeOfIdenticals());
  97. LOGGER.debug("based on roll: {}", rollAnalyze.getRoll());
  98. roll(game, rollAnalyze.getKeepingFor(highestEyeOfIdenticals));
  99.  
  100. }
  101.  
  102. @Override
  103. public String toString() {
  104. return "roll for identicals of " + highestEyeOfIdenticals;
  105. }
  106.  
  107. };
  108. }
  109.  
  110.  
  111. public Strategy createStartRollStrategy() {
  112. return new ContinueStrategy() {
  113.  
  114. @Override
  115. public void apply(YahtzeeGame game) {
  116. LOGGER.debug("apply strategy: roll for the first time...");
  117. roll(game);
  118. }
  119.  
  120. @Override
  121. public String toString() {
  122. return "roll for the first time...";
  123. }
  124. };
  125.  
  126.  
  127. }
  128.  
  129. public Strategy createWriteToBoardStrategy() {
  130. return new StopStrategy() {
  131.  
  132. @Override
  133. public void apply(YahtzeeGame game) {
  134. LOGGER.debug("apply strategy: write to board... do NOT roll anymore");
  135. }
  136.  
  137. @Override
  138. public String toString() {
  139. return "do not roll - write it into the board!";
  140. }
  141. };
  142. }
  143.  
  144. public Strategy createStrategyRollForMajorStreet() {
  145. return new ContinueStrategy() {
  146.  
  147. @Override
  148. public void apply(YahtzeeGame game) {
  149. LOGGER.debug("apply strategy: roll, try to get as much different since we have alreadey minor straight");
  150. roll(game, new Keeping(new HashSet<>(Arrays.asList(2, 3, 4, 5))));
  151. }
  152.  
  153. @Override
  154. public String toString() {
  155. return "roll for a major street";
  156. }
  157. };
  158. }
  159.  
  160. public Strategy createStrategyRollForSix(RollAnalyze rollAnalyze) {
  161. return createStartRollForEye(6, rollAnalyze);
  162. }
  163.  
  164. public Strategy createStrategyRollForFive(RollAnalyze rollAnalyze) {
  165. return createStartRollForEye(5, rollAnalyze);
  166. }
  167.  
  168. private Strategy createStartRollForEye(int eye, RollAnalyze rollAnalyze) {
  169. return new ContinueStrategy() {
  170.  
  171. @Override
  172. public void apply(YahtzeeGame game) {
  173. Keeping keeping = rollAnalyze.getKeepingFor(eye);
  174. LOGGER.debug("apply strategy: roll, try to get as much identicals of {} as possible", +eye);
  175. LOGGER.debug("based on roll: {}", keeping);
  176. roll(game, keeping);
  177. }
  178.  
  179. @Override
  180. public String toString() {
  181. return "roll one certain eye: " + eye;
  182. }
  183. };
  184. }
  185.  
  186. public Strategy createStrategyRollForMinorStreet(Integer... eyes) {
  187. return new ContinueStrategy() {
  188.  
  189. @Override
  190. public void apply(YahtzeeGame game) {
  191. LOGGER.debug("apply strategy: roll, try to get as much different since we have alreadey 3/4 of minor straight");
  192. roll(game, new Keeping(new HashSet<>(Arrays.asList(eyes))));
  193. }
  194.  
  195. @Override
  196. public String toString() {
  197. return "roll for a minor street";
  198. }
  199. };
  200. }
  201.  
  202. public Strategy createStrategyReRollAll() {
  203. return new ContinueStrategy() {
  204.  
  205. @Override
  206. public void apply(YahtzeeGame game) {
  207. LOGGER.debug("apply strategy: reRoll all...");
  208. roll(game);
  209. }
  210.  
  211. @Override
  212. public String toString() {
  213. return "reRoll all... ";
  214. }
  215. };
  216. }
  217.  
  218.  
  219. private abstract class TemplateStrategy implements Strategy {
  220.  
  221. private final boolean adviseToContinue;
  222.  
  223. private TemplateStrategy(final boolean adviseToContinue) {
  224. this.adviseToContinue = adviseToContinue;
  225. }
  226.  
  227. @Override
  228. public boolean adviseToContinueRolling() {
  229. return adviseToContinue;
  230. }
  231.  
  232. void roll(YahtzeeGame yahtzeeGame, Keeping keeping) {
  233. LOGGER.debug("set keeping: {}", keeping);
  234. yahtzeeGame.setKeepings(keeping);
  235. yahtzeeGame.roll();
  236. LOGGER.debug("...done with set Keeping and rolling...");
  237. }
  238.  
  239. void roll(YahtzeeGame yahtzeeGame) {
  240. yahtzeeGame.roll();
  241. LOGGER.debug("...done, rolling...");
  242. }
  243. }
  244.  
  245. private abstract class StopStrategy extends TemplateStrategy {
  246.  
  247. private StopStrategy() {
  248. super(false);
  249. }
  250.  
  251. }
  252.  
  253. private abstract class ContinueStrategy extends TemplateStrategy {
  254.  
  255. private ContinueStrategy() {
  256. super(true);
  257. }
  258.  
  259. }
  260. }
  261.  
  262. public class YahtzeePlayer extends BasePlayer<YahtzeeGame> {
  263.  
  264. private static final Logger LOGGER = LoggerFactory.getLogger(YahtzeePlayer.class);
  265.  
  266.  
  267. YahtzeePlayer(String name, int color, boolean isHuman) {
  268. super(name, color, isHuman);
  269. }
  270.  
  271. @Override
  272. public void performAiTurn() {
  273.  
  274. YahtzeeGame yahtzeeGame = getBoardGame();//weg damit
  275. StrategyAdviser strategyAdviser = new StrategyAdviser(yahtzeeGame);
  276. Strategy currentStrategy = strategyAdviser.getAdvise();
  277. while (currentStrategy.adviseToContinueRolling()) {
  278. currentStrategy.apply(yahtzeeGame);
  279. currentStrategy = strategyAdviser.getAdvise();
  280. }
  281.  
  282. WriteAdviser writeAdviser = new WriteAdviser(
  283. yahtzeeGame.getRoll(), yahtzeeGame.getBoard(), this, new RollAnalyze(yahtzeeGame.getRoll()));
  284. RowType rowType = writeAdviser.getOptimalRow();
  285.  
  286. if (!yahtzeeGame.getBoard().getRow(rowType, this).isEmpty()) {
  287. throw new IllegalStateException("error - writing into an already filled row");
  288. }
  289.  
  290. LOGGER.debug("write roll {} into rowType {}", yahtzeeGame.getRoll(), rowType);
  291. yahtzeeGame.write(rowType);
  292.  
  293.  
  294. LOGGER.debug("done Ai turn");
  295. yahtzeeGame.endPlayersTurn();
  296. yahtzeeGame.startPlayersTurn();
  297. }
  298.  
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement