Compare commits
2 Commits
21e956519e
...
5507db23dd
| Author | SHA1 | Date |
|---|---|---|
|
|
5507db23dd | |
|
|
e00f9231fb |
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
88
Board.h
88
Board.h
|
|
@ -3,9 +3,10 @@
|
|||
|
||||
#include "Types.h"
|
||||
#include "Panic.h"
|
||||
#include "Move.h"
|
||||
|
||||
#define BOARD_DEFAULT_VALUE { \
|
||||
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, \
|
||||
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, \
|
||||
|
|
@ -14,31 +15,31 @@
|
|||
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
|
||||
// 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_ENPASSANT 0x18
|
||||
#define PTR_REVMOV 0x19
|
||||
// free space
|
||||
|
||||
#define PTR_UNMOVE_START 0x28
|
||||
#define PTR_UNMOVE_LAST 0x7F
|
||||
#define PTR_UNMOVE_START 0x28
|
||||
#define PTR_UNMOVE_LAST 0x7F
|
||||
|
||||
byte field[128];
|
||||
byte PTR_UNMOVE;
|
||||
byte field[128];
|
||||
byte PTR_UNMOVE;
|
||||
|
||||
const byte field_default_value[] PROGMEM = BOARD_DEFAULT_VALUE;
|
||||
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);
|
||||
}
|
||||
|
|
@ -48,31 +49,31 @@ void board_init() {
|
|||
field[PTR_B_KING] = 0x74; // e8
|
||||
long* zob = (long*)&field[PTR_ZOBRIST];
|
||||
*zob = 0xDEADBEEF;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
struct Unmove {
|
||||
struct Unmove {
|
||||
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
|
||||
};
|
||||
};
|
||||
|
||||
bool black_moving() {
|
||||
bool black_moving() {
|
||||
return field[PTR_SIDE_AND_CASTLERIGHT] & 0x1;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long get_zobrist() {
|
||||
unsigned long get_zobrist() {
|
||||
long* addr = (long*) &field[PTR_ZOBRIST];
|
||||
return *addr;
|
||||
}
|
||||
}
|
||||
|
||||
void reset_unmake_stack() {
|
||||
void reset_unmake_stack() {
|
||||
PTR_UNMOVE = PTR_UNMOVE_START;
|
||||
}
|
||||
}
|
||||
|
||||
void next_unmove() {
|
||||
void next_unmove() {
|
||||
PTR_UNMOVE++;
|
||||
if(PTR_UNMOVE > PTR_UNMOVE_LAST) {
|
||||
panic(F("Unmove stack overflow"));
|
||||
|
|
@ -80,8 +81,8 @@ void next_unmove() {
|
|||
if(!(PTR_UNMOVE & 0x8)) {
|
||||
PTR_UNMOVE += 0x8;
|
||||
}
|
||||
}
|
||||
void prev_unmove() {
|
||||
}
|
||||
void prev_unmove() {
|
||||
PTR_UNMOVE--;
|
||||
if(PTR_UNMOVE < PTR_UNMOVE_START) {
|
||||
panic(F("Unmaking from empty stack"));
|
||||
|
|
@ -89,15 +90,15 @@ void prev_unmove() {
|
|||
if(!(PTR_UNMOVE & 0x8)) {
|
||||
PTR_UNMOVE -= 0x8;
|
||||
}
|
||||
}
|
||||
void 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 read_unmove() {
|
||||
}
|
||||
Unmove read_unmove() {
|
||||
Unmove u;
|
||||
byte* ptr = (byte*) &u;
|
||||
for(int i = sizeof(u) - 1; i >= 0; i--) {
|
||||
|
|
@ -108,10 +109,10 @@ Unmove read_unmove() {
|
|||
#endif
|
||||
}
|
||||
return u;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void print() {
|
||||
void print() {
|
||||
Serial.println(F("BOARD:"));
|
||||
for(char i = 7; i >= 0; i--) {
|
||||
for(byte j = 0; j < 16; j++) {
|
||||
|
|
@ -122,9 +123,9 @@ void print() {
|
|||
}
|
||||
Serial.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void make(Move m) {
|
||||
void make(Move m) {
|
||||
// TODO zobrist?
|
||||
|
||||
// fill unmove struct with basic data
|
||||
|
|
@ -231,9 +232,9 @@ void make(Move m) {
|
|||
field[PTR_SIDE_AND_CASTLERIGHT] ^= 0x01;
|
||||
|
||||
store_unmove(u);
|
||||
}
|
||||
}
|
||||
|
||||
void unmake() {
|
||||
void unmake() {
|
||||
Unmove u = read_unmove();
|
||||
field[PTR_REVMOV] = u.revmov;
|
||||
|
||||
|
|
@ -286,6 +287,7 @@ void unmake() {
|
|||
|
||||
field[PTR_SIDE_AND_CASTLERIGHT] ^= 0x01;
|
||||
field[PTR_ENPASSANT] = u.captured >> 4;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
15
Move.h
15
Move.h
|
|
@ -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
|
||||
39
Movegen.h
39
Movegen.h
|
|
@ -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)};
|
||||
|
|
|
|||
3
Tasks.h
3
Tasks.h
|
|
@ -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
10
Types.h
|
|
@ -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
|
||||
|
|
|
|||
78
Uci.h
78
Uci.h
|
|
@ -5,59 +5,67 @@
|
|||
|
||||
#define PS2(s) ([]{ static const char c[] PROGMEM = (s); return &c[0]; }())
|
||||
|
||||
typedef void uci_return;
|
||||
typedef uci_return (*uci_handler)();
|
||||
namespace Uci {
|
||||
typedef void uci_return;
|
||||
typedef uci_return (*uci_handler)();
|
||||
|
||||
struct uci_cmd {
|
||||
struct uci_cmd {
|
||||
const char* command;
|
||||
uci_handler handler;
|
||||
};
|
||||
};
|
||||
|
||||
void clear_line() {
|
||||
void clear_line() {
|
||||
int peek = Serial.peek();
|
||||
if(peek == -1) return;
|
||||
|
||||
do {
|
||||
peek = Serial.read();
|
||||
} while(peek != '\n');
|
||||
}
|
||||
}
|
||||
|
||||
uci_return uci_hello() {
|
||||
uci_return uci_hello() {
|
||||
Serial.println(F("id name ArduChess\nid author Quinten Kock\nuciok"));
|
||||
}
|
||||
}
|
||||
|
||||
uci_return uci_unimpl() {
|
||||
uci_return uci_unimpl() {
|
||||
Serial.println(F("Function not implemented yet"));
|
||||
}
|
||||
}
|
||||
|
||||
uci_return uci_unknown() {
|
||||
uci_return uci_unknown() {
|
||||
Serial.println(F("Not an UCI command"));
|
||||
}
|
||||
}
|
||||
|
||||
uci_return uci_perft() {
|
||||
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";
|
||||
const char UCI_COMMAND_debug[] PROGMEM = "debug";
|
||||
const char UCI_COMMAND_isready[] PROGMEM = "isready";
|
||||
const char UCI_COMMAND_setoption[] PROGMEM = "setoption";
|
||||
const char UCI_COMMAND_ucinewgame[] PROGMEM = "ucinewgame";
|
||||
const char UCI_COMMAND_position[] PROGMEM = "position";
|
||||
const char UCI_COMMAND_go[] PROGMEM = "go";
|
||||
const char UCI_COMMAND_stop[] PROGMEM = "stop";
|
||||
const char UCI_COMMAND_ponderhit[] PROGMEM = "ponderhit";
|
||||
const char UCI_COMMAND_quit[] PROGMEM = "quit";
|
||||
const char UCI_COMMAND_bench[] PROGMEM = "bench";
|
||||
const char UCI_COMMAND_perft[] PROGMEM = "perft";
|
||||
const char UCI_COMMAND_uci[] PROGMEM = "uci";
|
||||
const char UCI_COMMAND_debug[] PROGMEM = "debug";
|
||||
const char UCI_COMMAND_isready[] PROGMEM = "isready";
|
||||
const char UCI_COMMAND_setoption[] PROGMEM = "setoption";
|
||||
const char UCI_COMMAND_ucinewgame[] PROGMEM = "ucinewgame";
|
||||
const char UCI_COMMAND_position[] PROGMEM = "position";
|
||||
const char UCI_COMMAND_go[] PROGMEM = "go";
|
||||
const char UCI_COMMAND_stop[] PROGMEM = "stop";
|
||||
const char UCI_COMMAND_ponderhit[] PROGMEM = "ponderhit";
|
||||
const char UCI_COMMAND_quit[] PROGMEM = "quit";
|
||||
const char UCI_COMMAND_bench[] PROGMEM = "bench";
|
||||
const char UCI_COMMAND_perft[] PROGMEM = "perft";
|
||||
|
||||
|
||||
const uci_cmd UCI_COMMANDS[] PROGMEM = {
|
||||
const uci_cmd UCI_COMMANDS[] PROGMEM = {
|
||||
{UCI_COMMAND_uci, &uci_hello},
|
||||
{UCI_COMMAND_debug, &uci_unimpl},
|
||||
{UCI_COMMAND_isready, &uci_unimpl},
|
||||
|
|
@ -70,11 +78,11 @@ const uci_cmd UCI_COMMANDS[] PROGMEM = {
|
|||
{UCI_COMMAND_quit, &uci_unimpl},
|
||||
{UCI_COMMAND_bench, &bench},
|
||||
{UCI_COMMAND_perft, &uci_perft},
|
||||
};
|
||||
};
|
||||
|
||||
const uci_cmd UCI_INVALID = {PS2(""), &uci_unknown};
|
||||
const uci_cmd UCI_INVALID = {PS2(""), &uci_unknown};
|
||||
|
||||
uci_cmd get_uci_command(const char* command) {
|
||||
uci_cmd get_uci_command(const char* command) {
|
||||
size_t command_num = sizeof(UCI_COMMANDS) / sizeof(uci_cmd);
|
||||
for(size_t i = 0; i < command_num; i++) {
|
||||
size_t ci = 0;
|
||||
|
|
@ -92,9 +100,9 @@ uci_cmd get_uci_command(const char* command) {
|
|||
}
|
||||
}
|
||||
return UCI_INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
String read_word() {
|
||||
String read_word() {
|
||||
int incoming = Serial.read();
|
||||
String str = String();
|
||||
do {
|
||||
|
|
@ -104,9 +112,9 @@ String read_word() {
|
|||
incoming = Serial.read();
|
||||
} while(incoming != '\n' && incoming != ' ');
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
uci_return handle_uci() {
|
||||
uci_return handle_uci() {
|
||||
if(Serial.available()) {
|
||||
// There is input available; so likely a command
|
||||
String command = read_word();
|
||||
|
|
@ -114,7 +122,7 @@ uci_return handle_uci() {
|
|||
handler.handler();
|
||||
clear_line();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Reference in New Issue