add license information & rename typedefs

This commit is contained in:
2021-10-29 09:00:10 +02:00
parent 27a7b15038
commit 7bdc428485
10 changed files with 238 additions and 117 deletions

View File

@@ -12,6 +12,7 @@ CFLAGS += -g
CFLAGS += -Wall CFLAGS += -Wall
CFLAGS += -Wextra CFLAGS += -Wextra
CFLAGS += -pedantic CFLAGS += -pedantic
CFLAGS += -Wno-pointer-arith
#CFLAGS += -Werror #CFLAGS += -Werror
CFLAGS += -Wmissing-declarations CFLAGS += -Wmissing-declarations

View File

@@ -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 <https://www.gnu.org/licenses/gpl-3.0-standalone.htmlL>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#ifndef BOARD_H #ifndef BOARD_H
#define BOARD_H #define BOARD_H
#include <stdint.h> #include <stdint.h>
#include "chessdefs.h"
/* 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))
typedef struct { typedef struct {
unsigned char piece; piece_t piece;
//struct piece *s_piece; //struct piece *s_piece;
} BOARD[8*8*2]; /* 0x88 board */ } board_t[8*8*2]; /* 0x88 board */
/* definitions for 0x88 representation */ /* definitions for 0x88 representation
#define SQ88(f, r) (16 * (r) + (f)) */
#define FILE88(s) ((s) & 7) #define SQ88(f, r) (16 * (r) + (f)) /* from rank,file to sq88 */
#define RANK88(s) ((s) >> 8) #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_EMPTY ' '
#define CHAR_PAWN 'P' #define CHAR_PAWN 'P'
#define CHAR_KNIGHT 'N' #define CHAR_KNIGHT 'N'
@@ -29,9 +38,13 @@ typedef struct {
#define CHAR_QUEEN 'Q' #define CHAR_QUEEN 'Q'
#define CHAR_KING 'K' #define CHAR_KING 'K'
/* from human to machine
*/
#define C2FILE(c) (tolower(c) - 'a') #define C2FILE(c) (tolower(c) - 'a')
#define C2RANK(c) (tolower(c) - '1') #define C2RANK(c) (tolower(c) - '1')
#define FILE2C(c) ((c)+'a') /* from machine to human
#define RANK2C(c) ((c)+'1') */
#define FILE2C(f) ((f) + 'a')
#define RANK2C(r) ((r) + '1')
#endif #endif

View File

@@ -1,32 +1,34 @@
/* 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 <https://www.gnu.org/licenses/gpl-3.0-standalone.htmlL>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#ifndef CHESSDEFS_H #ifndef CHESSDEFS_H
#define CHESSDEFS_H #define CHESSDEFS_H
/* We use the following notation /* piece_t bits structure
* 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
*/ */
typedef unsigned char piece_t;
//#define INVALID -1 /* unused in 0x88 */
#define EMPTY 0 #define EMPTY 0
#define WHITE (0) /* 0000000 0 */ #define WHITE 0 /* 0x00 00000000 */
#define BLACK (1) /* 0000000 1 */ #define BLACK 1 /* 0x01 00000001 */
#define PAWN (1 << 1) /* 0000001 0 */ #define PAWN (1 << 1) /* 0x02 00000010 */
#define KNIGHT (1 << 2) /* 0000010 0 */ #define KNIGHT (1 << 2) /* 0x04 00000100 */
#define BISHOP (1 << 3) /* 0000100 0 */ #define BISHOP (1 << 3) /* 0x08 00001000 */
#define ROOK (1 << 4) /* 0001000 0 */ #define ROOK (1 << 4) /* 0x10 00010000 */
#define QUEEN (1 << 5) /* 0010000 0 */ #define QUEEN (1 << 5) /* 0x20 00100000 */
#define KING (1 << 6) /* 0100000 0 */ #define KING (1 << 6) /* 0x40 01000000 */
#define MASK_COLOR 0x01 /* 00000001 */ #define MASK_COLOR 0x01 /* 00000001 */
#define MASK_PIECE 0x7E /* 01111110 */ #define MASK_PIECE 0x7E /* 01111110 */
@@ -41,16 +43,28 @@
#define SET_BLACK(p) ((p) |= MASK_COLOR) #define SET_BLACK(p) ((p) |= MASK_COLOR)
#define SET_COLOR(p, c) (!(c)? SET_WHITE(p): SET_BLACK(p)) #define SET_COLOR(p, c) (!(c)? SET_WHITE(p): SET_BLACK(p))
#define TURN_WHITE 0 /* square_t bits structure : ffffrrrr
#define TURN_BLACK 1 * 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 #endif

View File

@@ -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 <https://www.gnu.org/licenses/gpl-3.0-standalone.htmlL>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include "chessdefs.h"
#include "position.h"
#include "board.h"
#include "fen.h"
/* Starting Position : /* Starting Position :
* rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 * rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
* After 1.e4 : * After 1.e4 :
@@ -18,28 +42,16 @@
* *
*/ */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include "chessdefs.h"
#include "position.h"
#include "board.h"
#include "fen.h"
// warning, we expect a valid fen input // warning, we expect a valid fen input
POS *fen2pos(POS *pos, char *fen) pos_t *fen2pos(pos_t *pos, char *fen)
{ {
char *p = fen; char *p = fen;
int rank, file, skip; short rank, file, skip;
BOARD *board = pos->board; board_t *board = pos->board;
# define SKIP_BLANK(p) for(;*(p) == ' '; (p)++) # 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) { for (rank = 7, file = 0; *p && *p != ' '; ++p) {
char cp = toupper(*p); char cp = toupper(*p);
switch (cp) { switch (cp) {
@@ -60,7 +72,7 @@ POS *fen2pos(POS *pos, char *fen)
goto color; goto color;
case CHAR_KING: case CHAR_KING:
board[SQ88(file, rank)]->piece = 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); //printf("f=%d r=%d *p=%c piece=%c\n", file, rank, *p, cp);
color: color:
SET_COLOR(board[SQ88(file, rank)]->piece, islower(*p)); SET_COLOR(board[SQ88(file, rank)]->piece, islower(*p));
@@ -86,13 +98,14 @@ POS *fen2pos(POS *pos, char *fen)
putchar('\n'); putchar('\n');
} }
/* 2) next move color
// 2) next move color */
SKIP_BLANK(p); SKIP_BLANK(p);
pos->turn = *p == 'w' ? TURN_WHITE : TURN_BLACK; pos->turn = *p == 'w' ? WHITE : BLACK;
p++; p++;
// 3) castle status /* 3) castle status
*/
SKIP_BLANK(p); SKIP_BLANK(p);
pos->castle = 0; pos->castle = 0;
if (*p != '-') { if (*p != '-') {
@@ -114,21 +127,20 @@ POS *fen2pos(POS *pos, char *fen)
} }
} }
// 4) en passant /* 4) en passant
*/
SKIP_BLANK(p); SKIP_BLANK(p);
//printf("pos=%d\n", (int)(p-fen));
pos->en_passant = 0; pos->en_passant = 0;
if (*p != '-') { if (*p != '-') {
//printf("passant=%c\n", *p);
SET_F(pos->en_passant, C2FILE(*p++)); SET_F(pos->en_passant, C2FILE(*p++));
SET_R(pos->en_passant, C2RANK(*p++)); SET_R(pos->en_passant, C2RANK(*p++));
//printf("passant=%c\n", *p);
} else { } else {
p++; p++;
} }
// 5) half moves since last capture or pawn move and /* 5) half moves since last capture or pawn move and
// 6) current move number * 6) current move number
*/
SKIP_BLANK(p); SKIP_BLANK(p);
//printf("pos=%d\n", (int)(p-fen)); //printf("pos=%d\n", (int)(p-fen));
sscanf(p, "%hd %hd", &pos->clock_50, &pos->curmove); sscanf(p, "%hd %hd", &pos->clock_50, &pos->curmove);
@@ -138,7 +150,7 @@ POS *fen2pos(POS *pos, char *fen)
#ifdef FENBIN #ifdef FENBIN
int main(int ac, char**av) int main(int ac, char**av)
{ {
POS *pos; pos_t *pos;
pos = pos_create(); pos = pos_create();
if (ac == 1) { if (ac == 1) {

View File

@@ -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 <https://www.gnu.org/licenses/gpl-3.0-standalone.htmlL>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#ifndef FEN_H #ifndef FEN_H
#define FEN_H #define FEN_H
#include "position.h" #include "position.h"
POS *fen2pos(POS *pos, char *fen); pos_t *fen2pos(pos_t *pos, char *fen);
#endif #endif

View File

@@ -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 <https://www.gnu.org/licenses/gpl-3.0-standalone.htmlL>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#ifndef ROOK_H #ifndef ROOK_H
#define ROOK_H #define ROOK_H
@@ -5,10 +18,8 @@
#include "position.h" #include "position.h"
typedef struct { typedef struct {
PIECE piece; piece_t t;
SQUARE from; square_t from, to;
SQUARE to;
} MOVE; } MOVE;
extern MOVE *moves_rook(POS *pos); extern MOVE *moves_rook(POS *pos);

View File

@@ -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 <https://www.gnu.org/licenses/gpl-3.0-standalone.htmlL>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#ifndef PIECE_H #ifndef PIECE_H
#define PIECE_H #define PIECE_H
#include "chessdefs.h" #include "chessdefs.h"
#include "board.h" #include "board.h"
#include "list.h"
typedef struct piece { typedef struct piece {
short piece; piece_t piece;
short color; square_t square;
SQUARE square;
short castle; short castle;
float value; int64_t value;
} PIECE, PLAYER_PIECES[16]; struct list_head list;
} piece_list_t;
#endif #endif

View File

@@ -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 <https://www.gnu.org/licenses/gpl-3.0-standalone.htmlL>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
@@ -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; int rank, file;
piece_t piece; piece_t piece;
BOARD *board = pos->board; board_t *board = pos->board;
printf(" +---+---+---+---+---+---+---+---+\n"); printf(" +---+---+---+---+---+---+---+---+\n");
for (rank = 7; rank >= 0; --rank) { for (rank = 7; rank >= 0; --rank) {
@@ -63,10 +76,10 @@ void pos_print(POS *pos)
if (pos->en_passant == 0) if (pos->en_passant == 0)
printf("None.\n"); printf("None.\n");
else else
printf("%d %d = %c%c\n", SQUARE_F(pos->en_passant), printf("%d %d = %c%c\n", GET_F(pos->en_passant),
SQUARE_R(pos->en_passant), GET_R(pos->en_passant),
FILE2C(SQUARE_F(pos->en_passant)), FILE2C(GET_F(pos->en_passant)),
RANK2C(SQUARE_R(pos->en_passant))); RANK2C(GET_R(pos->en_passant)));
printf("castle [%#x] : ", pos->castle); printf("castle [%#x] : ", pos->castle);
@@ -83,10 +96,10 @@ void pos_print(POS *pos)
printf("Current move = %d\n", pos->curmove); printf("Current move = %d\n", pos->curmove);
} }
POS *pos_init(POS *pos) pos_t *pos_init(pos_t *pos)
{ {
int file, rank; int file, rank;
BOARD *board = pos->board; board_t *board = pos->board;
for (rank = 0; rank < 8; ++rank) { for (rank = 0; rank < 8; ++rank) {
for (file = 0; file < 8; ++file) { 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->castle = 0;
pos->clock_50 = 0; pos->clock_50 = 0;
pos->curmove = 0; pos->curmove = 0;
@@ -105,18 +118,18 @@ POS *pos_init(POS *pos)
return 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"; static char *startfen="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
return fen2pos(pos, startfen); 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) { if (pos) {
pos->board = malloc(sizeof (BOARD)); pos->board = malloc(sizeof (board_t));
if (pos->board) if (pos->board)
pos_init(pos); pos_init(pos);
else { else {
@@ -124,5 +137,7 @@ POS *pos_create()
pos = NULL; pos = NULL;
} }
} }
INIT_LIST_HEAD(&pos->w_pieces);
INIT_LIST_HEAD(&pos->b_pieces);
return pos; return pos;
} }

View File

@@ -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 <https://www.gnu.org/licenses/gpl-3.0-standalone.htmlL>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#ifndef POSITION_H #ifndef POSITION_H
#define POSITION_H #define POSITION_H
#include <stdint.h> #include <stdint.h>
#include "chessdefs.h"
#include "board.h" #include "board.h"
#include "list.h"
typedef struct position { typedef struct position {
short turn; piece_t turn; /* we use only color bit */
short castle; castle_t castle;
square_t en_passant;
short clock_50; short clock_50;
short curmove; short curmove;
SQUARE en_passant; board_t *board;
BOARD *board; struct list_head w_pieces, b_pieces;
} POS; } pos_t;
void pos_print(POS *pos); void pos_print(pos_t *pos);
POS *pos_init(POS *pos); pos_t *pos_init(pos_t *pos);
POS *pos_startpos(POS *pos); pos_t *pos_startpos(pos_t *pos);
POS *pos_create(); pos_t *pos_create();
#endif #endif

View File

@@ -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 <https://www.gnu.org/licenses/gpl-3.0-standalone.htmlL>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#include "chessdefs.h" #include "chessdefs.h"
#include "position.h" #include "position.h"
#include "fen.h" #include "fen.h"