Compare commits
11 Commits
3aa41364b3
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 89e00df712 | |||
| 05a0b2b7f1 | |||
| e1fe8ee603 | |||
| eaaa0ca24d | |||
| c25de2cb55 | |||
| 9534803a60 | |||
| 5dd8f04286 | |||
| 9f06b9d86c | |||
| 76c69dbf18 | |||
| 49befc72b6 | |||
| bfe9335515 |
5
headers/eval.h
Normal file
5
headers/eval.h
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
#ifndef EVAL_H
|
||||||
|
#define EVAL_H
|
||||||
|
move *findBest(move* move, size_t size, game* g);
|
||||||
|
void makeMove(game *g, move* m);
|
||||||
|
#endif
|
||||||
@@ -3,4 +3,8 @@
|
|||||||
#include "types.h"
|
#include "types.h"
|
||||||
int pawnMove(game *g, move *moves);
|
int pawnMove(game *g, move *moves);
|
||||||
int knightMove(game *g, move* moves);
|
int knightMove(game *g, move* moves);
|
||||||
|
int rookMove(game *g, move *moves);
|
||||||
|
int bishopMove(game *g, move *moves);
|
||||||
|
int kingMove(game *g, move *moves);
|
||||||
|
int queenMove(game *g, move *moves);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ typedef struct {
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
int From;
|
int From;
|
||||||
int To;
|
int To;
|
||||||
|
char Promo;
|
||||||
} move;
|
} move;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
109
src/eval.c
Normal file
109
src/eval.c
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
#include "types.h"
|
||||||
|
#include "help.h"
|
||||||
|
#include "moves.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define PAWN_VALUE 100
|
||||||
|
#define KNIGHT_VALUE 320
|
||||||
|
#define BISHOP_VALUE 330
|
||||||
|
#define ROOK_VALUE 500
|
||||||
|
#define QUEEN_VALUE 900
|
||||||
|
#define KING_VALUE 20000
|
||||||
|
#define INF 9000000
|
||||||
|
#define MAX_DEPTH 3
|
||||||
|
|
||||||
|
int evaluateBoard(game *game);
|
||||||
|
void makeMove(game *g, move* m);
|
||||||
|
int negamax(game *g, int depth, int alpha, int beta, int color);
|
||||||
|
|
||||||
|
move *findBest(move* moves, size_t size, game* g){
|
||||||
|
int bestScore = -INF;
|
||||||
|
move *bestMove = NULL;
|
||||||
|
int color = g->whiteToMove ? 1 : -1;
|
||||||
|
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
game gg = *g;
|
||||||
|
makeMove(&gg, &moves[i]);
|
||||||
|
int score = -negamax(&gg, MAX_DEPTH, -INF, INF, -color);
|
||||||
|
if (score > bestScore || bestScore == -INF) {
|
||||||
|
bestScore = score;
|
||||||
|
bestMove = &moves[i];
|
||||||
|
printf("info score cp %d\n", g->whiteToMove ? bestScore : -bestScore);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bestMove;
|
||||||
|
}
|
||||||
|
|
||||||
|
int evaluateBoard(game *game) {
|
||||||
|
int score = 0;
|
||||||
|
score += __builtin_popcountll(game->white.pawns) * PAWN_VALUE;
|
||||||
|
score += __builtin_popcountll(game->white.knights) * KNIGHT_VALUE;
|
||||||
|
score += __builtin_popcountll(game->white.bishops) * BISHOP_VALUE;
|
||||||
|
score += __builtin_popcountll(game->white.rooks) * ROOK_VALUE;
|
||||||
|
score += __builtin_popcountll(game->white.queen) * QUEEN_VALUE;
|
||||||
|
score += __builtin_popcountll(game->white.king) * KING_VALUE;
|
||||||
|
|
||||||
|
score -= __builtin_popcountll(game->black.pawns) * PAWN_VALUE;
|
||||||
|
score -= __builtin_popcountll(game->black.knights) * KNIGHT_VALUE;
|
||||||
|
score -= __builtin_popcountll(game->black.bishops) * BISHOP_VALUE;
|
||||||
|
score -= __builtin_popcountll(game->black.rooks) * ROOK_VALUE;
|
||||||
|
score -= __builtin_popcountll(game->black.queen) * QUEEN_VALUE;
|
||||||
|
score -= __builtin_popcountll(game->black.king) * KING_VALUE;
|
||||||
|
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
|
void makeMove(game *g, move* m) {
|
||||||
|
long long from_bit = 1LL << m->From;
|
||||||
|
long long to_bit = 1LL << m->To;
|
||||||
|
|
||||||
|
long long *set = findSet(g, from_bit);
|
||||||
|
if (!set)
|
||||||
|
return;
|
||||||
|
*set &= ~from_bit;
|
||||||
|
|
||||||
|
long long *captured = findSet(g, to_bit);
|
||||||
|
if (captured) *captured &= ~to_bit;
|
||||||
|
|
||||||
|
if (m->Promo) {
|
||||||
|
char promoChar = g->whiteToMove ? toupper(m->Promo) : m->Promo;
|
||||||
|
long long *newSet = charToSet(g, promoChar);
|
||||||
|
if (newSet)
|
||||||
|
*newSet |= to_bit;
|
||||||
|
} else {
|
||||||
|
*set |= to_bit;
|
||||||
|
}
|
||||||
|
|
||||||
|
g->whiteToMove = !g->whiteToMove;
|
||||||
|
}
|
||||||
|
|
||||||
|
int negamax(game *g, int depth, int alpha, int beta, int color) {
|
||||||
|
if (depth == 0)
|
||||||
|
return color * evaluateBoard(g);
|
||||||
|
|
||||||
|
move moves[1500];
|
||||||
|
int cnt = pawnMove(g, moves);
|
||||||
|
cnt += knightMove(g, moves + cnt);
|
||||||
|
cnt += rookMove(g, moves + cnt);
|
||||||
|
cnt += bishopMove(g, moves + cnt);
|
||||||
|
cnt += kingMove(g, moves + cnt);
|
||||||
|
cnt += queenMove(g, moves + cnt);
|
||||||
|
|
||||||
|
if (cnt == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
int bestValue = -INF;
|
||||||
|
for (int i = 0; i < cnt; i++) {
|
||||||
|
game gg = *g;
|
||||||
|
makeMove(&gg, &moves[i]);
|
||||||
|
|
||||||
|
int value = -negamax(&gg, depth - 1, -beta, -alpha, -color);
|
||||||
|
|
||||||
|
if (value > bestValue) bestValue = value;
|
||||||
|
if (value > alpha) alpha = value;
|
||||||
|
if (alpha >= beta) break;
|
||||||
|
}
|
||||||
|
return bestValue;
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
long long fullSet(sets *s) {
|
long long fullSet(sets *s) {
|
||||||
return s->bishops ^ s->king ^ s->knights ^ s->pawns ^ s->queen ^ s->rooks;
|
return s->bishops | s->king | s->knights | s->pawns | s->queen | s->rooks;
|
||||||
}
|
}
|
||||||
|
|
||||||
long long fullSetBoth(game *g) {
|
long long fullSetBoth(game *g) {
|
||||||
|
|||||||
63
src/main.c
63
src/main.c
@@ -5,9 +5,11 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
#include "moves.h"
|
#include "moves.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "help.h"
|
#include "help.h"
|
||||||
|
#include "eval.h"
|
||||||
|
|
||||||
#define BUFF_SIZE 4096
|
#define BUFF_SIZE 4096
|
||||||
|
|
||||||
@@ -26,7 +28,8 @@ int main() {
|
|||||||
char ltz[] = "abcdefgh";
|
char ltz[] = "abcdefgh";
|
||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
while (1) {
|
while (1) {
|
||||||
(void)fgets(line, sizeof(line), stdin);
|
if(!fgets(line, sizeof(line), stdin))
|
||||||
|
return 0;
|
||||||
size_t len = strlen(line);
|
size_t len = strlen(line);
|
||||||
if (len - 1)
|
if (len - 1)
|
||||||
line[len - 1] = '\0';
|
line[len - 1] = '\0';
|
||||||
@@ -52,10 +55,19 @@ int main() {
|
|||||||
} else if (!strcmp("isready", token)) {
|
} else if (!strcmp("isready", token)) {
|
||||||
printf("readyok\n");
|
printf("readyok\n");
|
||||||
} else if (!strcmp("go", token)) {
|
} else if (!strcmp("go", token)) {
|
||||||
|
static move mov[1500];//big dumb buffer
|
||||||
move mov[200];
|
size_t cnt = 0;
|
||||||
int cnt = knightMove(g, mov);
|
cnt += pawnMove(g,mov);
|
||||||
move *m = &mov[0];
|
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 = "";
|
char *end = "";
|
||||||
int yTo, xTo, yFrom, xFrom;
|
int yTo, xTo, yFrom, xFrom;
|
||||||
@@ -64,9 +76,9 @@ int main() {
|
|||||||
xFrom = m->From % 8;
|
xFrom = m->From % 8;
|
||||||
yFrom = m->From / 8 + 1;
|
yFrom = m->From / 8 + 1;
|
||||||
|
|
||||||
if ((g->whiteToMove && yTo == 8) || (!g->whiteToMove && yTo == 1)) {
|
long long bit = 1LL << m->From;
|
||||||
// end = "q\n";
|
if (m->Promo) {
|
||||||
end = "\n";
|
end = "q\n";
|
||||||
} else {
|
} else {
|
||||||
end = "\n";
|
end = "\n";
|
||||||
}
|
}
|
||||||
@@ -113,31 +125,16 @@ game *fenGame(char *str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void playMoves(game *g, char *moves) {
|
void playMoves(game *g, char *moves) {
|
||||||
char *move;
|
char *moveStr;
|
||||||
move = strtok_r(moves, " ", &moves);
|
moveStr = strtok_r(moves, " ", &moves);
|
||||||
move = strtok_r(moves, " ", &moves);
|
moveStr = strtok_r(moves, " ", &moves);
|
||||||
while (move) {
|
while (moveStr) {
|
||||||
long long bit = 1LL << ((move[1] - '0' - 1) * 8 + (move[0] - 'a'));
|
move m;
|
||||||
long long *set = findSet(g, bit);
|
m.From = (moveStr[0]-'a') + (moveStr[1]-'1')*8;
|
||||||
if (!set) {
|
m.To = (moveStr[2]-'a') + (moveStr[3]-'1')*8;
|
||||||
printf("info fuck\n");
|
m.Promo = (strlen(moveStr) == 5) ? moveStr[4] : 0;
|
||||||
return;
|
makeMove(g, &m);
|
||||||
}
|
moveStr = strtok_r(moves, " ", &moves);
|
||||||
*set &= (~bit);
|
|
||||||
bit = 1LL << ((move[3] - '0' - 1) * 8 + (move[2] - 'a'));
|
|
||||||
long long *tmp = findSet(g, bit);
|
|
||||||
if (tmp)
|
|
||||||
*tmp &= (~bit);
|
|
||||||
if (strlen(move) == 5) {
|
|
||||||
char c = move[4];
|
|
||||||
if (g->whiteToMove)
|
|
||||||
c = toupper(c);
|
|
||||||
set = charToSet(g, c);
|
|
||||||
}
|
|
||||||
*set |= bit;
|
|
||||||
|
|
||||||
move = strtok_r(moves, " ", &moves);
|
|
||||||
g->whiteToMove = !g->whiteToMove;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
229
src/moves.c
229
src/moves.c
@@ -1,13 +1,18 @@
|
|||||||
#include "types.h"
|
|
||||||
#include "moves.h"
|
#include "moves.h"
|
||||||
#include "help.h"
|
#include "help.h"
|
||||||
|
#include "types.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int rookScan(game *g, move *moves, long long w, long long b);
|
||||||
|
int bishopScan(game *g, move *moves, long long w, long long b);
|
||||||
|
int kingScan(game *g, move *moves, long long w, long long b);
|
||||||
|
|
||||||
int pawnMove(game *g, move *moves) {
|
int pawnMove(game *g, move *moves) {
|
||||||
long long pawns = g->whiteToMove ? g->white.pawns : g->black.pawns;
|
long long pawns = g->whiteToMove ? g->white.pawns : g->black.pawns;
|
||||||
long long enemy = g->whiteToMove ? fullSet(&g->black) : fullSet(&g->white);
|
long long enemy = g->whiteToMove ? fullSet(&g->black) : fullSet(&g->white);
|
||||||
long long occupied = fullSetBoth(g);
|
long long occupied = fullSetBoth(g);
|
||||||
|
long long promo = (0xFFLL) << (g->whiteToMove ? 56 : 0);
|
||||||
|
|
||||||
int movdir = g->whiteToMove ? 8 : -8;
|
int movdir = g->whiteToMove ? 8 : -8;
|
||||||
int capLeft = g->whiteToMove ? 7 : -9;
|
int capLeft = g->whiteToMove ? 7 : -9;
|
||||||
@@ -17,41 +22,39 @@ int pawnMove(game *g, move *moves) {
|
|||||||
|
|
||||||
for (int i = 0; i < 64; ++i) {
|
for (int i = 0; i < 64; ++i) {
|
||||||
long long bit = 1LL << i;
|
long long bit = 1LL << i;
|
||||||
|
char p = 0;
|
||||||
if (!(pawns & bit))
|
if (!(pawns & bit))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// forword
|
// forword
|
||||||
int to = i + movdir;
|
int to = i + movdir;
|
||||||
|
p = (promo & (1LL << to)) ? 'q' : 0;
|
||||||
if (to >= 0 && to < 64 && !(occupied & (1LL << to))) {
|
if (to >= 0 && to < 64 && !(occupied & (1LL << to))) {
|
||||||
moves[index++] = (move){.From = i, .To = to};
|
moves[index++] = (move){.From = i, .To = to, .Promo = p};
|
||||||
}
|
}
|
||||||
|
|
||||||
// left
|
// left
|
||||||
to = i + capLeft;
|
to = i + capLeft;
|
||||||
|
p = (promo & (1LL << to)) ? 'q' : 0;
|
||||||
if (i % 8 > 0 && (to >= 0 && to < 64) && (enemy & (1LL << to))) {
|
if (i % 8 > 0 && (to >= 0 && to < 64) && (enemy & (1LL << to))) {
|
||||||
moves[index++] = (move){.From = i, .To = to};
|
moves[index++] = (move){.From = i, .To = to, .Promo = p};
|
||||||
}
|
}
|
||||||
|
|
||||||
// right
|
// right
|
||||||
to = i + capRight;
|
to = i + capRight;
|
||||||
|
p = (promo & (1LL << to)) ? 'q' : 0;
|
||||||
if (i % 8 < 7 && (to >= 0 && to < 64) && (enemy & (1LL << to))) {
|
if (i % 8 < 7 && (to >= 0 && to < 64) && (enemy & (1LL << to))) {
|
||||||
moves[index++] = (move){.From = i, .To = to};
|
moves[index++] = (move){.From = i, .To = to, .Promo = p};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int knightMove(game *g, move *moves) {
|
int knightMove(game *g, move *moves) {
|
||||||
long long knights = g->whiteToMove ? g->white.knights : g->black.knights;
|
long long knights = g->whiteToMove ? g->white.knights : g->black.knights;
|
||||||
long long occupied = g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
|
long long occupied = g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
|
||||||
|
|
||||||
int kMoves[] = {
|
int kMoves[] = {-17, -15, -10, -6, 6, 10, 15, 17};
|
||||||
-17, -15,
|
|
||||||
-10, -6,
|
|
||||||
6, 10,
|
|
||||||
15, 17
|
|
||||||
};
|
|
||||||
int index = 0;
|
int index = 0;
|
||||||
|
|
||||||
for (int i = 0; i < 64; i++) {
|
for (int i = 0; i < 64; i++) {
|
||||||
@@ -71,14 +74,212 @@ int knightMove(game *g, move *moves) {
|
|||||||
|
|
||||||
int fileDiff = abs(fromFile - toFile);
|
int fileDiff = abs(fromFile - toFile);
|
||||||
int rankDiff = abs(fromRank - toRank);
|
int rankDiff = abs(fromRank - toRank);
|
||||||
if (!((fileDiff == 1 && rankDiff == 2) || (fileDiff == 2 && rankDiff == 1)))
|
if (!((fileDiff == 1 && rankDiff == 2) ||
|
||||||
|
(fileDiff == 2 && rankDiff == 1)))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
long long destBit = 1LL << to;
|
long long destBit = 1LL << to;
|
||||||
if (occupied & destBit) // Skip if destination has friendly piece
|
if (occupied & destBit)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
moves[index++] = (move){.From = i, .To = to};
|
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
int rookMove(game *g, move *moves) {
|
||||||
|
return rookScan(g, moves, g->white.rooks, g->black.rooks);
|
||||||
|
}
|
||||||
|
|
||||||
|
int rookScan(game *g, move *moves, long long w, long long b) {
|
||||||
|
long long rooks = g->whiteToMove ? w : b;
|
||||||
|
long long occupied = g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
|
||||||
|
long long occupiedE =
|
||||||
|
!g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
|
||||||
|
int index = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < 64; i++) {
|
||||||
|
long long bit = 1LL << i;
|
||||||
|
int x = i % 8;
|
||||||
|
int y = i / 8;
|
||||||
|
if (!(rooks & bit))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// fwd
|
||||||
|
for (int j = y + 1; j < 8; j++) {
|
||||||
|
int to = (j * 8) + x;
|
||||||
|
long long lbit = 1LL << to;
|
||||||
|
if (occupied & lbit)
|
||||||
|
break;
|
||||||
|
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
||||||
|
if (occupiedE & lbit)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// bck
|
||||||
|
for (int j = y - 1; j >= 0; j--) {
|
||||||
|
int to = (j * 8) + x;
|
||||||
|
long long lbit = 1LL << to;
|
||||||
|
if (occupied & lbit)
|
||||||
|
break;
|
||||||
|
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
||||||
|
if (occupiedE & lbit)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// rht
|
||||||
|
for (int j = x + 1; j < 8; j++) {
|
||||||
|
int to = (y * 8) + j;
|
||||||
|
long long lbit = 1LL << to;
|
||||||
|
if (occupied & lbit)
|
||||||
|
break;
|
||||||
|
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
||||||
|
if (occupiedE & lbit)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// lft
|
||||||
|
for (int j = x - 1; j >= 0; j--) {
|
||||||
|
int to = (y * 8) + j;
|
||||||
|
long long lbit = 1LL << to;
|
||||||
|
if (occupied & lbit)
|
||||||
|
break;
|
||||||
|
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
||||||
|
if (occupiedE & lbit)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
int bishopMove(game *g, move *moves) {
|
||||||
|
return bishopScan(g, moves, g->white.bishops, g->black.bishops);
|
||||||
|
}
|
||||||
|
|
||||||
|
int bishopScan(game *g, move *moves, long long w, long long b) {
|
||||||
|
long long bishops = g->whiteToMove ? w : b;
|
||||||
|
long long occupied = g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
|
||||||
|
long long occupiedE =
|
||||||
|
!g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
|
||||||
|
int index = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < 64; i++) {
|
||||||
|
long long bit = 1LL << i;
|
||||||
|
int x = i % 8;
|
||||||
|
int y = i / 8;
|
||||||
|
|
||||||
|
if (!(bishops & bit))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// ur
|
||||||
|
for (int j = 1;; j++) {
|
||||||
|
int xTo = x + j;
|
||||||
|
int yTo = y + j;
|
||||||
|
if (xTo >= 8 || yTo >= 8)
|
||||||
|
break;
|
||||||
|
|
||||||
|
int to = (yTo * 8) + xTo;
|
||||||
|
long long lbit = 1LL << to;
|
||||||
|
|
||||||
|
if (occupied & lbit)
|
||||||
|
break;
|
||||||
|
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
||||||
|
if (occupiedE & lbit)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ul
|
||||||
|
for (int j = 1;; j++) {
|
||||||
|
int xTo = x - j;
|
||||||
|
int yTo = y + j;
|
||||||
|
if (xTo < 0 || yTo >= 8)
|
||||||
|
break;
|
||||||
|
|
||||||
|
int to = (yTo * 8) + xTo;
|
||||||
|
long long lbit = 1LL << to;
|
||||||
|
|
||||||
|
if (occupied & lbit)
|
||||||
|
break;
|
||||||
|
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
||||||
|
if (occupiedE & lbit)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// dr
|
||||||
|
for (int j = 1;; j++) {
|
||||||
|
int xTo = x + j;
|
||||||
|
int yTo = y - j;
|
||||||
|
if (xTo >= 8 || yTo < 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
int to = (yTo * 8) + xTo;
|
||||||
|
long long lbit = 1LL << to;
|
||||||
|
|
||||||
|
if (occupied & lbit)
|
||||||
|
break;
|
||||||
|
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
||||||
|
if (occupiedE & lbit)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// dl
|
||||||
|
for (int j = 1;; j++) {
|
||||||
|
int xTo = x - j;
|
||||||
|
int yTo = y - j;
|
||||||
|
if (xTo < 0 || yTo < 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
int to = (yTo * 8) + xTo;
|
||||||
|
long long lbit = 1LL << to;
|
||||||
|
|
||||||
|
if (occupied & lbit)
|
||||||
|
break;
|
||||||
|
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
||||||
|
if (occupiedE & lbit)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
int queenMove(game *g, move *moves){
|
||||||
|
int size = 0;
|
||||||
|
long long w,b;
|
||||||
|
w = g->white.queen;
|
||||||
|
b = g->black.queen;
|
||||||
|
size += rookScan(g,moves,w,b);
|
||||||
|
size += bishopScan(g,(moves+size),w,b);
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int kingMove(game *g, move *moves){
|
||||||
|
long long king = g->whiteToMove ? g->white.king : g->black.king;
|
||||||
|
long long occupied = g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
|
||||||
|
int index = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < 64; i++){
|
||||||
|
long long bit = 1LL << i;
|
||||||
|
int x = i % 8;
|
||||||
|
int y = i / 8;
|
||||||
|
int movesX[] = {-1, 0, 1, -1, 1, -1, 0, 1};
|
||||||
|
int movesY[] = {-1, -1, -1, 0, 0, 1, 1, 1};
|
||||||
|
|
||||||
|
if(!(king & bit))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
for (int j = 0; j < 8; ++j) {
|
||||||
|
int toX = x + movesX[j];
|
||||||
|
int toY = y + movesY[j];
|
||||||
|
int to = (toY * 8) + toX;
|
||||||
|
long long destBit = 1LL << to;
|
||||||
|
|
||||||
|
if (toX >= 0 && toX < 8 && toY >= 0 && toY < 8) {
|
||||||
|
if (!(occupied & destBit)) {
|
||||||
|
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return index;
|
return index;
|
||||||
|
|||||||
Reference in New Issue
Block a user