Skip to content

BlueBetaFish/chess-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bluebetafish - UCI Chess engine in C++

Overview:

Bluebetafish is a chess engine with an approximate rating of 1900. It evaluates chess positions and determines the best moves using minimax search algorithm with alpha-beta pruning. It supports integration with UCI (Universal Chess Interface Protocol) compatible GUIs.

Try it:

Getting Started

Download and run the executable from the bin directory, or compile the source code using the make command. Or download any UCI protocol supported GUI and load the engine.

1. Use the engine via command line using UCI commands: [Each command is explained later]

  • position fen r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -: will initialize the board with given position (input FEN string).

    $ ./bluebetafish_64bit_linux
    
    id name BlueBetaFish
    uciok
    position fen r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -
    Piece Board: 
    +---+---+---+---+---+---+---+---+
    | r |   |   |   | k |   |   | r |   8
    +---+---+---+---+---+---+---+---+
    | p |   | p | p | q | p | b |   |   7
    +---+---+---+---+---+---+---+---+
    | b | n |   |   | p | n | p |   |   6
    +---+---+---+---+---+---+---+---+
    |   |   |   | P | N |   |   |   |   5
    +---+---+---+---+---+---+---+---+
    |   | p |   |   | P |   |   |   |   4
    +---+---+---+---+---+---+---+---+
    |   |   | N |   |   | Q |   | p |   3
    +---+---+---+---+---+---+---+---+
    | P | P | P | B | B | P | P | P |   2
    +---+---+---+---+---+---+---+---+
    | R |   |   |   | K |   |   | R |   1
    +---+---+---+---+---+---+---+---+---+---+
    a   b   c   d   e   f   g   h   --------> File
    
    CurrentPlayer : WHITE ,  EnPassant square : -1 ,  Castling Rights = KQkq
    
  • go movetime 5000: engine finds the best move for 5000 milliseconds.

    go movetime 5000
    
    info score cp 30 depth 1 nodes 1073 time 2 pv e2a6
    info score cp 30 depth 2 nodes 1528 time 6 pv e2a6 b4c3
    info score cp 15 depth 3 nodes 5730 time 9 pv e2a6 b4c3 b2c3
    info score cp 15 depth 4 nodes 23335 time 27 pv e2a6 b4c3 b2c3 e6d5
    info score cp 5 depth 5 nodes 85732 time 88 pv e2a6 b4c3 d2c3 e6d5 e4d5
    info score cp 0 depth 6 nodes 209717 time 254 pv e2a6 e6d5 c3d5 b6d5 e4d5 e7e5
    info score cp -20 depth 7 nodes 505761 time 568 pv e2a6 e6d5 c3d5 b6d5 a6b7 a8b8 e4d5
    info score cp -20 depth 8 nodes 1203547 time 1286 pv e2a6 e6d5 c3d5 b6d5 a6b7 a8b8 e4d5 b8b7
    info score cp -60 depth 9 nodes 4158122 time 4238 pv e2a6 e6d5 e5g4 f6g4 c3d5 b6d5 f3g4 h3g2 g4g2
    bestmove e2a6
    
    Total Nodes of iterative deepening : 6262784
    
  • Output (bestmove e2a6): within 5 seconds, the engine could search upto depth 9 and best move is e2a6.

2. Play against the engine using GUI apps:

  • UCI-Compatible GUIs: Use any GUI that supports the UCI protocol, such as Arena (for Windows), SCID (for both Windows and Linux), etc.

    SCID_GUI_play_against_engine.mp4



Supported UCI commands

