Member-only story

Code GPT4 vs Chess Engine

FS Ndzomga
2 min readJul 25, 2023
import openai
import chess
import chess.engine
from mytoken import apikey
import json

# Initialize the OpenAI API for GPT-4
openai.api_key = apikey

# Initialize a chess board and the chess engine
engine = chess.engine.SimpleEngine.popen_uci("/opt/homebrew/Cellar/stockfish/16/bin/stockfish")


def play_game():
white_moves = [] # Variable to store the list of White's moves
black_moves = [] # Variable to store the list of Black's moves
board = chess.Board()

def get_gpt4_move(board):
feedback = ""
while True:
prompt = f"Current board: {board} White's move history: {white_moves}\nBlack's move history: {black_moves}\n\nChoose the next move for black in UCI format. The available legal moves are {list(board.legal_moves)}. {feedback}"
response = openai.ChatCompletion.create(
model="gpt-4-0613",
messages=[
{
"role": "system",
"content": "Imagine you are a renowned chess master with decades of experience. A critical moment arises in a high-stakes tournament game. It's your turn to move, and you have multiple promising options on the board. How do you approach this situation? What factors do you consider when deciding which move to play? Share your expert analysis, evaluating the strengths and weaknesses of each candidate move and explaining the reasoning behind your final choice. Your insights and strategic guidance will be highly valued by aspiring chess players worldwide. Show us the brilliance of a true chess master! Make sure to anticipate how…

--

--

FS Ndzomga
FS Ndzomga

Written by FS Ndzomga

Engineer passionate about data science, startups, philosophy and French literature. Built lycee.ai, discute.co and rimbaud.ai . Open for consulting gigs

No responses yet