Skip to content

Commit

Permalink
Cleanup v2 (#34)
Browse files Browse the repository at this point in the history
Cleaning code and releasing version 0.8.

* Code cleanup

* Updated version number and makefile for latest release
  • Loading branch information
rosenthj authored May 25, 2020
1 parent fd2c16e commit c477eea
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 1,311 deletions.
20 changes: 0 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#If you have clang, it seems to generate a faster compile as of the beginning of 2018
#CC=g++
#CC=x86_64-w64-mingw32-g++-posix
CC=clang++
CFLAGS=-c -DNDEBUG -O3 -flto -g3 -Wall -Wno-sign-compare -m64 -march=native -std=c++11 -Isrc -Isrc/general -Isrc/learning
LDFLAGS=-flto -Wall
Expand All @@ -10,25 +9,6 @@ EXECUTABLE:=Winter

all: $(SOURCES) $(EXECUTABLE)

target no_bmi: CFLAGS += -DNO_BMI
no_bmi: all

target ancient: CFLAGS=-c -DNDEBUG -O3 -Wall -Wno-sign-compare -m64 -DNO_BMI -std=c++11 -Isrc -Isrc/general -Isrc/learning
target ancient: LDFLAGS=-Wall -static
ancient: all

target older: CFLAGS=-c -DNDEBUG -O3 -Wall -Wno-sign-compare -m64 -msse4.1 -DNO_BMI -std=c++11 -Isrc -Isrc/general -Isrc/learning
target older: LDFLAGS=-Wall -static
older: all

target old: CFLAGS=-c -DNDEBUG -O3 -Wall -Wno-sign-compare -m64 -msse4.2 -DNO_BMI -std=c++11 -Isrc -Isrc/general -Isrc/learning
target old: LDFLAGS=-Wall -static
old: all

target new: CFLAGS=-c -DNDEBUG -O3 -Wall -Wno-sign-compare -m64 -mavx2 -mbmi -mbmi2 -std=c++11 -Isrc -Isrc/general -Isrc/learning
target new: LDFLAGS=-Wall -static
new: all

$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@ -lpthread

Expand Down
44 changes: 44 additions & 0 deletions Makefile.windows
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
CC=clang++
CFLAGS=-c -DNDEBUG -O3 -target x86_64-w64-windows-msvc -Wall -Wno-sign-compare -m64 -march=native -std=c++14 -Isrc -Isrc/general -Isrc/learning
LDFLAGS=-static -Wall
SOURCES=$(wildcard src/general/*.cc src/learning/*.cc src/*.cc)
OBJECTS=$(SOURCES:.cc=.o)
EXECUTABLE:=Winter.exe

all: $(SOURCES) $(EXECUTABLE)

target no_bmi: CFLAGS += -DNO_BMI
no_bmi: all

target ancient: CFLAGS=-c -DNDEBUG -O3 -target x86_64-w64-windows-msvc -Wall -Wno-sign-compare -m64 -DNO_BMI -std=c++14 -Isrc -Isrc/general -Isrc/learning
target ancient: LDFLAGS=-Wall -static
ancient: all

target older: CFLAGS=-c -DNDEBUG -O3 -target x86_64-w64-windows-msvc -Wall -Wno-sign-compare -m64 -msse4.1 -DNO_BMI -std=c++14 -Isrc -Isrc/general -Isrc/learning
target older: LDFLAGS=-Wall -static
older: all

target old: CFLAGS=-c -DNDEBUG -O3 -target x86_64-w64-windows-msvc -Wall -Wno-sign-compare -m64 -msse4.2 -DNO_BMI -std=c++14 -Isrc -Isrc/general -Isrc/learning
target old: LDFLAGS=-Wall -static
old: all

target new: CFLAGS=-c -DNDEBUG -O3 -target x86_64-w64-windows-msvc -Wall -Wno-sign-compare -m64 -mavx2 -mbmi -mbmi2 -std=c++14 -Isrc -Isrc/general -Isrc/learning
target new: LDFLAGS=-Wall -static
new: all

$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cc.o:
$(CC) $(CFLAGS) $< -o $@

clean: clean-src clean-general clean-learning

clean-src:
rm src/*.o

clean-general:
rm src/general/*.o

clean-learning:
rm src/learning/*.o
62 changes: 24 additions & 38 deletions src/board.cc
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ inline void AddPromotionMoves(const Square src, const Square des, std::vector<Mo

template<int Quiescent, MoveGenType move_gen_type, Color point_of_view>
inline void ConditionalAddPromotionMoves(const Square src, const Square des,
std::vector<Move> &moves, const MoveType move_type) {
std::vector<Move> &moves,
const MoveType move_type) {
const int back_rank = point_of_view == kWhite ? 7 : 0;
if (move_gen_type != MoveGenType::Fast || GetSquareY(des) != back_rank) {
moves.emplace_back(GetMove(src, des, move_type));
Expand All @@ -267,7 +268,8 @@ inline void ConditionalAddPromotionMoves(const Square src, const Square des,
}

inline void AddNonPromotionMovesLoop(BitBoard des, const Square square_dif,
std::vector<Move> &moves, const MoveType move_type) {
std::vector<Move> &moves,
const MoveType move_type) {
for (; des; bitops::PopLSB(des)) {
const Square destination = bitops::NumberOfTrailingZeros(des);
moves.emplace_back(GetMove(destination - square_dif, destination, move_type));
Expand All @@ -276,7 +278,8 @@ inline void AddNonPromotionMovesLoop(BitBoard des, const Square square_dif,

template<int Quiescent, MoveGenType move_gen_type, Color point_of_view>
inline void AddPawnMovesLoop(BitBoard des, const Square square_dif,
std::vector<Move> &moves, const MoveType move_type) {
std::vector<Move> &moves,
const MoveType move_type) {
for (; des; bitops::PopLSB(des)) {
const Square destination = bitops::NumberOfTrailingZeros(des);
ConditionalAddPromotionMoves<Quiescent, move_gen_type, point_of_view>(
Expand Down Expand Up @@ -357,21 +360,21 @@ Board::Board() {
en_passant = 0;
fifty_move_count = 0;
phase = 0;
for (int player = kWhite; player <= kBlack; player++) {
for (int player = kWhite; player <= kBlack; ++player) {
color_bitboards[player] = 0;
for (int piece_type = 0; piece_type < kNumPieceTypes - 1; piece_type++) {
for (int piece_type = 0; piece_type < kNumPieceTypes - 1; ++piece_type) {
piece_counts[player][piece_type] = 0;
}
}
for (int piece_type = 0; piece_type < kNumPieceTypes - 1; piece_type++) {
for (int piece_type = 0; piece_type < kNumPieceTypes - 1; ++piece_type) {
pt_bitboards[piece_type] = 0;
}

for (Square square = parse::StringToSquare("a1");
square <= parse::StringToSquare("h8"); square++) {
square <= parse::StringToSquare("h8"); ++square) {
pieces[square] = kNoPiece;
}
for (Color color = kWhite; color <= kBlack; color++) {
for (Color color = kWhite; color <= kBlack; ++color) {
AddPiece(parse::StringToSquare("a1") + (56*color), GetPiece(color, kRook));
AddPiece(parse::StringToSquare("b1") + (56*color), GetPiece(color, kKnight));
AddPiece(parse::StringToSquare("c1") + (56*color), GetPiece(color, kBishop));
Expand All @@ -380,7 +383,7 @@ Board::Board() {
AddPiece(parse::StringToSquare("f1") + (56*color), GetPiece(color, kBishop));
AddPiece(parse::StringToSquare("g1") + (56*color), GetPiece(color, kKnight));
AddPiece(parse::StringToSquare("h1") + (56*color), GetPiece(color, kRook));
for (int i = 0; i < 8; i++) {
for (int i = 0; i < kBoardLength; ++i) {
AddPiece(parse::StringToSquare("a2") + i + (40*color), GetPiece(color, kPawn));
}
}
Expand All @@ -391,9 +394,9 @@ Board::Board() {
std::vector<std::string> Board::GetFen() const {
std::vector<std::string> fen;
std::string board_fen = "";
for (int row = 7; row >= 0; row--) {
for (int row = 7; row >= 0; --row) {
int empty_in_a_row = 0;
for (int col = 0; col < 8; col++) {
for (int col = 0; col < 8; ++col) {
Square square = GetSquare(col, row);
if (get_piece(square) == kNoPiece) {
empty_in_a_row++;
Expand Down Expand Up @@ -470,24 +473,24 @@ void Board::SetBoard(std::vector<std::string> fen_tokens){
en_passant = 0;
fifty_move_count = 0;
phase = 0;
for (int player = kWhite; player <= kBlack; player++) {
for (int player = kWhite; player <= kBlack; ++player) {
color_bitboards[player] = 0;
for (int piece_type = 0; piece_type < kNumPieceTypes - 1; piece_type++) {
for (int piece_type = 0; piece_type < kNumPieceTypes - 1; ++piece_type) {
piece_counts[player][piece_type] = 0;
}
}
for (int piece_type = 0; piece_type < kNumPieceTypes - 1; piece_type++) {
for (int piece_type = 0; piece_type < kNumPieceTypes - 1; ++piece_type) {
pt_bitboards[piece_type] = 0;
}
for (Square square = parse::StringToSquare("a1");
square <= parse::StringToSquare("h8"); square++) {
square <= parse::StringToSquare("h8"); ++square) {
pieces[square] = kNoPiece;
}

std::vector<std::string> fen_position_rows = parse::split(fen_tokens[0], '/');
for (int row = 0; row < kBoardLength; row++) {
Square square = GetSquare(0, 7 - row);
for (size_t char_idx = 0; char_idx < fen_position_rows[row].length(); char_idx++) {
for (size_t char_idx = 0; char_idx < fen_position_rows[row].length(); ++char_idx) {
char c = fen_position_rows[row][char_idx];
switch (c){
case 'K': AddPiece(square, GetPiece(kWhite, kKing)); break;
Expand Down Expand Up @@ -547,7 +550,7 @@ void Board::evaluate_castling_rights(std::string fen_code){
castling_rights = 0;

int len = fen_code.length();
for(int i = 0; i < len; i++){
for(int i = 0; i < len; ++i){
char c = fen_code[i];
switch (c){
case 'K': castling_rights |= kWSCastle; break;
Expand Down Expand Up @@ -580,11 +583,11 @@ void Board::SetToSamePosition(const Board &board) {
phase = board.phase;
for (int player = kWhite; player <= kBlack; player++) {
color_bitboards[player] = board.color_bitboards[player];
for (int piece_type = 0; piece_type < kNumPieceTypes - 1; piece_type++) {
for (int piece_type = 0; piece_type < kNumPieceTypes - 1; ++piece_type) {
piece_counts[player][piece_type] = board.piece_counts[player][piece_type];
}
}
for (int piece_type = 0; piece_type < kNumPieceTypes - 1; piece_type++) {
for (int piece_type = 0; piece_type < kNumPieceTypes - 1; ++piece_type) {
pt_bitboards[piece_type] = board.pt_bitboards[piece_type];
}
for (Square square = parse::StringToSquare("a1");
Expand Down Expand Up @@ -840,7 +843,7 @@ std::vector<Move> Board::GetMoves(const BitBoard critical) {
}
if (!Quiescent) {
// Add castling moves
for (int right = 0 + 2*get_turn(); right < 2+2*get_turn(); right++) {
for (int right = 0 + 2*get_turn(); right < 2+2*get_turn(); ++right) {
if ((castling_rights & (0x1 << right))
&& !(castling_check_bbs[right] & in_check)
&& !(castling_empty_bbs[right] & all_pieces)) {
Expand Down Expand Up @@ -1014,15 +1017,6 @@ bool Board::MoveInListCanRepeat(const std::vector<Move> moves) {
pre_hashes.emplace_back(previous_hashes[index]);
}

/*for (int i = 0; i < potential_hashes.size(); i++) {
for (int j = 0; j < pre_hashes.size(); j++) {
if (potential_hashes[i] == pre_hashes[j]) {
std::cout << "found repetition" << std::endl;
return true;
}
}
}*/

std::sort(pre_hashes.begin(), pre_hashes.end());
std::sort(potential_hashes.begin(), potential_hashes.end());

Expand All @@ -1044,15 +1038,6 @@ bool Board::MoveInListCanRepeat(const std::vector<Move> moves) {
return true;
}
}
/*for (i = 0; i < potential_hashes.size(); i++) {
std::cout << potential_hashes[i] << " ";
}
std::cout << std::endl;
for (i = 0; i < pre_hashes.size(); i++) {
std::cout << pre_hashes[i] << " ";
}
std::cout << std::endl;
std::cout << "checked all, no reps found" << std::endl;*/
return false;
}

Expand Down Expand Up @@ -1123,6 +1108,7 @@ bool Board::NonNegativeSEESquare(const Square target) const {
attackers |= magic::GetAttackMap<kRook>(target, all_pieces) & (pt_bitboards[kRook] | pt_bitboards[kQueen]);
attackers |= magic::GetAttackMap<kKing>(target, all_pieces) & (pt_bitboards[kKing]);
attackers &= all_pieces;

while ((attackers & color_bitboards[cturn]) && score <= 0) {
if (cturn == turn && score == 0) {
return true;
Expand Down
Loading

0 comments on commit c477eea

Please sign in to comment.