1. position [fen | startpos] moves ....: Sets up the board in memory with given position

  • Sets up the position described in FEN string on the internal board and plays the moves on the internal chess board.
  • Pass startpos as argument if you want to initialize the board to starting position.
  • moves is an optional argument. If it is not passed, no move is played after the given fen/startpos.
  • Examples:
    • position startpos: initializes the board with starting position.

      Output: current position in memory is the starting position.

      Piece Board: 
      +---+---+---+---+---+---+---+---+
      | r | n | b | q | k | b | n | r |   8
      +---+---+---+---+---+---+---+---+
      | p | p | p | p | p | p | p | p |   7
      +---+---+---+---+---+---+---+---+
      |   |   |   |   |   |   |   |   |   6
      +---+---+---+---+---+---+---+---+
      |   |   |   |   |   |   |   |   |   5
      +---+---+---+---+---+---+---+---+
      |   |   |   |   |   |   |   |   |   4
      +---+---+---+---+---+---+---+---+
      |   |   |   |   |   |   |   |   |   3
      +---+---+---+---+---+---+---+---+
      | P | P | P | P | P | P | P | P |   2
      +---+---+---+---+---+---+---+---+
      | R | N | B | Q | K | B | N | R |   1
      +---+---+---+---+---+---+---+---+---+---+
      a   b   c   d   e   f   g   h   --------> File
      
      CurrentPlayer : WHITE ,  EnPassant square : -1 ,  Castling Rights = KQkq
      
    • position fen {FEN_STRING}: initializes the board with given position using FEN notation.

      The corresponding FEN notation of the given position is: r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -
      Command: position fen r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -

    • position startpos moves e2e4 e7e5 g2g3: initializes the board with starting position and plays the given moves.

      position startpos moves e2e4 e7e5 g2g3:
      Piece Board: 
      +---+---+---+---+---+---+---+---+
      | r | n | b | q | k | b | n | r |   8
      +---+---+---+---+---+---+---+---+
      | p | p | p | p |   | p | p | p |   7
      +---+---+---+---+---+---+---+---+
      |   |   |   |   |   |   |   |   |   6
      +---+---+---+---+---+---+---+---+
      |   |   |   |   | p |   |   |   |   5
      +---+---+---+---+---+---+---+---+
      |   |   |   |   | P |   |   |   |   4
      +---+---+---+---+---+---+---+---+
      |   |   |   |   |   |   | P |   |   3
      +---+---+---+---+---+---+---+---+
      | P | P | P | P |   | P |   | P |   2
      +---+---+---+---+---+---+---+---+
      | R | N | B | Q | K | B | N | R |   1
      +---+---+---+---+---+---+---+---+---+---+
      a   b   c   d   e   f   g   h   --------> File
      
      CurrentPlayer : BLACK ,  EnPassant square : -1 ,  Castling Rights = KQkq
      

2. go: For calculating best moves

  • Starts calculating best move for the current position set up with the position command.

  • There are different arguments for this command.

  • depth {DEPTH_VALUE}

    • Searches the position upto depth DEPTH_VALUE

    • Example: go depth 6

      Set up position:

      position fen r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -
      Piece Board: 
      +---+---+---+---+---+---+---+---+
      | r |   |   |   | k |   |   | r |   8
      +---+---+---+---+---+---+---+---+
      | p |   | p | p | q | p | b |   |   7
      +---+---+---+---+---+---+---+---+
      | b | n |   |   | p | n | p |   |   6
      +---+---+---+---+---+---+---+---+
      |   |   |   | P | N |   |   |   |   5
      +---+---+---+---+---+---+---+---+
      |   | p |   |   | P |   |   |   |   4
      +---+---+---+---+---+---+---+---+
      |   |   | N |   |   | Q |   | p |   3
      +---+---+---+---+---+---+---+---+
      | P | P | P | B | B | P | P | P |   2
      +---+---+---+---+---+---+---+---+
      | R |   |   |   | K |   |   | R |   1
      +---+---+---+---+---+---+---+---+---+---+
      a   b   c   d   e   f   g   h   --------> File
      
      CurrentPlayer : WHITE ,  EnPassant square : -1 ,  Castling Rights = KQkq
      

      Find best move: -
      Input: go depth 6
      Output:

      info score cp 30 depth 1 nodes 1073 time 1 pv e2a6
      info score cp 30 depth 2 nodes 1528 time 3 pv e2a6 b4c3
      info score cp 15 depth 3 nodes 5730 time 6 pv e2a6 b4c3 b2c3
      info score cp 15 depth 4 nodes 23335 time 23 pv e2a6 b4c3 b2c3 e6d5
      info score cp 5 depth 5 nodes 85732 time 90 pv e2a6 b4c3 d2c3 e6d5 e4d5
      info score cp 0 depth 6 nodes 209717 time 252 pv e2a6 e6d5 c3d5 b6d5 e4d5 e7e5
      bestmove e2a6
      
      Total Nodes of iterative deepening : 295449
      
    • Output interpretation:

      • bestmove {MOVE}
        • The engine has stopped searching and found the best move for the current position.
        • Example: bestmove e2a6 [Here best move is e2a6]
      • info: Engine output with more informations.
        • depth : current depth upto which search has been done.
        • time : time (in millisecond) taken to complete the current DFS.
        • nodes : number of nodes searched
        • pv {MOVE1} {MOVE2} {MOVE3} ..... : the principal variation line (best move sequence for both players) found.
        • score cp {SCORE} : the score (evaluation) in centi-pawn unit from engine's POV.
  • movetime {MOVETIME_VALUE}

    • Searches the position for MOVE_TIME milliseconds
    • Example: go movetime 5000 (Searches for 5 seconds)
      Output:
      go movetime 5000
      info score cp 30 depth 1 nodes 1073 time 1 pv e2a6
      info score cp 30 depth 2 nodes 1528 time 3 pv e2a6 b4c3
      info score cp 15 depth 3 nodes 5730 time 6 pv e2a6 b4c3 b2c3
      info score cp 15 depth 4 nodes 23335 time 26 pv e2a6 b4c3 b2c3 e6d5
      info score cp 5 depth 5 nodes 85732 time 71 pv e2a6 b4c3 d2c3 e6d5 e4d5
      info score cp 0 depth 6 nodes 209717 time 215 pv e2a6 e6d5 c3d5 b6d5 e4d5 e7e5
      info score cp -20 depth 7 nodes 505761 time 503 pv e2a6 e6d5 c3d5 b6d5 a6b7 a8b8 e4d5
      info score cp -20 depth 8 nodes 1203547 time 1153 pv e2a6 e6d5 c3d5 b6d5 a6b7 a8b8 e4d5 b8b7
      info score cp -60 depth 9 nodes 4158122 time 3873 pv e2a6 e6d5 e5g4 f6g4 c3d5 b6d5 f3g4 h3g2 g4g2
      bestmove e2a6
      
      Total Nodes of iterative deepening : 6670336
      
  • wtime {WTIME_VALUE} : Searches the position assuming white has WTIME_VALUE milliseconds remaining

  • btime {BTIME_VALUE} : Searches the position assuming black has BTIME_VALUE milliseconds remaining

    • Example: go wtime 180000 (Searches the position assuming white has 180 seconds)
      info score cp 30 depth 1 nodes 1073 time 2 pv e2a6
      info score cp 30 depth 2 nodes 1528 time 3 pv e2a6 b4c3
      info score cp 15 depth 3 nodes 5730 time 6 pv e2a6 b4c3 b2c3
      info score cp 15 depth 4 nodes 23335 time 23 pv e2a6 b4c3 b2c3 e6d5
      info score cp 5 depth 5 nodes 85732 time 73 pv e2a6 b4c3 d2c3 e6d5 e4d5
      info score cp 0 depth 6 nodes 209717 time 226 pv e2a6 e6d5 c3d5 b6d5 e4d5 e7e5
      info score cp -20 depth 7 nodes 505761 time 525 pv e2a6 e6d5 c3d5 b6d5 a6b7 a8b8 e4d5
      info score cp -20 depth 8 nodes 1203547 time 1255 pv e2a6 e6d5 c3d5 b6d5 a6b7 a8b8 e4d5 b8b7
      info score cp -60 depth 9 nodes 4158122 time 4100 pv e2a6 e6d5 e5g4 f6g4 c3d5 b6d5 f3g4 h3g2 g4g2
      bestmove e2a6
      
      Total Nodes of iterative deepening : 7729152
      
  • winc : white increment per move in milliseconds

  • binc : black increment per move in milliseconds

  • movestogo {MOVESTOGO} : time increment begins after MOVESTOGO moves


3. quit: The process quits.