Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. package tablut;
  2.  
  3. /*import java.lang.reflect.Array;*/
  4. /*import java.util.ArrayList;*/
  5. import java.util.List;
  6.  
  7. import static java.lang.Math.*;
  8.  
  9. import static tablut.Square.sq;
  10. import static tablut.Piece.*;
  11.  
  12. /** A Player that automatically generates moves.
  13. * @author Joymaneet Kaur
  14. */
  15. class AI extends Player {
  16.  
  17. /** A position-score magnitude indicating a win (for white if positive,
  18. * black if negative). */
  19. private static final int WINNING_VALUE = Integer.MAX_VALUE - 20;
  20. /** A position-score magnitude indicating a forced win in a subsequent
  21. * move. This differs from WINNING_VALUE to avoid putting off wins. */
  22. private static final int WILL_WIN_VALUE = Integer.MAX_VALUE - 40;
  23. /** A magnitude greater than a normal value. */
  24. private static final int INFTY = Integer.MAX_VALUE;
  25.  
  26. /** A new AI with no piece or controller (intended to produce
  27. * a template). */
  28. AI() {
  29.  
  30. this(null, null);
  31. }
  32.  
  33. /** A new AI playing PIECE under control of CONTROLLER. */
  34. AI(Piece piece, Controller controller) {
  35.  
  36. super(piece, controller);
  37. }
  38.  
  39. @Override
  40. Player create(Piece piece, Controller controller) {
  41.  
  42. return new AI(piece, controller);
  43. }
  44.  
  45. @Override
  46. String myMove() {
  47.  
  48. /** Return either a String denoting either a legal move for me
  49. * or another command (which may be invalid). Always returns the
  50. * latter if board().turn() is not myPiece() or if board.winner()
  51. * is not null. */
  52.  
  53. Move nextmove = findMove();
  54.  
  55. if (board().turn() != myPiece() || board().winner() != null) {
  56. return "not valid";
  57.  
  58. }
  59. if (board().turn() == myPiece() || board().winner() == null) {
  60. return board().legalMoves(myPiece()).get(0).toString();
  61. }
  62.  
  63. return "not valid";
  64. }
  65.  
  66. @Override
  67. boolean isManual() {
  68. return false;
  69. }
  70.  
  71. /** Return a move for me from the current position, assuming there
  72. * is a move. */
  73. private Move findMove() {
  74. Board b = new Board(board());
  75. _lastFoundMove = null;
  76.  
  77. return _lastFoundMove;
  78. }
  79.  
  80. /** The move found by the last call to one of the ...FindMove methods
  81. * below. */
  82. private Move _lastFoundMove;
  83.  
  84. /** Find a move from position BOARD and return its value, recording
  85. * the move found in _lastFoundMove iff SAVEMOVE. The move
  86. * should have maximal value or have value > BETA if SENSE==1,
  87. * and minimal value or value < ALPHA if SENSE==-1. Searches up to
  88. * DEPTH levels. Searching at level 0 simply returns a static estimate
  89. * of the board value and does not set _lastMoveFound. */
  90. private int findMove(Board board, int depth, boolean saveMove,
  91. int sense, int alpha, int beta) {
  92.  
  93. List<Move> legalblack = board.legalMoves(BLACK);
  94. List<Move> legalwhite = board.legalMoves(WHITE);
  95.  
  96. if (depth == 0 || board.kingPosition().isEdge()) {
  97. return staticScore(board);
  98. }
  99. if (board.kingPosition().isEdge()) {
  100. return staticScore(board);
  101. }
  102.  
  103. Move bestmove = null;
  104.  
  105.  
  106. if (sense == 1) {
  107. int bestnow = -INFTY;
  108. for (Move x : legalblack) {
  109. board.makeMove(x);
  110. int sense1 = findMove(board, depth - 1, true, -1, alpha, beta);
  111. if (sense1 > bestnow) {
  112. bestmove = x;
  113. bestnow = sense1;
  114. }
  115. alpha = Math.max(alpha, bestnow);
  116.  
  117. }
  118. }
  119.  
  120. if (sense == -1) {
  121. int bestnow = INFTY;
  122. for (Move x: legalwhite) {
  123. board.makeMove(x);
  124. int senseneg = findMove(board, depth - 1,
  125. false, 1, alpha, beta);
  126. if (senseneg < bestnow) {
  127. bestmove = x;
  128. bestnow = senseneg;
  129. }
  130. beta = Math.min(bestnow, beta);
  131.  
  132. }
  133. }
  134.  
  135. if (saveMove) {
  136. assert bestmove != null;
  137. _lastFoundMove = bestmove;
  138. }
  139.  
  140. return 0;
  141. }
  142.  
  143. /** Return a heuristically determined maximum search depth
  144. * based on characteristics of BOARD. */
  145. private static int maxDepth(Board board) {
  146.  
  147. return 4;
  148. }
  149.  
  150. /** Return a heuristic value for BOARD. */
  151. private int staticScore(Board board) {
  152.  
  153.  
  154. /* if kign postion at edge -- retun riwinig value
  155. if king is rookmove away from - side - will win
  156. othersiwede legalmoves that white has
  157.  
  158. */
  159.  
  160. int whites = 0;
  161. int blacks = 0;
  162.  
  163. if (board.kingPosition().isEdge()) {
  164. return WINNING_VALUE;
  165. }
  166.  
  167. final Square[] edges = {
  168. sq(0, 0), sq(0, 1),
  169. sq(0, 2), sq(1, 3),
  170. sq(0, 4), sq(0, 5),
  171. sq(0, 6), sq(0, 7),
  172. sq(0, 8), sq(0, 9),
  173. sq(9, 0), sq(9, 1),
  174. sq(9, 2), sq(9, 3),
  175. sq(9, 4), sq(9, 5),
  176. sq(9, 6), sq(9, 7),
  177. sq(9, 8), sq(9, 9),
  178. sq(0, 9), sq(1, 9),
  179. sq(2, 9), sq(3, 9),
  180. sq(4, 9), sq(5, 9),
  181. sq(6, 9), sq(7, 9),
  182. sq(8, 9), sq(9, 9),
  183. sq(0, 0), sq(1, 0),
  184. sq(2, 0), sq(3, 0),
  185. sq(4, 0), sq(5, 0),
  186. sq(6, 0), sq(7, 0),
  187. sq(8, 0), sq(9, 0)
  188. };
  189.  
  190. for (Square x : edges) {
  191. if (board.kingPosition().isRookMove(x)) {
  192. return WILL_WIN_VALUE;
  193. }
  194. }
  195.  
  196. for (int i = 0; i < 9; i++) {
  197. for (int j = 0; j < 9; j++) {
  198. if (board.get(i, j) == WHITE) {
  199. whites++;
  200. }
  201. if (board.get(i, j) == BLACK) {
  202. blacks++;
  203. }
  204. return whites - blacks;
  205.  
  206. }
  207. }
  208.  
  209. return 0;
  210. }
  211.  
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement