stellar

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

piece.c (2575B)


      1 #include <ctype.h>
      2 #include <stdlib.h>
      3 
      4 #include "piece.h"
      5 
      6 struct Piece {
      7     attack_f attacks;
      8     char *unicode;
      9     ePiece piece;
     10     eColor color;
     11     char code;
     12     char asci;
     13 };
     14 
     15 // clang-format off
     16 const struct Piece Pieces[2][6] = {
     17 [WHITE] = {
     18     [KNIGHT] = {.color = WHITE, .code = 'N', .asci = 'N', .unicode = "♘ ", .piece = KNIGHT, .attacks = attacks_knight_get},
     19     [BISHOP] = {.color = WHITE, .code = 'B', .asci = 'B', .unicode = "♗ ", .piece = BISHOP, .attacks = attacks_bishop_get},
     20      [QUEEN] = {.color = WHITE, .code = 'Q', .asci = 'Q', .unicode = "♕ ",  .piece = QUEEN,  .attacks = attacks_queen_get},
     21       [KING] = {.color = WHITE, .code = 'K', .asci = 'K', .unicode = "♔ ",   .piece = KING,   .attacks = attakcs_king_get},
     22       [PAWN] = {.color = WHITE, .code = 'P', .asci = 'P', .unicode = "♙ ",   .piece = PAWN,  .attacks = attacks_wpawn_get},
     23       [ROOK] = {.color = WHITE, .code = 'R', .asci = 'R', .unicode = "♖ ",   .piece = ROOK,   .attacks = attacks_rook_get},
     24     },
     25 [BLACK] = {
     26     [KNIGHT] = {.color = BLACK, .code = 'n', .asci = 'n', .unicode = "♞ ", .piece = KNIGHT, .attacks = attacks_knight_get},
     27     [BISHOP] = {.color = BLACK, .code = 'b', .asci = 'b', .unicode = "♝ ", .piece = BISHOP, .attacks = attacks_bishop_get},
     28      [QUEEN] = {.color = BLACK, .code = 'q', .asci = 'q', .unicode = "♛ ",  .piece = QUEEN,  .attacks = attacks_queen_get},
     29       [KING] = {.color = BLACK, .code = 'k', .asci = 'k', .unicode = "♚ ",   .piece = KING,   .attacks = attakcs_king_get},
     30       [PAWN] = {.color = BLACK, .code = 'p', .asci = 'p', .unicode = "♟ ",   .piece = PAWN,  .attacks = attacks_bpawn_get},
     31       [ROOK] = {.color = BLACK, .code = 'r', .asci = 'r', .unicode = "♜ ",   .piece = ROOK,   .attacks = attacks_rook_get},
     32     },
     33 };
     34 // clang-format on
     35 
     36 attack_f piece_attacks(Piece self) { return self->attacks; }
     37 char piece_asci(Piece self) { return self->asci; }
     38 char piece_code(Piece self) { return self->code; }
     39 char *piece_unicode(Piece self) { return self->unicode; }
     40 eColor piece_color(Piece self) { return self->color; }
     41 ePiece piece_piece(Piece self) { return self->piece; }
     42 int piece_index(Piece self) { return self->color * 8 + self->piece; }
     43 
     44 Piece piece_get(ePiece piece, eColor color) { return &Pieces[color][piece]; }
     45 Piece piece_from_index(int index) { return &Pieces[index / 8][index % 8]; }
     46 Piece piece_from_code(char code) {
     47     int color = (isupper(code)) ? WHITE : BLACK;
     48     for (int i = 0; i < 6; i++)
     49         if (Pieces[color][i].code == code) return &Pieces[color][i];
     50     return NULL;
     51 }