org/homelinux/largo/schach/KIBoard.java
author Markus Bröker <mbroeker@largo.homelinux.org>
Fri, 17 Dec 2010 22:30:33 +0100
changeset 13 f83884cc7d2f
parent 11 1afe167876fb
child 14 f12f77aa13b2
permissions -rw-r--r--
Source Code re-formatted
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     1
/**
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     2
 *   $Id: KIBoard.java 139 2008-04-25 05:02:55Z mbroeker $
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     3
 *  $URL: http://localhost/svn/eclipse/Schachspiel/trunk/org/homelinux/largo/schach/KIBoard.java $
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     4
 */
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     5
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     6
package org.homelinux.largo.schach;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     7
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     8
import org.homelinux.largo.games.board.Move;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     9
import org.homelinux.largo.games.board.chessboard.ChessBoard;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    10
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    11
public class KIBoard extends ChessBoard {
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    12
    static final long serialVersionUID = 200208;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    13
    static final int game_const = 500;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    14
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    15
    protected boolean SIMU_DEBUG = false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    16
    private boolean negate_estimation = false;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    17
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    18
    private Move bestMove;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    19
    private int desired_depth;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    20
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    21
    public KIBoard(int w, int h) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    22
        super(w, h);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    23
        desired_depth = -1;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    24
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    25
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    26
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    27
     * Set the current DEBUG-Level: simu: print all valid, simulated moves undo:
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    28
     * print all undos
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    29
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    30
    public void setDebug(boolean simu, boolean undo) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    31
        SIMU_DEBUG = simu;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    32
        UNDO_DEBUG = undo;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    33
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    34
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    35
    public boolean simu_debug() {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    36
        return SIMU_DEBUG;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    37
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    38
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    39
    public boolean undo_debug() {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    40
        return UNDO_DEBUG;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    41
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    42
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    43
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    44
     * This function flips the sides: Player A maximizes and Player B minimizes
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    45
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    46
    public void negateEstimation(boolean b) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    47
        negate_estimation = b;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    48
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    49
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    50
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    51
     * The Minimax-Algorithm works for TWO-PLAYER Games Player A minimizes,
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    52
     * Player B maximizes
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    53
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    54
    public int estimateFunction() {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    55
        int i;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    56
        int white = 0;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    57
        int black = 0;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    58
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    59
        for (i = 0; i < 64; i++) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    60
            if (isWhite(i))
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    61
                white += getValue(i);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    62
            else if (isBlack(i))
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    63
                black += getValue(i);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    64
        }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    65
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    66
        // Bewerte Entwicklung, Anzahl der Steine
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    67
        // Das hier ist alles zu lahm...
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    68
        white += controls("white");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    69
        black += controls("black");
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    70
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    71
        /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    72
         * solves ticket #3
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    73
         */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    74
        if (negate_estimation)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    75
            return white - black;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    76
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    77
        return black - white;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    78
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    79
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    80
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    81
     * simulates and returns the next possible move for a Player or null, when
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    82
     * no more moves are possible. The Turn will flip automatically if
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    83
     * "simulate(...)" finds a valid move.
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    84
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    85
    Move simulate(int t, int o) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    86
        Move move;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    87
        int value;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    88
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    89
        if (validTurn(o)) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    90
            while (t < 64) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    91
                if (!simulateMove(t, o)) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    92
                    t++;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    93
                    continue;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    94
                }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    95
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    96
                value = estimateFunction();
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    97
                if (SIMU_DEBUG)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
    98
                    print("SIMU", t, o, value);
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    99
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   100
                move = new Move(value, t, o);
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   101
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   102
                /* Flip Sides */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   103
                setBlacksTurn(!isBlacksTurn());
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   104
                return move;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   105
            }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   106
        }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   107
        return null;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   108
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   109
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   110
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   111
     * AlphaBeta-Algorithm: the Maximizer
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   112
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   113
    int max_alpha_beta(int depth, int alpha, int beta) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   114
        int moveValue;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   115
        int o, t;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   116
        Move move;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   117
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   118
        if (desired_depth == -1)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   119
            desired_depth = depth;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   120
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   121
        for (o = 0; o < 64; o++) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   122
            t = 0;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   123
            while ((move = simulate(t, o)) != null) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   124
                /* particular move from black */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   125
                t = move.target;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   126
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   127
                if (depth == 0 || Math.abs(move.value) > game_const)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   128
                    moveValue = move.value;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   129
                else
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   130
                    /* best next move */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   131
                    moveValue = min_alpha_beta(depth - 1, alpha, beta);
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   132
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   133
                undo();
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   134
                t++;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   135
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   136
                if (moveValue >= beta)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   137
                    return beta;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   138
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   139
                if (moveValue > alpha) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   140
                    alpha = moveValue;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   141
                    if (depth == desired_depth)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   142
                        bestMove = move;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   143
                }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   144
            }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   145
        }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   146
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   147
        return alpha;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   148
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   149
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   150
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   151
     * AlphaBeta-Algorithm: the Minimizer
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   152
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   153
    int min_alpha_beta(int depth, int alpha, int beta) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   154
        int moveValue;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   155
        int o, t;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   156
        Move move;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   157
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   158
        for (o = 0; o < 64; o++) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   159
            t = 0;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   160
            while ((move = simulate(t, o)) != null) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   161
                /* particular move from white */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   162
                t = move.target;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   163
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   164
                if (depth == 0 || Math.abs(move.value) > game_const)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   165
                    moveValue = move.value;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   166
                else
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   167
                    /* best next move */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   168
                    moveValue = max_alpha_beta(depth - 1, alpha, beta);
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   169
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   170
                undo();
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   171
                t++;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   172
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   173
                if (moveValue <= alpha)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   174
                    return alpha;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   175
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   176
                if (moveValue < beta)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   177
                    beta = moveValue;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   178
            }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   179
        }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   180
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   181
        return beta;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   182
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   183
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   184
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   185
     * Evaluates the next, best Move after search_depth half-moves. When the
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   186
     * Machine has Black, set negateEstimation(false); When the Machine has
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   187
     * White, set negateEstimation(true);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   188
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   189
    public boolean doBestMove(int search_depth) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   190
        int value;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   191
        desired_depth = -1;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   192
        bestMove = null;
7
93fe1f21e0d8 Class Variables have default values via default
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 3
diff changeset
   193
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   194
        value = max_alpha_beta(search_depth, Integer.MIN_VALUE, Integer.MAX_VALUE);
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   195
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   196
        if (bestMove == null) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   197
            System.err.println("Computing once more...");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   198
            value = max_alpha_beta(--search_depth, Integer.MIN_VALUE, Integer.MAX_VALUE);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   199
            if (bestMove == null) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   200
                System.out.println("Check Mate " + value);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   201
                return false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   202
            }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   203
        }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   204
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   205
        if (doMove(bestMove.target, bestMove.origin)) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   206
            print(getColor(bestMove.target), bestMove.target, bestMove.origin, bestMove.value);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   207
            return true;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   208
        }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   209
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   210
        print("Check Mate", bestMove.target, bestMove.origin, bestMove.value);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   211
        return false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 11
diff changeset
   212
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   213
}