Code Search for Developers
 
 
  

CheckMate.java from GridBlocks at Krugle


Show CheckMate.java syntax highlighted

package chess.logic;

import java.rmi.RemoteException;

import chess.data.Piece;
import chess.data.Ply;
import chess.data.Square;
import fi.hip.gb.mobile.MobileAgent;

/**
 * Runs the game, makes plies and stores the state of the game
 * 
 * @author Juho Karppinen
 */
@MobileAgent (jobID=-1)
public class CheckMate {
	
	private GameState state;
	
    /**
     * Start new game
     */
	public CheckMate() {
        this(new GameState());
	}	

    /**
     * Resume old game.
     * @param oldState
     */
	public CheckMate(GameState oldState) {
        Square.initialize(); 
		state = oldState;
	}
    
	/**
	 * Player wants to ply, first validate the ply and then do it.
	 * 
	 * @param ply ply user is going to make
	 * @return computers move
	 * @throws RemoteException if failed to ply
	 */
	public Ply playerPly(int from, int to) throws RemoteException {
        Piece piece = this.state.pieceAt(from);
        Ply ply = new Ply(piece, from, to);
        
        System.out.println("Moving from " + from + " to " + to);
        System.out.println(ply.getStartPosition()  + " " + ply.getEndPosition());
        
		if(state.getTurn() != ply.getPiece().getColor()) {
			throw new RemoteException("Not my turn");
		}
		
		String valid = new Validator(state).validatePlayerPly(ply);
		if(valid != null) {
		    throw new RemoteException("Failed to move the piece: " + valid);
		} else {
			state.ply(ply);
			//CheckMate.instance().repaintPly(ply);
			//CheckMate.setInformation();

			// next player
			state.setTurn(state.getTurn()*-1);
			//CheckMate.instance().refresh();
			
			

	        Ply bestPly = computerPly(false);
	        //System.out.println("Computer moved "
	        //        + Square.toString(bestPly.getStartPosition())
	        //        + Square.toString(bestPly.getEndPosition()) + " = "
	        //        + bestPly.getScore() + " (" + bestPly.getCalculations()
	        //        + " moves)");
			return bestPly;
		}
	}
	
	/**
	 * Do the computers turn
	 * @param simulate only simulates the possible moves
	 * @return Computed Ply object
	 */
	public Ply computerPly(final boolean simulate) {
	    /*
		new Thread(new Runnable() {
			public void run() {
	    */				
				int searchDepth = 1;
				Ply bestPly = null;
				Iterator iterator = new Iterator(state, simulate);
				do {
					 bestPly = iterator.calculate(searchDepth);
					searchDepth++;
				} while(searchDepth < 2);
                
                if(bestPly == null)
                    return null;
				
				// statistics
				bestPly.setCalculation(iterator.getCalculations());
				
				// next player
				if(! simulate) {
					state.ply(bestPly);
					//CheckMate.instance().repaintPly(bestPly);
					
					state.setTurn(state.getTurn()*-1);
					//CheckMate.instance().refresh();
				}
				
				return bestPly;
								
		//	}
		//}).start();
	}
	
	public GameState getGameState() {
		return state;
	}
		
	public void endGame() {
	}
    
    public static void main(String[] args) {
        CheckMate game = new CheckMate();
        MobileAgent agent = game.getClass().getAnnotation(MobileAgent.class);
        
        //GameState state = game.getGameState();
        chess.ui.CheckGUI gui = new chess.ui.CheckGUI();
        gui.init();
    }
}




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