Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tablut;
- /*import java.lang.reflect.Array;*/
- /*import java.util.ArrayList;*/
- import java.util.List;
- import static java.lang.Math.*;
- import static tablut.Square.sq;
- import static tablut.Piece.*;
- /** A Player that automatically generates moves.
- * @author Joymaneet Kaur
- */
- class AI extends Player {
- /** A position-score magnitude indicating a win (for white if positive,
- * black if negative). */
- private static final int WINNING_VALUE = Integer.MAX_VALUE - 20;
- /** A position-score magnitude indicating a forced win in a subsequent
- * move. This differs from WINNING_VALUE to avoid putting off wins. */
- private static final int WILL_WIN_VALUE = Integer.MAX_VALUE - 40;
- /** A magnitude greater than a normal value. */
- private static final int INFTY = Integer.MAX_VALUE;
- /** A new AI with no piece or controller (intended to produce
- * a template). */
- AI() {
- this(null, null);
- }
- /** A new AI playing PIECE under control of CONTROLLER. */
- AI(Piece piece, Controller controller) {
- super(piece, controller);
- }
- @Override
- Player create(Piece piece, Controller controller) {
- return new AI(piece, controller);
- }
- @Override
- String myMove() {
- /** Return either a String denoting either a legal move for me
- * or another command (which may be invalid). Always returns the
- * latter if board().turn() is not myPiece() or if board.winner()
- * is not null. */
- Move nextmove = findMove();
- if (board().turn() != myPiece() || board().winner() != null) {
- return "not valid";
- }
- if (board().turn() == myPiece() || board().winner() == null) {
- return board().legalMoves(myPiece()).get(0).toString();
- }
- return "not valid";
- }
- @Override
- boolean isManual() {
- return false;
- }
- /** Return a move for me from the current position, assuming there
- * is a move. */
- private Move findMove() {
- Board b = new Board(board());
- _lastFoundMove = null;
- return _lastFoundMove;
- }
- /** The move found by the last call to one of the ...FindMove methods
- * below. */
- private Move _lastFoundMove;
- /** Find a move from position BOARD and return its value, recording
- * the move found in _lastFoundMove iff SAVEMOVE. The move
- * should have maximal value or have value > BETA if SENSE==1,
- * and minimal value or value < ALPHA if SENSE==-1. Searches up to
- * DEPTH levels. Searching at level 0 simply returns a static estimate
- * of the board value and does not set _lastMoveFound. */
- private int findMove(Board board, int depth, boolean saveMove,
- int sense, int alpha, int beta) {
- List<Move> legalblack = board.legalMoves(BLACK);
- List<Move> legalwhite = board.legalMoves(WHITE);
- if (depth == 0 || board.kingPosition().isEdge()) {
- return staticScore(board);
- }
- if (board.kingPosition().isEdge()) {
- return staticScore(board);
- }
- Move bestmove = null;
- if (sense == 1) {
- int bestnow = -INFTY;
- for (Move x : legalblack) {
- board.makeMove(x);
- int sense1 = findMove(board, depth - 1, true, -1, alpha, beta);
- if (sense1 > bestnow) {
- bestmove = x;
- bestnow = sense1;
- }
- alpha = Math.max(alpha, bestnow);
- }
- }
- if (sense == -1) {
- int bestnow = INFTY;
- for (Move x: legalwhite) {
- board.makeMove(x);
- int senseneg = findMove(board, depth - 1,
- false, 1, alpha, beta);
- if (senseneg < bestnow) {
- bestmove = x;
- bestnow = senseneg;
- }
- beta = Math.min(bestnow, beta);
- }
- }
- if (saveMove) {
- assert bestmove != null;
- _lastFoundMove = bestmove;
- }
- return 0;
- }
- /** Return a heuristically determined maximum search depth
- * based on characteristics of BOARD. */
- private static int maxDepth(Board board) {
- return 4;
- }
- /** Return a heuristic value for BOARD. */
- private int staticScore(Board board) {
- /* if kign postion at edge -- retun riwinig value
- if king is rookmove away from - side - will win
- othersiwede legalmoves that white has
- */
- int whites = 0;
- int blacks = 0;
- if (board.kingPosition().isEdge()) {
- return WINNING_VALUE;
- }
- final Square[] edges = {
- sq(0, 0), sq(0, 1),
- sq(0, 2), sq(1, 3),
- sq(0, 4), sq(0, 5),
- sq(0, 6), sq(0, 7),
- sq(0, 8), sq(0, 9),
- sq(9, 0), sq(9, 1),
- sq(9, 2), sq(9, 3),
- sq(9, 4), sq(9, 5),
- sq(9, 6), sq(9, 7),
- sq(9, 8), sq(9, 9),
- sq(0, 9), sq(1, 9),
- sq(2, 9), sq(3, 9),
- sq(4, 9), sq(5, 9),
- sq(6, 9), sq(7, 9),
- sq(8, 9), sq(9, 9),
- sq(0, 0), sq(1, 0),
- sq(2, 0), sq(3, 0),
- sq(4, 0), sq(5, 0),
- sq(6, 0), sq(7, 0),
- sq(8, 0), sq(9, 0)
- };
- for (Square x : edges) {
- if (board.kingPosition().isRookMove(x)) {
- return WILL_WIN_VALUE;
- }
- }
- for (int i = 0; i < 9; i++) {
- for (int j = 0; j < 9; j++) {
- if (board.get(i, j) == WHITE) {
- whites++;
- }
- if (board.get(i, j) == BLACK) {
- blacks++;
- }
- return whites - blacks;
- }
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement