# Chess Engine vs. Engine vs. Human GITHUB
# Chess Engine vs. Engine vs. Human GITHUB
In this program, we use the Python chess library to allow a human player to play
against two different chess engines, or to have the engines play against each
other. The game uses a textual representation of the chess board, which is printed
to the console after each move.
## Features
- **Multiple Chess Engines**: The program supports multiple chess engines. In this
version, it uses the LC0, Stockfish, and Komodo engines, but more can be added.
- **Engine vs. Engine Games**: The program can have two chess engines play a game
against each other. The engines' moves are displayed in Standard Algebraic Notation
(SAN).
- **Human vs. Engine Games**: A human player can play a game against one of the
chess engines. The player enters their moves in SAN format. The program checks the
validity of the moves.
- **Choice of Color**: The human player can choose to play as white or black.
- **Display of the Chess Board**: The program prints a textual representation of
the chess board after each move. The board includes rank and file labels.
- **Display of Game Results**: After a game ends, the program prints the result
(who won or if it was a draw) and the Portable Game Notation (PGN) of the game.
## How to Use
1. **Playing Engine vs. Engine**: To have two engines play a game against each
other, run the program with the names of the engines as command-line arguments. For
example, to have LC0 play against Stockfish, use the command: `python EnginesEvE.py
LC0 Stockfish`.
2. **Playing as a Human vs. Engine**: To play a game against one of the engines,
run the program with "Human" as one of the command-line arguments. For example, to
play against LC0, use the command: `python EnginesEvE.py Human LC0`. When you start
the game, the program will ask you to choose your color.
## How We Built It
We used the Python chess library, which provides the classes and functions needed
to represent chess games and boards, to make and validate moves, and to interact
with UCI chess engines. We used Python's built-in argparse module to handle
command-line arguments.
We created a function, `play_game`, that plays a game between two players (which
can be either engines or a human). If it's an engine's turn, the function asks the
engine for a move and applies it to the board. If it's the human's turn, the
function asks the human player for a move, checks its validity, and applies it to
the board if it's valid.
The `print_board` function prints a textual representation of the chess board, with
ranks and files labeled. The `play_chosen_engines` function plays a game between
two specified players, prints the PGN of the game, and announces the winner.
## Code
import chess
import chess.engine
import chess.pgn
import io
import argparse
def print_board(board):
print(' +-----------------+')
for i in range(8):
print(' ' + str(8-i) + ' | ' + ' '.join(board.unicode().split('\n')[i]) + '
|')
print(' +-----------------+')
print(' a b c d e f g h ')
if result == '1-0':
print(f"{engine1_name} wins")
elif result == '0-1':
print(f"{engine2_name} wins")
else:
print("Draw")
def main():
parser = argparse.ArgumentParser(description='Play a game between two chess
bots.')
parser.add_argument('engine1', help='The name of the first engine')
parser.add_argument('engine2', help='The name of the second engine')
args = parser.parse_args()
engines = {
'LC0': chess.engine.SimpleEngine.popen_uci(r"C:\\Users\\xdevi\\Downloads\\
lc0-v0.30.0-windows-cpu-openblas\\lc0.exe"),
'Stockfish': chess.engine.SimpleEngine.popen_uci(r"C:\\Users\\xdevi\\
Downloads\\stockfish\\stockfish-windows-x86-64-avx2.exe"),
'Komodo': chess.engine.SimpleEngine.popen_uci(r"C:\Users\xdevi\Downloads\
komodo-14\Windows\komodo-14.1-64bit.exe")
}
engines['LC0'].configure({'WeightsFile': r'C:\\Users\\xdevi\\Downloads\\lc0-
v0.30.0-windows-cpu-openblas\\weights_run1_814631.lc0'})
engine1_is_white = True
if "Human" in [engine1_name, engine2_name]:
color = input("Choose your color (white/black): ")
if color.lower() == 'black':
engine1_is_white = False if engine1_name == "Human" else True
if __name__ == "__main__":
main()