Code Search for Developers
 
 
  

Generator.java from GridBlocks at Krugle


Show Generator.java syntax highlighted

package chess.logic;

import java.util.Enumeration;
import java.util.Vector;

import chess.data.Piece;
import chess.data.Ply;
import chess.data.Square;

/**
 * Generates all possible moves
 * 
 * @author Juho Karppinen
 * @version $Id: Generator.java 102 2004-11-12 14:31:37Z jkarppin $
 */
public class Generator {
	// current piece and its position
	private Piece curPiece = null;
	private int curPosition = -1;
	private int curCol = -1;
	private int curRow = -1;
	
	// all valid plies are put in here
	Vector curPlies = new Vector();
	
	// my color, either <code>Piece.WHITE</code> or <code>Piece.BLACK</code>
	private int myColor = 0;
	
	/**
	 * the direction of attack, <code>Piece.WHITE</code> goes up,
	 * <code>Piece.BLACK</code> goes down
	 */ 
	private int direction = 0;
	
	// current state of the game
	private GameState curState;
	
	/**
	 * Generate all possible moves
	 * @param state the state of the game
	 * @param whiteColor is color white
	 */
	public Generator(GameState state, boolean whiteColor) {
		curState = state;
		
		if(whiteColor)
			myColor = direction = 1;
		else
			myColor = direction = -1;
		
		generatePlies();
	}
	
	/**
	 * Gets all generated plies
	 * 
	 * @return an array of possible moves
	 */
	public Ply[] getPlies() {
		return (Ply[]) curPlies.toArray(new Ply[curPlies.size()]);
	}
	
	/**
	 * Generate plies for every piece on the board. 
	 */
	private void generatePlies() {
		for(Enumeration e=curState.allPositions();e.hasMoreElements();){
			// position of the piece
			curPosition = ((Integer) e.nextElement()).intValue();
			curCol = Square.getColumn(curPosition);
			curRow = Square.getRow(curPosition);
			
			// our piece
			curPiece = curState.pieceAt(curPosition);
			
			if(curPiece.getColor() != myColor) {
				continue; // wrong color
				
			} else if(curPiece.getType() == Piece.PAWN){
				generatePawnPlies();
			} else if(curPiece.getType() == Piece.BISHOP){
				generateDirectionPlies(Validator.bishopDirs);
			} else if(curPiece.getType() == Piece.KNIGHT){
				generateOffsetPlies(Validator.knightMoves);
			} else if(curPiece.getType() == Piece.KING) {
				generateOffsetPlies(Validator.kingMoves);
			} else if(curPiece.getType() == Piece.ROOK) {
				generateDirectionPlies(Validator.rookDirs);
			} else if(curPiece.getType() == Piece.QUEEN) {
				// queen can do both the bishops and rooks plies
				generateDirectionPlies(Validator.bishopDirs);
				generateDirectionPlies(Validator.rookDirs);
			}
		}
	}
	
	/**
	 * Generates all moves for pawn
	 */
	private void generatePawnPlies() {
		// can we move forward
		int targetPos = Square.indexAt(curCol, curRow+direction );
		if(curState.pieceAt(targetPos) == null) {
			addPly(targetPos);
			
			// can we move forward 2 steps
			targetPos = Square.indexAt(curCol, curRow+direction*2 );
			if(curPiece.getHomePosition() == curState.pos(curPiece) 
			      && curState.pieceAt(targetPos) == null) {
			    
				addPly(targetPos);
			}
		}
		
		// can we eat from left
		targetPos = Square.indexAt(curCol-1, curRow+direction );
		Piece target = curState.pieceAt(targetPos);
		if(target != null && target.getColor() != myColor) {
			addPly(targetPos);
		}
		// can we eat from right
		targetPos = Square.indexAt(curCol+1, curRow+direction );
		target = curState.pieceAt(targetPos);
		if(target != null && target.getColor() != myColor) {
			addPly(targetPos);
		}
	}
	
	/**
	 * Generates all possible moves which can be reachable
	 * by the piece
	 * @param allowedDirections an array of allowed directions
	 */
	private void generateDirectionPlies(int [][] allowedDirections) {
		// first go throught all possible directions
		for(int i=0; i< allowedDirections.length; i++) {
			// now iterate all possible lengths, starting from 1
			int diff = 1;
			int targetPos = 0;
			while( targetPos != -1 ) {
				targetPos = Square.indexAt(
						curCol + allowedDirections[i][0]*diff, 
						curRow + allowedDirections[i][1]*diff );
				if(targetPos != -1) {
					// valid position, check if we can move into it
					Piece target = curState.pieceAt(targetPos);
					if(target == null || (target != null && target.getColor() != myColor) ) {
						// empty square or eats enemy
						addPly(targetPos);
						// cannot continue after eating
						if(target != null)
							targetPos = -1;
					} else {
						// someone blocks the way
						targetPos = -1;
					}
					diff++;
				}
			}
		}
	}
	
	
	/**
	 * Generate all moves which are allowed for the piece
	 * @param allowedMoves all possible offsets which are possible for piece
	 */
	private void generateOffsetPlies(int [][] allowedMoves) {
		for(int i=0; i < allowedMoves.length; i++) {
			int targetPos = Square.indexAt(
					curCol + allowedMoves[i][0],	
					curRow + allowedMoves[i][1] );
			if(targetPos != -1) {
				// position is valid
				Piece target = curState.pieceAt(targetPos);
				if(target == null || (target != null && target.getColor() != myColor) ) {
					// empty square or eats enemy
					addPly(targetPos);
				}
			}
		}
	}
	
	private void addPly(int endPosition) {
		curPlies.add(new Ply(curPiece, curPosition, endPosition));
	}
}




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