Hi,
I have written the code of my first chess engine after getting some help with ChatGPT.
It’s a simple brute force engine and the code is about 2 sides of A4.
However, when I try to compile in Clang, I get error after error. I think it’s not the code, which has been checked, but my use of Clang.
Can someone recommend a simple way forward? I’m open to any suggestions
Thanks
Help compiling code
Moderators: hgm, Rebel, chrisw
-
- Posts: 2872
- Joined: Wed Mar 10, 2010 10:18 pm
- Location: Hamburg, Germany
- Full name: Srdja Matovic
Re: Help compiling code
If it is not your code then check for library functions and the according header includes, like
If you use non standard libraries you have to specify in compiler, e.g. add "-lm" for libmath.
Take a look into my simple Makefile, maybe you find there your missing part:
https://gitlab.com/smatovic/ZetaDva/-/b ... type=heads
--
Srdja
Code: Select all
#include <stdio.h> /* for print and scan */
#include <stdlib.h> /* for malloc free */
#include <string.h> /* for string compare */
Take a look into my simple Makefile, maybe you find there your missing part:
https://gitlab.com/smatovic/ZetaDva/-/b ... type=heads
--
Srdja
-
- Posts: 460
- Joined: Mon Jun 07, 2010 3:13 am
- Location: Holland, MI
- Full name: Martin W
Re: Help compiling code
How did you check the code? What are the errors?Werewolf wrote: ↑Sat Sep 14, 2024 2:13 pm Hi,
I have written the code of my first chess engine after getting some help with ChatGPT.
It’s a simple brute force engine and the code is about 2 sides of A4.
However, when I try to compile in Clang, I get error after error. I think it’s not the code, which has been checked, but my use of Clang.
Can someone recommend a simple way forward? I’m open to any suggestions
Thanks
-
- Posts: 1888
- Joined: Thu Sep 18, 2008 10:24 pm
Re: Help compiling code
Yes you're right to question my code. I am a total beginner and probably haven't done a very good job.gaard wrote: ↑Sun Sep 15, 2024 4:11 pmHow did you check the code? What are the errors?Werewolf wrote: ↑Sat Sep 14, 2024 2:13 pm Hi,
I have written the code of my first chess engine after getting some help with ChatGPT.
It’s a simple brute force engine and the code is about 2 sides of A4.
However, when I try to compile in Clang, I get error after error. I think it’s not the code, which has been checked, but my use of Clang.
Can someone recommend a simple way forward? I’m open to any suggestions
Thanks
If you have comments I have pasted the code below. Also now I will try other compilers as I'm getting in a twist with Clang
Sept 2024 Carl Bicknell
BRUTEFORCE 001
#include <stdio.h>
#include <stdlib.h>
#include <windows.h> // For Windows threads
#include <string.h> // Add this line for string functions like strncmp
// Basic board and piece constants
int board[8][8]; // Simple 2D board array representation
int piece_values[] = {100, 320, 330, 500, 900}; // Pawn, Knight, Bishop, Rook, Queen
// Evaluation function: simple piece count + bonuses for king safety, castling, etc.
int evaluate() {
int score = 0;
// Basic material evaluation + simple central pawn bonus and bishop pair
return score;
}
// Basic move generation: Generate legal moves (simplified for demo purposes)
void generate_moves() {
// Move generation code here
}
// Minimax Search: Fixed-depth search
int minimax(int depth, int isMaximizing) {
if (depth == 0) return evaluate();
if (isMaximizing) {
int best = -10000;
generate_moves(); // Replace with proper move generation
best = minimax(depth - 1, 0);
return best;
} else {
int best = 10000;
generate_moves();
best = minimax(depth - 1, 1);
return best;
}
}
// Thread function for search
DWORD WINAPI threaded_search(LPVOID arg) {
int depth = *(int*)arg;
int score = minimax(depth, 1); // Run minimax search
return 0;
}
// UCI Protocol Handler
void uci_loop() {
char input[2000];
while (1) {
fgets(input, 2000, stdin);
if (strncmp(input, "uci", 3) == 0) {
printf("id name BruteChess\n");
printf("id author YourName\n");
printf("uciok\n");
} else if (strncmp(input, "isready", 7) == 0) {
printf("readyok\n");
} else if (strncmp(input, "position", 8) == 0) {
// Handle position setup
} else if (strncmp(input, "go", 2) == 0) {
// Start search with threads
HANDLE threads[64]; // Array of threads
int depth = 5; // Example depth
for (int i = 0; i < 64; i++) {
threads = CreateThread(NULL, 0, threaded_search, &depth, 0, NULL);
}
// Wait for threads to finish
WaitForMultipleObjects(64, threads, TRUE, INFINITE);
printf("bestmove e2e4\n"); // Dummy move, replace with actual best move
} else if (strncmp(input, "quit", 4) == 0) {
break;
}
}
}
int main() {
printf("Chess Engine Ready\n");
uci_loop();
return 0;
}
-
- Posts: 882
- Joined: Fri Aug 21, 2020 1:25 am
- Location: Planet Earth, Sol system
- Full name: Michael J Sherwin
Re: Help compiling code
Werewolf wrote: ↑Sun Sep 15, 2024 8:31 pmYes you're right to question my code. I am a total beginner and probably haven't done a very good job.gaard wrote: ↑Sun Sep 15, 2024 4:11 pmHow did you check the code? What are the errors?Werewolf wrote: ↑Sat Sep 14, 2024 2:13 pm Hi,
I have written the code of my first chess engine after getting some help with ChatGPT.
It’s a simple brute force engine and the code is about 2 sides of A4.
However, when I try to compile in Clang, I get error after error. I think it’s not the code, which has been checked, but my use of Clang.
Can someone recommend a simple way forward? I’m open to any suggestions
Thanks
If you have comments I have pasted the code below. Also now I will try other compilers as I'm getting in a twist with Clang
Sept 2024 Carl Bicknell
BRUTEFORCE 001
#include <stdio.h>
#include <stdlib.h>
#include <windows.h> // For Windows threads
#include <string.h> // Add this line for string functions like strncmp
// Basic board and piece constants
int board[8][8]; // Simple 2D board array representation
int piece_values[] = {100, 320, 330, 500, 900}; // Pawn, Knight, Bishop, Rook, Queen
// Evaluation function: simple piece count + bonuses for king safety, castling, etc.
int evaluate() {
int score = 0;
// Basic material evaluation + simple central pawn bonus and bishop pair
return score;
}
// Basic move generation: Generate legal moves (simplified for demo purposes)
void generate_moves() {
// Move generation code here
}
// Minimax Search: Fixed-depth search
int minimax(int depth, int isMaximizing) {
if (depth == 0) return evaluate();
if (isMaximizing) {
int best = -10000;
generate_moves(); // Replace with proper move generation
best = minimax(depth - 1, 0);
return best;
} else {
int best = 10000;
generate_moves();
best = minimax(depth - 1, 1);
return best;
}
}
// Thread function for search
DWORD WINAPI threaded_search(LPVOID arg) {
int depth = *(int*)arg;
int score = minimax(depth, 1); // Run minimax search
return 0;
}
// UCI Protocol Handler
void uci_loop() {
char input[2000];
while (1) {
fgets(input, 2000, stdin);
if (strncmp(input, "uci", 3) == 0) {
printf("id name BruteChess\n");
printf("id author YourName\n");
printf("uciok\n");
} else if (strncmp(input, "isready", 7) == 0) {
printf("readyok\n");
} else if (strncmp(input, "position", 8) == 0) {
// Handle position setup
} else if (strncmp(input, "go", 2) == 0) {
// Start search with threads
HANDLE threads[64]; // Array of threads
int depth = 5; // Example depth
for (int i = 0; i < 64; i++) {
threads = CreateThread(NULL, 0, threaded_search, &depth, 0, NULL);
}
// Wait for threads to finish
WaitForMultipleObjects(64, threads, TRUE, INFINITE);
printf("bestmove e2e4\n"); // Dummy move, replace with actual best move
} else if (strncmp(input, "quit", 4) == 0) {
break;
}
}
}
int main() {
printf("Chess Engine Ready\n");
uci_loop();
return 0;
}
Best to scrap that and start super simple with something that you totally understand.
Here's an example to start with. Don't worry about communicating with a protocol at first. Just get a console version working.
Code: Select all
enum { EXIT, GETCMD, SEARCH, MOVE };
struct Move
{
char fromSquare;
char toSquare;
char fromType;
char toType;
};
void StartSearch(Move* m)
{
mode = MOVE;
}
void GameMove(Move* m)
{
mode = GETCMD;
}
void Initialize()
{
mode = GETCMD;
}
void GetCommand()
{
}
int main()
{
Move move;
Initialize();
while (mode != EXIT)
{
if (mode == GETCMD) GetCommand();
if (mode == SEARCH) StartSearch(&move);
if (mode == MOVE) GameMove(move);
}
return 0;
}
-
- Posts: 251
- Joined: Sat Mar 11, 2006 8:31 am
- Location: Malmö, Sweden
- Full name: Bo Persson
Re: Help compiling code
The main problem is here, when the loop tries to assign to the whole array at once. Use
Code: Select all
threads[i] = ...
The only other warning I see is in
Code: Select all
int score = minimax(depth, 1);
-
- Posts: 1888
- Joined: Thu Sep 18, 2008 10:24 pm
Re: Help compiling code
Thanks everyone.
I decided to start small and go from there. In the spirit of Brute Force, I got a fully working noughts and crosses program working this morning that calculates all possible moves before playing (perfectly). It was multithreaded at 16 threads.
Eventually I'll work towards chess.
I decided to start small and go from there. In the spirit of Brute Force, I got a fully working noughts and crosses program working this morning that calculates all possible moves before playing (perfectly). It was multithreaded at 16 threads.
Eventually I'll work towards chess.