Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
drkameleon committed Nov 16, 2016
1 parent 3c2f8a3 commit cd461b6
Show file tree
Hide file tree
Showing 36 changed files with 11,187 additions and 0 deletions.
80 changes: 80 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#**********************************************************************
#
# Bogart
# Chess Engine
#
# Copyright (C) 2009-2013 Dr.Kameleon
#
#**********************************************************************
# Makefile
#**********************************************************************

#=======================================================
# Definitions
#=======================================================

APP = bogart

# Tools & commands
CLANG = clang++
CPP = g++
RM = rm
CLOC = tools/cloc-1.51.pl

# C++ stuff
SOURCES = main.cpp\
cmdline.cpp\
init.cpp\
bitboard.cpp\
timer.cpp\
log.cpp\
board.cpp\
board_helper.cpp\
board_gen.cpp\
board_eval.cpp\
board_search.cpp\
engine.cpp\
benchmark.cpp

OBJECTS = main.o\
cmdline.o\
init.o\
bitboard.o\
timer.o\
log.o\
board.o\
board_helper.o\
board_gen.o\
board_eval.o\
board_search.o\
engine.o\
benchmark.o

# Flags
CLANG_FLAGS = -I /usr/local/include/boost -lboost_program_options
CLOC_FLAGS = --exclude-lang=Perl

#=======================================================
# Functions
#=======================================================

all: ${APP}

${APP}: ${OBJECTS}
${CLANG} ${OBJECTS} -flto -o ${APP} ${CLANG_FLAGS}

${OBJECTS}: ${SOURCES}
${CLANG} -O4 -c ${SOURCES}

count:
./${CLOC} . ${CLOC_FLAGS}

clean:
${RM} *.o

cleanall:
${RM} *.o ${APP}

#=======================================================
# Finita la musica, passata la fiesta...
#=======================================================
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
![Logo](graphics/logo.png)

# bogart

A Chess Engine experiment in D
96 changes: 96 additions & 0 deletions benchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**********************************************************************
|
| Bogart
| Chess Engine
|
| Copyright (C) 2009-2013 Dr.Kameleon
|
|**********************************************************************
| benchmark.cpp
|**********************************************************************/

#include "bogart.h"

//=======================================================
// Constructors
//=======================================================

Benchmark::Benchmark(Board* b)
{
this->board = b;
/*
this->board->debug();
cout << "KEY : " << this->board->key << endl;
Move m = Move("e2e4",this->board);
MAKE(m,this->board);
HASHUNHASH(m,this->board);
this->board->debug();
cout << "KEY : " << this->board->key << endl;
UNMAKE(m,this->board);
HASHUNHASH(m,this->board);
this->board->debug();
cout << "KEY : " << this->board->key << endl;
cout << "--------------" << endl;*/

cout << BENCH_HEADER("Benchmark");
cout << BENCH_SUBHEADER("Board");
this->board->debug();
cout << BENCH_SUBHEADER("Result");
}

//=======================================================
// Functions
//=======================================================

void Benchmark::perft(UINT depth, BOOL loop)
{
int start = (loop)? 1 : depth;

for (int i=start; i<=depth; i++)
{
Timer *timer = new Timer();
UINT result = this->board->countNodes(0,i);
cout << "Perft(" << i << ") = " << left << setw(20) << result << "(" << (timer->timeElapsed()/1000) << "s)" << endl;
}
cout << endl;
}

void Benchmark::eval(UINT depth, BOOL loop)
{
this->board->kingTropism();
int start = (loop)? 1 : depth;

for (int i=start; i<=depth; i++)
{
Timer *timer = new Timer();
Move result = this->board->search(AlphaBetaPvs,i);
cout << "Eval(" << i << ") = " << left << setw(20) << result.out() << "(" << (timer->timeElapsed()/1000) << "s)" << endl;
}
cout << endl;
}

void Benchmark::generate()
{
vector<Move> moves;
this->board->possibleMoves(moves);

int cnt=0;

FOREACH(Move,move,moves)
{
MAKE((*move),this->board);

if (!this->board->isKingAttacked())
{
cout << ++cnt << "\t: " << (*move).notation() << endl;
}

UNMAKE((*move),this->board);
}
cout << endl;
}
47 changes: 47 additions & 0 deletions benchmark.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**********************************************************************
|
| Bogart
| Chess Engine
|
| Copyright (C) 2009-2013 Dr.Kameleon
|
|**********************************************************************
| benchmark.h
|**********************************************************************/

#ifndef __BENCHMARK_H__
#define __BENCHMARK_H__

//=======================================================
// Macros
//=======================================================

#define BENCH_HEADER(X) "========================================" << endl\
<< X << endl << "========================================" << endl;
#define BENCH_SUBHEADER(X) endl << "-------------------------" << endl\
<< X << endl << "-------------------------" << endl;

