Code Search for Developers
 
 
  

Iterator.java from GridBlocks at Krugle


Show Iterator.java syntax highlighted

package chess.logic;

import chess.data.Piece;
import chess.data.Ply;
import chess.ui.CheckGUI;

/**
 * Iterates throught all possible moves, and chooses best of them.
 * 
 * @author Juho Karppinen
 * @version $Id: Iterator.java 1073 2006-06-07 12:18:06Z jkarppin $
 */
public class Iterator {
	// current depth
	private int curSearchDepth;
	
	// starting position
	private GameState startState;
	
	// are we simulating moves
	private boolean simulate = false;
	
	private int calculations = 0;
	
	public Iterator(GameState gameState, boolean simulatePlies) {
		startState = gameState;
		simulate = simulatePlies;
	}

	/**
	 * Start calculating alpha-beta minmax with given 
	 * search depth
	 * @param searchDepth depth of the search
	 * @return best move so far
	 */
	public Ply calculate(int searchDepth) {
		this.curSearchDepth = searchDepth;
		
		int curAlpha = Integer.MIN_VALUE;
		int curBeta = Integer.MAX_VALUE;
		int bestIndex = -1;
		
		boolean white = (startState.getTurn()==Piece.WHITE);
		
		// generate all possible moves
		Ply[] plies = new Generator(startState, white).getPlies();
		calculations += plies.length;
		for(int i=0; i < plies.length; i++) {
			// make the move
			startState.ply(plies[i]);
			
			if(simulate) {
				CheckGUI.instance().repaintPly(plies[i]);
				try {	
					Thread.sleep(300);	
				} catch(Exception e) {
				}
			}
			
			int val = iterate(plies[i], startState, ! white, 1, curAlpha, curBeta);
			if( white ) {
				if( val > curAlpha) {
			    curAlpha = val;
			    bestIndex = i;
				}
			} else {
				if( val < curBeta) {
			    curBeta = val;
			    bestIndex = i;
				}
			}

			// go back to original move
			startState.undoPly();
			
			if(white && ( curAlpha >= curBeta)
					|| !white && ( curBeta <= curAlpha)) {
			  // no need to continue this branch
			  break;
			}
			
			if(simulate) {
				CheckGUI.instance().repaintPly(plies[i]);
			}
		}

		if(bestIndex != -1) {
			// set the score and return best ply
			plies[bestIndex].setScore(white ? curAlpha : curBeta);
			return plies[bestIndex];
		} else {
			System.out.println("NULL " + plies.length); 
			return null;
		} 
	}
	
	/**
	 * Continue iteration deeper on the tree
	 * 
	 * @param ply last ply
	 * @param state current state of the board
	 * @param white is the current color white
	 * @param depth current depth of iteration, if too deep
	 * @param alpha current alpha value
	 * @param beta current beta value
	 * @return score of this iteration
	 */
	public int iterate(Ply ply, GameState state, boolean white, int depth, int alpha, int beta) {
    if(depth > curSearchDepth) {

    	// we are on the leaf or too deep, return the score now
    	return new Analyzer(state).analyze(white);
    }
    
    // else continue deeper
    
    int curAlpha = alpha;
    int curBeta = beta;
    int bestIndex = -1;
    
		// generate new set of plies
		Ply[] plies = new Generator(state, white).getPlies();
		calculations += plies.length;
		for(int i=0; i < plies.length; i++) {
			state.ply(plies[i]);
		
			if(simulate) {
				CheckGUI.instance().repaintPly(plies[i]);
				/*
				try {	
					Thread.sleep(300);	
				} catch(Exception e) {
				}*/
			}
		
			int val = iterate(plies[i], state, ! white, depth + 1, curAlpha, curBeta);
			if( white ) {
				if( val > curAlpha) {
			    curAlpha = val;
			    bestIndex = i;
				}
			} else {
				if( val < curBeta) {
			    curBeta = val;
			    bestIndex = i;
				}
			}
			
			state.undoPly();
			if(simulate) {
				CheckGUI.instance().repaintPly(plies[i]);
			}
			
			if(white && ( curAlpha >= curBeta)
					|| ! white && ( curBeta <= curAlpha)) {
			  // no need to continue this branch
			  break;
			}
		}
		/*
		if(plies.length == 0) {
			// no possible moves left
		}
		*/
		
		return (white) ? curAlpha : curBeta;
	}
	
	public int getCalculations() {
		return calculations;
	}
}




See more files for this project here

GridBlocks

GridBlocks builds a grid application framework via easy-to-use building blocks in distributed environment. The framework offers components for Grid security, distributed storage, computing, and Portlet web interfaces.

Project homepage: http://sourceforge.net/projects/gridblocks
Programming language(s): Java,JSP,XML
License: other

  Analyzer.java
  CheckMate.java
  GameState.java
  Generator.java
  Iterator.java
  Validator.java