diff --git a/ArduChess.ino b/ArduChess.ino index 805f350..5c7723e 100644 --- a/ArduChess.ino +++ b/ArduChess.ino @@ -17,9 +17,6 @@ void setup() { value = Serial.read(); b.field[0x32] = char_to_piece(value); - - Move m = {0x14, 0x34, P_EMPTY}; - b.make(m); } void loop() { @@ -27,8 +24,16 @@ void loop() { Serial.println(F("hello")); delay(1000); Serial.println(b.get_zobrist()); - b.print(); Serial.println(sizeof(b)); + Move m = {0x14, 0x34, P_EMPTY}; + Serial.println(F("Initial board")); + b.print(); b.make(m); + Serial.println(F("Board after e2e4")); + b.print(); + b.unmake(); + Serial.println(F("Board after unmake")); + b.print(); + delay(1000); } diff --git a/Board.h b/Board.h index 7d05e5d..7435f20 100644 --- a/Board.h +++ b/Board.h @@ -8,6 +8,7 @@ struct Unmove { byte sq_from; byte sq_to; + byte captured; byte enpassant; }; @@ -16,10 +17,14 @@ class Board { Board(); void make(Move m); - void unmove(); + 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; @@ -36,7 +41,14 @@ class Board { 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) @@ -51,14 +63,9 @@ class Board { // free space static const byte PTR_UNMOVE_START = 0x28; - static const byte PTR_UNMOVE_LAST = 0x7E; + static const byte PTR_UNMOVE_LAST = 0x7F; byte PTR_UNMOVE = PTR_UNMOVE_START; - void next_unmove(); - void prev_unmove(); - - void store_unmove(Unmove u); - Unmove read_unmove(); }; @@ -77,11 +84,47 @@ void Board::print() { Serial.println(); } } + void Board::make(Move m) { - Unmove u = Unmove {0xAA, 0xBB, 0xCC}; + Unmove u; + u.captured = field[m.sq_to]; + u.sq_from = m.sq_from; + u.sq_to = m.sq_to; + + byte new_val = m.pc_prom & 0b1111; + if(new_val != P_EMPTY) { + u.sq_to |= 0x80; + } else { + new_val = field[m.sq_from]; + } + field[m.sq_to] = field[m.sq_from]; + field[m.sq_from] = P_EMPTY; + + // TODO handle castling + // TODO handle castling rights + // TODO handle revmov clock + // TODO handle enpassant + + field[PTR_SIDE_AND_CASTLERIGHT] ^= 0x01; + store_unmove(u); - Unmove u2 = read_unmove(); - Serial.println(u2.sq_to, HEX); +} + +void Board::unmake() { + field[PTR_SIDE_AND_CASTLERIGHT] ^= 0x01; + + Unmove u = read_unmove(); + if((u.sq_to & 0x80) == 0) { + // regular move + field[u.sq_from] = field[u.sq_to]; + } else { + // piece was promoted + // so the source is a pawn + field[u.sq_from] = W_PAWN | black_moving() << 3; + } + + field[u.sq_to] = u.captured & 0b1111; + } void Board::next_unmove() { @@ -102,7 +145,6 @@ void Board::prev_unmove() { PTR_UNMOVE -= 0x8; } } - void Board::store_unmove(Unmove u) { byte *ub = (byte*) &u; for(size_t i = 0; i < sizeof(u); i++) {