//=======================================================
// Prototype
//=======================================================

class Benchmark
{
public:
//-----------------------------------------
// Methods
//-----------------------------------------
Benchmark(Board* b);

void perft(UINT depth, BOOL loop);
void eval(UINT depth, BOOL loop);
void generate();

//-----------------------------------------
// Variables
//-----------------------------------------
Board* board;
int depth;
};

#endif
65 changes: 65 additions & 0 deletions bitboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**********************************************************************
|
| Bogart
| Chess Engine
|
| Copyright (C) 2009-2013 Dr.Kameleon
|
|**********************************************************************
| bitboard.cpp
|**********************************************************************/

#include "bogart.h"

//=======================================================
// Output
//=======================================================

const char* Bitboard::str(U64 bitboard)
{
static char b[65];
b[0] = '\0';

for (U64 z = (1ULL<<63); z > 0; z >>= 1)
{
strcat(b, ((bitboard & z) == z) ? "1" : "0");
}
return b;
}

string Bitboard::out(U64 bitboard)
{
string res = "";

for (int i=63; i>=0; i--)
{
U64 position = 1ULL<<i;
(bitboard&position)==position ? res += "X " : res += ". ";
if (i%8==0) res += "\n";
}

return res;
}

string Bitboard::outAndMark(U64 bitboard, int pos)
{
string res = "";

for (int i=63; i>=0; i--)
{
U64 position = 1ULL<<i;
if (i==pos) res+= "O ";
else
{
(bitboard&position)==position ? res += "X " : res += ". ";
}
if (i%8==0) res += "\n";
}

return res;
}

void Bitboard::print(U64 bitboard)
{
cout << Bitboard::out(bitboard);
}
96 changes: 96 additions & 0 deletions bitboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**********************************************************************
|
| Bogart
| Chess Engine
|
| Copyright (C) 2009-2013 Dr.Kameleon
|
|**********************************************************************
| bitboard.h
|**********************************************************************/

#ifndef __BITBOARD_H__
#define __BITBOARD_H__

//=======================================================
// Macros
//=======================================================

#define BITAT(X,Y) (((X)&(1ULL<<(Y)))>>(Y))
#define BITCOUNT(X) __builtin_popcountll((X))
#define ISBITSET(X,Y) ((X)&(1ULL<<(Y)))
#define SETBIT(X,Y) X|=(1ULL<<(Y))
#define UNSETBIT(X,Y) X&=(~(1ULL<<(Y)))
#define TOGGLEBIT(X,Y) X^=(1ULL<<(Y))
#define TOGGLEBITS(X,Y,Z) X^=((1ULL<<(Y))|(1ULL<<(Z)))
#define VFLIPBITS(X) __builtin_bswap64(X)
#define FIRSTBIT(X) Bitboard::firstBit((X))
#define BITSSET(X) Bitboard::bitsSet((X))

//=======================================================
// Prototype
//=======================================================

class Bitboard
{
public:
//-----------------------------------------
// Methods
//-----------------------------------------
static inline UINT firstBit(U64 bitboard);
static inline vector<UINT> bitsSet(U64 bitboard);
static inline U64 getRandom();

static const char* str(U64 bitboard);
static string out(U64 bitboard);
static string outAndMark(U64 bitboard, int pos);
static void print(U64 bitboard);

};

//=======================================================
// Inline Functions
//=======================================================

inline UINT Bitboard::firstBit(U64 bitboard)
{
const int index64[64] = {
0, 47, 1, 56, 48, 27, 2, 60,
57, 49, 41, 37, 28, 16, 3, 61,
54, 58, 35, 52, 50, 42, 21, 44,
38, 32, 29, 23, 17, 11, 4, 62,
46, 55, 26, 59, 40, 36, 15, 53,
34, 51, 20, 43, 31, 22, 10, 45,
25, 39, 14, 33, 19, 30, 9, 24,
13, 18, 8, 12, 7, 6, 5, 63
};

const U64 debruijn64 = 0x03F79D71B4CB0A89ULL;
assert (bitboard != 0);
return index64[((bitboard ^ (bitboard-1)) * debruijn64) >> 58];
}

inline vector<UINT> Bitboard::bitsSet(U64 bitboard)
{
UINT n;
vector<UINT> res;

res.reserve(64);
for (n = 0; bitboard != 0; n++, bitboard &= (bitboard - 1))
{
res.push_back(log2(bitboard & ~(bitboard-1)));
}
return res;
}


inline U64 Bitboard::getRandom()
{
U64 r30 = RAND_MAX*rand()+rand();
U64 s30 = RAND_MAX*rand()+rand();
U64 t4 = rand() & 0xf;

return (r30 << 34) + (s30 << 4) + t4;
}

#endif
Loading

0 comments on commit cd461b6

Please sign in to comment.