org/homelinux/largo/schach/KIBoard.java
changeset 7 93fe1f21e0d8
parent 3 673c7ef3411e
child 11 1afe167876fb
equal deleted inserted replaced
6:ad18534e08fd 7:93fe1f21e0d8
    79 	/**
    79 	/**
    80 	 * simulates and returns the next possible move for a Player or null, when no more moves are possible.
    80 	 * simulates and returns the next possible move for a Player or null, when no more moves are possible.
    81 	 * The Turn will flip automatically if "simulate(...)" finds a valid move.
    81 	 * The Turn will flip automatically if "simulate(...)" finds a valid move.
    82 	 */
    82 	 */
    83 	Move simulate(int t, int o) {
    83 	Move simulate(int t, int o) {
    84 		Move move = null;
    84 		Move move;
    85 		int value;
    85 		int value;
    86 
    86 
    87 		if ( validTurn(o) ) {
    87 		if ( validTurn(o) ) {
    88 			while ( t < 64 ) {
    88 			while ( t < 64 ) {
    89 				if (!simulateMove(t, o) ) {
    89 				if (!simulateMove(t, o) ) {
   109 	 * AlphaBeta-Algorithm: the Maximizer
   109 	 * AlphaBeta-Algorithm: the Maximizer
   110 	 */
   110 	 */
   111 	int max_alpha_beta (int depth, int alpha, int beta) {
   111 	int max_alpha_beta (int depth, int alpha, int beta) {
   112 		int moveValue;
   112 		int moveValue;
   113 		int o, t;
   113 		int o, t;
   114 		Move move = null;
   114 		Move move;
   115 
   115 
   116 		if ( desired_depth == -1 )
   116 		if ( desired_depth == -1 )
   117 			desired_depth = depth;
   117 			desired_depth = depth;
   118 
   118 
   119 		for (o = 0; o < 64; o++) {
   119 		for (o = 0; o < 64; o++) {
   148 	 * AlphaBeta-Algorithm: the Minimizer
   148 	 * AlphaBeta-Algorithm: the Minimizer
   149 	 */
   149 	 */
   150 	int min_alpha_beta(int depth, int alpha, int beta) {
   150 	int min_alpha_beta(int depth, int alpha, int beta) {
   151 		int moveValue;
   151 		int moveValue;
   152 		int o, t;
   152 		int o, t;
   153 		Move move = null;
   153 		Move move;
   154 
   154 
   155 		for (o = 0; o < 64; o++) {
   155 		for (o = 0; o < 64; o++) {
   156 			t=0;
   156 			t=0;
   157 			while ( (move=simulate(t, o)) != null ) {
   157 			while ( (move=simulate(t, o)) != null ) {
   158 				/* particular move from white */
   158 				/* particular move from white */
   182 	 * When the Machine has Black, set negateEstimation(false);
   182 	 * When the Machine has Black, set negateEstimation(false);
   183 	 * When the Machine has White, set negateEstimation(true);
   183 	 * When the Machine has White, set negateEstimation(true);
   184 	 */
   184 	 */
   185 	public boolean doBestMove(int search_depth) {
   185 	public boolean doBestMove(int search_depth) {
   186 		int value;
   186 		int value;
   187 
       
   188 		desired_depth = -1;
   187 		desired_depth = -1;
   189 		bestMove = null;
   188 		bestMove = null;
       
   189 
   190 		value = max_alpha_beta(search_depth, Integer.MIN_VALUE, Integer.MAX_VALUE);
   190 		value = max_alpha_beta(search_depth, Integer.MIN_VALUE, Integer.MAX_VALUE);
   191 
   191 
   192 		if ( bestMove == null ) {
   192 		if ( bestMove == null ) {
   193 			System.err.println("Computing once more...");
   193 			System.err.println("Computing once more...");
   194 			value = max_alpha_beta(1, Integer.MIN_VALUE, Integer.MAX_VALUE);
   194 			value = max_alpha_beta(--search_depth, Integer.MIN_VALUE, Integer.MAX_VALUE);
   195 			if (bestMove == null) {
   195 			if (bestMove == null) {
   196 				System.out.println("Check Mate");
   196 				System.out.println("Check Mate " + value);
   197 				return false;
   197 				return false;
   198 			}
   198 			}
   199 		}
   199 		}
   200 
   200 
   201 		if (doMove(bestMove.target, bestMove.origin)) {
   201 		if (doMove(bestMove.target, bestMove.origin)) {