141 lines
3.3 KiB
C
141 lines
3.3 KiB
C
#include <assert.h>
|
|
#include <ctype.h>
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include "moves.h"
|
|
#include "types.h"
|
|
#include "help.h"
|
|
#include "eval.h"
|
|
|
|
#define BUFF_SIZE 4096
|
|
|
|
game *fenGame(char *str);
|
|
void playMoves(game *g, char *moves);
|
|
long long *findSet(game *g, long long bit);
|
|
long long *charToSet(game *g, char c);
|
|
|
|
int cmain() {
|
|
setbuf(stdin, NULL);
|
|
setbuf(stdout, NULL);
|
|
game *g = NULL;
|
|
|
|
char line[BUFF_SIZE];
|
|
char *lineRest, *token;
|
|
char ltz[] = "abcdefgh";
|
|
int cnt = 0;
|
|
while (1) {
|
|
if(!fgets(line, sizeof(line), stdin))
|
|
return 0;
|
|
size_t len = strlen(line);
|
|
if (len - 1)
|
|
line[len - 1] = '\0';
|
|
token = strtok_r(line, " ", &lineRest);
|
|
if (!strcmp("uci", token)) {
|
|
printf("id name RatChess 0.0\n");
|
|
printf("id author rat<3\n");
|
|
printf("uciok\n");
|
|
} else if (!strcmp("quit", token)) {
|
|
return 0;
|
|
} else if (!strcmp("setoption", token)) {
|
|
} else if (!strcmp("position", token)) {
|
|
token = strtok_r(lineRest, " ", &lineRest);
|
|
if (!strcmp("fen", token)) {
|
|
g = fenGame(lineRest + 1);
|
|
for (int i = 0; i < 6; i++)
|
|
token = strtok_r(lineRest, " ", &lineRest);
|
|
} else {
|
|
g = fenGame("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
|
|
}
|
|
playMoves(g, lineRest);
|
|
} else if (!strcmp("ucinewgame", token)) {
|
|
} else if (!strcmp("isready", token)) {
|
|
printf("readyok\n");
|
|
} else if (!strcmp("go", token)) {
|
|
static move mov[1500];//big dumb buffer
|
|
size_t cnt = 0;
|
|
cnt += pawnMove(g,mov);
|
|
cnt += knightMove(g,(mov+cnt));
|
|
cnt += rookMove(g,(mov+cnt));
|
|
cnt += bishopMove(g,(mov+cnt));
|
|
cnt += kingMove(g,(mov+cnt));
|
|
cnt += queenMove(g,(mov+cnt));
|
|
move *m = findBest(mov,cnt,g);
|
|
if(m == NULL){
|
|
printf("bestmove 0000\n");
|
|
continue;
|
|
}
|
|
|
|
char *end = "";
|
|
int yTo, xTo, yFrom, xFrom;
|
|
xTo = m->To % 8;
|
|
yTo = m->To / 8 + 1;
|
|
xFrom = m->From % 8;
|
|
yFrom = m->From / 8 + 1;
|
|
|
|
long long bit = 1LL << m->From;
|
|
if (m->Promo) {
|
|
end = "q\n";
|
|
} else {
|
|
end = "\n";
|
|
}
|
|
printf("bestmove %c%d%c%d%s", ltz[xFrom], yFrom, ltz[xTo], yTo, end);
|
|
}
|
|
}
|
|
}
|
|
|
|
game *fenGame(char *str) {
|
|
int rank = 7;
|
|
int file = 0;
|
|
size_t pos = 0;
|
|
game *g;
|
|
g = malloc(sizeof(game));
|
|
memset(g, 0, sizeof(game));
|
|
|
|
while (str[pos] != '\0' && str[pos] != ' ') {
|
|
char current_char = *str;
|
|
|
|
if (isdigit(*str)) {
|
|
int empty_squares = *str - '0';
|
|
file += empty_squares;
|
|
str++;
|
|
continue;
|
|
}
|
|
|
|
if (current_char == '/') {
|
|
rank--;
|
|
file = 0;
|
|
str++;
|
|
continue;
|
|
}
|
|
|
|
long long bit = 1ULL << (rank * 8 + file);
|
|
long long *set = charToSet(g, *str);
|
|
if (set)
|
|
*set |= bit;
|
|
file++;
|
|
str++;
|
|
}
|
|
str++;
|
|
g->whiteToMove = (*str == 'w') ? true : false;
|
|
return g;
|
|
}
|
|
|
|
void playMoves(game *g, char *moves) {
|
|
char *moveStr;
|
|
moveStr = strtok_r(moves, " ", &moves);
|
|
moveStr = strtok_r(moves, " ", &moves);
|
|
while (moveStr) {
|
|
move m;
|
|
m.From = (moveStr[0]-'a') + (moveStr[1]-'1')*8;
|
|
m.To = (moveStr[2]-'a') + (moveStr[3]-'1')*8;
|
|
m.Promo = (strlen(moveStr) == 5) ? moveStr[4] : 0;
|
|
makeMove(g, &m);
|
|
moveStr = strtok_r(moves, " ", &moves);
|
|
}
|
|
}
|
|
|