stellar

Stellar - Chess engine written in C
Log | Files | Refs

moves.c (2445B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 #include <cul/assert.h>
      5 #include <cul/mem.h>
      6 
      7 #include "moves.h"
      8 
      9 int move_cmp(Move a, Move b) { return *(uint32_t *)&a == *(uint32_t *)&b; }
     10 
     11 Move move_encode(Square src, Square tgt, Piece piece, Piece capture,
     12                  Piece promote, int dbl, int enpassant, int castle) {
     13     return (Move){
     14         .source = src,
     15         .target = tgt,
     16         .dbl = dbl,
     17         .enpassant = enpassant,
     18         .castle = castle,
     19         .capture = capture != NULL,
     20         .promote = promote != NULL,
     21         .piece = piece_index(piece),
     22         .piece_capture = capture ? piece_index(capture) : 0,
     23         .piece_promote = promote ? piece_index(promote) : 0,
     24     };
     25 }
     26 
     27 void move_print(Move move) {
     28     printf("%5s %5s  %2c  %2c   %2c %4d %4d %4d %4d %4d\n",
     29            square_to_coordinates[move_source(move)],
     30            square_to_coordinates[move_target(move)],
     31            piece_asci(move_piece(move)),
     32            move_capture(move) ? piece_asci(move_piece_capture(move)) : '.',
     33            move_promote(move) ? piece_asci(move_piece_promote(move)) : '.',
     34            move_double(move) ? 1 : 0, move_enpassant(move) ? 1 : 0,
     35            move_castle(move) ? 1 : 0, move_capture(move) ? 1 : 0,
     36            move_promote(move) ? 1 : 0);
     37 }
     38 
     39 MoveList *move_list_new(void) {
     40     MoveList *p;
     41     NEW0(p);
     42     return p;
     43 }
     44 
     45 void move_list_free(MoveList **p) { FREE(*p); }
     46 
     47 Move move_list_index_move(const MoveList *self, int index) {
     48     return self->moves[index].move;
     49 }
     50 
     51 int move_list_index_score(const MoveList *self, int index) {
     52     return self->moves[index].score;
     53 }
     54 
     55 void move_list_index_score_set(MoveList *self, int index, int score) {
     56     self->moves[index].score = score;
     57 }
     58 
     59 int move_list_size(const MoveList *self) { return self->count; }
     60 void move_list_reset(MoveList *self) { self->count = 0; }
     61 
     62 void move_list_add(MoveList *self, Move move) {
     63     self->moves[self->count++].move = move;
     64 }
     65 
     66 int move_list_cmp(const void *a, const void *b) {
     67     return ((MoveE *)a)->score <= ((MoveE *)b)->score;
     68 }
     69 
     70 void move_list_sort(MoveList *list) {
     71     qsort(list->moves, list->count, sizeof(MoveE), move_list_cmp);
     72 }
     73 
     74 void move_list_print(const MoveList *self) {
     75     printf("Score   From    To  Pi  Cap  Prmt  Dbl  Enp  Cst  C   P\n");
     76     for (int i = 0; i < self->count; i++) {
     77         printf("%5d: ", self->moves[i].score);
     78         move_print(self->moves[i].move);
     79     }
     80     printf("Total: %d\n", self->count);
     81 }