diff --git a/Makefile b/Makefile index e9a63cd..0d8565d 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,7 @@ CFLAGS += -g CFLAGS += -Wall CFLAGS += -Wextra CFLAGS += -pedantic +CFLAGS += -Wno-pointer-arith #CFLAGS += -Werror CFLAGS += -Wmissing-declarations diff --git a/src/board.h b/src/board.h index 96b4789..05f3097 100644 --- a/src/board.h +++ b/src/board.h @@ -1,26 +1,35 @@ +/* board.h - board definitions. + * + * Copyright (C) 2021 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + #ifndef BOARD_H #define BOARD_H #include - -/* ffff rrrr */ -typedef unsigned char SQUARE; -#define SQUARE_F(s) ((s) >> 4) -#define SQUARE_R(s) ((s) & 0x0f) -#define SET_F(s, f) ((s) &= 0x0f, (s) |= (f)<<4) -#define SET_R(s, r) ((s) &= 0xf0, (s) |= (r)) +#include "chessdefs.h" typedef struct { - unsigned char piece; + piece_t piece; //struct piece *s_piece; -} BOARD[8*8*2]; /* 0x88 board */ +} board_t[8*8*2]; /* 0x88 board */ -/* definitions for 0x88 representation */ -#define SQ88(f, r) (16 * (r) + (f)) -#define FILE88(s) ((s) & 7) -#define RANK88(s) ((s) >> 8) +/* definitions for 0x88 representation + */ +#define SQ88(f, r) (16 * (r) + (f)) /* from rank,file to sq88 */ +#define FILE88(s) ((s) & 7) /* from sq88 to file */ +#define RANK88(s) ((s) >> 8) /* from sq88 to rank */ -/* piece notation */ +/* piece human notation + */ #define CHAR_EMPTY ' ' #define CHAR_PAWN 'P' #define CHAR_KNIGHT 'N' @@ -29,9 +38,13 @@ typedef struct { #define CHAR_QUEEN 'Q' #define CHAR_KING 'K' -#define C2FILE(c) (tolower(c)-'a') -#define C2RANK(c) (tolower(c)-'1') -#define FILE2C(c) ((c)+'a') -#define RANK2C(c) ((c)+'1') +/* from human to machine + */ +#define C2FILE(c) (tolower(c) - 'a') +#define C2RANK(c) (tolower(c) - '1') +/* from machine to human + */ +#define FILE2C(f) ((f) + 'a') +#define RANK2C(r) ((r) + '1') #endif diff --git a/src/chessdefs.h b/src/chessdefs.h index e208d27..e91f918 100644 --- a/src/chessdefs.h +++ b/src/chessdefs.h @@ -1,56 +1,70 @@ +/* chessdefs.h - generic chess definitions. + * + * Copyright (C) 2021 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + #ifndef CHESSDEFS_H #define CHESSDEFS_H -/* We use the following notation - * Bit Binary Piece/color - * - * 0 0000 0001 White - * 1 0000 0010 Black - * - * 2 0000 0100 Pawn - * 3 0000 1000 Knight - * 4 0001 0000 Bishop - * 5 0010 0000 Rook - * 6 0100 0000 Queen - * 7 1000 0000 King +/* piece_t bits structure */ +typedef unsigned char piece_t; -//#define INVALID -1 /* unused in 0x88 */ -#define EMPTY 0 +#define EMPTY 0 -#define WHITE (0) /* 0000000 0 */ -#define BLACK (1) /* 0000000 1 */ +#define WHITE 0 /* 0x00 00000000 */ +#define BLACK 1 /* 0x01 00000001 */ -#define PAWN (1 << 1) /* 0000001 0 */ -#define KNIGHT (1 << 2) /* 0000010 0 */ -#define BISHOP (1 << 3) /* 0000100 0 */ -#define ROOK (1 << 4) /* 0001000 0 */ -#define QUEEN (1 << 5) /* 0010000 0 */ -#define KING (1 << 6) /* 0100000 0 */ +#define PAWN (1 << 1) /* 0x02 00000010 */ +#define KNIGHT (1 << 2) /* 0x04 00000100 */ +#define BISHOP (1 << 3) /* 0x08 00001000 */ +#define ROOK (1 << 4) /* 0x10 00010000 */ +#define QUEEN (1 << 5) /* 0x20 00100000 */ +#define KING (1 << 6) /* 0x40 01000000 */ -#define MASK_COLOR 0x01 /* 0000000 1 */ -#define MASK_PIECE 0x7E /* 0111111 0 */ +#define MASK_COLOR 0x01 /* 00000001 */ +#define MASK_PIECE 0x7E /* 01111110 */ -#define COLOR(p) ((p) & MASK_COLOR) -#define PIECE(p) ((p) & MASK_PIECE) +#define COLOR(p) ((p) & MASK_COLOR) +#define PIECE(p) ((p) & MASK_PIECE) -#define IS_WHITE(p) (!COLOR(p)) -#define IS_BLACK(p) (COLOR(p)) +#define IS_WHITE(p) (!COLOR(p)) +#define IS_BLACK(p) (COLOR(p)) #define SET_WHITE(p) ((p) &= ~MASK_COLOR) #define SET_BLACK(p) ((p) |= MASK_COLOR) #define SET_COLOR(p, c) (!(c)? SET_WHITE(p): SET_BLACK(p)) -#define TURN_WHITE 0 -#define TURN_BLACK 1 +/* square_t bits structure : ffffrrrr + * ffff: file + * rrrr: rank + */ +typedef unsigned char square_t; -typedef unsigned char piece_t, color_t; +#define GET_F(s) ((s) >> 4) +#define GET_R(s) ((s) & 0x0f) +#define SET_F(s, f) ((s) &= 0x0f, (s) |= (f)<<4) +#define SET_R(s, r) ((s) &= 0xf0, (s) |= (r)) + +/* castle_t bits structure + */ +typedef unsigned char castle_t; + +#define CASTLE_WK 1 /* 0x01 00000001 */ +#define CASTLE_WQ (1 << 1) /* 0x02 00000010 */ +#define CASTLE_BK (1 << 2) /* 0x04 00000100 */ +#define CASTLE_BQ (1 << 3) /* 0x08 00001000 */ + +#define CASTLE_W 0x03 /* 00000011 W castle mask */ +#define CASTLE_B 0x0C /* 00001100 B castle mask */ -#define CASTLE_WK 0x01 -#define CASTLE_WQ 0x02 -#define CASTLE_BK 0x04 -#define CASTLE_BQ 0x08 -#define CASTLE_W 0x03 /* white castle mask */ -#define CASTLE_B 0x0C /* black castle mask */ #endif diff --git a/src/fen.c b/src/fen.c index a02d407..65b54a5 100644 --- a/src/fen.c +++ b/src/fen.c @@ -1,3 +1,27 @@ +/* fen.c - fen notation. + * + * Copyright (C) 2021 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include +#include +#include +#include +#include + +#include "chessdefs.h" +#include "position.h" +#include "board.h" +#include "fen.h" + /* Starting Position : * rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 * After 1.e4 : @@ -18,28 +42,16 @@ * */ -#include -#include -#include -#include -#include - -#include "chessdefs.h" -#include "position.h" -#include "board.h" -#include "fen.h" - // warning, we expect a valid fen input -POS *fen2pos(POS *pos, char *fen) +pos_t *fen2pos(pos_t *pos, char *fen) { char *p = fen; - int rank, file, skip; - BOARD *board = pos->board; + short rank, file, skip; + board_t *board = pos->board; # define SKIP_BLANK(p) for(;*(p) == ' '; (p)++) - //pos_init(pos); - - // 1) get piece placement information + /* 1) get piece placement information + */ for (rank = 7, file = 0; *p && *p != ' '; ++p) { char cp = toupper(*p); switch (cp) { @@ -60,7 +72,7 @@ POS *fen2pos(POS *pos, char *fen) goto color; case CHAR_KING: board[SQ88(file, rank)]->piece = KING; - goto color; + //goto color; //printf("f=%d r=%d *p=%c piece=%c\n", file, rank, *p, cp); color: SET_COLOR(board[SQ88(file, rank)]->piece, islower(*p)); @@ -86,13 +98,14 @@ POS *fen2pos(POS *pos, char *fen) putchar('\n'); } - - // 2) next move color + /* 2) next move color + */ SKIP_BLANK(p); - pos->turn = *p == 'w' ? TURN_WHITE : TURN_BLACK; + pos->turn = *p == 'w' ? WHITE : BLACK; p++; - // 3) castle status + /* 3) castle status + */ SKIP_BLANK(p); pos->castle = 0; if (*p != '-') { @@ -114,21 +127,20 @@ POS *fen2pos(POS *pos, char *fen) } } - // 4) en passant + /* 4) en passant + */ SKIP_BLANK(p); - //printf("pos=%d\n", (int)(p-fen)); pos->en_passant = 0; if (*p != '-') { - //printf("passant=%c\n", *p); SET_F(pos->en_passant, C2FILE(*p++)); SET_R(pos->en_passant, C2RANK(*p++)); - //printf("passant=%c\n", *p); } else { p++; } - // 5) half moves since last capture or pawn move and - // 6) current move number + /* 5) half moves since last capture or pawn move and + * 6) current move number + */ SKIP_BLANK(p); //printf("pos=%d\n", (int)(p-fen)); sscanf(p, "%hd %hd", &pos->clock_50, &pos->curmove); @@ -138,7 +150,7 @@ POS *fen2pos(POS *pos, char *fen) #ifdef FENBIN int main(int ac, char**av) { - POS *pos; + pos_t *pos; pos = pos_create(); if (ac == 1) { diff --git a/src/fen.h b/src/fen.h index c566973..a709816 100644 --- a/src/fen.h +++ b/src/fen.h @@ -1,8 +1,21 @@ +/* fen.h - fen notation. + * + * Copyright (C) 2021 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + #ifndef FEN_H #define FEN_H #include "position.h" -POS *fen2pos(POS *pos, char *fen); +pos_t *fen2pos(pos_t *pos, char *fen); #endif diff --git a/src/move.h b/src/move.h index d3770f7..32a7c4e 100644 --- a/src/move.h +++ b/src/move.h @@ -1,3 +1,16 @@ +/* move.h - move management. + * + * Copyright (C) 2021 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + #ifndef ROOK_H #define ROOK_H @@ -5,10 +18,8 @@ #include "position.h" typedef struct { - PIECE piece; - SQUARE from; - SQUARE to; - + piece_t t; + square_t from, to; } MOVE; extern MOVE *moves_rook(POS *pos); diff --git a/src/piece.h b/src/piece.h index 38e5c5f..67d6075 100644 --- a/src/piece.h +++ b/src/piece.h @@ -1,15 +1,29 @@ +/* piece.h - piece definitions. + * + * Copyright (C) 2021 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + #ifndef PIECE_H #define PIECE_H #include "chessdefs.h" #include "board.h" +#include "list.h" typedef struct piece { - short piece; - short color; - SQUARE square; + piece_t piece; + square_t square; short castle; - float value; -} PIECE, PLAYER_PIECES[16]; + int64_t value; + struct list_head list; +} piece_list_t; #endif diff --git a/src/position.c b/src/position.c index 5c45822..20ed58f 100644 --- a/src/position.c +++ b/src/position.c @@ -1,3 +1,16 @@ +/* position.c - position management. + * + * Copyright (C) 2021 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + #include #include #include @@ -42,11 +55,11 @@ inline static char piece2char(unsigned char p) } -void pos_print(POS *pos) +void pos_print(pos_t *pos) { int rank, file; piece_t piece; - BOARD *board = pos->board; + board_t *board = pos->board; printf(" +---+---+---+---+---+---+---+---+\n"); for (rank = 7; rank >= 0; --rank) { @@ -63,10 +76,10 @@ void pos_print(POS *pos) if (pos->en_passant == 0) printf("None.\n"); else - printf("%d %d = %c%c\n", SQUARE_F(pos->en_passant), - SQUARE_R(pos->en_passant), - FILE2C(SQUARE_F(pos->en_passant)), - RANK2C(SQUARE_R(pos->en_passant))); + printf("%d %d = %c%c\n", GET_F(pos->en_passant), + GET_R(pos->en_passant), + FILE2C(GET_F(pos->en_passant)), + RANK2C(GET_R(pos->en_passant))); printf("castle [%#x] : ", pos->castle); @@ -83,10 +96,10 @@ void pos_print(POS *pos) printf("Current move = %d\n", pos->curmove); } -POS *pos_init(POS *pos) +pos_t *pos_init(pos_t *pos) { int file, rank; - BOARD *board = pos->board; + board_t *board = pos->board; for (rank = 0; rank < 8; ++rank) { for (file = 0; file < 8; ++file) { @@ -95,7 +108,7 @@ POS *pos_init(POS *pos) } } - pos->turn = TURN_WHITE; + pos->turn = WHITE; pos->castle = 0; pos->clock_50 = 0; pos->curmove = 0; @@ -105,18 +118,18 @@ POS *pos_init(POS *pos) return pos; } -POS *pos_startpos(POS *pos) +pos_t *pos_startpos(pos_t *pos) { static char *startfen="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; return fen2pos(pos, startfen); } -POS *pos_create() +pos_t *pos_create() { - POS *pos = malloc(sizeof(POS)); + pos_t *pos = malloc(sizeof(pos_t)); if (pos) { - pos->board = malloc(sizeof (BOARD)); + pos->board = malloc(sizeof (board_t)); if (pos->board) pos_init(pos); else { @@ -124,5 +137,7 @@ POS *pos_create() pos = NULL; } } + INIT_LIST_HEAD(&pos->w_pieces); + INIT_LIST_HEAD(&pos->b_pieces); return pos; } diff --git a/src/position.h b/src/position.h index dcdd481..3cf95ca 100644 --- a/src/position.h +++ b/src/position.h @@ -1,22 +1,37 @@ +/* position.h - position management definitions. + * + * Copyright (C) 2021 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + #ifndef POSITION_H #define POSITION_H #include - +#include "chessdefs.h" #include "board.h" +#include "list.h" typedef struct position { - short turn; - short castle; + piece_t turn; /* we use only color bit */ + castle_t castle; + square_t en_passant; short clock_50; short curmove; - SQUARE en_passant; - BOARD *board; -} POS; + board_t *board; + struct list_head w_pieces, b_pieces; +} pos_t; -void pos_print(POS *pos); -POS *pos_init(POS *pos); -POS *pos_startpos(POS *pos); -POS *pos_create(); +void pos_print(pos_t *pos); +pos_t *pos_init(pos_t *pos); +pos_t *pos_startpos(pos_t *pos); +pos_t *pos_create(); #endif diff --git a/src/rook.c b/src/rook.c index b32e29b..ad37157 100644 --- a/src/rook.c +++ b/src/rook.c @@ -1,3 +1,16 @@ +/* rook.c - A simple pool manager. + * + * Copyright (C) 2021 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + #include "chessdefs.h" #include "position.h" #include "fen.h"