chessengineimpl.java

来自「chess 一个beguanyu国际象棋的一个Java源码」· Java 代码 · 共 1,221 行 · 第 1/3 页

JAVA
1,221
字号
/*	ChessEngineImpl - A class to implement a engine to play chess.	Copyright (C) 2002,2003 Andreas Rueckert <mail@andreas-rueckert.de>	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 2	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, write to the Free Software	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.*/package de.java_chess.javaChess.engine;import de.java_chess.javaChess.*;import de.java_chess.javaChess.bitboard.*;import de.java_chess.javaChess.board.*;import de.java_chess.javaChess.engine.hashtable.*;import de.java_chess.javaChess.engine.opening_book.*;import de.java_chess.javaChess.engine.opening_book.action.*;import de.java_chess.javaChess.engine.permanent_brain.*;import de.java_chess.javaChess.game.*;import de.java_chess.javaChess.notation.*;import de.java_chess.javaChess.ply.*;import de.java_chess.javaChess.renderer2d.EnginePanel;import de.java_chess.javaChess.renderer2d.StatusPanel;import de.java_chess.javaChess.listener.EngineStatusListener;import java.awt.event.*;import javax.swing.*;import java.util.Vector;import java.util.List;import java.util.ArrayList;import java.util.Iterator;import java.io.File;import java.io.PrintWriter;import java.io.FileWriter;import java.io.IOException;import java.io.FileNotFoundException;import java.io.BufferedOutputStream;import java.io.FileOutputStream;import java.io.BufferedReader;import java.io.FileReader;/** * This class implements the functionality to play the * actual game of chess */public class ChessEngineImpl implements ChessEngine, Runnable, ActionListener {    // Instance variables    /**     * The current game.     */    private Game _game;    /**     * The board to operate on.     */    private Board _board;    /**     * A analyzer for the boards.     */    private BitBoardAnalyzer _analyzer;    /**     * The maximum search depth.     */    private int _maxSearchTime = 5000;    /**     * Flag to indicate if the engine operates on the white pieces.     */    private boolean _white;    /**     * The generator for the plies.     */    private PlyGenerator _plyGenerator;    /**     * Flag to indicate, if the permanent brain function should be used.     */    private boolean _usePermanentBrain = false;    /**     * The permanent brain.     */    private PermanentBrain _permanentBrain;    /**     * The menu item to toggle the permanent brain.     */    private JCheckBoxMenuItem _permanentBrainMenuItem;    /**     * A hashtable for computed plies.     */    private PlyHashtable _hashtable;    /**     * The opening book.     */    private OpeningBook _openingBook;    /**     * Flag to indicate, if we are still in the opening book lines.     */    private boolean _inOpeningBook;    /**     * The currently used search depth.     */    private int _searchDepth;    /**     * A counter for the analyzed boards.     */    private long _analyzedBoards;    /**     * A thread to search for the best move.     */    private volatile Thread _searchThread;    /**     * Flag to stop the search.     */    private boolean _stopSearch;    /**     * The best computed ply so far.     */    private AnalyzedPly _bestPly = null;    /**     * The menu items for the various fix search times.     */    private JMenuItem [] _fixSearchTimeMenuItem;    /**     * The menu items for the various average search times.     */    private JMenuItem [] _avSearchTimeMenuItem;    /**     * The predefined search times (in seconds).     */    private int [] _searchTime = { 3, 5, 10, 15, 30, 45, 60};    /**     * The menu items for the various hashtable sizes.     */    private JMenuItem [] _hashtableSizeMenuItem;    /**     * The predefined hashtable sizes.     */    private int [] _hashtableSizes = { 5000, 10000, 20000, 50000, 100000 };    /**     * The menu items for the various search times.     */    private EnginePanel _enginePanel = null;    /**     * The menu items for the various search times.     */    private StatusPanel _statusPanel = null;    /**     * The last ply from the user.     */    Ply _lastUserPly = null;    /**     * The Logfile     */    File f;    /**     * The filename for the Logfile     */    private static final String LOG_FILENAME = "JCEngine.log";    /**     * The vector for buffering the existing lines from the Logfile     */    Vector vBuffer;    /**     * Flag that indicates if the chosen time control is fixed or average time     */    private boolean bFixedTime = true;    /**     * The ButtonGroup for the time controls     */    ButtonGroup buttonGroupSearchTime;    /**     * The ButtonGroup for the hash sizes     */    ButtonGroup buttonGroupHashSize;    /**     * The list of listeners     */    private List listeners;    // Constructors    /**     * Create a new engine instance with a given board.     *     * @param game The current game.     * @param notation The current notation.     * @param board The new board.     * @param white Flag, to indicate if the engine operates on the white pieces.     */    public ChessEngineImpl( Game game, GameNotation notation, Board board, boolean white)    {	listeners = new ArrayList();	this.createLogFile();	setGame( game);	setBoard( board);	setWhite( white);	_hashtable = new PlyHashtableImpl( 10000);	_plyGenerator = new PlyGenerator( getGame(), _hashtable);	_analyzer = new BitBoardAnalyzerImpl( getGame(), _plyGenerator);	_plyGenerator.setAnalyzer( _analyzer);	setPermanentBrain( new PermanentBrain( this));	startPermanentBrain();	setOpeningBook( new OpeningBookImpl( notation));	setInOpeningBook( true);    }    // Methods    /**     * Reset the engine for a new game.     */    public void reset() {	setInOpeningBook( true);  // Default is, that we are in the opening book.	getHashtable().reset();   // Reset the hash tables.	// Remove the engine status messages.	if( _enginePanel != null) {	    _enginePanel.setText("");	}	// After a reset, the user is about to move.	if ( this._statusPanel != null ) {	    this._statusPanel.setStatusText( "Your turn..." );	}	// Start the permanent brain, if the user wants it to be used.	startPermanentBrain();	// Reset the last user ply.	_lastUserPly = null;    }    /**     * Get the current game.     *     * @return The current game.     */    public final Game getGame() {	return _game;    }    /**     * Set the current game.     *     * @param The current game.     */    public final void setGame( Game game) {	_game = game;    }    /**     * Get the current board.     *     * @return The current board.     */    public Board getBoard() {	return _board;    }    /**     * Set the board.     *     * @param board The new board.     */    public void setBoard( Board board) {	_board = board;    }    /**     * Get the current hashtable for this ply generator.     *     * @return The current hashtable for this ply generator.     */    public final PlyHashtable getHashtable() {	return _hashtable;    }    /**     * Set a new hashtable for this ply generator.     *     * @param hashtable The new hashtable for this ply generator.     */    public final void setHashtable( PlyHashtable hashtable) {	_hashtable = hashtable;    }    /**     * Get the permanent brain.     *     * @return The permanent brain.     */    public final PermanentBrain getPermanentBrain() {	return _permanentBrain;    }    /**     * Set a new permanent brain.     *     * @param permanentBrain The new permanent brain.     */    private final void setPermanentBrain( PermanentBrain permanentBrain) {	_permanentBrain = permanentBrain;    }    /**     * Check, if the permanent brain should be used.     *     * @return A flag, that indicates, if the permanent brain should be used.     */    private final boolean usePermanentBrain() {	return _usePermanentBrain;    }    /**     * (De-)activate the permanent brain function.     *     * @param active Flag to indicate, if the permanent brain function should be used.     */    private final void activatePermanentBrain( boolean active) {	_usePermanentBrain = active;	// Since the permanent brain is eventually active already, stop it now.	if( ! usePermanentBrain()) {	    stopPermanentBrain();	}	// If the permanent brain is activated, it will be use for the next(!) move.    }    /**     * Get the maximum search time.     *     * @return The maximum search time.     */    public final int getMaximumSearchTime() {	return _maxSearchTime;    }    /**     * Set the maximum search time.     *     * @param depth The new search time.     */    public final void setMaximumSearchTime( int time) {	_maxSearchTime = time;    }    /**     * Get the color of this engine.     *     * @param white true, if the engine operates with the white pieces.     */    public boolean isWhite() {	return _white;    }    /**     * Set the color of the engine.     *     * @param white flag to indicate if the engine operates on the white pieces.     */    public void setWhite( boolean white) {	_white = white;    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?