Frames

First open

0
1package chess;
2
3import chess.ai.BestMoveCalcultor;
4import chess.board.*;
5import chess.domain.Piece;
6
7import javax.swing.*;
8import java.awt.*;
9import java.awt.event.ActionEvent;
10import java.awt.event.ActionListener;
11import java.util.Set;
12
13/**
14 * @author akashMaurya
15 * @Date 09/10/17.
16 */
17
18public class MoveListener implements ActionListener {
19
20 private Board board;
21
22 private MoveListener() {
23 }
24
25 private static final MoveListener moveListener = new MoveListener();
26
27 public static MoveListener getMoveListener() {
28 return moveListener;
29 }
30
31 @Override
32 public void actionPerformed(ActionEvent e) {
33 final Square source = (Square) e.getSource();
34 performMove(source);
35 }
36
37 public void performMove(Square source) {
38 if (source.getPiece() != null) {
39 final Piece piece = source.getPiece();
40 if (piece.getColor() != getBoard().getGame().getTurn() && GameMove.getMoveState() != MoveState.PARTIAL) {
41 return;
42 }
43 }
44 final MoveState moveState = GameMove.move(getBoard().getGame(), source);
45 BoardUtils.resetSquareColors();
46 final boolean checkStateBlack = getBoard().getGame().isCheckState(Color.BLACK);
47 if (checkStateBlack) {
48 final Square kingSquare = getBoard().getGame().getKingSquare(Color.BLACK);
49 kingSquare.setBackground(Color.RED);
50 kingSquare.getPiece().setHasMoved(true);
51 if (getBoard().getGame().isCheckMate(Color.BLACK, kingSquare)) {
52 JOptionPane.showMessageDialog(getBoard(), "Game over, White Won");
53 }
54
55 }
56
57 final boolean checkStateWhite = getBoard().getGame().isCheckState(Color.WHITE);
58 if (checkStateWhite) {
59 final Square kingSquare = getBoard().getGame().getKingSquare(Color.WHITE);
60 kingSquare.setBackground(Color.RED);
61 kingSquare.getPiece().setHasMoved(true);
62 if (getBoard().getGame().isCheckMate(Color.WHITE, kingSquare)) {
63 JOptionPane.showMessageDialog(getBoard(), "Game over, Black Won");
64 }
65 }
66 if (moveState == MoveState.PARTIAL) {
67 final Set<Square> possibleSquares = MoveCalculator.getPossibleSquares(source);
68 if (AppUtils.isNotEmpty(possibleSquares)) {
69 for (Square possibleSquare : possibleSquares) {
70 possibleSquare.setBackground(Color.YELLOW);
71 }
72 }
73 } else if (moveState == MoveState.COMPLETE) {
74 //Move is complete
75 getBoard().getScoreSquare().setText(String.valueOf(board.getGame().gameValue()));
76 getBoard().getGame().changeTurn();
77
78 getBoard().paint();
79 if (getBoard().getGame().getTurn() == Color.WHITE) {
80 BestMoveCalcultor.play(getBoard(), Color.WHITE, BestMoveCalcultor.MAX_DEPTH);
81 getBoard().paint();
82 }
83
84 }
85
86
87 }
88
89 private Board getBoard() {
90 if (board == null) {
91 board = BoardUtils.getInitialBoard();
92 }
93 return board;
94 }
95}
96