Compare commits

...

2 Commits

Author SHA1 Message Date
Quinten Kock 5507db23dd Remove Move.h (moved to Types.h) 2020-06-20 03:38:03 +02:00
Quinten Kock e00f9231fb Put more stuff in namespaces 2020-06-20 03:28:30 +02:00
7 changed files with 403 additions and 399 deletions

View File

@ -5,14 +5,11 @@
#include "Config.h"
#include "Board.h"
#include "Move.h"
#include "Movegen.h"
#include "Types.h"
#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();
@ -21,5 +18,5 @@ void setup() {
void loop() {
// put your main code here, to run repeatedly:
handle_uci();
Uci::handle_uci();
}

View File

@ -3,7 +3,8 @@
#include "Types.h"
#include "Panic.h"
#include "Move.h"
namespace Board {
#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, \
@ -38,7 +39,7 @@ byte PTR_UNMOVE;
const byte field_default_value[] PROGMEM = BOARD_DEFAULT_VALUE;
void board_init() {
void init() {
for(int i = 0; i < 128; i++) {
field[i] = pgm_read_byte_near(field_default_value + i);
}
@ -287,6 +288,7 @@ void unmake() {
field[PTR_SIDE_AND_CASTLERIGHT] ^= 0x01;
field[PTR_ENPASSANT] = u.captured >> 4;
}
}
#endif

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,7 +2,6 @@
#define __MOVEGEN_H_INC
#include "Types.h"
#include "Move.h"
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};
@ -28,11 +27,11 @@ 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;
@ -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,7 +134,7 @@ Move Movegen::generate_pawn() {
direction = 1; // next try, go ahead further
offset = color ? -0x10 : 0x10;
target_square = square + offset;
if(field[target_square] ||
if(Board::field[target_square] ||
(square & 0x70) == (color ? 0x10 : 0x60)
) {
// moving ahead is not possible, not even a capture!
@ -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)};

View File

@ -1,6 +1,9 @@
#ifndef __TASKS_H_INC
#define __TASKS_H_INC
#include "Types.h"
#include "Movegen.h"
unsigned long pseudo_perft(byte depth) {
// only checks pseudolegality
// but, it should work overall

10
Types.h
View File

@ -40,4 +40,14 @@ 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) {
//}
#endif

10
Uci.h
View File

@ -5,6 +5,7 @@
#define PS2(s) ([]{ static const char c[] PROGMEM = (s); return &c[0]; }())
namespace Uci {
typedef void uci_return;
typedef uci_return (*uci_handler)();
@ -36,11 +37,18 @@ uci_return uci_unknown() {
uci_return uci_perft() {
int depth = Serial.parseInt();
unsigned long startTime = millis();
unsigned long result = pseudo_perft(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";
@ -115,6 +123,6 @@ uci_return handle_uci() {
clear_line();
}
}
}
#endif