Code Search for Developers
 
 
  

GameState.java from GridBlocks at Krugle


Show GameState.java syntax highlighted

package chess.logic;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Stack;

import chess.data.Piece;
import chess.data.Ply;
import chess.data.Square;
import fi.hip.gb.serializer.XMLSerializer;

/**
 * Stores the state of the game.
 * 
 * @author Juho Karppinen
 */
public class GameState extends Hashtable {
	public final static int CHECK = 0;
	public final static int CHECK_MATE = 1;
		
	// which turn is next
	private int turn;
	
	// history of all plies
	private Stack history = new Stack();

	/**
	 * Initializes the starting positions
	 */
	public GameState() {
		super(32);
		
		// white always starts
		this.turn = Piece.WHITE;
		
		// initialize the pieces on the board
		for(int i=Square.A; i <= Square.H; i++) {
			this.put( new Integer(Square.indexAt(i, 2)), 
					new Piece(Piece.WHITE, Piece.PAWN, Square.indexAt(i, 2)) );
			this.put( new Integer(Square.indexAt(i, 7)),
					new Piece(Piece.BLACK, Piece.PAWN, Square.indexAt(i, 7)));
		}

		this.put( new Integer(Square.A1),	new Piece(Piece.WHITE, Piece.ROOK, Square.A1));
		this.put( new Integer(Square.H1), new Piece(Piece.WHITE, Piece.ROOK, Square.H1));
		this.put( new Integer(Square.A8),	new Piece(Piece.BLACK, Piece.ROOK, Square.A8));
		this.put( new Integer(Square.H8),	new Piece(Piece.BLACK, Piece.ROOK, Square.H8));

		this.put( new Integer(Square.B1),	new Piece(Piece.WHITE, Piece.KNIGHT, Square.B1));
		this.put( new Integer(Square.G1),	new Piece(Piece.WHITE, Piece.KNIGHT, Square.G1));
		this.put( new Integer(Square.B8),	new Piece(Piece.BLACK, Piece.KNIGHT, Square.B8));
		this.put( new Integer(Square.G8),	new Piece(Piece.BLACK, Piece.KNIGHT, Square.G8));

		this.put( new Integer(Square.C1),	new Piece(Piece.WHITE, Piece.BISHOP, Square.C1));
		this.put( new Integer(Square.F1),	new Piece(Piece.WHITE, Piece.BISHOP, Square.F1));
		this.put( new Integer(Square.C8),	new Piece(Piece.BLACK, Piece.BISHOP, Square.C8));
		this.put( new Integer(Square.F8),	new Piece(Piece.BLACK, Piece.BISHOP, Square.F8));
		
		this.put( new Integer(Square.D1),	new Piece(Piece.WHITE, Piece.QUEEN, Square.D1));
		this.put( new Integer(Square.D8),	new Piece(Piece.BLACK, Piece.QUEEN, Square.D8));
		
		this.put( new Integer(Square.E1),	new Piece(Piece.WHITE, Piece.KING, Square.E1));
		this.put( new Integer(Square.E8),	new Piece(Piece.BLACK, Piece.KING, Square.E8));
	}
	
	/**
	 * Makes a move
	 * 
	 * @param ply piece to be moved
	 */
	public void ply(Ply ply) {
		/*
		System.out.println("moving " 
				+ Square.toString(ply.getStartPosition())
				+ Square.toString(ply.getEndPosition()));
		*/
		history.push(ply);
		
		// remove from the old location
		if(ply.getStartPosition() != -1) {
			this.remove(new Integer(ply.getStartPosition()));
		}
	
		// piece which will be eaten
		Piece oldPiece = (Piece)this.get(new Integer(ply.getEndPosition()));
		ply.setOldPiece(oldPiece);
		
		// put into the new location
		this.put(new Integer(ply.getEndPosition()), ply.getPiece());
	}
	
	/**
	 * Undo the last ply
	 */
	public void undoPly() {
		/*
		System.out.println("umoving " 
				+ Square.toString(lastPly.getStartPosition())
				+ Square.toString(lastPly.getEndPosition()));
		*/
		
		Ply lastPly = (Ply) history.pop();
		
		// moved piece back to original location 
		this.remove(new Integer(lastPly.getEndPosition()));
		this.put(new Integer(lastPly.getStartPosition()), lastPly.getPiece());

		// bring back the replaced piece
		if(lastPly.getOldPiece() != null) {
			this.put(new Integer(lastPly.getEndPosition()), lastPly.getOldPiece());
		}
	}
	
	/**
	 * Gets piece from given location
	 * @param index index
	 * @return found piece, or null if empty
	 */
	public Piece pieceAt(int index) {
		if(index == -1)
			return null;
		
		return (Piece)this.get(new Integer(index));
	}

	/**
	 * Gets piece from given location
	 * @param col 1...8 (<code>Square.A</code> ... <code>Square.H</code>)
	 * @param row 1...8
	 * @return found piece, or null if empty
	 */

	public Piece pieceAt(int col, int row) {
		return pieceAt(Square.indexAt(col, row));
	}	
	
	/**
	 * Gets position of the piece
	 * @param piece 
	 * @return index of the square, or -1 if not found
	 */
	public int pos(Piece piece) {
		for(Enumeration e=this.keys(); e.hasMoreElements();) {
			Integer pos = (Integer) e.nextElement();
			if(piece.equals(this.get(pos))) {
				return pos.intValue();
			}
		}
		return -1;
	}
	
	/**
	 * Which one has next turn
	 * @return either <code>Piece.WHITE</code> or <code>Piece.BLACK</code>.
	 */
	public int getTurn() {
		return this.turn;
	}
	public void setTurn(int newTurn) {
		this.turn = newTurn;
	}
	
	/**
	 * Enumeration containing all pieces
	 * @return <code>Piece</code> objects
	 */
	public Enumeration allPieces() {
		return this.elements();
	}

	/**
	 * Enumeration containing all piece positions 
	 * @return square indexes as <code>Integer</code> format
	 */
	public Enumeration allPositions() {
		return this.keys();
	}
    
	public String toString() {
        try {
            return new XMLSerializer(this.getClass()).serialize(this);
        } catch (Exception e) {
            return e.getMessage();
        }
	}
}




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