Package chesstrainer.ui

Source Code of chesstrainer.ui.CAnalysisBoard

/*
* Copyright (C) 2014 Tim Declercq <caveman1917@hotmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package chesstrainer.ui;

import chesstrainer.CMove;
import chesstrainer.CPosition;
import chesstrainer.SMoveGenerator;
import chesstrainer.util.ESquare;
import java.awt.BorderLayout;
import java.util.Set;
import javax.swing.JPanel;

/**
*
* @author Tim Declercq <caveman1917@hotmail.com>
*/
public class CAnalysisBoard extends JPanel implements IBoardListener {
   
    private CBoard board;
    private CPosition position;
    private ESquare selectedSquare = null;
   
    public CAnalysisBoard(CPosition position) {
        this.position = position;
        board = new CBoard(position);
        board.addBoardListener(this);
        this.setLayout(new BorderLayout());
        this.add(board, BorderLayout.CENTER);
    }
   
    public CAnalysisBoard() {
        this(new CPosition());
    }
   
    private boolean tryMove(ESquare from, ESquare to) {
        CMove move = new CMove(from, to);
        if (!SMoveGenerator.isLegalMove(position, move)) {
            return false;
        }
        position.makeMove(move);
        board.repaint();
        return true;
    }

    @Override
    public void boardClicked(ESquare square, int button) {
        board.getSelectedSquares().clear();
       
        if (button == IBoardListener.BUTTON_RIGHT) {
            Set<CMove> moves = SMoveGenerator.getLegalMoves(position, square);
            for (CMove move : moves) {
                board.getSelectedSquares().add(move.getTo());
            }
        }
       
        if (button == IBoardListener.BUTTON_LEFT) {
            if (selectedSquare == null && position.getPosition().containsKey(square)) {
                selectedSquare = square;
                board.getSelectedSquares().add(square);
            } else {
                boolean m = tryMove(selectedSquare, square);
                if (square == selectedSquare) {
                    selectedSquare = null;
                } else {
                    if (!m && position.getPosition().containsKey(square)) {
                        selectedSquare = square;
                        board.getSelectedSquares().add(square);
                   
                }
            }
        }
       
        board.repaint();
    }

    @Override
    public void boardDragged(ESquare from, ESquare to, int button) {
        board.getSelectedSquares().clear();
        tryMove(from, to);
        board.repaint();
    }
   
}
TOP

Related Classes of chesstrainer.ui.CAnalysisBoard

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.