Advertisement
loloof64

TestOfChessPresso_choosingPromotionPiece

Apr 6th, 2012
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.40 KB | None | 0 0
  1. package com.gmail.bernabe.laurent.j2se.chesspresso_test;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import javax.swing.JFrame;
  6. import javax.swing.JOptionPane;
  7.  
  8. import chesspresso.Chess;
  9. import chesspresso.game.Game;
  10. import chesspresso.game.view.GameBrowser;
  11. import chesspresso.move.IllegalMoveException;
  12. import chesspresso.move.Move;
  13. import chesspresso.position.Position;
  14.  
  15. public class ChessFrame extends JFrame {
  16.  
  17.     public ChessFrame(){
  18.         PROMOTION_LISTENER = new MyPromotionListener();
  19.         setTitle("Chesspresso gamebrowser test");
  20.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  21.        
  22.         add(gameBrowser);
  23.         pack();
  24.        
  25.         try {
  26.             addMove(Chess.E2, Chess.E4, null, PROMOTION_LISTENER);
  27.             addMove(Chess.D7, Chess.D5, null, PROMOTION_LISTENER);
  28.             addMove(Chess.E4, Chess.D5, null, PROMOTION_LISTENER);
  29.             addMove(Chess.E7, Chess.E6, null, PROMOTION_LISTENER);
  30.             addMove(Chess.D5, Chess.E6, null, PROMOTION_LISTENER);
  31.             addMove(Chess.H7, Chess.H6, null, PROMOTION_LISTENER);
  32.             addMove(Chess.E6, Chess.F7, null, PROMOTION_LISTENER);
  33.             addMove(Chess.E8, Chess.E7, null, PROMOTION_LISTENER);
  34.             addMove(Chess.F7, Chess.G8, null, PROMOTION_LISTENER);
  35.         } catch (IllegalMoveException e) {
  36.             // TODO Auto-generated catch block
  37.             e.printStackTrace();
  38.         }
  39.     }
  40.    
  41.     public interface PromotionListener {
  42.  
  43.         int getPromotionPiece();
  44.        
  45.     }
  46.    
  47.     private void addMove(int fromSquareIndex, int toSquareIndex, String comment,
  48.             ChessFrame.PromotionListener promotionListener)
  49.             throws IllegalMoveException {
  50.         matchedMoves.clear();
  51.         Position position = chessGame.getPosition();
  52.         short [] legalMoves = position.getAllMoves();
  53.        
  54.         for (int moveIndex = 0; moveIndex < legalMoves.length; moveIndex++){
  55.             short testedMove = legalMoves[moveIndex];
  56.             if (Move.getFromSqi(testedMove) == fromSquareIndex
  57.                     && Move.getToSqi(testedMove) == toSquareIndex){
  58.                 // move is legal
  59.                 matchedMoves.add(testedMove);
  60.             }
  61.         }
  62.        
  63.         if (matchedMoves.size() == 0)
  64.             throw new IllegalMoveException("");
  65.        
  66.         short move = Move.ILLEGAL_MOVE;
  67.         if (Move.isPromotion(matchedMoves.get(0))){
  68.             int promotionPiece = promotionListener.getPromotionPiece();
  69.             for (Short testedMove : matchedMoves){
  70.                 if (Move.getPromotionPiece(testedMove) == promotionPiece) {
  71.                     move = testedMove;
  72.                     break;
  73.                 }
  74.             }
  75.         }
  76.         else
  77.             move = matchedMoves.get(0);
  78.        
  79.         chessGame.getPosition().doMove(move);
  80.         chessGame.addComment(comment);
  81.     }
  82.    
  83.     private class MyPromotionListener implements PromotionListener {
  84.  
  85.         @Override
  86.         public int getPromotionPiece() {
  87.             int selectionID = JOptionPane.showOptionDialog(ChessFrame.this,
  88.                     "Choose your promotion piece", "Promotion piece selection",
  89.                     JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE, null,
  90.                     new String[]{"Queen", "Rook", "Bishop", "Knight"}, 0);
  91.             int promotionPiece;
  92.             switch(selectionID){
  93.             case 1:
  94.                 promotionPiece =Chess.ROOK;
  95.                 break;
  96.             case 2:
  97.                 promotionPiece =Chess.BISHOP;
  98.                 break;
  99.             case 3:
  100.                 promotionPiece =Chess.KNIGHT;
  101.                 break;
  102.             default:
  103.             case 0:
  104.                 promotionPiece = Chess.QUEEN;
  105.             }
  106.             return promotionPiece;
  107.         }
  108.        
  109.     }
  110.    
  111.     private ArrayList<Short> matchedMoves = new ArrayList<Short>(10);
  112.     private Game chessGame = new Game();
  113.     private GameBrowser gameBrowser = new GameBrowser(chessGame);
  114.     private final MyPromotionListener PROMOTION_LISTENER;
  115.     private static final long serialVersionUID = -6856238414376776882L;
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement