0% found this document useful (0 votes)
97 views3 pages

# Chess Engine vs. Engine vs. Human GITHUB

This Python program allows chess engines and humans to play games against each other using a textual representation of the chess board. It supports multiple chess engines, engine vs engine games, and human vs engine games where the human enters moves in standard notation. After each move, it prints the board state and at the end announces the winner or draw and shows the full game notation. The program uses the Python chess library and argparse to handle the engine and game logic.

Uploaded by

XDEVILOP GAMING
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views3 pages

# Chess Engine vs. Engine vs. Human GITHUB

This Python program allows chess engines and humans to play games against each other using a textual representation of the chess board. It supports multiple chess engines, engine vs engine games, and human vs engine games where the human enters moves in standard notation. After each move, it prints the board state and at the end announces the winner or draw and shows the full game notation. The program uses the Python chess library and argparse to handle the engine and game logic.

Uploaded by

XDEVILOP GAMING
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

# Chess Engine Vs. Engine Vs.

Human in Python with Textual Representation

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

The complete Python script is in the assets labelled EnginesEvE.py or

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 ')

def play_game(engine1, engine2, engine1_name, engine2_name, engine1_is_white=True):


board = chess.Board()
while not board.is_game_over():
print_board(board) # print the current state of the board
if board.turn == engine1_is_white: # Check if it's engine1's turn
if engine1_name == "Human":
move = input("Enter your move in SAN format: ")
try:
board.push_san(move)
print(f"{engine1_name} plays {move}")
except:
print("Invalid move. Please enter a valid move in SAN format.")
else:
result = engine1.play(board, chess.engine.Limit(time=2.0))
print(f"{engine1_name} plays {board.san(result.move)}")
board.push(result.move)
else:
if engine2_name == "Human":
move = input("Enter your move in SAN format: ")
try:
board.push_san(move)
print(f"{engine2_name} plays {move}")
except:
print("Invalid move. Please enter a valid move in SAN format.")
else:
result = engine2.play(board, chess.engine.Limit(time=2.0))
print(f"{engine2_name} plays {board.san(result.move)}")
board.push(result.move)
print_board(board) # print the final state of the board
return board, board.result()

def play_chosen_engines(engine_dict, engine1_name, engine2_name,


engine1_is_white=True):
print(f"{engine1_name} is playing as {'White' if engine1_is_white else
'Black'}.")
print(f"{engine2_name} is playing as {'Black' if engine1_is_white else
'White'}.")
board, result = play_game(engine_dict[engine1_name], engine_dict[engine2_name],
engine1_name, engine2_name, engine1_is_white)
game = chess.pgn.Game.from_board(board)
print(str(game))

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_name = args.engine1 + '1' if args.engine1 == args.engine2 else


args.engine1
engine2_name = args.engine2 + '2' if args.engine1 == args.engine2 else
args.engine2

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

play_chosen_engines(engines, engine1_name, engine2_name, engine1_is_white)

for engine in engines.values():


engine.quit()

if __name__ == "__main__":
main()

You might also like