Advertisement
loloof64

TestOfChessPressoCorrected

Apr 6th, 2012
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.69 KB | None | 0 0
  1.  
  2. package com.gmail.bernabe.laurent.j2se.chesspresso_test;
  3.  
  4. import javax.swing.JFrame;
  5.  
  6. import chesspresso.Chess;
  7. import chesspresso.game.Game;
  8. import chesspresso.game.view.GameBrowser;
  9. import chesspresso.move.IllegalMoveException;
  10. import chesspresso.move.Move;
  11. import chesspresso.position.Position;
  12.  
  13. public class ChessFrame extends JFrame {
  14.  
  15.     public ChessFrame(){
  16.         setTitle("Chesspresso gamebrowser test");
  17.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18.        
  19.         add(gameBrowser);
  20.         pack();
  21.        
  22.         try {
  23.             addMove(Chess.E2, Chess.E4, "Debut pour une partie ouverte");
  24.             addMove(Chess.E7, Chess.E5, "Reponse symetrique forte");
  25.             addMove(Chess.G1, Chess.F3, null);
  26.             addMove(Chess.B8, Chess.C6, null);
  27.             addMove(Chess.F1, Chess.B5, null);
  28.         } catch (IllegalMoveException e) {
  29.             // TODO Auto-generated catch block
  30.             e.printStackTrace();
  31.         }
  32.     }
  33.    
  34.     private void addMove(int fromSquareIndex, int toSquareIndex, String comment)
  35.             throws IllegalMoveException {
  36.         Position position = chessGame.getPosition();
  37.         short [] legalMoves = position.getAllMoves();
  38.         for (int moveIndex = 0; moveIndex < legalMoves.length; moveIndex++){
  39.             short testedMove = legalMoves[moveIndex];
  40.             if (Move.getFromSqi(testedMove) == fromSquareIndex
  41.                     && Move.getToSqi(testedMove) == toSquareIndex){
  42.                 try {
  43.                     position.doMove(testedMove);
  44.                     chessGame.addComment(comment);
  45.                 } catch (IllegalMoveException e) {
  46.                     e.printStackTrace();
  47.                 }
  48.                 return;
  49.             }
  50.         }
  51.         throw new IllegalMoveException("");
  52.     }
  53.    
  54.    
  55.     private Game chessGame = new Game();
  56.     private GameBrowser gameBrowser = new GameBrowser(chessGame);
  57.     private static final long serialVersionUID = -6856238414376776882L;
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement