Compare commits

..

13 Commits

6 changed files with 720 additions and 268 deletions

View File

@ -9,65 +9,175 @@
#include "Movegen.h"
#include "Types.h"
Board b = Board();
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() {
b = Board();
int startTime = micros();
unsigned long startTime = micros();
b.make({0x14, 0x34, P_EMPTY});
b.make({0x64, 0x54, P_EMPTY});
b.make({0x34, 0x44, P_EMPTY});
b.make({0x63, 0x43, P_EMPTY});
b.make({0x44, 0x53, P_EMPTY});
b.unmake();
b.unmake();
b.unmake();
b.unmake();
b.unmake();
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();
int elapsed = micros() - startTime;
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 5 moves"));
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)"));
}
void setup() {
// put your setup code here, to run once:
board_init();
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
perft_test();
bench();
Serial.println(F("hello"));
b = Board();
int startTime = micros();
b.print();
b.make({0x14, 0x34, P_EMPTY});
b.print();
b.make({0x64, 0x54, P_EMPTY});
b.print();
b.make({0x34, 0x44, P_EMPTY});
b.print();
b.make({0x63, 0x43, P_EMPTY});
b.print();
b.make({0x44, 0x53, P_EMPTY});
b.print();
b.unmake();
b.print();
b.unmake();
b.print();
b.unmake();
b.print();
b.unmake();
b.print();
b.unmake();
b.print();
int elapsed = micros() - startTime;
Serial.print(elapsed);
Serial.println(F("microseconds for 5 moves"));
}
void loop() {

456
Board.h
View File

@ -5,229 +5,74 @@
#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, \
};
// 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_ENPASSANT 0x18
#define PTR_REVMOV 0x19
// free space
#define PTR_UNMOVE_START 0x28
#define PTR_UNMOVE_LAST 0x7F
byte field[128];
byte PTR_UNMOVE;
const byte field_default_value[] PROGMEM = BOARD_DEFAULT_VALUE;
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;
}
struct Unmove {
byte sq_from; // 0b(1unused)(3rank)(1unused)(3file)
byte sq_to; // 0b(1promoted?)(3rank)(1unused)(3file)
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
};
class Board {
public:
Board();
void make(Move m);
void unmake();
void print();
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;
}
byte field[128] = {
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,
};
private:
// Private function defs
void next_unmove();
void prev_unmove();
void store_unmove(Unmove u);
Unmove read_unmove();
// 0x88-fill definitions
static const byte PTR_SIDE_AND_CASTLERIGHT = 0x08; //byte (1=side, 2,4=white castle, 8,16=black)
// CAN FILL 0x09
static const byte PTR_W_KING = 0x0A; // byte (points to index or maybe 64-arr index)
// const byte PTR_B_KING = 0x0B; (PTR_W_KING | COLOR or PTR_W_KING + COLOR)
static const byte PTR_ZOBRIST = 0x0C; // 4 bytes
// 0x0D
// 0x0E
// 0x0F
static const byte PTR_ENPASSANT = 0x18;
static const byte PTR_REVMOV = 0x19;
// free space
static const byte PTR_UNMOVE_START = 0x28;
static const byte PTR_UNMOVE_LAST = 0x7F;
byte PTR_UNMOVE = PTR_UNMOVE_START;
};
Board::Board() {
// Then, other data we need to store is assigned 0-fields.
field[PTR_ZOBRIST+1] = 1;
bool black_moving() {
return field[PTR_SIDE_AND_CASTLERIGHT] & 0x1;
}
void Board::print() {
Serial.println(F("BOARD:"));
for(char i = 7; i >= 0; i--) {
for(byte j = 0; j < 16; j++) {
if(j == 8)
Serial.print("| ");
Serial.print(field[i*16 + j], HEX);
Serial.print(" ");
}
Serial.println();
}
unsigned long get_zobrist() {
long* addr = (long*) &field[PTR_ZOBRIST];
return *addr;
}
void Board::make(Move m) {
// TODO handle revmov clock
// 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;
if(u.captured || piece_type == W_PAWN) {
field[PTR_REVMOV]++;
} else {
field[PTR_REVMOV] = 0;
}
// Calculate the move 'amount' (unique signature for dx,dy)
int sq_diff = (int)m.sq_from - (int)m.sq_to;
int sq_diff_abs = abs(sq_diff);
// TODO test the csatling code
// 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 = 0x70*black_moving();
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
// We are doing the simple way:
// unset it any time a move is made from the original position.
if(m.sq_from == 0x00)
field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b00010;
if(m.sq_from == 0x07)
field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b00100;
if(m.sq_from == 0x04)
field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b00110;
if(m.sq_from == 0x70)
field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b01000;
if(m.sq_from == 0x77)
field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b10000;
if(m.sq_from == 0x74)
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) == (0x50 - 0x30*black_moving())
) {
// all EP-conditions are met
// therefore, delete the EP-vurnerable pawn
byte ep_field = m.sq_to - 16 + 32*black_moving();
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(new_val != P_EMPTY) {
// promoting; indicate this in the sq_to byte in unmove.
u.sq_to |= 0x80;
} else {
// not promoting; so keep the same piece type
new_val = field[m.sq_from];
}
// copy or promote our piece.
field[m.sq_to] = new_val;
// then delete the original copy.
field[m.sq_from] = P_EMPTY;
// Switch sides
field[PTR_SIDE_AND_CASTLERIGHT] ^= 0x01;
store_unmove(u);
void reset_unmake_stack() {
PTR_UNMOVE = PTR_UNMOVE_START;
}
void Board::unmake() {
Unmove u = read_unmove();
field[PTR_REVMOV] = u.revmov;
byte sq_to = u.sq_to & 0x77;
byte prom_ep_capt = u.sq_to & 0x88;
if(prom_ep_capt == 0) {
// regular move
field[u.sq_from] = field[sq_to];
} else if (prom_ep_capt == 0x80) {
// piece was promoted
// so the source is a pawn
field[u.sq_from] = W_PAWN | (field[u.sq_to] & 0b1000);
} else if (prom_ep_capt == 0x08) {
// we did an enpassant capture
byte ep_sq = (u.sq_to & 0x07) | (u.sq_from & 0x70);
field[ep_sq] = W_PAWN | black_moving() << 3;
// also undo the regular move
field[u.sq_from] = field[sq_to];
}
field[sq_to] = u.captured & 0b1111;
field[PTR_SIDE_AND_CASTLERIGHT] ^= 0x01;
field[PTR_ENPASSANT] = u.captured >> 4;
}
void Board::next_unmove() {
void next_unmove() {
PTR_UNMOVE++;
if(PTR_UNMOVE > PTR_UNMOVE_LAST) {
panic(F("Unmove stack overflow"));
@ -236,7 +81,7 @@ void Board::next_unmove() {
PTR_UNMOVE += 0x8;
}
}
void Board::prev_unmove() {
void prev_unmove() {
PTR_UNMOVE--;
if(PTR_UNMOVE < PTR_UNMOVE_START) {
panic(F("Unmaking from empty stack"));
@ -245,14 +90,14 @@ void Board::prev_unmove() {
PTR_UNMOVE -= 0x8;
}
}
void Board::store_unmove(Unmove u) {
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 Board::read_unmove() {
Unmove read_unmove() {
Unmove u;
byte* ptr = (byte*) &u;
for(int i = sizeof(u) - 1; i >= 0; i--) {
@ -265,4 +110,185 @@ Unmove Board::read_unmove() {
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(" "));
}
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 csatling code
// 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;
}
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.
// TODO handle castle rights and unmake
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;
}
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_SIDE_AND_CASTLERIGHT] ^= 0x01;
field[PTR_ENPASSANT] = u.captured >> 4;
}
#endif

View File

@ -1,3 +1,11 @@
// CLEAR_UNMOVE is a feature that clears the unmake stack when it is not used.
// This is useful for making it more human readable
//#define _ACF_CLEAR_UNMOVE
// PANIC_BLINK makes the Arduino blink an error code when it panics.
// Costs a lot of flash though (around 700 bytes)
//#define _ACF_PANIC_BLINK
#define _ACF_DEBUG_PRINT
#define _ACF_ACTIVITY_BLINK

255
Movegen.h
View File

@ -1,4 +1,259 @@
#ifndef __MOVEGEN_H_INC
#define __MOVEGEN_H_INC
#include "Types.h"
#include "Move.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};
#define INVALID_MOVE Move{0xFF, 0xFF, P_EMPTY}
class Movegen {
public:
Move next_move();
private:
byte square = 0;
byte promote = P_EMPTY;
byte direction = 0;
byte target_square = 0;
Move generate_pawn();
Move generate_non_sliding(byte piece_type);
Move generate_sliding(byte piece_type);
};
Move Movegen::next_move() {
while(square <= 0x77) {
if(square & 0x88) square += 8;
byte piece_type = field[square] & 0x7;
if(
(field[square] & 0x7) &&
(field[square] & 0x8) == black_moving() << 3
) {
// there is an own piece to investigate
Move m;
if(piece_type == W_PAWN) {
m = generate_pawn();
} else if(piece_type & 0b0100) {
// bishop, rook and queen are 01xx.
m = generate_sliding(piece_type);
} else {
m = generate_non_sliding(piece_type);
}
if(m.sq_to != 255) {
return m;
}
}
square++;
direction = 0;
target_square = square;
}
return INVALID_MOVE;
}
Move Movegen::generate_sliding(byte piece_type) {
if(direction == 0) {
// if we can move horizontally (rook: 0b110, queen: 0b111):
// then start at the beginning, else (bishop: 0b101) start halfway
direction = piece_type & 0b10 ? 0 : 4;
}
byte offset = SLIDE_OFFSETS[direction];
target_square += offset;
while(target_square & 0x88 || promote == 0x10) {
promote = P_EMPTY;
// we leapt out of bounds, so find the next direction to move
NEXT_DIRECTION:
direction++;
if(direction >= 4 << (piece_type & 0b1)) {
// direction >= 4 for rook, direction >= 8 for bishop and queen
return INVALID_MOVE;
}
offset = SLIDE_OFFSETS[direction];
target_square = square + offset;
}
// 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];
if(target) {
// we encountered a piece! there are two outcomes here:
// second, it can be the opponent's. then, we can capture it!
if((target & 0x8) == (piece & 0x8)) {
// first, it is possible it is one of our own.
// this means we are blocked
// therefore, invalidate our direction and try again.
goto NEXT_DIRECTION;
} else {
promote = 0x10; // signal value
return Move{square, target_square, P_EMPTY};
}
}
// no obstructions means happy sliding piece :D
return Move{square, target_square, P_EMPTY};
}
Move Movegen::generate_non_sliding(byte piece_type) {
GNS_START:
if(direction >= 8) {
if(piece_type == W_KNGT) return INVALID_MOVE;
else {
// TODO implement castling
return INVALID_MOVE;
}
}
const byte* offsets = piece_type == W_KING ? SLIDE_OFFSETS : KNIGHT_OFFSETS;
target_square = square + offsets[direction];
direction++;
byte target = field[target_square];
byte piece = field[square];
if((target_square & 0x88) || (target && (target & 0x8) == (piece & 0x8))) {
// uh oh, off board or same color obstacle
goto GNS_START;
}
return Move{square, target_square, P_EMPTY};
}
Move Movegen::generate_pawn() {
// TODO: implement capture promotion
byte color = black_moving();
byte offset;
byte target;
GP_START:
switch(direction) {
case 0:
// regular move 1 ahead
direction = 1; // next try, go ahead further
offset = color ? -0x10 : 0x10;
target_square = square + offset;
if(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
// moving 2 ahead is also impossible, so dont try that.
direction = 2;
goto GP_START;
} else {
return Move{square, target_square, P_EMPTY};
}
// fall through
case 1:
// move 2 ahead
direction = 2;
offset = color ? -0x20 : 0x20;
target_square = square + offset;
if(!(field[target_square]) &&
(square & 0x70) == (color ? 0x60 : 0x10)
) {
return Move{square, target_square, P_EMPTY};
}
// no break because if this goes wrong
// we can always try the next possibility.
// fall through
case 2:
// capture left or EP-capture left
direction = 3;
offset = color ? -0x11 : 0xF;
target_square = square + offset;
target = field[target_square];
if(!(target_square & 0x88)) {
if(target && (target & 0x8) != (field[square] & 0x8)) {
// normal capture allowded
return Move{square, target_square, P_EMPTY};
} else if(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;
if(
ep_col == (target_square & 0x7) &&
(square & 0x70) == (color ? 0x30 : 0x40)
) {
// EP-capture possible
return Move{square, target_square, P_EMPTY};
}
}
}
// fall through
case 3:
// capture right or EP-capture right
direction = 4;
offset = color ? -0xF : 0x11;
target_square = square + offset;
target = field[target_square];
if(!(target_square & 0x88)) {
if(target && (target & 0x8) != (field[square] & 0x8)) {
// normal capture allowded
return Move{square, target_square, P_EMPTY};
} else if(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;
if(
ep_col == (target_square & 0x7) &&
(square & 0x70) == (color ? 0x30 : 0x40)
) {
// EP-capture possible
return Move{square, target_square, P_EMPTY};
}
}
}
// fall through
case 4:
// try promoting (queen)
direction = 5;
offset = color ? -0x10 : 0x10;
target_square = square + offset;
target = field[target_square];
if(target && (target_square & 0x70) == (color ? 0x70 : 0x00)) {
// we can promote!
return Move{square, target_square, (Piece)(W_QUEN | color << 3)};
} else {
// other promotions are also impossible, so skip them
direction = 8;
}
goto GP_START;
break;
case 5:
direction = 6;
offset = color ? -0x10 : 0x10;
target_square = square + offset;
// we can promote! we know this
// because it was possible in the previous case.
return Move{square, target_square, (Piece)(W_KNGT | color << 3)};
case 6:
direction = 7;
offset = color ? -0x10 : 0x10;
target_square = square + offset;
// we can promote! we know this
// because it was possible in the previous case.
return Move{square, target_square, (Piece)(W_ROOK | color << 3)};
case 7:
direction = 8;
offset = color ? -0x10 : 0x10;
target_square = square + offset;
// we can promote! we know this
// because it was possible in the previous case.
return Move{square, target_square, (Piece)(W_BSHP | color << 3)};
default:
// to my knowledge, all a pawn can do is
// move 1
// move 2
// (EP-)capture left
// (EP-)capture right
// and the 4 promotions
return INVALID_MOVE;
}
}
#endif

57
Panic.h
View File

@ -1,12 +1,57 @@
#ifndef __PANIC_H_INC
#define __PANIC_H_INC
void panic(const __FlashStringHelper* message) {
while(true) {
Serial.println(F("PANIC!"));
Serial.println(message);
delay(1000);
#pragma GCC push_options
#pragma GCC optimize("-Os")
#ifdef _ACF_ACTIVITY_BLINK
bool ledhi;
void blink() {
ledhi = !ledhi;
if(ledhi) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
}
#else
void blink() {}
#endif
#ifdef _ACF_PANIC_BLINK
void sos() {
while(true) {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(200);
digitalWrite(LED_BUILTIN, HIGH);
delay(150);
digitalWrite(LED_BUILTIN, LOW);
delay(150);
digitalWrite(LED_BUILTIN, HIGH);
delay(150);
digitalWrite(LED_BUILTIN, LOW);
delay(150);
digitalWrite(LED_BUILTIN, HIGH);
delay(150);
digitalWrite(LED_BUILTIN, LOW);
delay(150);
delay(2000);
}
}
#endif
void panic(const __FlashStringHelper* message) {
Serial.println(F("PANIC!"));
Serial.println(message);
#ifdef _ACF_PANIC_BLINK
sos();
#endif
while(true);
}
#pragma GCC pop_options
#endif

View File

@ -1,6 +1,14 @@
#ifndef __TYPES_H_INC
#define __TYPES_H_INC
#include "Config.h""
#ifdef _ACF_DEBUG_PRINT
#define DEBUG(x) Serial.println(F(x)); Serial.flush()
#else
#define DEBUG(x)
#endif
enum Piece: byte {
P_EMPTY = 0b0000,
P_ANY = 0b1000,