From 36a4ff5676ef0cfa0e75012204894ff1ebac1a17 Mon Sep 17 00:00:00 2001 From: Quinten Kock Date: Thu, 18 Jun 2020 20:47:11 +0200 Subject: [PATCH] Better board_init, implement basic castling in unmake --- Board.h | 66 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/Board.h b/Board.h index 05592de..a685923 100644 --- a/Board.h +++ b/Board.h @@ -16,21 +16,11 @@ B_ROOK, B_KNGT, B_BSHP, B_QUEN, B_KING, B_BSHP, B_KNGT, B_ROOK, 0, 0, 0, 0, 0, 0, 0, 0, \ }; - -byte field[128]; -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); - } -} - // 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) -// const byte PTR_B_KING = 0x0B; (PTR_W_KING | COLOR or PTR_W_KING + COLOR) +#define PTR_B_KING 0x0B // (PTR_W_KING | COLOR or PTR_W_KING + COLOR) #define PTR_ZOBRIST 0x0C // 4 bytes // 0x0D // 0x0E @@ -42,11 +32,28 @@ void board_init() { #define PTR_UNMOVE_START 0x28 #define PTR_UNMOVE_LAST 0x7F + +byte field[128]; byte PTR_UNMOVE = PTR_UNMOVE_START; +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); + } + 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_to; // 0b(1promoted?)(3rank)(1ep_capture?)(3file) byte captured; // 0b(4enpassantinfo)(1color)(3piecetype) byte revmov; // 8bit integer }; @@ -117,7 +124,6 @@ void print() { } void make(Move m) { - // TODO handle revmov clock // TODO zobrist? // fill unmove struct with basic data @@ -136,7 +142,7 @@ void make(Move m) { } // Calculate the move 'amount' (unique signature for dx,dy) - int sq_diff = (int)m.sq_from - (int)m.sq_to; + int sq_diff = (int)m.sq_to - (int)m.sq_from; int sq_diff_abs = abs(sq_diff); @@ -158,17 +164,18 @@ void make(Move m) { // 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) + // TODO handle castle rights and unmake + if(m.sq_from == 0x00) // white queenside rook field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b00010; - if(m.sq_from == 0x07) + if(m.sq_from == 0x07) // white kingside rook field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b00100; - if(m.sq_from == 0x04) + if(m.sq_from == 0x04) // white king field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b00110; - if(m.sq_from == 0x70) + if(m.sq_from == 0x70) // black queenside rook field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b01000; - if(m.sq_from == 0x77) + if(m.sq_from == 0x77) // black kingside rook field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b10000; - if(m.sq_from == 0x74) + if(m.sq_from == 0x74) // black king field[PTR_SIDE_AND_CASTLERIGHT] &= ~0b11000; @@ -192,7 +199,7 @@ void make(Move m) { // handle enpassant setup (double pawn move) if( piece_type == W_PAWN && - (sq_diff_abs == 32) + sq_diff_abs == 32 ) { // we are doing a pawn double-move. // therefore, it allows enpassant in the next move. @@ -244,6 +251,23 @@ void unmake() { // also undo the regular move field[u.sq_from] = field[sq_to]; } + // TODO handle castling rights + + int sq_diff = (int)sq_to - (int)u.sq_from; + int sq_diff_abs = abs(sq_diff); + if((field[u.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 = u.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;