📄 chessengineimpl.java
字号:
/* 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.game.*;import de.java_chess.javaChess.ply.*;import de.java_chess.javaChess.renderer2d.EnginePanel;import java.awt.event.*;import javax.swing.*;/** * 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. */ boolean _white; /** * The generator for the plies. */ PlyGenerator _plyGenerator; /** * A hashtable for computed plies. */ PlyHashtable _hashtable; /** * The currently used search depth. */ int _searchDepth; /** * A counter for the analyzed boards. */ long _analyzedBoards; /** * A thread to search for the best move. */ Thread _searchThread; /** * Flag to stop the search. */ boolean _stopSearch; /** * The best computed ply so far. */ AnalyzedPly _bestPly = null; /** * The menu items for the various search times. */ private JMenuItem [] _searchTimeMenuItem; /** * 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 [] _hashtableSize = { 5000, 10000, 20000, 50000, 100000 }; /** * The menu items for the various search times. */ private EnginePanel enginePanel = null; // Constructors /** * Create a new engine instance with a given board. * * @param board The new board. * @param white Flag, to indicate if the engine operates on the white pieces. */ public ChessEngineImpl( Game game, Board board, boolean white) { setGame( game); setBoard( board); setWhite( white); _hashtable = new PlyHashtableImpl( 10000); _plyGenerator = new PlyGenerator( getGame(), _hashtable); _analyzer = new BitBoardAnalyzerImpl( _plyGenerator); _plyGenerator.setAnalyzer( _analyzer); } // Methods /** * 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 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; } /** * Start a new thread to search for a ply. */ public void start() { if( _searchThread == null) { _stopSearch = false; _searchThread = new Thread( this); _searchThread.start(); } } /** * Compute the best ply for the current position. * * @return The best known ply for the current position. */ public Ply computeBestPly() { _bestPly = null; // Remove ply from last computation. long startTime = System.currentTimeMillis(); start(); try { Thread.sleep( getMaximumSearchTime()); _stopSearch = true; _searchThread.join(); // Wait for the search thread to end the search at this search depth. _searchThread = null; // Remove the thread, so it can be recreated for the next move. } catch( InterruptedException ignored) {} long usedTime = System.currentTimeMillis() - startTime; if( _bestPly != null) { String sOut1 = "Best ply: " + _bestPly.getPly().toString() + " with score " + _bestPly.getScore() + " and search depth " + _searchDepth; String sOut2 = "Analyzed boards: " + _analyzedBoards + " in " + usedTime + " ms"; if ( this.enginePanel != null ) { this.enginePanel.modifyText( sOut1 ); this.enginePanel.modifyText( sOut2 ); } return _bestPly.getPly(); } return null; } /** * The main method of the search thread. */ public void run() { _analyzedBoards = 0; _searchDepth = 0; // The following search is rather inefficent at the moment, since we should try to get a principal variant // from a search, so we can presort the plies for the next search. // Thread currentThread = Thread.currentThread(); String sOutput; do { _searchDepth++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -