Compare commits

...

4 Commits

3 changed files with 79 additions and 53 deletions

View File

@ -17,6 +17,9 @@ void setup() {
value = Serial.read(); value = Serial.read();
b.field[0x32] = char_to_piece(value); b.field[0x32] = char_to_piece(value);
Move m = {0x14, 0x34, P_EMPTY};
b.make(m);
} }
void loop() { void loop() {
@ -26,4 +29,6 @@ void loop() {
Serial.println(b.get_zobrist()); Serial.println(b.get_zobrist());
b.print(); b.print();
Serial.println(sizeof(b)); Serial.println(sizeof(b));
Move m = {0x14, 0x34, P_EMPTY};
b.make(m);
} }

120
Board.h
View File

@ -2,27 +2,23 @@
#define __BOARD_H_INC #define __BOARD_H_INC
#include "Types.h" #include "Types.h"
#include "Panic.h"
#include "Move.h"
struct Unmake { struct Unmove {
byte sq_from; byte sq_from;
byte sq_to; byte sq_to;
byte enpassant; byte enpassant;
}; };
class Board { class Board {
public: public:
Board(); Board();
void print() { void make(Move m);
for(char i = 7; i >= 0; i--) { void unmove();
for(byte j = 0; j < 16; j++) {
Serial.print(field[i*16 + j], HEX); void print();
Serial.print(" ");
}
Serial.println();
}
}
unsigned long get_zobrist() { unsigned long get_zobrist() {
long* addr = (long*) &field[PTR_ZOBRIST]; long* addr = (long*) &field[PTR_ZOBRIST];
@ -42,7 +38,7 @@ class Board {
private: private:
static const byte PTR_SIDE_AND_CASTLERIGHT = 0x08; //byte (1=side, 2,4=white castle, 8,16=black) static const byte PTR_SIDE_AND_CASTLERIGHT = 0x08; //byte (1=side, 2,4=white castle, 8,16=black)
static const byte PTR_PTR_UNMAKE = 0x09; //byte (points to index) // CAN FILL 0x09
static const byte PTR_W_KING = 0x0A; // byte (points to index or maybe 64-arr index) 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) // const byte PTR_B_KING = 0x0B; (PTR_W_KING | COLOR or PTR_W_KING + COLOR)
static const byte PTR_ZOBRIST = 0x0C; // 4 bytes static const byte PTR_ZOBRIST = 0x0C; // 4 bytes
@ -52,49 +48,17 @@ class Board {
static const byte PTR_ENPASSANT = 0x18; static const byte PTR_ENPASSANT = 0x18;
static const byte PTR_REVMOV = 0x19; static const byte PTR_REVMOV = 0x19;
// free space
static const byte PTR_UNMAKE_START = 0x28; static const byte PTR_UNMOVE_START = 0x28;
static const byte PTR_UNMAKE_LAST = 0x7F; static const byte PTR_UNMOVE_LAST = 0x7E;
inline byte get_unmake() __attribute__((always_inline)) { byte PTR_UNMOVE = PTR_UNMOVE_START;
return field[PTR_PTR_UNMAKE];
}
byte next_unmake() {
field[PTR_PTR_UNMAKE]++;
if(get_unmake() > PTR_UNMAKE_LAST) {
field[PTR_PTR_UNMAKE] = PTR_UNMAKE_START;
return PTR_UNMAKE_START;
}
if(!(get_unmake() & 0x8)) {
field[PTR_PTR_UNMAKE] += 0x8;
}
return get_unmake();
}
byte prev_unmake() {
field[PTR_PTR_UNMAKE]--;
if(get_unmake() < PTR_UNMAKE_START) {
field[PTR_PTR_UNMAKE] = PTR_UNMAKE_LAST;
return PTR_UNMAKE_LAST;
}
if(!(get_unmake() & 0x8)) {
field[PTR_PTR_UNMAKE] -= 0x8;
}
return get_unmake();
}
void store_unmake(Unmake *u) { void next_unmove();
field[get_unmake()] = *(byte*)u; void prev_unmove();
for(size_t i = 0; i < sizeof(u); i++) {
field[next_unmake()] = *(byte*)(u+i); void store_unmove(Unmove u);
} Unmove read_unmove();
}
Unmake read_unmake() {
Unmake u;
byte* ptr = (byte*) &u;
for(int i = sizeof(u) - 1; i >= 0; i++) {
ptr[i] = field[prev_unmake()];
}
return u;
}
}; };
@ -104,5 +68,55 @@ Board::Board() {
field[PTR_ZOBRIST+1] = 1; field[PTR_ZOBRIST+1] = 1;
} }
void Board::print() {
for(char i = 7; i >= 0; i--) {
for(byte j = 0; j < 16; j++) {
Serial.print(field[i*16 + j], HEX);
Serial.print(" ");
}
Serial.println();
}
}
void Board::make(Move m) {
Unmove u = Unmove {0xAA, 0xBB, 0xCC};
store_unmove(u);
Unmove u2 = read_unmove();
Serial.println(u2.sq_to, HEX);
}
void Board::next_unmove() {
PTR_UNMOVE++;
if(PTR_UNMOVE > PTR_UNMOVE_LAST) {
panic(F("Unmove stack overflow"));
}
if(!(PTR_UNMOVE & 0x8)) {
PTR_UNMOVE += 0x8;
}
}
void Board::prev_unmove() {
PTR_UNMOVE--;
if(PTR_UNMOVE < PTR_UNMOVE_START) {
panic(F("Unmaking from empty stack"));
}
if(!(PTR_UNMOVE & 0x8)) {
PTR_UNMOVE -= 0x8;
}
}
void Board::store_unmove(Unmove u) {
byte *ub = (byte*) &u;
for(size_t i = 0; i < sizeof(u); i++) {
next_unmove();
field[PTR_UNMOVE] = ub[i];
}
}
Unmove Board::read_unmove() {
Unmove u;
byte* ptr = (byte*) &u;
for(int i = sizeof(u) - 1; i >= 0; i--) {
ptr[i] = field[PTR_UNMOVE];
prev_unmove();
}
return u;
}
#endif #endif

7
Panic.h Normal file
View File

@ -0,0 +1,7 @@
void panic(const __FlashStringHelper* message) {
while(true) {
Serial.println(F("PANIC!"));
Serial.println(message);
delay(1000);
}
}