Compare commits

...

13 Commits
master ... uci

9 changed files with 755 additions and 475 deletions

View File

@ -5,181 +5,19 @@
#include "Config.h"
#include "Board.h"
#include "Move.h"
#include "Movegen.h"
#include "Types.h"
unsigned long pseudo_perft(byte depth) {
// only checks pseudolegality
// but, it should work overall
if(depth == 0) return 1;
if(depth == 3) blink();
unsigned long move_count = 0;
Movegen gen;
Move m;
while (true) {
m = gen.next_move();
if(m.sq_to != 255) {
make(m);
move_count += pseudo_perft(depth-1);
unmake();
} else {
break;
}
}
return move_count;
}
void perft_test() {
for(byte i = 0; i < 5; i++) {
Serial.print(F("Perft "));
Serial.print(i);
Serial.print(F(": "));
Serial.println(pseudo_perft(i));
}
}
void debug_movegen() {
make({0x14, 0x34, P_EMPTY});
Movegen gen = Movegen();
Move m;
do {
DEBUG("start movegen");
m = gen.next_move();
Serial.print(F("FROM "));
Serial.print(m.sq_from, HEX);
Serial.print(F(" TO "));
Serial.print(m.sq_to, HEX);
Serial.print(F(" PROMOTE "));
Serial.println(m.pc_prom);
if(m.sq_from != 255) {
DEBUG("make");
make(m);
print();
DEBUG("unmake");
unmake();
DEBUG("unmake done");
}
} while (m.sq_from != 255);
}
void debug_castle() {
print();
make({0x06, 0x25, P_EMPTY}); print();
make({0x76, 0x55, P_EMPTY}); print();
make({0x16, 0x26, P_EMPTY}); print();
make({0x63, 0x43, P_EMPTY}); print();
make({0x05, 0x16, P_EMPTY}); print();
make({0x62, 0x42, P_EMPTY}); print();
make({0x04, 0x06, P_EMPTY}); print();
unmake(); print();
unmake(); print();
unmake(); print();
unmake(); print();
unmake(); print();
unmake(); print();
unmake(); print();
}
void debug_ep() {
print();
make({0x14, 0x34, P_EMPTY});
print();
make({0x64, 0x54, P_EMPTY});
print();
make({0x34, 0x44, P_EMPTY});
print();
make({0x63, 0x43, P_EMPTY});
print();
make({0x44, 0x53, P_EMPTY});
print();
unmake();
print();
unmake();
print();
unmake();
print();
unmake();
print();
unmake();
print();
}
void bench() {
unsigned long startTime = micros();
make({0x14, 0x34, P_EMPTY});
make({0x64, 0x54, P_EMPTY});
make({0x34, 0x44, P_EMPTY});
make({0x63, 0x43, P_EMPTY});
make({0x44, 0x53, P_EMPTY});
unmake();
unmake();
unmake();
unmake();
unmake();
make({0x06, 0x25, P_EMPTY});
make({0x76, 0x55, P_EMPTY});
make({0x16, 0x26, P_EMPTY});
make({0x63, 0x43, P_EMPTY});
make({0x05, 0x16, P_EMPTY});
make({0x62, 0x42, P_EMPTY});
make({0x04, 0x06, P_EMPTY});
unmake();
unmake();
unmake();
unmake();
unmake();
unmake();
unmake();
unsigned long elapsed = micros() - startTime;
Serial.print(elapsed);
Serial.println(F(" microseconds for make/unmake"));
for(int i = 1; i <= 4; i++) {
startTime = millis();
pseudo_perft(i);
elapsed = millis() - startTime;
Serial.print(elapsed);
Serial.print(F(" milliseconds for pseudo_perft("));
Serial.print(i);
Serial.println(')');
}
Movegen gen;
Move move[20];
startTime = micros();
for(int i = 0; i < 20; i++) {
move[i] = gen.next_move();
}
elapsed = micros() - startTime;
Serial.print(elapsed);
Serial.println(F(" microseconds for movegen(init_pos)"));
startTime = micros();
for(int i = 0; i < 20; i++) {
make(move[i]);
unmake();
}
elapsed = micros() - startTime;
Serial.print(elapsed);
Serial.println(F(" microseconds for make/unmake(init_pos)"));
}
#include "Uci.h"
void setup() {
// put your setup code here, to run once:
board_init();
Board::init();
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
perft_test();
bench();
//debug_check();
//perft_test();
//bench();
}
void loop() {
// put your main code here, to run repeatedly:
Uci::handle_uci();
}

542
Board.h
View File

@ -3,290 +3,302 @@
#include "Types.h"
#include "Panic.h"
#include "Move.h"
#define BOARD_DEFAULT_VALUE { \
W_ROOK, W_KNGT, W_BSHP, W_QUEN, W_KING, W_BSHP, W_KNGT, W_ROOK, 0, 0, 0, 0, 0, 0, 0, 0, \
W_PAWN, W_PAWN, W_PAWN, W_PAWN, W_PAWN, W_PAWN, W_PAWN, W_PAWN, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
B_PAWN, B_PAWN, B_PAWN, B_PAWN, B_PAWN, B_PAWN, B_PAWN, B_PAWN, 0, 0, 0, 0, 0, 0, 0, 0, \
B_ROOK, B_KNGT, B_BSHP, B_QUEN, B_KING, B_BSHP, B_KNGT, B_ROOK, 0, 0, 0, 0, 0, 0, 0, 0, \
};
namespace Board {
// 0x88-fill definitions
#define PTR_SIDE_AND_CASTLERIGHT 0x08 //byte (1=side, 2,4=white castle, 8,16=black)
// CAN FILL 0x09
#define PTR_W_KING 0x0A // byte (points to index or maybe 64-arr index)
#define PTR_B_KING 0x0B // (PTR_W_KING | COLOR or PTR_W_KING + COLOR)
#define PTR_ZOBRIST 0x0C // 4 bytes
// 0x0D
// 0x0E
// 0x0F
#define BOARD_DEFAULT_VALUE { \
W_ROOK, W_KNGT, W_BSHP, W_QUEN, W_KING, W_BSHP, W_KNGT, W_ROOK, 0, 0, 0, 0, 0, 0, 0, 0, \
W_PAWN, W_PAWN, W_PAWN, W_PAWN, W_PAWN, W_PAWN, W_PAWN, W_PAWN, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
B_PAWN, B_PAWN, B_PAWN, B_PAWN, B_PAWN, B_PAWN, B_PAWN, B_PAWN, 0, 0, 0, 0, 0, 0, 0, 0, \
B_ROOK, B_KNGT, B_BSHP, B_QUEN, B_KING, B_BSHP, B_KNGT, B_ROOK, 0, 0, 0, 0, 0, 0, 0, 0, \
};
#define PTR_ENPASSANT 0x18
#define PTR_REVMOV 0x19
// free space
// 0x88-fill definitions
#define PTR_SIDE_AND_CASTLERIGHT 0x08 //byte (1=side, 2,4=white castle, 8,16=black)
// CAN FILL 0x09
#define PTR_W_KING 0x0A // byte (points to index or maybe 64-arr index)
#define PTR_B_KING 0x0B // (PTR_W_KING | COLOR or PTR_W_KING + COLOR)
#define PTR_ZOBRIST 0x0C // 4 bytes
// 0x0D
// 0x0E
// 0x0F
#define PTR_UNMOVE_START 0x28
#define PTR_UNMOVE_LAST 0x7F
#define PTR_ENPASSANT 0x18
#define PTR_REVMOV 0x19
// free space
byte field[128];
byte PTR_UNMOVE;
#define PTR_UNMOVE_START 0x28
#define PTR_UNMOVE_LAST 0x7F
const byte field_default_value[] PROGMEM = BOARD_DEFAULT_VALUE;
byte field[128];
byte PTR_UNMOVE;
void board_init() {
for(int i = 0; i < 128; i++) {
field[i] = pgm_read_byte_near(field_default_value + i);
}
PTR_UNMOVE = PTR_UNMOVE_START;
field[PTR_SIDE_AND_CASTLERIGHT] = 0b11110; // all castle rights allowed, white to move
field[PTR_W_KING] = 0x04; // e1
field[PTR_B_KING] = 0x74; // e8
long* zob = (long*)&field[PTR_ZOBRIST];
*zob = 0xDEADBEEF;
}
const byte field_default_value[] PROGMEM = BOARD_DEFAULT_VALUE;
struct Unmove {
byte sq_from; // 0b(1kingside_castle?)(3rank)(1queenside_castle?)(3file)
byte sq_to; // 0b(1promoted?)(3rank)(1ep_capture?)(3file)
byte captured; // 0b(4enpassantinfo)(1color)(3piecetype)
byte revmov; // 8bit integer
};
bool black_moving() {
return field[PTR_SIDE_AND_CASTLERIGHT] & 0x1;
}
unsigned long get_zobrist() {
long* addr = (long*) &field[PTR_ZOBRIST];
return *addr;
}
void reset_unmake_stack() {
PTR_UNMOVE = PTR_UNMOVE_START;
}
void next_unmove() {
PTR_UNMOVE++;
if(PTR_UNMOVE > PTR_UNMOVE_LAST) {
panic(F("Unmove stack overflow"));
}
if(!(PTR_UNMOVE & 0x8)) {
PTR_UNMOVE += 0x8;
}
}
void prev_unmove() {
PTR_UNMOVE--;
if(PTR_UNMOVE < PTR_UNMOVE_START) {
panic(F("Unmaking from empty stack"));
}
if(!(PTR_UNMOVE & 0x8)) {
PTR_UNMOVE -= 0x8;
}
}
void store_unmove(Unmove u) {
byte *ub = (byte*) &u;
for(byte i = 0; i < sizeof(u); i++) {
field[PTR_UNMOVE] = ub[i];
next_unmove();
}
}
Unmove read_unmove() {
Unmove u;
byte* ptr = (byte*) &u;
for(int i = sizeof(u) - 1; i >= 0; i--) {
prev_unmove();
ptr[i] = field[PTR_UNMOVE];
#ifdef _ACF_CLEAR_UNMOVE
field[PTR_UNMOVE] = 0;
#endif
}
return u;
}
void print() {
Serial.println(F("BOARD:"));
for(char i = 7; i >= 0; i--) {
for(byte j = 0; j < 16; j++) {
if(j == 8)
Serial.print(F("| "));
Serial.print(field[i*16 + j], HEX);
Serial.print(F(" "));
void init() {
for(int i = 0; i < 128; i++) {
field[i] = pgm_read_byte_near(field_default_value + i);
}
Serial.println();
}
}
void make(Move m) {
// TODO zobrist?
// fill unmove struct with basic data
Unmove u;
u.revmov = field[PTR_REVMOV];
u.captured = field[m.sq_to] | (field[PTR_ENPASSANT] << 4);
u.sq_from = m.sq_from;
u.sq_to = m.sq_to;
byte piece_type = field[m.sq_from] & 0x7;
byte color = black_moving();
if(field[m.sq_to] || piece_type == W_PAWN) {
field[PTR_REVMOV] = 0;
} else {
field[PTR_REVMOV]++;
PTR_UNMOVE = PTR_UNMOVE_START;
field[PTR_SIDE_AND_CASTLERIGHT] = 0b11110; // all castle rights allowed, white to move
field[PTR_W_KING] = 0x04; // e1
field[PTR_B_KING] = 0x74; // e8
long* zob = (long*)&field[PTR_ZOBRIST];
*zob = 0xDEADBEEF;
}
// Calculate the move 'amount' (unique signature for dx,dy)
int sq_diff = (int)m.sq_to - (int)m.sq_from;
int sq_diff_abs = abs(sq_diff);
void swap_side() {
field[PTR_SIDE_AND_CASTLERIGHT] ^= 0x01;
}
struct Unmove {
byte sq_from; // 0b(1kingside_castle?)(3rank)(1queenside_castle?)(3file)
byte sq_to; // 0b(1promoted?)(3rank)(1ep_capture?)(3file)
byte captured; // 0b(4enpassantinfo)(1color)(3piecetype)
byte revmov; // 8bit integer
};
// TODO test the castling code more extensively
// Handle castling
if(piece_type == W_KING && sq_diff_abs == 2) {
// We are castling! After all, a king cannot move
// more than one position except when castling.
// Since we don't care about legality; just do it
byte castle_source = (color ? 0x70 : 0x0);
if(sq_diff == 2) {
castle_source += 0x7;
bool black_moving() {
return field[PTR_SIDE_AND_CASTLERIGHT] & 0x1;
}
unsigned long get_zobrist() {
long* addr = (long*) &field[PTR_ZOBRIST];
return *addr;
}
void reset_unmake_stack() {
PTR_UNMOVE = PTR_UNMOVE_START;
}
void next_unmove() {
PTR_UNMOVE++;
if(PTR_UNMOVE > PTR_UNMOVE_LAST) {
panic(F("Unmove stack overflow"));
}
byte castle_target = m.sq_from + (sq_diff/2);
field[castle_target] = field[castle_source];
field[castle_source] = P_EMPTY;
}
// Handle castling rights
// First store the current rights in the unmove
byte our_rights = field[PTR_SIDE_AND_CASTLERIGHT] >> (color ? 3 : 1);
if(our_rights & 0b10) // kingside allowed
u.sq_from |= 0x80;
if(our_rights & 0b01) // queenside allowed
u.sq_from |= 0x08;
// We are doing the simple way:
// unset it any time a move is made from the original position.
if(m.sq_from == 0x00) // white queenside rook
field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b00010;
else if(m.sq_from == 0x07) // white kingside rook
field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b00100;
else if(m.sq_from == 0x04) // white king
field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b00110;
else if(m.sq_from == 0x70) // black queenside rook
field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b01000;
else if(m.sq_from == 0x77) // black kingside rook
field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b10000;
else if(m.sq_from == 0x74) // black king
field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b11000;
// TODO: test enpassant code more than basics
// handle enpassant capture
if(
field[PTR_ENPASSANT] &&
piece_type == W_PAWN &&
(m.sq_to & 0x7) == (field[PTR_ENPASSANT] & 0x7) &&
(m.sq_to & 0x70) == (color ? 0x20 : 0x50)
) {
// all EP-conditions are met
// therefore, delete the EP-vurnerable pawn
byte ep_field = m.sq_to + (color ? 16 : -16);
field[ep_field] = P_EMPTY;
// also put information that we did an EP-capture
u.sq_to |= 0x08;
}
// handle enpassant setup (double pawn move)
if(
piece_type == W_PAWN &&
sq_diff_abs == 32
) {
// we are doing a pawn double-move.
// therefore, it allows enpassant in the next move.
field[PTR_ENPASSANT] = 0b1000 | (m.sq_from & 0x7);
} else {
// no enpassant in the next turn.
field[PTR_ENPASSANT] = 0;
}
// are we promoting?
byte new_val = m.pc_prom & 0b1111;
if(m.pc_prom != P_EMPTY) {
// promoting; indicate this in the sq_to byte in unmove.
field[m.sq_to] = m.pc_prom;
u.sq_to |= 0x80;
} else {
// not promoting; so keep the same piece type
field[m.sq_to] = field[m.sq_from];
}
// then delete the original copy.
field[m.sq_from] = P_EMPTY;
// Switch sides
field[PTR_SIDE_AND_CASTLERIGHT] ^= 0x01;
store_unmove(u);
}
void unmake() {
Unmove u = read_unmove();
field[PTR_REVMOV] = u.revmov;
byte sq_from = u.sq_from & 0x77;
byte sq_to = u.sq_to & 0x77;
byte prom_ep_capt = u.sq_to & 0x88;
if(prom_ep_capt == 0) {
// regular move
field[sq_from] = field[sq_to];
} else if (prom_ep_capt == 0x80) {
// piece was promoted
// so the source is a pawn
field[sq_from] = W_PAWN | (field[sq_to] & 0b1000);
} else if (prom_ep_capt == 0x08) {
// we did an enpassant capture
byte ep_sq = (sq_to & 0x07) | (sq_from & 0x70);
field[ep_sq] = W_PAWN | black_moving() << 3;
// also undo the regular move
field[sq_from] = field[sq_to];
}
byte castleright_offset = 3 - 2*black_moving();
if(u.sq_from & 0x80) {
// restore king side castling rights
field[PTR_SIDE_AND_CASTLERIGHT] |= 0b10 << castleright_offset;
}
if(u.sq_from & 0x08) {
field[PTR_SIDE_AND_CASTLERIGHT] |= 0b01 << castleright_offset;
}
int sq_diff = (int)sq_to - (int)sq_from;
int sq_diff_abs = abs(sq_diff);
if((field[sq_from] & 0x7) == W_KING && sq_diff_abs == 2) {
// we castled
byte castle_source = 0x70*!black_moving();
if(sq_diff == 2) {
castle_source += 0x7;
if(!(PTR_UNMOVE & 0x8)) {
PTR_UNMOVE += 0x8;
}
byte castle_target = sq_from + (sq_diff/2);
// move rook back to original position
field[castle_source] = field[castle_target];
// and clear where it was put
field[castle_target] = P_EMPTY;
}
void prev_unmove() {
PTR_UNMOVE--;
if(PTR_UNMOVE < PTR_UNMOVE_START) {
panic(F("Unmaking from empty stack"));
}
if(!(PTR_UNMOVE & 0x8)) {
PTR_UNMOVE -= 0x8;
}
}
void store_unmove(Unmove u) {
byte *ub = (byte*) &u;
for(byte i = 0; i < sizeof(u); i++) {
field[PTR_UNMOVE] = ub[i];
next_unmove();
}
}
Unmove read_unmove() {
Unmove u;
byte* ptr = (byte*) &u;
for(int i = sizeof(u) - 1; i >= 0; i--) {
prev_unmove();
ptr[i] = field[PTR_UNMOVE];
#ifdef _ACF_CLEAR_UNMOVE
field[PTR_UNMOVE] = 0;
#endif
}
return u;
}
field[sq_to] = u.captured & 0b1111;
void print() {
Serial.println(F("BOARD:"));
for(char i = 7; i >= 0; i--) {
for(byte j = 0; j < 16; j++) {
if(j == 8)
Serial.print(F("| "));
Serial.print(field[i*16 + j], HEX);
Serial.print(F(" "));
}
Serial.println();
}
}
void make(Move m) {
// TODO zobrist?
// fill unmove struct with basic data
Unmove u;
u.revmov = field[PTR_REVMOV];
u.captured = field[m.sq_to] | (field[PTR_ENPASSANT] << 4);
u.sq_from = m.sq_from;
u.sq_to = m.sq_to;
byte piece_type = field[m.sq_from] & 0x7;
byte color = black_moving();
if(field[m.sq_to] || piece_type == W_PAWN) {
field[PTR_REVMOV] = 0;
} else {
field[PTR_REVMOV]++;
}
// Calculate the move 'amount' (unique signature for dx,dy)
int sq_diff = (int)m.sq_to - (int)m.sq_from;
int sq_diff_abs = abs(sq_diff);
// TODO test the castling code more extensively
// Handle castling
if(piece_type == W_KING) {
// Update the king position
field[PTR_W_KING | Board::black_moving()] = m.sq_to;
if(sq_diff_abs == 2) {
// We are castling! After all, a king cannot move
// more than one position except when castling.
// Since we don't care about legality; just do it
byte castle_source = (color ? 0x70 : 0x0);
if(sq_diff == 2) {
castle_source += 0x7;
}
byte castle_target = m.sq_from + (sq_diff/2);
field[castle_target] = field[castle_source];
field[castle_source] = P_EMPTY;
}
}
// Handle castling rights
// First store the current rights in the unmove
byte our_rights = field[PTR_SIDE_AND_CASTLERIGHT] >> (color ? 3 : 1);
if(our_rights & 0b10) // kingside allowed
u.sq_from |= 0x80;
if(our_rights & 0b01) // queenside allowed
u.sq_from |= 0x08;
// We are doing the simple way:
// unset it any time a move is made from the original position.
if(m.sq_from == 0x00) // white queenside rook
field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b00010;
else if(m.sq_from == 0x07) // white kingside rook
field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b00100;
else if(m.sq_from == 0x04) // white king
field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b00110;
else if(m.sq_from == 0x70) // black queenside rook
field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b01000;
else if(m.sq_from == 0x77) // black kingside rook
field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b10000;
else if(m.sq_from == 0x74) // black king
field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b11000;
// TODO: test enpassant code more than basics
// handle enpassant capture
if(
field[PTR_ENPASSANT] &&
piece_type == W_PAWN &&
(m.sq_to & 0x7) == (field[PTR_ENPASSANT] & 0x7) &&
(m.sq_to & 0x70) == (color ? 0x20 : 0x50)
) {
// all EP-conditions are met
// therefore, delete the EP-vurnerable pawn
byte ep_field = m.sq_to + (color ? 16 : -16);
field[ep_field] = P_EMPTY;
// also put information that we did an EP-capture
u.sq_to |= 0x08;
}
// handle enpassant setup (double pawn move)
if(
piece_type == W_PAWN &&
sq_diff_abs == 32
) {
// we are doing a pawn double-move.
// therefore, it allows enpassant in the next move.
field[PTR_ENPASSANT] = 0b1000 | (m.sq_from & 0x7);
} else {
// no enpassant in the next turn.
field[PTR_ENPASSANT] = 0;
}
// are we promoting?
if(m.pc_prom != P_EMPTY) {
// promoting; indicate this in the sq_to byte in unmove.
field[m.sq_to] = m.pc_prom;
u.sq_to |= 0x80;
} else {
// not promoting; so keep the same piece type
field[m.sq_to] = field[m.sq_from];
}
// then delete the original copy.
field[m.sq_from] = P_EMPTY;
// Switch sides
field[PTR_SIDE_AND_CASTLERIGHT] ^= 0x01;
store_unmove(u);
}
void unmake() {
Unmove u = read_unmove();
field[PTR_REVMOV] = u.revmov;
byte sq_from = u.sq_from & 0x77;
byte sq_to = u.sq_to & 0x77;
byte prom_ep_capt = u.sq_to & 0x88;
if(prom_ep_capt == 0) {
// regular move
field[sq_from] = field[sq_to];
} else if (prom_ep_capt == 0x80) {
// piece was promoted
// so the source is a pawn
field[sq_from] = W_PAWN | (field[sq_to] & 0b1000);
} else if (prom_ep_capt == 0x08) {
// we did an enpassant capture
byte ep_sq = (sq_to & 0x07) | (sq_from & 0x70);
field[ep_sq] = W_PAWN | black_moving() << 3;
// also undo the regular move
field[sq_from] = field[sq_to];
}
byte castleright_offset = 3 - 2*black_moving();
if(u.sq_from & 0x80) {
// restore king side castling rights
field[PTR_SIDE_AND_CASTLERIGHT] |= 0b10 << castleright_offset;
}
if(u.sq_from & 0x08) {
field[PTR_SIDE_AND_CASTLERIGHT] |= 0b01 << castleright_offset;
}
field[PTR_SIDE_AND_CASTLERIGHT] ^= 0x01;
int sq_diff = (int)sq_to - (int)sq_from;
int sq_diff_abs = abs(sq_diff);
if((field[sq_from] & 0x7) == W_KING) {
field[PTR_W_KING | Board::black_moving()] = sq_from;
if(sq_diff_abs == 2) {
// we castled
byte castle_source = 0x70*black_moving();
if(sq_diff == 2) {
castle_source += 0x7;
}
byte castle_target = sq_from + (sq_diff/2);
// move rook back to original position
field[castle_source] = field[castle_target];
// and clear where it was put
field[castle_target] = P_EMPTY;
}
}
field[sq_to] = u.captured & 0b1111;
field[PTR_ENPASSANT] = u.captured >> 4;
}
field[PTR_SIDE_AND_CASTLERIGHT] ^= 0x01;
field[PTR_ENPASSANT] = u.captured >> 4;
}

View File

@ -8,4 +8,7 @@
#define _ACF_DEBUG_PRINT
#define _ACF_ACTIVITY_BLINK
#define _ACF_ACTIVITY_BLINK
//#define _ACF_BENCH_BIG

15
Move.h
View File

@ -1,15 +0,0 @@
#ifndef __MOVE_H_INC
#define __MOVE_H_INC
#include "Types.h"
struct Move {
byte sq_from; // 0x88
byte sq_to; // 0x88
Piece pc_prom; // 0b(4unused)(1color)(3type)
};
//Move move_from_str(char* s) {
//}
#endif

View File

@ -2,10 +2,10 @@
#define __MOVEGEN_H_INC
#include "Types.h"
#include "Move.h"
#include "Board.h"
const static byte SLIDE_OFFSETS[] = {-1, 1, -16, 16, 15, 17, -15, -17};
const static byte KNIGHT_OFFSETS[] = {-31, 31, -33, 33, -18, 18, -14, 14};
const static byte SLIDE_OFFSETS[] = {255, 1, 240, 16, 15, 17, 241, 239};
const static byte KNIGHT_OFFSETS[] = {225, 31, 223, 33, 238, 18, 242, 14};
#define INVALID_MOVE Move{0xFF, 0xFF, P_EMPTY}
@ -26,13 +26,11 @@ class Movegen {
Move Movegen::next_move() {
while(square <= 0x77) {
if(square & 0x88) square += 8;
byte piece_type = field[square] & 0x7;
byte piece_type = Board::field[square] & 0x7;
if(
(field[square] & 0x7) &&
(field[square] & 0x8) == black_moving() << 3
(Board::field[square] & 0x7) &&
(Board::field[square] & 0x8) == Board::black_moving() << 3
) {
// there is an own piece to investigate
Move m;
@ -49,6 +47,7 @@ Move Movegen::next_move() {
}
}
square++;
if(square & 0x88) square += 8;
direction = 0;
target_square = square;
}
@ -78,8 +77,8 @@ Move Movegen::generate_sliding(byte piece_type) {
}
// currently, we are at the next move target
// this means: we can try to generate this as a move!
byte piece = field[square];
byte target = field[target_square];
byte piece = Board::field[square];
byte target = Board::field[target_square];
if(target) {
// we encountered a piece! there are two outcomes here:
// second, it can be the opponent's. then, we can capture it!
@ -112,8 +111,8 @@ Move Movegen::generate_non_sliding(byte piece_type) {
direction++;
byte target = field[target_square];
byte piece = field[square];
byte target = Board::field[target_square];
byte piece = Board::field[square];
if((target_square & 0x88) || (target && (target & 0x8) == (piece & 0x8))) {
// uh oh, off board or same color obstacle
@ -125,7 +124,7 @@ Move Movegen::generate_non_sliding(byte piece_type) {
Move Movegen::generate_pawn() {
// TODO: implement capture promotion
byte color = black_moving();
byte color = Board::black_moving();
byte offset;
byte target;
GP_START:
@ -135,8 +134,8 @@ Move Movegen::generate_pawn() {
direction = 1; // next try, go ahead further
offset = color ? -0x10 : 0x10;
target_square = square + offset;
if(field[target_square] ||
(square & 0x70) == (color ? 0x10 : 0x60)
if(Board::field[target_square] ||
(square & 0x70) == (color ? 0x10 : 0x60)
) {
// moving ahead is not possible, not even a capture!
// this is either due to a blockade or a pending promotion
@ -152,7 +151,7 @@ Move Movegen::generate_pawn() {
direction = 2;
offset = color ? -0x20 : 0x20;
target_square = square + offset;
if(!(field[target_square]) &&
if(!(Board::field[target_square]) &&
(square & 0x70) == (color ? 0x60 : 0x10)
) {
return Move{square, target_square, P_EMPTY};
@ -165,15 +164,15 @@ Move Movegen::generate_pawn() {
direction = 3;
offset = color ? -0x11 : 0xF;
target_square = square + offset;
target = field[target_square];
target = Board::field[target_square];
if(!(target_square & 0x88)) {
if(target && (target & 0x8) != (field[square] & 0x8)) {
if(target && (target & 0x8) != (Board::field[square] & 0x8)) {
// normal capture allowded
return Move{square, target_square, P_EMPTY};
} else if(field[PTR_ENPASSANT]) {
} else if(Board::field[PTR_ENPASSANT]) {
// note that EP being legal only happens
// when the target field is empty. so this saves some effort.
byte ep_col = field[PTR_ENPASSANT] & 0x7;
byte ep_col = Board::field[PTR_ENPASSANT] & 0x7;
if(
ep_col == (target_square & 0x7) &&
(square & 0x70) == (color ? 0x30 : 0x40)
@ -189,15 +188,15 @@ Move Movegen::generate_pawn() {
direction = 4;
offset = color ? -0xF : 0x11;
target_square = square + offset;
target = field[target_square];
target = Board::field[target_square];
if(!(target_square & 0x88)) {
if(target && (target & 0x8) != (field[square] & 0x8)) {
if(target && (target & 0x8) != (Board::field[square] & 0x8)) {
// normal capture allowded
return Move{square, target_square, P_EMPTY};
} else if(field[PTR_ENPASSANT]) {
} else if(Board::field[PTR_ENPASSANT]) {
// note that EP being legal only happens
// when the target field is empty. so this saves some effort.
byte ep_col = field[PTR_ENPASSANT] & 0x7;
byte ep_col = Board::field[PTR_ENPASSANT] & 0x7;
if(
ep_col == (target_square & 0x7) &&
(square & 0x70) == (color ? 0x30 : 0x40)
@ -213,7 +212,7 @@ Move Movegen::generate_pawn() {
direction = 5;
offset = color ? -0x10 : 0x10;
target_square = square + offset;
target = field[target_square];
target = Board::field[target_square];
if(target && (target_square & 0x70) == (color ? 0x70 : 0x00)) {
// we can promote!
return Move{square, target_square, (Piece)(W_QUEN | color << 3)};

231
Tasks.h Normal file
View File

@ -0,0 +1,231 @@
#ifndef __TASKS_H_INC
#define __TASKS_H_INC
#include "Types.h"
#include "Movegen.h"
#include "Threat.h"
unsigned long pseudo_perft(byte depth) {
// only checks pseudolegality
// but, it should work overall
if(depth == 0) return 1;
if(depth == 3) blink();
unsigned long move_count = 0;
Movegen gen;
Move m;
while((m = gen.next_move()).sq_to != 255) {
Board::make(m);
move_count += pseudo_perft(depth-1);
Board::unmake();
}
return move_count;
}
unsigned long perft(byte depth) {
if(depth == 0) return 1;
if(depth == 2) blink();
unsigned long move_count = 0;
Movegen gen;
Move m;
while((m = gen.next_move()).sq_to != 255) {
if(make_safe(m)) {
move_count += perft(depth-1);
Board::unmake();
}
}
return move_count;
}
unsigned long divide(byte depth) {
// only checks pseudolegality
// but, it should work overall
if(depth == 0) return 1;
if(depth == 2) blink();
unsigned long move_count = 0;
Movegen gen;
Move m;
while((m = gen.next_move()).sq_to != 255){
print_move(m);
Serial.print(F(" "));
if(make_safe(m)) {
unsigned long count = perft(depth-1);
Serial.println(count);
move_count += count;
Board::unmake();
}
}
return move_count;
}
void perft_test() {
for(byte i = 0; i < 5; i++) {
Serial.print(F("Perft "));
Serial.print(i);
Serial.print(F(": "));
Serial.println(pseudo_perft(i));
}
}
void debug_movegen() {
Board::make({0x14, 0x34, P_EMPTY});
Movegen gen = Movegen();
Move m;
do {
DEBUG("start movegen");
m = gen.next_move();
Serial.print(F("FROM "));
Serial.print(m.sq_from, HEX);
Serial.print(F(" TO "));
Serial.print(m.sq_to, HEX);
Serial.print(F(" PROMOTE "));
Serial.println(m.pc_prom);
if(m.sq_from != 255) {
DEBUG("make");
Board::make(m);
Board::print();
DEBUG("unmake");
Board::unmake();
DEBUG("unmake done");
}
} while (m.sq_from != 255);
}
void debug_castle() {
Board::print();
Board::make({0x06, 0x25, P_EMPTY}); Board::print();
Board::make({0x76, 0x55, P_EMPTY}); Board::print();
Board::make({0x16, 0x26, P_EMPTY}); Board::print();
Board::make({0x63, 0x43, P_EMPTY}); Board::print();
Board::make({0x05, 0x16, P_EMPTY}); Board::print();
Board::make({0x62, 0x42, P_EMPTY}); Board::print();
Board::make({0x04, 0x06, P_EMPTY}); Board::print();
Board::unmake(); Board::print();
Board::unmake(); Board::print();
Board::unmake(); Board::print();
Board::unmake(); Board::print();
Board::unmake(); Board::print();
Board::unmake(); Board::print();
Board::unmake(); Board::print();
}
void debug_check() {
Board::make({0x14, 0x34, P_EMPTY});
Board::make({0x63, 0x43, P_EMPTY});
Board::make({0x05, 0x41, P_EMPTY});
Board::print();
Serial.print(Threat::is_check());
Serial.println(Threat::illegal());
divide(1);
Board::make({0x60, 0x40, P_EMPTY});
Board::print();
Serial.print(Threat::is_check());
Serial.println(Threat::illegal());
Board::unmake();
Board::unmake();
Board::unmake();
Board::unmake();
}
void debug_ep() {
Board::print();
Board::make({0x14, 0x34, P_EMPTY});
Board::print();
Board::make({0x64, 0x54, P_EMPTY});
Board::print();
Board::make({0x34, 0x44, P_EMPTY});
Board::print();
Board::make({0x63, 0x43, P_EMPTY});
Board::print();
Board::make({0x44, 0x53, P_EMPTY});
Board::print();
Board::unmake();
Board::print();
Board::unmake();
Board::print();
Board::unmake();
Board::print();
Board::unmake();
Board::print();
Board::unmake();
Board::print();
}
void bench() {
// TODO reduce code size of this (by moving repeated constants from here to PROGMEM and loops?)
unsigned long startTime;
unsigned long elapsed;
#ifdef _ACF_BENCH_BIG
startTime = micros();
Board::make({0x14, 0x34, P_EMPTY});
Board::make({0x64, 0x54, P_EMPTY});
Board::make({0x34, 0x44, P_EMPTY});
Board::make({0x63, 0x43, P_EMPTY});
Board::make({0x44, 0x53, P_EMPTY});
Board::unmake();
Board::unmake();
Board::unmake();
Board::unmake();
Board::unmake();
Board::make({0x06, 0x25, P_EMPTY});
Board::make({0x76, 0x55, P_EMPTY});
Board::make({0x16, 0x26, P_EMPTY});
Board::make({0x63, 0x43, P_EMPTY});
Board::make({0x05, 0x16, P_EMPTY});
Board::make({0x62, 0x42, P_EMPTY});
Board::make({0x04, 0x06, P_EMPTY});
Board::unmake();
Board::unmake();
Board::unmake();
Board::unmake();
Board::unmake();
Board::unmake();
Board::unmake();
elapsed = micros() - startTime;
Serial.print(elapsed);
Serial.println(F(" microseconds for make/unmake"));
#endif
for(int i = 1; i <= 4; i++) {
startTime = millis();
pseudo_perft(i);
elapsed = millis() - startTime;
Serial.print(elapsed);
Serial.print(F(" milliseconds for pseudo_perft("));
Serial.print(i);
Serial.println(')');
}
Movegen gen;
Move move[20];
startTime = micros();
for(int i = 0; i < 20; i++) {
move[i] = gen.next_move();
}
elapsed = micros() - startTime;
Serial.print(elapsed);
Serial.println(F(" microseconds for movegen(init_pos)"));
startTime = micros();
for(int i = 0; i < 20; i++) {
Board::make(move[i]);
Board::unmake();
}
elapsed = micros() - startTime;
Serial.print(elapsed);
Serial.println(F(" microseconds for make/unmake(init_pos)"));
}
#endif

59
Threat.h Normal file
View File

@ -0,0 +1,59 @@
#ifndef __THREAT_H_INC
#define __THREAT_H_INC
namespace Threat {
byte can_capture(byte square) {
// TODO make this more efficient
byte t = 0;
Movegen gen = Movegen();
Move m;
while((m = gen.next_move()).sq_to != 255) {
if(m.sq_to == square) {
t++;
}
}
return t;
}
byte threats(byte square) {
byte t = 0;
Board::swap_side();
t = can_capture(square);
Board::swap_side();
return t;
}
bool is_check() {
byte king_ptr = Board::field[PTR_W_KING | Board::black_moving()];
return (threats(king_ptr)) > 0;
}
bool illegal() {
// if the enemy king can be captured, move they did is illegal.
byte king_ptr = Board::field[PTR_W_KING | !Board::black_moving()];
return (can_capture(king_ptr)) > 0;
}
}
bool make_safe(Move m) {
byte sq_diff = m.sq_to - m.sq_from;
if(m.sq_from == Board::field[PTR_W_KING | Board::black_moving()] &&
abs(sq_diff) == 2
) {
// we are castling, so check if we are threatened in the square the king moves through
byte sq = m.sq_from + (sq_diff/2);
if(Threat::is_check() || Threat::threats(sq) > 0) {
return false; // castle blocked; return failure
}
}
Board::make(m);
if(Threat::illegal()) {
Board::unmake();
return false;
}
return true;
}
#endif

27
Types.h
View File

@ -1,7 +1,7 @@
#ifndef __TYPES_H_INC
#define __TYPES_H_INC
#include "Config.h""
#include "Config.h"
#ifdef _ACF_DEBUG_PRINT
#define DEBUG(x) Serial.println(F(x)); Serial.flush()
@ -40,4 +40,29 @@ char piece_to_char(Piece p) {
return CHAR_STRS[p];
}
struct Move {
byte sq_from; // 0x88
byte sq_to; // 0x88
Piece pc_prom; // 0b(4unused)(1color)(3type)
};
//Move move_from_str(char* s) {
//}
void print_sq(byte sq) {
Serial.print((char)((sq & 0x07) + 'a'));
Serial.print((char)(((sq & 0x70) >> 4) + '1'));
}
void print_move(Move m) {
if(m.sq_from == 0xFF) {
Serial.print(F("INVALID_MOVE"));
return;
}
print_sq(m.sq_from);
print_sq(m.sq_to);
Piece pc = (Piece)(m.pc_prom & 0x7);
if(pc != 0) Serial.print(piece_to_char(pc));
}
#endif

128
Uci.h Normal file
View File

@ -0,0 +1,128 @@
#ifndef __UCI_H_INC
#define __UCI_H_INC
#include "Tasks.h"
#define PS2(s) ([]{ static const char c[] PROGMEM = (s); return &c[0]; }())
namespace Uci {
typedef void uci_return;
typedef uci_return (*uci_handler)();
struct uci_cmd {
const char* command;
uci_handler handler;
};
void clear_line() {
int peek = Serial.peek();
if(peek == -1) return;
do {
peek = Serial.read();
} while(peek != '\n');
}
uci_return uci_hello() {
Serial.println(F("id name ArduChess\nid author Quinten Kock\nuciok"));
}
uci_return uci_unimpl() {
Serial.println(F("Function not implemented yet"));
}
uci_return uci_unknown() {
Serial.println(F("Not an UCI command"));
}
uci_return uci_perft() {
int depth = Serial.parseInt();
unsigned long startTime = millis();
unsigned long result = divide(depth);
unsigned long elapsed = millis() - startTime;
Serial.print(F("perft("));
Serial.print(depth);
Serial.print(F(") result: "));
Serial.println(result);
Serial.print(F("Completed in "));
Serial.print(elapsed);
Serial.println(F("ms"));
}
const char UCI_COMMAND_uci[] PROGMEM = "uci";
const char UCI_COMMAND_debug[] PROGMEM = "debug";
const char UCI_COMMAND_isready[] PROGMEM = "isready";
const char UCI_COMMAND_setoption[] PROGMEM = "setoption";
const char UCI_COMMAND_ucinewgame[] PROGMEM = "ucinewgame";
const char UCI_COMMAND_position[] PROGMEM = "position";
const char UCI_COMMAND_go[] PROGMEM = "go";
const char UCI_COMMAND_stop[] PROGMEM = "stop";
const char UCI_COMMAND_ponderhit[] PROGMEM = "ponderhit";
const char UCI_COMMAND_quit[] PROGMEM = "quit";
const char UCI_COMMAND_bench[] PROGMEM = "bench";
const char UCI_COMMAND_perft[] PROGMEM = "perft";
const uci_cmd UCI_COMMANDS[] PROGMEM = {
{UCI_COMMAND_uci, &uci_hello},
{UCI_COMMAND_debug, &uci_unimpl},
{UCI_COMMAND_isready, &uci_unimpl},
{UCI_COMMAND_setoption, &uci_unimpl},
{UCI_COMMAND_ucinewgame, &uci_unimpl},
{UCI_COMMAND_position, &uci_unimpl},
{UCI_COMMAND_go, &uci_unimpl},
{UCI_COMMAND_stop, &uci_unimpl},
{UCI_COMMAND_ponderhit, &uci_unimpl},
{UCI_COMMAND_quit, &uci_unimpl},
{UCI_COMMAND_bench, &bench},
{UCI_COMMAND_perft, &uci_perft},
};
const uci_cmd UCI_INVALID = {PS2(""), &uci_unknown};
uci_cmd get_uci_command(const char* command) {
size_t command_num = sizeof(UCI_COMMANDS) / sizeof(uci_cmd);
for(size_t i = 0; i < command_num; i++) {
size_t ci = 0;
uci_cmd to_try;
memcpy_P(&to_try, &UCI_COMMANDS[i], sizeof(uci_cmd));
while(true) {
char reference = pgm_read_byte_near(to_try.command + ci);
if(reference != command[ci]) {
break;
}
if(command[ci] == '\0') {
return to_try;
}
ci++;
}
}
return UCI_INVALID;
}
String read_word() {
int incoming = Serial.read();
String str = String();
do {
if(incoming != -1) {
str += (char)incoming;
}
incoming = Serial.read();
} while(incoming != '\n' && incoming != ' ');
return str;
}
uci_return handle_uci() {
if(Serial.available()) {
// There is input available; so likely a command
String command = read_word();
uci_cmd handler = get_uci_command(command.c_str());
handler.handler();
clear_line();
}
}
}
#endif