1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
| public class ChessExample {
private static final int PREFERRED_SIZE = 64;
private static final Color WHITE_SQUARE = new Color(0xFC,0xFC,0xFF);
private static final Color BLACK_SQUARE = new Color(0x03,0x03,0x00);
private static final Color SELECTION_COLOR = new Color(0xFF,0xA4,0x77);
public static final ImageIcon BLACK_PAWN = new ImageIcon(ChessExample.class.getResource("blackpawn.png"));
public static final ImageIcon WHITE_PAWN = new ImageIcon(ChessExample.class.getResource("whitepawn.png"));
public static void main(String[] args) {
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
GameBoard gameboard = new GameBoard(8,8);
for(int i=0;i<8; i++) {
gameboard.setPiece(1,i,new Pawn(Player.BLACK));
gameboard.setPiece(6,i,new Pawn(Player.WHITE));
}
frame.add(gameboard);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static class GameBoard extends JPanel {
private static final long serialVersionUID = 1L;
private final int nbColumns;
private final int nbLines;
private final Controller controller;
public GameBoard(int nbColumns, int nbLines) {
super(new GridLayout(nbLines, nbColumns));
this.nbColumns=nbColumns;
this.nbLines=nbLines;
this.controller = new Controller(this); // le contrôleur doit avoir acces à la vue et au modèle
setPreferredSize(new Dimension(nbColumns*PREFERRED_SIZE,nbLines*PREFERRED_SIZE));
}
public int getNbLines() {
return nbLines;
}
public int getNbColumns() {
return nbColumns;
}
public void setPiece(int column, int line, Piece piece) {
controller.setPiece(column, line, piece);
}
public Square getSquare(int column, int line) {
return controller.getSquare(column, line);
}
}
public enum Player {
WHITE,
BLACK;
}
public enum PieceType {
PAWN(WHITE_PAWN,BLACK_PAWN),
/*KING,
QUEEN,
ROOK,
BISHOP,
KNIGHT*/
;
private final ImageIcon whiteImage;
private final ImageIcon blackImage;
private PieceType(ImageIcon whiteImage, ImageIcon blackImage) {
this.whiteImage=whiteImage;
this.blackImage=blackImage;
}
public ImageIcon getImage(Player player) {
switch(player) {
case BLACK:
return blackImage;
case WHITE:
return whiteImage;
default:
throw new IllegalStateException("Unknown player "+player);
}
}
}
public static abstract class Piece {
private final Player player;
private final PieceType type;
public Piece(PieceType type, Player player) {
this.type=type;
this.player=player;
}
public PieceType getType() {
return type;
}
public Player getPlayer() {
return player;
}
public ImageIcon getImage() {
return getType().getImage(player);
}
}
public static class Pawn extends Piece {
public Pawn(Player player) {
super(PieceType.PAWN,player);
}
}
public static class Square extends JPanel {
private static final long serialVersionUID = 1L;
private final Color color;
private final int column;
private final int line;
private Piece piece;
public Square(int column, int line, Color color) {
super(new BorderLayout()); // centrer la pièce
this.color=color;
setBackground(color);
this.column=column;
this.line=line;
setPreferredSize(new Dimension(PREFERRED_SIZE, PREFERRED_SIZE));
}
public int getLine() {
return line;
}
public int getColumn() {
return column;
}
public void setPiece(Piece piece) {
this.piece=piece;
if ( piece==null ) {
removeAll();
}
else {
add(new JLabel(piece.getImage()));
revalidate();
}
}
public Piece getPiece() {
return piece;
}
public boolean isEmpty() {
return piece==null;
}
public void setSelection(boolean selection) {
setBackground(selection?SELECTION_COLOR:color);
}
}
public static class Controller {
private final GameBoard board;
private final Square[][] squares;
private Square currentMove;
public Controller(GameBoard board) {
this.board=board;
this.squares=new Square[board.getNbLines()][board.getNbColumns()];
final int nbPieces=board.getNbLines()*board.getNbColumns();
for(int i=0; i<nbPieces; i++) {
final int line = i/board.getNbColumns();
final int column = i%board.getNbColumns();
final Square square = new Square(column, line, i%2==line%2?WHITE_SQUARE:BLACK_SQUARE);
board.add(square);
squares[line][column]=square;
}
MouseAdapter mouseAdapter = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Square square = (Square) e.getSource();
if ( !square.isEmpty() ) {
if ( isMoving() ) {
if ( square.getPiece()==currentMove.getPiece() ) {
// cancel move
stopMove();
board.repaint();
}
else if ( square.getPiece().getPlayer()==currentMove.getPiece().getPlayer() ) {
JOptionPane.showMessageDialog(board, "Une pièce à vous est déjà dans cette case","Mouvement impossible", JOptionPane.WARNING_MESSAGE);
}
else if ( moveIsAllowed(currentMove.getPiece(), currentMove, square) ) {
doCapture(square);
moveTo(currentMove, square);
stopMove();
board.repaint();
}
}
else {
startMove(square);
board.repaint();
}
}
else if ( isMoving() ) {
moveTo(currentMove, square);
stopMove();
}
}
};
for(Square[] line : squares) {
for(Square square : line) {
square.addMouseListener(mouseAdapter);
}
}
}
/**
* Cette méthode ne teste pas si la pièce à le droit de se trouver dans cette case
* @param column
* @param line
* @param piece
*/
public void setPiece(int column, int line, Piece piece) {
if ( squares[column][line].isEmpty() ) {
squares[column][line].setPiece(piece);
}
else {
throw new IllegalStateException("Case occupée");
}
}
private void moveTo(Square fromSquare, Square toSquare) {
final Piece piece = fromSquare.getPiece();
if ( piece==null ) throw new IllegalStateException("Pas de pièce dans la case de départ");
if ( moveIsAllowed(piece, fromSquare, toSquare) ) {
toSquare.setPiece(piece);
fromSquare.setPiece(null);
}
else {
JOptionPane.showMessageDialog(board, "Ce mouvement n'est pas autorisé par les règles des Echecs","Mouvement impossible", JOptionPane.WARNING_MESSAGE);
}
}
private void stopMove() {
if ( currentMove!=null ) {
currentMove.setSelection(false);
currentMove=null;
}
}
private void doCapture(Square square) {
// TODO
square.setPiece(null); // temp
}
private boolean moveIsAllowed(Piece piece, Square fromSquare, Square toSquare) {
return true; // TODO
}
public Square getSquare(int column, int line) {
return squares[line][column];
}
public boolean isMoving() {
return currentMove!=null;
}
private void startMove(Square square) {
if ( currentMove!=null ) {
stopMove();
}
currentMove=square;
square.setSelection(true);
}
}
} |
Partager