package com.gmail.bernabe.laurent.j2se.chesspresso_test;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import chesspresso.Chess;
import chesspresso.game.Game;
import chesspresso.game.view.GameBrowser;
import chesspresso.move.IllegalMoveException;
import chesspresso.move.Move;
import chesspresso.position.Position;
public class ChessFrame extends JFrame {
public ChessFrame(){
PROMOTION_LISTENER = new MyPromotionListener();
setTitle("Chesspresso gamebrowser test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(gameBrowser);
pack();
try {
addMove(Chess.E2, Chess.E4, null, PROMOTION_LISTENER);
addMove(Chess.D7, Chess.D5, null, PROMOTION_LISTENER);
addMove(Chess.E4, Chess.D5, null, PROMOTION_LISTENER);
addMove(Chess.E7, Chess.E6, null, PROMOTION_LISTENER);
addMove(Chess.D5, Chess.E6, null, PROMOTION_LISTENER);
addMove(Chess.H7, Chess.H6, null, PROMOTION_LISTENER);
addMove(Chess.E6, Chess.F7, null, PROMOTION_LISTENER);
addMove(Chess.E8, Chess.E7, null, PROMOTION_LISTENER);
addMove(Chess.F7, Chess.G8, null, PROMOTION_LISTENER);
} catch (IllegalMoveException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public interface PromotionListener {
int getPromotionPiece();
}
private void addMove(int fromSquareIndex, int toSquareIndex, String comment,
ChessFrame.PromotionListener promotionListener)
throws IllegalMoveException {
matchedMoves.clear();
Position position = chessGame.getPosition();
short [] legalMoves = position.getAllMoves();
for (int moveIndex = 0; moveIndex < legalMoves.length; moveIndex++){
short testedMove = legalMoves[moveIndex];
if (Move.getFromSqi(testedMove) == fromSquareIndex
&& Move.getToSqi(testedMove) == toSquareIndex){
// move is legal
matchedMoves.add(testedMove);
}
}
if (matchedMoves.size() == 0)
throw new IllegalMoveException("");
short move = Move.ILLEGAL_MOVE;
if (Move.isPromotion(matchedMoves.get(0))){
int promotionPiece = promotionListener.getPromotionPiece();
for (Short testedMove : matchedMoves){
if (Move.getPromotionPiece(testedMove) == promotionPiece) {
move = testedMove;
break;
}
}
}
else
move = matchedMoves.get(0);
chessGame.getPosition().doMove(move);
chessGame.addComment(comment);
}
private class MyPromotionListener implements PromotionListener {
@Override
public int getPromotionPiece() {
int selectionID = JOptionPane.showOptionDialog(ChessFrame.this,
"Choose your promotion piece", "Promotion piece selection",
JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE, null,
new String[]{"Queen", "Rook", "Bishop", "Knight"}, 0);
int promotionPiece;
switch(selectionID){
case 1:
promotionPiece =Chess.ROOK;
break;
case 2:
promotionPiece =Chess.BISHOP;
break;
case 3:
promotionPiece =Chess.KNIGHT;
break;
default:
case 0:
promotionPiece = Chess.QUEEN;
}
return promotionPiece;
}
}
private ArrayList<Short> matchedMoves = new ArrayList<Short>(10);
private Game chessGame = new Game();
private GameBrowser gameBrowser = new GameBrowser(chessGame);
private final MyPromotionListener PROMOTION_LISTENER;
private static final long serialVersionUID = -6856238414376776882L;
}