73 Commits

Author SHA1 Message Date
345834159a change submodule url to relative 2023-12-29 16:35:19 +01:00
09633a4287 replace libs with brlib submodule 2023-12-29 09:44:33 +01:00
cb9c81e8f9 edit env.sh 2023-12-29 08:31:30 +01:00
072f3ced9b Makefile/.gitignore: cleanup (cont.) 2023-12-15 09:17:12 +01:00
77bde22d00 add brlib's bits.c (to remove problematic logs in bits.h) 2023-12-13 21:35:57 +01:00
e2d32a7300 Makefile: Hure rewriting/simplification (!) 2023-12-13 21:31:01 +01:00
01c5765888 remove -DBIN_xxx from brchess source files. parts moved to ./test (TODO) 2023-12-13 21:30:39 +01:00
a0ccad58e5 bits.h: remove logs in macros 2023-12-11 15:05:57 +01:00
be790056f6 Makefile: fix/simplify binaries generation 2023-12-11 15:02:31 +01:00
e0da38e697 debug.[ch]: remove dependancies from bits.h 2023-12-11 14:59:30 +01:00
11d3501a35 copyright dates 2023-11-15 12:28:57 +01:00
48b5420830 Add check array in position structure 2023-07-25 06:54:33 +02:00
f27b649503 typo in bits.c 2023-07-14 23:14:57 +02:00
605ef7d201 add first TODO (valgrind/mem check) 2023-07-14 22:15:33 +02:00
aa1e9fdeda add license 2023-07-14 21:46:10 +02:00
d3c78cb0af remove TOC 2023-07-14 21:05:44 +02:00
4ff9df9369 Initial STATUS.org 2023-07-14 21:00:29 +02:00
b3fde55107 .gitignore: valgrind.out 2023-07-14 08:45:46 +02:00
f2d4f07069 valgrind: ignore libreadline errors 2023-07-14 08:35:47 +02:00
b855ba59aa do not generate moves in pvs() terminal nodes 2023-07-13 10:39:36 +02:00
20403a0795 improve search() and pvs() output 2023-07-13 09:49:02 +02:00
c7e2aec77c Move bitboards constants to bitboard.h 2023-07-13 09:48:16 +02:00
754b011d05 fix _moves_gen_eval_sort() 2023-07-12 21:34:04 +02:00
e2a3563fce use eval_simple() as base for eval() 2023-07-12 21:32:26 +02:00
d852e0bc1d adjust nodecounts, fix PVS 2023-07-12 21:31:27 +02:00
3de87daa5a bug fix in pos_dup() 2023-07-12 21:30:24 +02:00
b5ed42746e cosmetic changes 2023-07-12 21:29:51 +02:00
0ca495576d add simple_eval and pvs. DO NOT USE PVS (need to use simple_eval) 2023-07-11 22:24:26 +02:00
ed9b9cc646 add nodes_count and moves_{generated,counted} in pos struct 2023-07-11 22:22:33 +02:00
88d2d4061f add moves sort 2023-07-11 22:21:42 +02:00
9932a64c97 add pvs(), aka Principal Variation Search 2023-07-11 22:20:43 +02:00
af1f5db507 add BB flip_V and FLIP_H macros 2023-07-11 22:13:06 +02:00
d76c10797a add debug_timer_elapsed() 2023-07-10 18:35:17 +02:00
36aa34a38b add list_sort lib 2023-07-10 18:33:58 +02:00
d9f03acb02 fix piece color in move_do 2023-07-10 13:17:26 +02:00
683b6ad66b add debug_level_get 2023-07-10 13:11:38 +02:00
b1e6461f6f Add DEBUG_DEBUG_C 2023-07-10 13:10:53 +02:00
65f1bef987 cleanup 2023-07-09 16:22:42 +02:00
23e49f463e add Emacs .dirs-local.el 2023-07-09 16:04:20 +02:00
48319cf21a add M_PR_NL option in move_print() 2023-07-09 15:53:57 +02:00
0b787c8a90 Add negamax function (no α β pruning) 2023-07-09 15:44:50 +02:00
4bca805404 add move_t forward decl. 2023-07-09 15:42:41 +02:00
0df87ff41c Add bestmove in pos struct, add pos_check() 2023-07-09 15:36:14 +02:00
892bdcd004 debug: Add flush option 2023-07-09 15:33:37 +02:00
120a459206 add pos_del function 2023-07-07 15:53:57 +02:00
7952a34c88 Add pos negamax function 2023-07-07 15:53:26 +02:00
531bfa4fb0 remove unused lib includes 2023-07-07 12:28:22 +02:00
12f972e152 move.c: Code cleanup, add promotion in move_do 2023-07-07 12:11:17 +02:00
183e9ef2be Improve move gen, delete ling pos, 3 steps moves generation 2023-07-07 02:21:14 +02:00
6f7a04cc89 Add move_do (dup position), remove &board in move struct 2023-07-06 12:22:26 +02:00
a08b006b98 Add pre-processor and assembly generation 2023-07-06 12:19:00 +02:00
8857dec6cd cleanup 2023-07-02 12:15:22 +02:00
aa7cb11056 add FILE* output 2023-07-02 11:36:04 +02:00
fe5b21aad9 add CPPFLAGS in bin compile 2023-07-01 20:55:05 +02:00
bab3ea95b9 Makefile: cleanup, separate sections, better bin gen, add ccls/valgrind 2023-06-29 09:48:55 +02:00
09ceca44e5 pos: keep possibility to have opponent moves (NULL move) 2023-06-25 16:02:23 +02:00
1e4af66379 moge gen: all moves count as mobility 2023-06-25 16:01:13 +02:00
5bd8f9042a eval: add functions to get eval details 2023-06-25 15:56:36 +02:00
373a73cb52 eval_t is now s32 2023-06-25 15:55:12 +02:00
9c6830bc56 add inittial pos init function 2023-06-25 15:54:46 +02:00
9b25a6ba8c Makefile deps improved, improve Emacs dir locals. 2023-06-24 12:14:02 +02:00
1154f141c9 start bitboard integration 2023-06-22 16:08:57 +02:00
cb94ca52b9 rename to brchess 2023-06-22 16:07:16 +02:00
6f3570ef40 save changes made sparsely the last 2 years (segfault everywhere) 2023-06-22 16:04:25 +02:00
d9b42e2b43 Fix invalid fonction definition with unnamed param 2023-06-22 14:54:54 +02:00
8036b289a6 move typedefs alltogether 2023-06-22 14:54:31 +02:00
e1570fa34a latest brlib version 2023-06-21 14:36:45 +02:00
aaa9cb8690 switch to s8...s64 and u8...u64 integer notation 2021-11-20 19:22:57 +01:00
05a64ec742 simplify 0x88 macros / remove redundant chessdefs.h equivalent ones 2021-11-20 16:15:45 +01:00
6b2c1702f6 revert to color at LSB, to avoid easy mistakes
close #3
2021-11-19 12:15:53 +01:00
b582798751 nes fen cases 2021-11-19 11:43:01 +01:00
097dae84d6 Refuckulate castle_Q: forgot a pointer when copying from castle_K 2021-11-16 21:47:40 +01:00
da0ee468e0 add prpieces(): print position White & Black pieces list 2021-11-16 21:46:22 +01:00
42 changed files with 2135 additions and 2331 deletions

3
.dir-locals.el Normal file
View File

@@ -0,0 +1,3 @@
((nil . ((compile-command . (concat "make -C "
(vc-root-dir)
" -k -j2")))))

25
.gitignore vendored
View File

@@ -1,16 +1,17 @@
core
vgcore.*
GPATH
GRTAGS
GTAGS
fen
pool
piece
move
bits
eval
debug
bodichess
*.s
/test/
*.i
*.old
*.save
/.ccls-root
/.ccls-cache/
/obj/
/lib/
/libobj/
/bin/
/dep/
/tmp/
/notused/
valgrind.out
compile_commands.json

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "brlib"]
path = brlib
url = ../brlib.git

View File

@@ -1,21 +0,0 @@
4k3/4p3/8/b7/1BR1p2p/1Q3P2/5N2/4K3 w - - 0 1
r1bq1rk1/pppp1ppp/2n2n2/4p3/2B1P3/3PPN2/PPP3PP/RN1QK2R b KQ - 1 7
6k1/6pp/R2p4/p1p5/8/1P1r3P/6P1/6K1 b - - 3 37
# both can castle queen only
r3k2r/8/3B4/8/8/3b4/8/R3K2R w KQkq - 0 1
r3k2r/8/3BB3/8/8/3bb3/8/R3K2R w KQkq - 0 1
r2bkb1r/8/8/8/8/3bb3/8/R2BKB1R w KQkq - 0 1
# illegal positions
4k3/8/8/8/7b/8/8/4K3 b - - 0 1
2r1k3/3P4/8/8/8/8/8/4K3 w - - 0 1
# only kings on A1/H8
k7/8/8/8/8/8/8/K7 b - - 0 1
# only one move possible (Pf2xBg3)
k7/8/8/1p1p4/pPpPp3/P1PpPpb1/NBNP1P2/KBB1B3 w - - 0 1
# only 2 moves possible (Ph5xg6 e.p., Ph5-h6)
k7/8/8/1p1p2pP/pPpPp3/P1PpPp2/NBNP1P2/KBB1B3 w - g6 0 1

351
Makefile
View File

@@ -1,111 +1,300 @@
# Makefile - GNU make only.
#
# Copyright (C) 2021 Bruno Raoult ("br")
# Copyright (C) 2021-2023 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>.
#
# 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.html>.
#
# SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
#
BINDIR := ./bin
SRCDIR := ./src
OBJDIR := ./obj
DEPS := make.deps
SHELL := /bin/bash
CC := gcc
LD := ld
BEAR := bear
TOUCH := touch
RM := rm
RMDIR := rmdir
SRC=$(wildcard $(SRCDIR)/*.c)
INC=$(wildcard $(SRCDIR)/*.h)
SRC_S=$(notdir $(SRC))
SRCDIR := ./src
INCDIR := ./include
OBJDIR := ./obj
CC=gcc
LIBSRCDIR := ./libsrc
LIBOBJDIR := ./libobj
.SECONDEXPANSION:
OBJ=$(addprefix $(OBJDIR)/,$(SRC_S:.c=.o))
BIN=fen pool piece move debug eval bits bodichess
BINDIR := ./bin
LIBDIR := ./lib
DEPDIR := ./dep
LIBS = -lreadline -lncurses
CFLAGS += -std=gnu99
CCLSROOT := .ccls-root
CCLSFILE := compile_commands.json
#CFLAGS += -O2
CFLAGS += -g
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -march=native
#CFLAGS += -pedantic
#CFLAGS += -Wno-pointer-arith
#CFLAGS += -Werror
CFLAGS += -Wmissing-declarations
SRC := $(wildcard $(SRCDIR)/*.c) # project sources
SRC_FN := $(notdir $(SRC)) # source basename
OBJ := $(addprefix $(OBJDIR)/,$(SRC_FN:.c=.o))
##################################### DEBUG flags
CFLAGS += -DDEBUG # global
CFLAGS += -DDEBUG_POOL # memory pools management
CFLAGS += -DDEBUG_FEN # FEN decoding
CFLAGS += -DDEBUG_MOVE # move generation
CFLAGS += -DDEBUG_EVAL # eval functions
CFLAGS += -DDEBUG_PIECE # piece list management
#CFLAGS += -DDEBUG_BITS # bits functions (take care !)
LIBSRC := $(wildcard $(LIBSRCDIR)/*.c) # lib sources
LIBSRC_FN := $(notdir $(LIBSRC)) # lib sources basename
LIBOBJ := $(addprefix $(LIBOBJDIR)/,$(LIBSRC_FN:.c=.o)) # and lib obj ones
#CFLAGS += -DDEBUG_EVAL # sleep 1 sec within main loop (SIGINTR test)
#CFLAGS += -DDEBUG_EVAL2 # eval 2
#CFLAGS += -DDEBUG_EVAL3 # eval 3
#CFLAGS += -DDEBUG_MEM # malloc
LIB := br_$(shell uname -m) # library name
SLIB := $(addsuffix .a, $(LIBDIR)/lib$(LIB)) # static lib
DLIB := $(addsuffix .so, $(LIBDIR)/lib$(LIB)) # dynamic lib
.PHONY: all cflags clean
DEP_FN := $(SRC_FN) $(LIBSRC_FN)
DEP := $(addprefix $(DEPDIR)/,$(DEP_FN:.c=.d))
compile: cflags $(OBJ) $(BIN)
TARGET_FN := brchess
TARGET := $(addprefix $(BINDIR)/,$(TARGET_FN))
cflags:
@echo CFLAGS used: $(CFLAGS)
LDFLAGS := -L$(LIBDIR)
LIBS := -l$(LIB) -lreadline -lncurses
all: clean compile
##################################### pre-processor flags
CPPFLAGS := -I$(INCDIR)
#CPPFLAGS += -DDEBUG # global
CPPFLAGS += -DDEBUG_DEBUG # enable log() functions
#CPPFLAGS += -DDEBUG_DEBUG_C # enable verbose log() settings
CPPFLAGS += -DDEBUG_POOL # memory pools management
CPPFLAGS += -DDEBUG_FEN # FEN decoding
CPPFLAGS += -DDEBUG_MOVE # move generation
CPPFLAGS += -DDEBUG_EVAL # eval functions
CPPFLAGS += -DDEBUG_PIECE # piece list management
CPPFLAGS += -DDEBUG_SEARCH # move search
$(DEPS): $(SRC) $(INC)
@echo generating dependancies.
@$(CC) -MM $(SRC) > $@
@sed -i "s|\(.*\.o\):|${OBJDIR}/\0:|" $@
# remove extraneous spaces (due to spaces before comments)
CPPFLAGS := $(strip $(CPPFLAGS))
include $(DEPS)
##################################### compiler flags
CFLAGS := -std=gnu11
#CFLAGS += -O2
CFLAGS += -g
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -march=native
CFLAGS += -Wmissing-declarations
# for gprof
# CFLAGS += -pg
# Next one may be useful for valgrind (when invalid instructions)
# CFLAGS += -mno-tbm
clean:
rm -rf $(OBJ) core $(BIN)
CFLAGS := $(strip $(CFLAGS))
#$(OBJ): $(OBJDIR)/%.o: $(SRCDIR)/%.c
# @mkdir -p $(@D)
# $(CC) -c $(CFLAGS) -o $@ $<
$(OBJDIR)/%.o:
@mkdir -p $(@D)
@echo compiling $@.
@$(CC) -c $(CFLAGS) -o $@ $<
##################################### archiver/linker/dependency flags
ARFLAGS := rcs
LDFLAGS := -L$(LIBDIR)
DEPFLAGS = -MMD -MP -MF $(DEPDIR)/$*.d
#fen: CFLAGS+=-DBIN_$$@
#$(BIN): $$(subst $(OBJDIR)/$$@.o,,$(OBJ)) $(SRCDIR)/$$@.c
# @echo compiling $@.
# @$(CC) -DBIN_$@ $(CFLAGS) $^ $(LIBS) -o $@
##################################### General targets
.PHONY: all compile clean cleanall
# TODO: find a better dependancy graph
$(BIN): $$(subst $(OBJDIR)/$$@.o,,$(OBJ)) $(SRCDIR)/$$@.c
@echo compiling $@.
@echo NEED_TO_CHANGE_THIS=$^
@$(CC) -DBIN_$@ $(CFLAGS) $^ $(LIBS) -o $@
all: $(TARGET)
#pool: CFLAGS+=-DPOOLBIN
#pool: $$(subst $(OBJDIR)/$$@.o,,$(OBJ)) $(SRCDIR)/$$@.c
# $(CC) $(CFLAGS) $^ -o $@
compile: libobjs objs
# piece: CFLAGS+=-DPIECEBIN
# piece: $$(subst $(OBJDIR)/$$@.o,,$(OBJ)) $(SRCDIR)/$$@.c
# $(CC) $(CFLAGS) $^ -o $@
clean: cleandep cleanobj cleanlib cleanlibobj cleanbin
# move: CFLAGS+=-DMOVEBIN
# move: $$(subst $(OBJDIR)/$$@.o,,$(OBJ)) $(SRCDIR)/$$@.c
# $(CC) $(CFLAGS) $^ -o $@
cleanall: clean cleandepdir cleanobjdir cleanlibdir cleanlibobjdir cleanbindir
# debug: CFLAGS+=-DDEBUGBIN
# debug: $$(subst $(OBJDIR)/$$@.o,,$(OBJ)) $(SRCDIR)/$$@.c
# $(CC) $(CFLAGS) $^ -o $@
##################################### cleaning functions
# rmfiles - deletes a list of files in a directory if they exist.
# $(1): the directory
# $(2): the list of files to delete
# $(3): The string to include in action output - "cleaning X files."
# see: https://stackoverflow.com/questions/6783243/functions-in-makefiles
#
# Don't use wildcard like "$(DIR)/*.o", so we can control mismatches between
# list and actual files in directory.
# See rmdir below.
define rmfiles
@#echo "rmfiles=+$(1)+"
$(eval $@_EXIST = $(wildcard $(1)))
@#echo "existfile=+${$@_EXIST}+"
@if [[ -n "${$@_EXIST}" ]]; then \
echo "cleaning $(2) files." ; \
$(RM) ${$@_EXIST} ; \
fi
endef
#.PHONY: bits
#bits2: src/bits.c
# $(CC) $(CFLAGS) -S $^ -o $@.s
# $(CC) $(CFLAGS) $^ -o $@
# rmdir - deletes a directory if it exists.
# $(1): the directory
# $(2): The string to include in action output - "removing X dir."
#
# Don't use $(RM) -rf, to control unexpected dep files.
# See rmfile above.
define rmdir
@#echo "rmdir +$(1)+"
$(eval $@_EXIST = $(wildcard $(1)))
@#echo "existdir=+${$@_EXIST}+"
@if [[ -n "${$@_EXIST}" ]]; then \
echo "removing $(2) dir." ; \
$(RMDIR) ${$@_EXIST} ; \
fi
endef
##################################### dirs creation
.PHONY: alldirs
ALLDIRS := $(DEPDIR) $(OBJDIR) $(LIBDIR) $(LIBOBJDIR) $(BINDIR)
alldirs: $(ALLDIRS)
# Here, we have something like:
# a: a
# a will be built if (1) older than a, or (2) does not exist. Here only (2).
$(ALLDIRS): $@
@echo creating $@ directory.
@mkdir -p $@
##################################### Dependencies files
.PHONY: cleandep cleandepdir
-include $(wildcard $(DEP))
# Don't use $(DEPDIR)/*.d, to control mismatches between dep and src files.
# See second rule below.
cleandep:
$(call rmfiles,$(DEP),depend)
@#echo cleaning dependency files.
@#$(RM) -f $(DEP)
cleandepdir:
$(call rmdir,$(DEPDIR),depend)
@#[ -d $(DEPDIR) ] && echo cleaning depend files && $(RM) -f $(DEP) || true
##################################### brchess objects
.PHONY: objs cleanobj cleanobjdir
objs: $(OBJ)
cleanobj:
$(call rmfiles,$(OBJ),object)
cleanobjdir: cleanobj
$(call rmdir,$(OBJDIR),objects)
# The part right of '|' are "order-only prerequisites": They are build as
# "normal" ones, but do not imply to rebuild target.
$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR) $(DEPDIR)
@echo compiling brchess $< "->" $@.
@$(CC) -c $(DEPFLAGS) $(CPPFLAGS) $(CFLAGS) $< -o $@
##################################### brlib objects
.PHONY: libobjs cleanlibobj cleanlibobjdir
libobjs: $(LIBOBJ)
cleanlibobj:
$(call rmfiles,$(LIBOBJ),brlib object)
cleanlibobjdir:
$(call rmdir,$(LIBOBJDIR),brlib objects)
$(LIBOBJDIR)/%.o: $(LIBSRCDIR)/%.c | $(LIBOBJDIR) $(DEPDIR)
@echo compiling library $< "->" $@.
$(CC) -c $(DEPFLAGS) $(CPPFLAGS) $(CFLAGS) $< -o $@
##################################### brlib libraries
.PHONY: libs cleanlib cleanlibdir
cleanlib:
$(call rmfiles,$(DLIB) $(SLIB),library)
cleanlibdir:
$(call rmdir,$(LIBDIR),libraries)
libs: $(DLIB) $(SLIB)
$(DLIB): CFLAGS += -fPIC
$(DLIB): LDFLAGS += -shared
$(DLIB): $(LIBOBJ) | $(LIBDIR)
@echo building $@ shared library.
@$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@
$(SLIB): $(LIBOBJ) | $(LIBDIR)
@echo building $@ static library.
$(AR) $(ARFLAGS) $@ $^
##################################### brchess binaries
.PHONY: targets cleanbin cleanbindir
targets: $(TARGET)
cleanbin:
$(call rmfiles,$(TARGET),binary)
cleanbindir:
$(call rmdir,$(BINDIR),binaries)
# We don't use static lib, but we could build it here
$(TARGET): $(DLIB) $(OBJ) | $(BINDIR) $(SLIB)
@echo generating $@ executable.
@$(CC) $(LDFLAGS) $(OBJ) $(LIBS) -o $@
##################################### pre-processed (.i) and assembler (.s) output
%.i: %.c
@echo generating $@
@$(CC) -E $(CPPFLAGS) $(CFLAGS) $< -o $@
%.s: %.c
@echo generating $@
@$(CC) -S -fverbose-asm $(CPPFLAGS) $(CFLAGS) $< -o $@
##################################### LSP (ccls)
.PHONY: ccls
ccls: $(CCLSFILE)
$(CCLSROOT):
@echo creating project root file.
@$(TOUCH) $@
# generate compile_commands.json.
# Need to add includes and Makefile dependencies.
# also, if cclsfile is newer than sources, no need to clean objects file
# (and to run bear).
# maybe run cleanobj cleanlibobj in commands ?
$(CCLSFILE): cleanobj cleanlibobj $(SRC) $(LIBSRC) | $(CCLSROOT)
@echo "Generating ccls compile commands file ($@)."
@$(BEAR) -- make compile
#.PHONY: bear
#bear: cleanobj cleanlibobj Makefile | $(CCLSROOT)
# @$(BEAR) -- make compile
##################################### valgrind (mem check)
.PHONY: memcheck
VALGRIND = valgrind
VALGRINDFLAGS = --leak-check=full --show-leak-kinds=all
VALGRINDFLAGS += --track-origins=yes --sigill-diagnostics=yes
VALGRINDFLAGS += --quiet --show-error-list=yes
VALGRINDFLAGS += --log-file=valgrind.out
# We need to suppress libreadline leaks here. See :
# https://stackoverflow.com/questions/72840015
VALGRINDFLAGS += --suppressions=etc/libreadline.supp
memcheck: targets
@$(VALGRIND) $(VALGRINDFLAGS) $(BINDIR)/brchess
##################################### Makefile debug
.PHONY: showflags wft
showflags:
@echo CFLAGS: "$(CFLAGS)"
@echo CPPFLAGS: $(CPPFLAGS)
@echo DEPFLAGS: $(DEPFLAGS)
@echo LDFLAGS: $(LDFLAGS)
@echo DEPFLAGS: $(DEPFLAGS)
wtf:
@printf "LIBOBJDIR=%s\n\n" "$(LIBOBJDIR)"
@printf "LIBOBJ=%s\n\n" "$(LIBOBJ)"
@printf "OBJDIR=%s\n\n" "$(OBJDIR)"
@printf "OBJ=%s\n\n" "$(OBJ)"
@#echo LIBOBJ=$(LIBOBJ)
@#echo DEP=$(DEP)
@#echo LIBSRC=$(LIBSRC)

1
README.org Normal file
View File

@@ -0,0 +1 @@
A basic chess program. The goal is not to make it good, but at least to be able to play a full game - Far to be the case !

57
STATUS.org Normal file
View File

@@ -0,0 +1,57 @@
#+OPTIONS: toc:nil
* Misc
- License: [[https://www.gnu.org/licenses/gpl-3.0-standalone.html][GNU General Public License v3.0 or later]].
- Code is in C (gnu11 = C11 + some GNU extensions), tested only on Linux x86-64.
- [[https://git.raoult.com/bruno/brchess][Source Code]].
* Done or partially done
*** Text interface
- basic commands like "init", "fen xxxx", "depth n", "search", "pvs", etc...
*** Dual [[https://en.wikipedia.org/wiki/0x88][0x88]] / [[https://en.wikipedia.org/wiki/Bitboard#Chess_bitboards][bitboard]] representations
- I started with a [[https://en.wikipedia.org/wiki/0x88][0x88 board representation]]
- But... wanted to switch to [[https://en.wikipedia.org/wiki/Bitboard#Chess_bitboards][bitboard]] lately
- Today there is a messy mix of the two representations.
*** A [[https://www.chessprogramming.org/Pseudo-Legal_Move][pseudo-legal move]] generator
- Does not check for some invalid moves (especially king-pinned pieces moves, which could be very expensive).
I believe some programs do not do it too, and prefer to see invalid positions at next ply (TODO).
- Still with 0x88 board (needs to be rewritten with bitboards).
*** *Very expensive* pieces and moves list
- They should be converted into arrays, to allow fast duplication for move_do(), the function which actually makes a move.
*** A basic eval function
- preferred squares for pieces
- mobility
*** "Move Search" functions
**** Basic [[https://en.wikipedia.org/wiki/Negamax][negamax]] search function
- No [[https://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning][alpha-beta pruning]]
**** [[https://en.wikipedia.org/wiki/Principal_variation_search][Principal Variation Search]] (PVS) function
- Alpha-beta pruning
- Basic moves [[https://www.chessprogramming.org/Move_Ordering][pre-ordering]]
It makes the PVS/alpha-beta pruning quite decent. For example, a 6 ply depth search gives:
- negamax: 1,196 secs for 125,799 Knodes
- PVS: 14 secs for 1,575 Knodes
- Both make search at a fixed depth (still no [[https://www.chessprogramming.org/Quiescence_Search][quiescence search]] at terminal nodes).
* Missing
*** All Chess rules
- Mate/Pat detection (!)
Not trivial, as using pseudo-valid moves implies mate/pat detection is late
- Special rules like 50 moves/position repetition
*** Actual game management
- Play a move, ask engine to play a move
- A standard interface for usual software (like SCID)
Probably [[https://www.gnu.org/software/xboard/engine-intf.html][xboard]] first, as it looks simpler than [[https://en.wikipedia.org/wiki/Universal_Chess_Interface][UCI]].
*** Search improvement
- Book-keeping of moves during search (did not decide the method).
- [[https://en.wikipedia.org/wiki/Zobrist_hashing][Positions hashing]] / transposition detection
* Next steps planned (no specific order)
*** Memory cleanup
- proper recusive position/moves cleanup in search() and pvs()
- use valgrind to find possible other unfreed memory
*** Replace the current interface with a basic xboard one
1. Check which commands are necessary/mandatory.
1. This will allow easy testing with common software
*** Mate/Pat detection
*** In Search, detect King capture
*** In Search, do something when no move available

1
brlib Submodule

Submodule brlib added at 97d6570be7

View File

@@ -1 +0,0 @@
((c-mode . ((compile-command . "make -C ../build -j2 whatever"))))

6
etc/libreadline.supp Normal file
View File

@@ -0,0 +1,6 @@
{
ignore_libreadline_leaks
Memcheck:Leak
...
obj:*/libreadline.so.*
}

View File

@@ -1,17 +0,0 @@
./obj/bits.o:: src/bits.c src/bits.h src/debug.h
./obj/bodichess.o:: src/bodichess.c src/chessdefs.h src/bits.h src/debug.h \
src/board.h src/piece.h src/list.h src/pool.h src/move.h src/position.h \
src/fen.h src/eval.h src/bodichess.h
./obj/debug.o:: src/debug.c src/debug.h
./obj/eval.o:: src/eval.c src/eval.h src/position.h src/chessdefs.h src/bits.h \
src/debug.h src/board.h src/piece.h src/list.h src/pool.h
./obj/fen.o:: src/fen.c src/debug.h src/chessdefs.h src/bits.h src/position.h \
src/board.h src/piece.h src/list.h src/pool.h src/fen.h
./obj/move.o:: src/move.c src/chessdefs.h src/bits.h src/debug.h src/board.h \
src/piece.h src/list.h src/pool.h src/move.h src/position.h
./obj/piece.o:: src/piece.c src/chessdefs.h src/bits.h src/debug.h src/piece.h \
src/list.h src/pool.h src/board.h src/position.h
./obj/pool.o:: src/pool.c src/list.h src/pool.h src/debug.h
./obj/position.o:: src/position.c src/chessdefs.h src/bits.h src/debug.h \
src/position.h src/board.h src/piece.h src/list.h src/pool.h src/move.h \
src/fen.h

26
scripts/env.sh Executable file
View File

@@ -0,0 +1,26 @@
#!/usr/bin/env bash
#
# env.sh - set environment for brchess developer.
#
# Copyright (C) 2023 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.html>.
#
# SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
#
# USAGE: source env.sh [arg]
#
# This file will actually be sourced if it was never sourced in current bash
# environment.
if [[ ! -v _BRCHESS_ENV_ ]]; then
export _BRCHESS_ENV_=1 BRCHESS_ROOT BRLIB_DIR LD_LIBRARY_PATH
BRCHESS_ROOT=$(realpath -L "$(dirname "${BASH_SOURCE[0]}")/..")
BRLIB_DIR="$BRCHESS_ROOT/brlib/lib"
LD_LIBRARY_PATH=/mypath${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
#printf "R=%s L=%s LD=%s\n" "$BRCHESS_ROOT" "$BRLIB_DIR" "$LD_LIBRARY_PATH"
printf "Chess environment complete.\n"
fi

70
src/bitboard.h Normal file
View File

@@ -0,0 +1,70 @@
/* bitboard.h - bitboard 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.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#ifndef BITBOARD_H
#define BITBOARD_H
#include <bits.h>
#include "chessdefs.h"
#include "piece.h"
enum bb_square {
A1 = 1UL << 0, B1 = 1UL << 1, C1 = 1UL << 2, D1 = 1UL << 3,
E1 = 1UL << 4, F1 = 1UL << 5, G1 = 1UL << 6, H1 = 1UL << 7,
A2 = 1UL << 8, B2 = 1UL << 9, C2 = 1UL << 10, D2 = 1UL << 11,
E2 = 1UL << 12, F2 = 1UL << 13, G2 = 1UL << 14, H2 = 1UL << 15,
A3 = 1UL << 16, B3 = 1UL << 17, C3 = 1UL << 18, D3 = 1UL << 19,
E3 = 1UL << 20, F3 = 1UL << 21, G3 = 1UL << 22, H3 = 1UL << 23,
A4 = 1UL << 24, B4 = 1UL << 25, C4 = 1UL << 26, D4 = 1UL << 27,
E4 = 1UL << 28, F4 = 1UL << 29, G4 = 1UL << 30, H4 = 1UL << 31,
A5 = 1UL << 32, B5 = 1UL << 33, C5 = 1UL << 34, D5 = 1UL << 35,
E5 = 1UL << 36, F5 = 1UL << 37, G5 = 1UL << 38, H5 = 1UL << 39,
A6 = 1UL << 40, B6 = 1UL << 41, C6 = 1UL << 42, D6 = 1UL << 43,
E6 = 1UL << 44, F6 = 1UL << 45, G6 = 1UL << 46, H6 = 1UL << 47,
A7 = 1UL << 48, B7 = 1UL << 49, C7 = 1UL << 50, D7 = 1UL << 51,
E7 = 1UL << 52, F7 = 1UL << 53, G7 = 1UL << 54, H7 = 1UL << 55,
A8 = 1UL << 56, B8 = 1UL << 57, C8 = 1UL << 58, D8 = 1UL << 59,
E8 = 1UL << 60, F8 = 1UL << 61, G8 = 1UL << 62, H8 = 1UL << 63,
};
enum bb_files {
F_1 = A1 | A2 | A3 | A4 | A5 | A6 | A7 | A8,
F_2 = B1 | B2 | B3 | B4 | B5 | B6 | B7 | B8,
F_3 = C1 | C2 | C3 | C4 | C5 | C6 | C7 | C8,
F_4 = D1 | D2 | D3 | D4 | D5 | D6 | D7 | D8,
F_5 = E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8,
F_6 = F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8,
F_7 = G1 | G2 | G3 | G4 | G5 | G6 | G7 | G8,
F_8 = H1 | H2 | H3 | H4 | H5 | H6 | H7 | H8,
};
enum bb_ranges {
R_1 = A1 | B1 | C1 | D1 | E1 | F1 | G1 | H1,
R_2 = A2 | B2 | C2 | D2 | E2 | F2 | G2 | H2,
R_3 = A3 | B3 | C3 | D3 | E3 | F3 | G3 | H3,
R_4 = A4 | B4 | C4 | D4 | E4 | F4 | G4 | H4,
R_5 = A5 | B5 | C5 | D5 | E5 | F5 | G5 | H5,
R_6 = A6 | B6 | C6 | D6 | E6 | F6 | G6 | H6,
R_7 = A7 | B7 | C7 | D7 | E7 | F7 | G7 | H7,
R_8 = A8 | B8 | C8 | D8 | E8 | F8 | G8 | H8,
};
#endif /* BOARD_H */

View File

@@ -1,85 +0,0 @@
/* bits.c - bits.h tests
*
* 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 "bits.h"
#ifdef BIN_bits
#include <stdio.h>
#include <stdlib.h>
static inline int _popcount64(u64 n)
{
int count = 0;
while (n) {
count++;
n &= (n - 1);
}
return count;
}
static inline int _ctz64(u64 n)
{
return _popcount64((n & -n) - 1);
}
static inline int _clz64(u64 n)
{
u64 r, q;
r = (n > 0xFFFFFFFF) << 5; n >>= r;
q = (n > 0xFFFF) << 4; n >>= q; r |= q;
q = (n > 0xFF ) << 3; n >>= q; r |= q;
q = (n > 0xF ) << 2; n >>= q; r |= q;
q = (n > 0x3 ) << 1; n >>= q; r |= q;
r |= (n >> 1);
return __WORDSIZE - r - 1;
}
static inline int _ffs64(u64 n)
{
if (n == 0)
return (0);
return _popcount64(n ^ ~-n);
}
int main(int ac, char **av)
{
u64 u = 123, _tmp;
int curbit;
int base = 10;
debug_init(0);
if (ac > 2)
base = atoi(*(av+2));
if (ac > 1) {
u = strtoul(*(av+1), NULL, base);
printf("base=%d input=%#lx\n", base, u);
printf("popcount64(%lu) = %d/%d\n", u, popcount64(u), _popcount64(u));
printf("ctz64(%lu) = %d/%d\n", u, ctz64(u), _ctz64(u));
printf("clz64(%lu) = %d/%d\n", u, clz64(u), _clz64(u));
printf("ffs64(%lu) = %d/%d\n", u, ffs64(u), _ffs64(u));
printf("\n");
bit_for_each64(curbit, _tmp, u) {
printf("loop: curbit=%d tmp=%ld\n", curbit, _tmp);
}
printf("\n");
bit_for_each64_2(curbit, _tmp, u) {
printf("loop2: curbit=%d tmp=%ld\n", curbit, _tmp);
}
}
return 0;
}
#endif /* BIN_bits */

View File

@@ -1,163 +0,0 @@
/* bits.h - bits functions.
*
* 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 BITS_H
#define BITS_H
#include <stdint.h>
/* next include will define __WORDSIZE: 32 or 64
*/
#include <bits/wordsize.h>
#include "debug.h"
#ifndef __has_builtin
#define __has_builtin(x) 0
#endif
/* no plan to support 32bits for now...
*/
#if __WORDSIZE != 64
ERROR_64_BYTES_WORDSIZE_ONLY
#endif
typedef uint64_t u64;
typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t u8;
typedef unsigned int uint;
typedef unsigned char uchar;
/* count trailing zeroes : 00101000 -> 3
* ^^^
*/
static inline int ctz64(u64 n)
{
# if __has_builtin(__builtin_ctzl)
# ifdef DEBUG_BITS
log_f(1, "builtin ctzl.\n");
# endif
return __builtin_ctzl(n);
# elif __has_builtin(__builtin_clzl)
# ifdef DEBUG_BITS
log_f(1, "builtin clzl.\n");
# endif
return __WORDSIZE - (__builtin_clzl(n & -n) + 1);
# else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
return popcount64((n & n) 1);
# endif
}
/* count leading zeroes : 00101000 -> 2
* ^^
*/
static inline int clz64(u64 n)
{
# if __has_builtin(__builtin_clzl)
# ifdef DEBUG_BITS
log_f(1, "builtin.\n");
# endif
return __builtin_clzl(n);
# else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
u64 r, q;
r = (n > 0xFFFFFFFF) << 5; n >>= r;
q = (n > 0xFFFF) << 4; n >>= q; r |= q;
q = (n > 0xFF ) << 3; n >>= q; r |= q;
q = (n > 0xF ) << 2; n >>= q; r |= q;
q = (n > 0x3 ) << 1; n >>= q; r |= q;
r |= (n >> 1);
return __WORDSIZE - r - 1;
# endif
}
/* find first set : 00101000 -> 4
* ^
*/
static inline uint ffs64(u64 n)
{
# if __has_builtin(__builtin_ffsl)
# ifdef DEBUG_BITS
log_f(1, "builtin ffsl.\n");
# endif
return __builtin_ffsll(n);
# elif __has_builtin(__builtin_ctzl)
# ifdef DEBUG_BITS
log_f(1, "builtin ctzl.\n");
# endif
if (n == 0)
return (0);
return __builtin_ctzl(n) + 1;
# else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
return popcount64(n ^ ~-n);
# endif
}
static inline int popcount64(u64 n)
{
# if __has_builtin(__builtin_popcountl)
# ifdef DEBUG_BITS
log_f(1, "builtin.\n");
# endif
return __builtin_popcountl(n);
# else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
int count = 0;
while (n) {
count++;
n &= (n - 1);
}
return count;
# endif
}
/** bit_for_each64 - iterate over an u64 bits
* @pos: an int used as current bit
* @tmp: a temp u64 used as temporary storage
* @ul: the u64 to loop over
*
* Usage:
* u64 u=139, _t; // u=b10001011
* int cur;
* bit_for_each64(cur, _t, u) {
* printf("%d\n", cur);
* }
* This will display the position of each bit in u: 1, 2, 4, 8
*
* I should probably re-think the implementation...
*/
#define bit_for_each64(pos, tmp, ul) \
for (tmp = ul, pos = ffs64(tmp); tmp; tmp &= (tmp - 1), pos = ffs64(tmp))
/** or would it be more useful (counting bits from zero instead of 1) ?
*/
#define bit_for_each64_2(pos, tmp, ul) \
for (tmp = ul, pos = ctz64(tmp); tmp; tmp ^= 1<<pos, pos = ctz64(tmp))
#endif /* BITS_H */

View File

@@ -5,7 +5,7 @@
* 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>.
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
@@ -27,9 +27,12 @@ typedef struct board_s {
/* 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) >> 4) /* from sq88 to rank */
#define SQ88(f, r) (((r) << 4) | (f)) /* from rank,file to sq88 */
#define F88(s) ((s) & 0x0f) /* from sq88 to file */
#define R88(s) ((s) >> 4) /* from sq88 to rank */
#define SETF88(s, r) ((s) &= 0xf0, (s) |= (r))
#define SETR88(s, f) ((s) &= 0x0f, (s) |= (f)<<4)
#define SQ88_NOK(s) ((s) & 0x88) /* invalid square */
#define SQ88_OK(s) (!SQ88_NOK(s))
@@ -37,7 +40,7 @@ typedef struct board_s {
/* definitions for bitboard representation
*/
#define BB(f, r) (1ULL << (8 * (r) + (f))) /* from rank,file to bitboard */
#define SQ88_2_BB(s) (BB(FILE88(s), RANK88(s))) /* from sq88 to bitboard */
#define SQ88_2_BB(s) (BB(F88(s), R88(s))) /* from sq88 to bitboard */
#define FILEBB(b) ((b) % 8) /* from sq88 to file */
#define RANKBB(b) ((b) / 8) /* from sq88 to rank */
@@ -74,32 +77,4 @@ enum x88_square {
x88_A8=0x70, x88_B8, x88_C8, x88_D8, x88_E8, x88_F8, x88_G8, x88_H8,
};
/* necessary not to become mad to set bitboards
*/
enum bb_square{
A1=(u64)1, B1=(u64)A1<<1, C1=(u64)B1<<1, D1=(u64)C1<<1,
E1=(u64)D1<<1, F1=(u64)E1<<1, G1=(u64)F1<<1, H1=(u64)G1<<1,
A2=(u64)A1<<8, B2=(u64)B1<<8, C2=(u64)C1<<8, D2=(u64)D1<<8,
E2=(u64)E1<<8, F2=(u64)F1<<8, G2=(u64)G1<<8, H2=(u64)H1<<8,
A3=(u64)A2<<8, B3=(u64)B2<<8, C3=(u64)C2<<8, D3=(u64)D2<<8,
E3=(u64)E2<<8, F3=(u64)F2<<8, G3=(u64)G2<<8, H3=(u64)H2<<8,
A4=(u64)A3<<8, B4=(u64)B3<<8, C4=(u64)C3<<8, D4=(u64)D3<<8,
E4=(u64)E3<<8, F4=(u64)F3<<8, G4=(u64)G3<<8, H4=(u64)H3<<8,
A5=(u64)A4<<8, B5=(u64)B4<<8, C5=(u64)C4<<8, D5=(u64)D4<<8,
E5=(u64)E4<<8, F5=(u64)F4<<8, G5=(u64)G4<<8, H5=(u64)H4<<8,
A6=(u64)A5<<8, B6=(u64)B5<<8, C6=(u64)C5<<8, D6=(u64)D5<<8,
E6=(u64)E5<<8, F6=(u64)F5<<8, G6=(u64)G5<<8, H6=(u64)H5<<8,
A7=(u64)A6<<8, B7=(u64)B6<<8, C7=(u64)C6<<8, D7=(u64)D6<<8,
E7=(u64)E6<<8, F7=(u64)F6<<8, G7=(u64)G6<<8, H7=(u64)H6<<8,
A8=(u64)A7<<8, B8=(u64)B7<<8, C8=(u64)C7<<8, D8=(u64)D7<<8,
E8=(u64)E7<<8, F8=(u64)F7<<8, G8=(u64)G7<<8, H8=(u64)H7<<8,
};
#endif /* BOARD_H */

View File

@@ -1,39 +1,42 @@
/* bodichess.c - main loop.
/* brchess.c - main loop.
*
* Copyright (C) 2021 Bruno Raoult ("br")
* Copyright (C) 2021-2023 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>.
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#include <stdio.h>
#include <err.h>
#include <stdlib.h>
#include <string.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <br.h>
#include <list.h>
#include <debug.h>
#include "brchess.h"
#include "chessdefs.h"
#include "board.h"
#include "piece.h"
#include "move.h"
#include "list.h"
#include "debug.h"
#include "fen.h"
#include "eval.h"
#include "bodichess.h"
#include "eval-simple.h"
#include "search.h"
typedef struct {
struct command {
char *name; /* User printable name */
int (*func)(pos_t *, char *); /* function doing the job */
int (*func)(pos_t *, char *); /* function doing the job */
char *doc; /* function doc */
} COMMAND;
};
/* readline example inspired by :
* - https://thoughtbot.com/blog/tab-completion-in-gnu-readline
@@ -44,39 +47,54 @@ char *commands_generator(const char *, int);
char *escape(const char *);
int quote_detector(char *, int);
int execute_line (pos_t *, char *line);
COMMAND *find_command (char *);
struct command *find_command (char *);
char *stripwhite (char *string);
/* The names of functions that actually do the manipulation. */
int do_help(pos_t *, char*);
int do_fen(pos_t *, char*);
int do_init(pos_t *, char*);
int do_pos(pos_t *, char*);
int do_genmoves(pos_t *, char*);
int do_prmoves(pos_t *, char*);
int do_prmovepos(pos_t *pos, char *arg);
//int do_prmovepos(pos_t *pos, char *arg);
int do_prpieces(pos_t *pos, char *arg);
int do_memstats(pos_t *, char*);
int do_eval(pos_t *, char*);
int do_simple_eval(pos_t *, char*);
int do_move(pos_t *, char*);
int do_quit(pos_t *, char*);
int do_debug(pos_t *, char*);
int do_depth(pos_t *, char*);
int do_search(pos_t *, char*);
int do_pvs(pos_t *, char*);
COMMAND commands[] = {
struct command commands[] = {
{ "help", do_help, "Display this text" },
{ "?", do_help, "Synonym for 'help'" },
{ "fen", do_fen, "Set position to FEN" },
{ "init", do_init, "Set position to normal start position" },
{ "pos", do_pos, "Print current position" },
{ "quit", do_quit, "Quit" },
{ "genmove", do_genmoves, "Generate next move list" },
{ "genmove", do_genmoves, "Generate move list for " },
{ "prmoves", do_prmoves, "Print position move list" },
{ "prmovepos", do_prmovepos, "Print Nth move resulting position" },
// { "prmovepos", do_prmovepos, "Print Nth move resulting position" },
{ "prpieces", do_prpieces, "Print Pieces (from pieces lists)" },
{ "memstats", do_memstats, "Generate next move list" },
{ "eval", do_eval, "Eval current position" },
{ "simple-eval", do_simple_eval, "Simple eval current position" },
{ "do_move", do_move, "execute nth move on current position" },
{ "debug", do_debug, "Set log level to LEVEL" },
{ "depth", do_depth, "Set search depth to N" },
{ "search", do_search, "Search best move (negamax)" },
{ "pvs", do_pvs, "Search best move (Principal Variation Search)" },
{ NULL, (int(*)()) NULL, NULL }
};
static int done=0;
static int depth=1;
int bodichess(pos_t *pos)
int brchess(pos_t *pos)
{
char *buffer, *s;
@@ -106,9 +124,7 @@ int bodichess(pos_t *pos)
}
//char **commands_completion(const char *text, int start, int end)
char **commands_completion(const char *text,
__attribute__((unused)) int start,
__attribute__((unused)) int end)
char **commands_completion(const char *text, __unused int start, __unused int end)
{
rl_attempted_completion_over = 1;
return rl_completion_matches(text, commands_generator);
@@ -185,7 +201,7 @@ int quote_detector(char *line, int index)
int execute_line(pos_t *pos, char *line)
{
register int i;
COMMAND *command;
struct command *command;
char *word;
/* Isolate the command word. */
@@ -219,7 +235,7 @@ int execute_line(pos_t *pos, char *line)
/* Look up NAME as the name of a command, and return a pointer to that
command. Return a NULL pointer if NAME isn't a command name. */
COMMAND *find_command(char *name)
struct command *find_command(char *name)
{
register int i;
@@ -227,7 +243,7 @@ COMMAND *find_command(char *name)
if (strcmp(name, commands[i].name) == 0)
return &commands[i];
return (COMMAND *)NULL;
return (struct command *)NULL;
}
/* Strip whitespace from the start and end of STRING. Return a pointer
@@ -250,64 +266,90 @@ char *stripwhite(char *string)
return s;
}
int do_eval(__attribute__((unused)) pos_t *pos,
__attribute__((unused)) char *arg)
int do_eval(__unused pos_t *pos, __unused char *arg)
{
eval_t material[2], control[2], mobility[2];
for (int color = WHITE; color <= BLACK; ++color) {
material[color] = eval_material(pos, color);
control[color] = eval_square_control(pos, color);
mobility[color] = eval_mobility(pos, color);
printf("%s: material=%d mobility=%d controlled=%d\n",
color? "Black": "White", material[color],
mobility[color], control[color]);
}
eval_t res = eval(pos);
printf("eval=%ld (%.3f pawns)\n", res, (float)res/100);
printf("eval = %d centipawns\n", res);
return 1;
}
int do_simple_eval(__unused pos_t *pos, __unused char *arg)
{
eval_t eval = eval_simple(pos);
printf("eval = %d centipawns\n", eval);
return 1;
}
int do_fen(pos_t *pos, char *arg)
{
log_f(1, "%s\n", arg);
fen2pos(pos, arg);
return 1;
}
int do_pos(pos_t *pos,
__attribute__((unused)) char *arg)
int do_init(pos_t *pos, __unused char *arg)
{
pos_startpos(pos);
return 1;
}
int do_pos(pos_t *pos, __unused char *arg)
{
log_f(1, "%s\n", arg);
pos_print(pos);
return 1;
}
int do_genmoves(pos_t *pos,
__attribute__((unused)) char *arg)
int do_genmoves(pos_t *pos, __unused char *arg)
{
log_f(1, "%s\n", arg);
moves_gen(pos, OPPONENT(pos->turn), false);
moves_gen(pos, pos->turn, true);
moves_gen_all(pos);
return 1;
}
int do_prmoves(pos_t *pos,
__attribute__((unused)) char *arg)
int do_prmoves(pos_t *pos, __unused char *arg)
{
log_f(1, "%s\n", arg);
moves_print(pos, M_PR_SEPARATE);
uint debug_level = debug_level_get();
debug_level_set(1);
moves_print(pos, M_PR_SEPARATE | M_PR_NUM | M_PR_LONG);
debug_level_set(debug_level);
return 1;
}
int do_prmovepos(pos_t *pos, char *arg)
{
struct list_head *p_cur, *tmp;
int movenum = atoi(arg), cur = 0; /* starts with 0 */
move_t *move;
/*
* int do_prmovepos(pos_t *pos, char *arg)
* {
* struct list_head *p_cur, *tmp;
* int movenum = atoi(arg), cur = 0; /\* starts with 0 *\/
* move_t *move;
*
* log_f(1, "%s\n", arg);
* list_for_each_safe(p_cur, tmp, &pos->moves[pos->turn]) {
* move = list_entry(p_cur, move_t, list);
* if (cur++ == movenum) {
* pos_print(move->newpos);
* break;
* }
* }
*
* return 1;
* }
*/
int do_prpieces(pos_t *pos, __unused char *arg)
{
log_f(1, "%s\n", arg);
list_for_each_safe(p_cur, tmp, &pos->moves) {
move = list_entry(p_cur, move_t, list);
if (cur++ == movenum)
break;
}
pos_print(move->newpos);
pos_pieces_print(pos);
return 1;
}
int do_memstats(__attribute__((unused)) pos_t *pos,
__attribute__((unused)) char *arg)
int do_memstats(__unused pos_t *pos,__unused char *arg)
{
moves_pool_stats();
piece_pool_stats();
@@ -315,14 +357,36 @@ int do_memstats(__attribute__((unused)) pos_t *pos,
return 1;
}
int do_quit(__attribute__((unused)) pos_t *pos,
__attribute__((unused)) char *arg)
int do_move(__unused pos_t *pos, __unused char *arg)
{
int i = 1, nmove = atoi(arg);
move_t *move;
pos_t *newpos;
if (list_empty(&pos->moves[pos->turn])) {
log_f(1, "No moves list.\n");
return 0;
}
list_for_each_entry(move, &pos->moves[pos->turn], list) {
if (i == nmove)
goto doit;
i++;
}
log_f(1, "Invalid <%d> move, should be <1-%d>.\n", nmove, i);
return 0;
doit:
newpos = move_do(pos, move);
pos_print(newpos);
return 1;
}
int do_quit(__unused pos_t *pos, __unused char *arg)
{
return done = 1;
}
int do_debug(__attribute__((unused)) pos_t *pos,
__attribute__((unused)) char *arg)
int do_debug(__unused pos_t *pos, __unused char *arg)
{
debug_level_set(atoi(arg));
return 1;
@@ -330,15 +394,14 @@ int do_debug(__attribute__((unused)) pos_t *pos,
/* Print out help for ARG, or for all of the commands if ARG is
not present. */
int do_help(__attribute__((unused)) pos_t *pos,
__attribute__((unused)) char *arg)
int do_help(__unused pos_t *pos, __unused char *arg)
{
register int i;
int i;
int printed = 0;
for (i = 0; commands[i].name; i++) {
if (!*arg || (strcmp(arg, commands[i].name) == 0)) {
printf("%s\t\t%s.\n", commands[i].name, commands[i].doc);
printf("%-11.11s%s.\n", commands[i].name, commands[i].doc);
printed++;
}
}
@@ -363,10 +426,61 @@ int do_help(__attribute__((unused)) pos_t *pos,
return 0;
}
#ifdef BIN_bodichess
int do_depth(__unused pos_t *pos, char *arg)
{
depth = atoi(arg);
printf("depth = %d\n", depth);
return 1;
}
int do_search(pos_t *pos, __unused char *arg)
{
int debug_level = debug_level_get();
float timer1, timer2, nodes_sec;
timer1 = debug_timer_elapsed();
negamax(pos, depth, pos->turn == WHITE ? 1 : -1);
timer2 = debug_timer_elapsed();
nodes_sec = (float) pos->node_count / ((float) (timer2 - timer1) / (float)NANOSEC);
log(1, "best=");
debug_level_set(1);
move_print(0, pos->bestmove, 0);
debug_level_set(debug_level);
log(1, " negamax=%d\n", pos->bestmove->negamax);
printf("Depth:%d Nodes:%luK time:%.02fs (%.0f kn/s)\n", depth,
pos->node_count / 1000, (timer2 - timer1)/NANOSEC, nodes_sec/1000);
return 1;
}
int do_pvs(pos_t *pos, __unused char *arg)
{
int debug_level = debug_level_get();
float timer1, timer2, nodes_sec;
eval_t _pvs;
timer1 = debug_timer_elapsed();
moves_gen_eval_sort(pos);
_pvs = pvs(pos, depth, EVAL_MIN, EVAL_MAX, pos->turn == WHITE ? 1 : -1);
timer2 = debug_timer_elapsed();
nodes_sec = (float) pos->node_count / ((float) (timer2 - timer1) / (float)NANOSEC);
log(1, "best=");
if (pos->bestmove) {
debug_level_set(1);
move_print(0, pos->bestmove, 0);
debug_level_set(debug_level);
log(1, " pvs=%d stored=%d\n", _pvs, pos->bestmove->negamax);
} else {
log(1, "<no-best-move>");
}
printf("Depth:%d Nodes:%luK time:%.02fs (%.0f kn/s)\n", depth,
pos->node_count / 1000, (timer2 - timer1)/NANOSEC, nodes_sec/1000);
return 1;
}
/** main()
* options:
int bodichess(pos_t *pos)
int brchess(pos_t *pos)
*
*/
static int usage(char *prg)
@@ -382,11 +496,12 @@ int main(int ac, char **av)
pos_t *pos;
int opt;
debug_init(1);
piece_pool_init();
moves_pool_init();
pos_pool_init();
pos = pos_get();
debug_init(1, stderr, true);
eval_simple_init();
while ((opt = getopt(ac, av, "d:f:")) != -1) {
switch (opt) {
@@ -400,10 +515,8 @@ int main(int ac, char **av)
return usage(*av);
}
}
printf("optind = %d ac = %d\n", optind, ac);
if (optind < ac)
return usage(*av);
return bodichess(pos);
return brchess(pos);
}
#endif /* BIN_bodichess */

View File

@@ -5,17 +5,17 @@
* 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>.
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#ifndef BODICHESS_H
#define BODICHESS_H
#ifndef BRCHESS_H
#define BRCHESS_H
#include "position.h"
int bodichess(pos_t *pos);
int brchess(pos_t *pos);
#endif /* BODICHESS_H */
#endif /* BRCHESS_H */

View File

@@ -5,7 +5,7 @@
* 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>.
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
@@ -14,45 +14,62 @@
#ifndef CHESSDEFS_H
#define CHESSDEFS_H
#include "bits.h"
#include <bits.h>
/* piece_t bits structure
* MSB 8 7 6 5 4 3 2 1 LSB
* 1: color (0 for white)
* 2-7: bit set for pawn (2), knight, bishop, rook, queen, king (7)
*/
typedef u8 piece_t;
enum {
E_EMPTY = 0,
E_COLOR, /* LSB */
E_PAWN,
E_KNIGHT,
E_BISHOP,
E_ROOK,
E_QUEEN,
E_KING,
E_COLOR = 8
};
/* pos_t bitboards tables
*/
enum {
BB_ALL = 0, /* OR of all bitboards */
BB_UNUSED, /* future use ? */
BB_PAWN = E_PAWN,
BB_KNIGHT,
BB_BISHOP,
BB_ROOK,
BB_QUEEN,
BB_KING,
BB_END
};
/* piece bitmask in piece_t
*/
enum {
EMPTY = 0,
PAWN = 1 << (E_PAWN - 1), /* 0x01 00000001 */
KNIGHT = 1 << (E_KNIGHT - 1), /* 0x02 00000010 */
BISHOP = 1 << (E_BISHOP - 1), /* 0x04 00000100 */
ROOK = 1 << (E_ROOK - 1), /* 0x08 00001000 */
QUEEN = 1 << (E_QUEEN - 1), /* 0x10 00010000 */
KING = 1 << (E_KING - 1), /* 0x20 00100000 */
PAWN = 1 << (E_PAWN - 1), /* 1<<(2-1) = 0x02 00000010 */
KNIGHT = 1 << (E_KNIGHT - 1), /* 0x04 00000100 */
BISHOP = 1 << (E_BISHOP - 1), /* 0x08 00001000 */
ROOK = 1 << (E_ROOK - 1), /* 0x10 00010000 */
QUEEN = 1 << (E_QUEEN - 1), /* 0x20 00100000 */
KING = 1 << (E_KING - 1), /* 0x40 01000000 */
};
#define PIECETOBB(p) (ffs64(PIECE(p))) /* from piece_t to bb piece array */
#define WHITE 0 /* 0x00 00000000 */
#define BLACK 1 /* 0x01 00000001 */
#define OPPONENT(p) !(p)
#define MASK_PIECE 0x3F /* 00111111 */
#define MASK_COLOR 0x80 /* 10000000 */
#define MASK_COLOR 0x01 /* 00000001 */
#define MASK_PIECE 0x7E /* 01111110 */
#define COLOR(p) ((p) & MASK_COLOR) /* bitmask */
#define VCOLOR(p) (!!COLOR(p)) /* WHITE/BLACK */
#define PIECE(p) ((p) & MASK_PIECE)
#define E_PIECE(p) (ffs64(PIECE(p))) /* convert mask to E_XX */
@@ -63,43 +80,50 @@ enum {
#define SET_BLACK(p) ((p) |= MASK_COLOR)
#define SET_COLOR(p, c) (!(c)? SET_WHITE(p): SET_BLACK(p))
/* flip a 0-63 square:
* Vertical: G8 (62) becomes G1 (6)
* Horizontal: G8 (62) becomes B8 (57)
*/
#define FLIP_V(sq) ((sq) ^ 56)
#define FLIP_H(sq) ((sq) ^ 7)
/* square_t bits structure : rrrrffff
* ffff: file
* rrrr: rank
*/
typedef unsigned char square_t;
#define GET_R(s) ((s) >> 4)
#define GET_F(s) ((s) & 0x0f)
#define SET_R(s, f) ((s) &= 0x0f, (s) |= (f)<<4)
#define SET_F(s, r) ((s) &= 0xf0, (s) |= (r))
#define SQUARE(f, r) ((r) << 4 | (f))
/* castle_t bits structure
*/
typedef unsigned char castle_t;
#define CASTLE_WK 1 /* 0x01 00000001 */
#define CASTLE_WK (1 << 0) /* 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_W (CASTLE_WK | CASTLE_WQ) /* 00000011 W castle mask */
#define CASTLE_B (CASTLE_BK | CASTLE_BQ) /* 00001100 B castle mask */
/* game phases
*/
#define OPENING 0
#define MIDDLEGAME 1
#define ENDGAME 2
/* bitboard
*/
typedef uint64_t bitboard_t;
typedef u64 bitboard_t;
/* eval type
*/
typedef int64_t eval_t;
typedef s32 eval_t;
/* forward typedefs
*/
typedef struct piece_list_s piece_list_t;
typedef struct board_s board_t;
typedef struct pos_s pos_t;
typedef struct move_s move_t;
#endif

View File

@@ -1,106 +0,0 @@
/* debug.c - debug/log 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 <stdarg.h>
#include <time.h>
#include "debug.h"
#define NANOSEC 1000000000 /* nano sec in sec */
#define MILLISEC 1000000 /* milli sec in sec */
static int64_t timer_start; /* in nanosecond */
static uint32_t debug_level=0;
void debug_level_set(uint32_t level)
{
debug_level = level;;
log(0, "debug level set to %u\n", level);
}
void debug_init(uint32_t level)
{
struct timespec timer;
debug_level_set(level);
if (!clock_gettime(CLOCK_MONOTONIC, &timer)) {
timer_start = timer.tv_sec * NANOSEC + timer.tv_nsec;
}
else {
timer_start = 0;
}
log(0, "timer started.\n");
}
inline static int64_t timer_elapsed()
{
struct timespec timer;
clock_gettime(CLOCK_MONOTONIC, &timer);
return (timer.tv_sec * NANOSEC + timer.tv_nsec) - timer_start;
}
/* void debug - log function
* @timestamp : boolean
* @indent : indent level (2 spaces each)
* @src : source file/func name (or NULL)
* @line : line number
*/
void debug(uint32_t level, bool timestamp, uint32_t indent, const char *src,
uint32_t line, const char *fmt, ...)
{
if (level > debug_level)
return;
va_list ap;
if (indent)
printf("%*s", 2*(indent-1), "");
if (timestamp) {
int64_t diff = timer_elapsed();
printf("%ld.%03ld ", diff/NANOSEC, (diff/1000000)%1000);
printf("%010ld ", diff);
}
if (src) {
if (line)
printf("[%s:%u] ", src, line);
else
printf("[%s] ", src);
}
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
}
#ifdef BIN_debug
#include <unistd.h>
int main()
{
int foo=1;
debug_init(5);
log(0, "log0=%d\n", foo++);
log(1, "log1=%d\n", foo++);
log(2, "log2=%d\n", foo++);
log_i(2, "log_i 2=%d\n", foo++);
log_i(5, "log_i 5=%d\n", foo++);
log_i(6, "log_i 6=%d\n", foo++);
log_it(4, "log_it 4=%d\n", foo++);
log_f(1, "log_f 5=%d\n", foo++);
}
#endif

View File

@@ -1,66 +0,0 @@
/* move.h - debug/log 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 DEBUG_H
#define DEBUG_H
#include <stdbool.h>
#include <stdint.h>
void debug_init(uint32_t level);
void debug_level_set(uint32_t level);
void debug_devel_set(uint32_t level);
void debug(uint32_t level, bool timestamp, uint32_t indent,
const char *src, uint32_t line, const char *, ...);
#ifdef DEBUG
/* format: only printf
*/
#define log(level, fmt, args...) \
debug((level), false, 0, NULL, 0, fmt, ##args)
/* format: func name, no line number, no indent, no timestamp
* foo:15 val=2
*/
#define log_f(level, fmt, args...) \
debug((level), false, 0, __func__, 0, fmt, ##args)
/* format : func name, indent, no timestamp
* foo:15 val=2
*/
#define log_i(level, fmt, args...) \
debug((level), false, (level), __func__, __LINE__, fmt, ##args)
/* format : func name, indent, timestamp
* []foo:15 val=2
*/
#define log_it(level, fmt, args...) \
debug((level), true, (level), __func__, __LINE__, fmt, ##args)
/* format: file name, no indent, no timestamp
* foo:15 val=2
*
* #define log_f(level, fmt, args...) \
* debug((level), false, 0, __FILE__, __LINE__, fmt, args)
*/
#else
#define log(level, fmt, args...)
#define log_i(...)
#define log_it(...)
#define log_f(...)
#endif /* DEBUG */
#endif /* DEBUG_H */

199
src/eval-simple.c Normal file
View File

@@ -0,0 +1,199 @@
/* eval-simple.c - simple position evaluation.
*
* Copyright (C) 2023 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.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#include <bits.h>
#include <debug.h>
#include "piece.h"
#include "eval-simple.h"
#include "position.h"
/*
* Tables are from https://www.chessprogramming.org/Simplified_Evaluation_Function
*
* Attention! Tables are black point of view (to be visually easier to read).
*/
static int mg_pawn[] = {
0, 0, 0, 0, 0, 0, 0, 0,
50, 50, 50, 50, 50, 50, 50, 50,
10, 10, 20, 30, 30, 20, 10, 10,
5, 5, 10, 25, 25, 10, 5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
5, -5, -10, 0, 0, -10, -5, 5,
5, 10, 10, -20, -20, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0
};
static int mg_knight[] = {
-50, -40, -30, -30, -30, -30, -40, -50,
-40, -20, 0, 0, 0, 0, -20, -40,
-30, 0, 10, 15, 15, 10, 0, -30,
-30, 5, 15, 20, 20, 15, 5, -30,
-30, 0, 15, 20, 20, 15, 0, -30,
-30, 5, 10, 15, 15, 10, 5, -30,
-40, -20, 0, 5, 5, 0, -20, -40,
-50, -40, -30, -30, -30, -30, -40, -50
};
static int mg_bishop[] = {
-20, -10, -10, -10, -10, -10, -10, -20,
-10, 0, 0, 0, 0, 0, 0, -10,
-10, 0, 5, 10, 10, 5, 0, -10,
-10, 5, 5, 10, 10, 5, 5, -10,
-10, 0, 10, 10, 10, 10, 0, -10,
-10, 10, 10, 10, 10, 10, 10, -10,
-10, 5, 0, 0, 0, 0, 5, -10,
-20, -10, -10, -10, -10, -10, -10, -20
};
static int mg_rook[] = {
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 10, 10, 10, 10, 10, 5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
0, 0, 0, 5, 5, 0, 0, 0
};
static int mg_queen[] = {
-20, -10, -10, -5, -5, -10, -10, -20,
-10, 0, 0, 0, 0, 0, 0, -10,
-10, 0, 5, 5, 5, 5, 0, -10,
-5, 0, 5, 5, 5, 5, 0, -5,
0, 0, 5, 5, 5, 5, 0, -5,
-10, 5, 5, 5, 5, 5, 0, -10,
-10, 0, 5, 0, 0, 0, 0, -10,
-20, -10, -10, -5, -5, -10, -10, -20
};
static int mg_king[] = {
-30, -40, -40, -50, -50, -40, -40, -30,
-30, -40, -40, -50, -50, -40, -40, -30,
-30, -40, -40, -50, -50, -40, -40, -30,
-30, -40, -40, -50, -50, -40, -40, -30,
-20, -30, -30, -40, -40, -30, -30, -20,
-10, -20, -20, -20, -20, -20, -20, -10,
20, 20, 0, 0, 0, 0, 20, 20,
20, 30, 10, 0, 0, 10, 30, 20
};
static int eg_king[] = {
-50, -40, -30, -20, -20, -30, -40, -50,
-30, -20, -10, 0, 0, -10, -20, -30,
-30, -10, 20, 30, 30, 20, -10, -30,
-30, -10, 30, 40, 40, 30, -10, -30,
-30, -10, 30, 40, 40, 30, -10, -30,
-30, -10, 20, 30, 30, 20, -10, -30,
-30, -30, 0, 0, 0, 0, -30, -30,
-50, -30, -30, -30, -30, -30, -30, -50
};
/* as pieces bitboard tables start at position 2; we make these tables
* bigger.
*/
static int *mg_tables[] = {
NULL,
NULL,
mg_pawn,
mg_knight,
mg_bishop,
mg_rook,
mg_queen,
mg_king
};
static int *eg_tables[] = {
NULL,
NULL,
mg_pawn,
mg_knight,
mg_bishop,
mg_rook,
mg_queen,
eg_king
};
/* to flip vertically a square, we need to XOR it with 56
*/
static int mg_table[2][6 + 2][64];
static int eg_table[2][6 + 2][64];
void eval_simple_init(void)
{
# ifdef DEBUG_EVAL
log_f(1, "initializing piece tables\n");
# endif
for (int piece = BB_PAWN; piece <= BB_KING; ++piece) {
for (int square = 0; square < 64; ++square) {
mg_table[WHITE][piece][square] = mg_tables[piece][FLIP_V(square)];
eg_table[WHITE][piece][square] = eg_tables[piece][FLIP_V(square)];
mg_table[BLACK][piece][square] = mg_tables[piece][square];
eg_table[BLACK][piece][square] = eg_tables[piece][square];
}
}
}
/**
* eval_simple() - simple and fast position evaluation
* @pos: &position to evaluate
*
* This function is normally used only during initialization,
* or when changing phase (middlegame <--> endgame), as the eval
* will be done increntally when doing moves.
*
* @return: the @pos evaluation in centipawns
*/
eval_t eval_simple(pos_t *pos)
{
eval_t eval[2] = { 0, 0 };
int eg = simple_is_endgame(pos);
int (*gg)[6 + 2][64]= eg? eg_table: mg_table;
pos->eval_simple_phase = ENDGAME;
# ifdef DEBUG_EVAL
log_f(5, "phase = %s.\n", eg? "endgame": "midgame");
# endif
for (int color = WHITE; color <= BLACK; ++color) {
for (uint piece = PAWN; piece <= KING; piece <<= 1) {
int bb = PIECETOBB(piece), cur;
u64 _t;
# ifdef DEBUG_EVAL
log_f(5, "p=%u bb=%d %s %s: count=%d val=%ld ", piece, bb, color? "black": "white",
P_SYM(piece), popcount64(pos->bb[color][bb]),
popcount64(pos->bb[color][bb]) * P_VALUE(piece));
# endif
eval[color] += popcount64(pos->bb[color][bb]) * P_LETTER(piece);
bit_for_each64_2(cur, _t, pos->bb[color][bb]) {
# ifdef DEBUG_EVAL
log(5, "sq=%d:%d ", cur, gg[color][bb][cur]);
# endif
eval[color] += gg[color][bb][cur];
}
# ifdef DEBUG_EVAL
log(5, "\n");
# endif
}
}
# ifdef DEBUG_EVAL
log_f(2, "eval:%d white:%d black:%d\n", eval[WHITE] - eval[BLACK],
eval[WHITE], eval[BLACK]);
# endif
return eval[WHITE] - eval[BLACK];
}

47
src/eval-simple.h Normal file
View File

@@ -0,0 +1,47 @@
/* eval-simple.h - simple position evaluation.
*
* 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.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#ifndef EVAL_SIMPLE_H
#define EVAL_SIMPLE_H
#include "chessdefs.h"
/* no queen on board */
#define simple_no_queen(p, c) \
( !(p)->bb[c][BB_QUEEN] )
#define simple_one_queen(p, c) \
( popcount64((p)->bb[c][BB_QUEEN]) == 1 )
#define simple_no_rook(p, c) \
(!(p)->bb[c][BB_ROOK])
#define simple_one_minor_piece(p, c) \
(popcount64((p)->bb[c][BB_KNIGHT] | (p)->bb[c][BB_BISHOP]) == 1)
#define simple_is_endgame(p) \
( (simple_no_queen(p, WHITE) || \
(simple_one_queen(p, WHITE) && \
simple_no_rook(p, WHITE) && \
simple_one_minor_piece(p, WHITE))) \
&& \
(simple_no_queen(p, BLACK) || \
(simple_one_queen(p, BLACK) && \
simple_no_rook(p, BLACK) && \
simple_one_minor_piece(p, BLACK))) )
void eval_simple_init(void);
eval_t eval_simple(pos_t *pos);
#endif /* EVAL_SIMPLE_H */

View File

@@ -1,11 +1,11 @@
/* eval.c - static position evaluation.
*
* Copyright (C) 2021 Bruno Raoult ("br")
* Copyright (C) 2021-2023 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>.
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
@@ -13,93 +13,83 @@
#include <stdio.h>
#include <list.h>
#include <debug.h>
#include "position.h"
#include "eval.h"
#include "list.h"
#include "debug.h"
#include "eval-simple.h"
eval_t eval(pos_t *pos)
inline eval_t eval_material(pos_t *pos, bool color)
{
eval_t material[2] = {0};
eval_t control[2] = {0};
eval_t res = 0;
struct list_head *p_cur, *p_tmp, *list;
piece_list_t *piece;
/* 1) pieces value
/* I need to do something about the king, if it can be potentially taken
* if pseudo-moves include a pinned piece on King.
*/
for (int color=0; color <2; ++color) {
list = &pos->pieces[color];
list_for_each_safe(p_cur, p_tmp, list) {
piece = list_entry(p_cur, piece_list_t, list);
if (PIECE(piece->piece) != KING)
material[color] += piece->value;
}
for (uint piece = PAWN; piece < KING; piece <<= 1) {
uint bb = PIECETOBB(piece);
# ifdef DEBUG_EVAL
log_f(2, "color=%u piece=%u bb=%u=%c count=%ul val=%ld\n",
color, piece, bb, P_LETTER(piece), popcount64(pos->bb[color][bb]),
P_VALUE(piece));
# endif
/* attention here */
res += popcount64(pos->bb[color][bb]) * P_VALUE(piece);
}
res = material[WHITE] - material[BLACK];
# ifdef DEBUG_EVAL
log_f(2, "material: W:%ld B:%ld eval=%ld (%.3f pawns)\n",
material[WHITE], material[BLACK],
material[WHITE] - material[BLACK], (float)res/100);
# endif
/* 2) square control: 10 square controls diff = 1 pawn
*/
control[WHITE] = popcount64(pos->controlled[WHITE]);
control[BLACK] = popcount64(pos->controlled[BLACK]);
res = control[WHITE] - control[BLACK];
# ifdef DEBUG_EVAL
log_f(2, "square control: W:%ld B:%ld eval=%ld (%.3f pawns)\n",
control[WHITE], control[BLACK],
res, (float)res/10);
# endif
/* 3) mobility: 5 mobility diff = 1 pawn
*/
res = pos->mobility[WHITE] - pos->mobility[BLACK];
# ifdef DEBUG_EVAL
log_f(2, "mobility: W:%ld B:%ld eval=%ld (%.3f pawns)\n",
pos->mobility[WHITE], pos->mobility[BLACK],
res, (float)res/5);
# endif
res = material[WHITE] - material[BLACK] +
(control[WHITE] - control[BLACK]) * 10 +
(pos->mobility[WHITE] - pos->mobility[BLACK]) * 20;
# ifdef DEBUG_EVAL
log_f(2, "eval: %ld (%.3f pawns)\n",
res, (float)res/100);
# endif
return res;
}
#ifdef BIN_eval
#include "fen.h"
#include "move.h"
int main(int ac, char**av)
inline eval_t eval_mobility(pos_t *pos, bool color)
{
pos_t *pos;
eval_t res;
debug_init(2);
piece_pool_init();
moves_pool_init();
pos_pool_init();
pos = pos_get();
if (ac == 1) {
pos_startpos(pos);
} else {
fen2pos(pos, av[1]);
}
moves_gen(pos, OPPONENT(pos->turn), false);
moves_gen(pos, pos->turn, true);
pos_print(pos);
pos_pieces_print(pos);
res = eval(pos);
printf("eval=%ld (%.3f pawns)\n", res, (float)res/100);
return pos->mobility[color];
}
inline eval_t eval_square_control(pos_t *pos, bool color)
{
return popcount64(pos->controlled[color]);
}
eval_t eval(pos_t *pos)
{
eval_t simple = 0, control[2] = {0};
if (pos->eval != EVAL_INVALID)
return pos->eval;
/* 1) pieces value */
//material[WHITE] = eval_material(pos, WHITE);
//material[BLACK] = eval_material(pos, BLACK);
simple = eval_simple(pos);
# ifdef DEBUG_EVAL
log_f(2, "eval_simple=%d\n", simple);
# endif
/* 2) square control: 10 square controls diff = 1 pawn */
control[WHITE] = eval_square_control(pos, WHITE);
control[BLACK] = eval_square_control(pos, BLACK);
# ifdef DEBUG_EVAL
log_f(2, "square control: W:%d B:%d diff=%d\n",
control[WHITE], control[BLACK],
(control[WHITE] - control[BLACK]) * 10);
# endif
/* 3) mobility: 10 mobility diff = 1 pawn
*/
# ifdef DEBUG_EVAL
log_f(2, "mobility: W:%u B:%u diff=%d\n",
pos->mobility[WHITE], pos->mobility[BLACK],
(pos->mobility[WHITE] - pos->mobility[BLACK]) * 10);
# endif
eval_t res = simple +
(control[WHITE] - control[BLACK]) * 10 +
(pos->mobility[WHITE] - pos->mobility[BLACK]) * 10;
# ifdef DEBUG_EVAL
log_f(2, "eval: %d\n", res);
# endif
pos->eval = res;
return res;
}
#endif

View File

@@ -5,7 +5,7 @@
* 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>.
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
@@ -14,7 +14,23 @@
#ifndef EVAL_H
#define EVAL_H
#include "position.h"
#include <limits.h>
#include "chessdefs.h"
#include "piece.h"
/* max pieces eval is KING_VALUE + 9*QUEEN_VALUE + 2*ROOK_VALUE + 2*BISHOP_VALUE
* + 2*KNIGHT_VALUE which around 30000.
* We are on secure side with -50000/+50000
*/
#define EVAL_MAX (50000)
#define EVAL_MIN (-EVAL_MAX)
#define EVAL_INVALID INT_MIN
#define EVAL_MATE EVAL_MAX
eval_t eval_material(pos_t *pos, bool color);
eval_t eval_mobility(pos_t *pos, bool color);
eval_t eval_square_control(pos_t *pos, bool color);
eval_t eval(pos_t *pos);

View File

@@ -5,7 +5,7 @@
* 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>.
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
@@ -17,7 +17,8 @@
#include <string.h>
#include <ctype.h>
#include "debug.h"
#include <debug.h>
#include "chessdefs.h"
#include "position.h"
#include "board.h"
@@ -48,7 +49,7 @@
pos_t *fen2pos(pos_t *pos, char *fen)
{
char *p = fen;
short rank, file, skip, color;
short rank, file, skip, color, bbpiece;
piece_t piece;
board_t *board = pos->board;
# define SKIP_BLANK(p) for(;*(p) == ' '; (p)++)
@@ -61,34 +62,41 @@ pos_t *fen2pos(pos_t *pos, char *fen)
char cp = toupper(*p);
switch (cp) {
case CHAR_PAWN:
bbpiece = BB_PAWN;
piece = PAWN;
goto set_square;
case CHAR_KNIGHT:
bbpiece = BB_KNIGHT;
piece = KNIGHT;
goto set_square;
case CHAR_BISHOP:
bbpiece = BB_BISHOP;
piece = BISHOP;
goto set_square;
case CHAR_ROOK:
bbpiece = BB_ROOK;
piece = ROOK;
goto set_square;
case CHAR_QUEEN:
bbpiece = BB_QUEEN;
piece = QUEEN;
goto set_square;
case CHAR_KING:
bbpiece = BB_KING;
piece = KING;
pos->king[color]=SQ88(file, rank);
//pos->bb[color][BB_KING] = BB(file, rank);
//goto set_square;
set_square:
# ifdef DEBUG_FEN
log_i(5, "f=%d r=%d *p=%c piece=%c color=%d\n",
file, rank, *p, cp, color);
file, rank, *p, cp, color);
# endif
pos->bb[color][bbpiece] |= BB(file, rank);
pos->occupied[color] |= BB(file, rank);
SET_COLOR(piece, color);
board[SQ88(file, rank)].piece = piece;
board[SQ88(file, rank)].s_piece =
piece_add(pos, piece, SQUARE(file, rank));
piece_add(pos, piece, SQ88(file, rank));
file++;
break;
case '/':
@@ -146,8 +154,10 @@ pos_t *fen2pos(pos_t *pos, char *fen)
SKIP_BLANK(p);
pos->en_passant = 0;
if (*p != '-') {
SET_F(pos->en_passant, C2FILE(*p++));
SET_R(pos->en_passant, C2RANK(*p++));
//SET_F(pos->en_passant, C2FILE(*p++));
//SET_R(pos->en_passant, C2RANK(*p++));
pos->en_passant = SQ88(C2FILE(*p), C2RANK(*(p+1)));
pos += 2;
} else {
p++;
}
@@ -156,25 +166,10 @@ pos_t *fen2pos(pos_t *pos, char *fen)
* 6) current move number
*/
SKIP_BLANK(p);
log_i(5, "pos=%d\n", (int)(p-fen));
//log_i(5, "pos=%d\n", (int)(p-fen));
sscanf(p, "%hd %hd", &pos->clock_50, &pos->curmove);
# ifdef DEBUG_FEN
log_i(5, "50 rule=%d current move=%d\n", pos->clock_50, pos->curmove);
# endif
return pos;
}
#ifdef BIN_fen
int main(int ac, char**av)
{
pos_t *pos;
debug_init(5);
piece_pool_init();
pos_pool_init();
pos = pos_get();
if (ac == 1) {
pos_startpos(pos);
} else {
fen2pos(pos, av[1]);
}
pos_print(pos);
}
#endif

View File

@@ -5,7 +5,7 @@
* 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>.
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*

View File

@@ -1,995 +0,0 @@
/* SPDX-License-Identifier: GPL-2.0 */
/* adaptation of kernel's <linux/list.h>
* Main change is that I don't use READ_ONCE and WRITE_ONCE
* See https://www.kernel.org/doc/Documentation/memory-barriers.txt
*/
#ifndef __BR_LIST_H
#define __BR_LIST_H
#include <stddef.h>
#include <stdbool.h>
/************ originally in <include/linux/types.h> */
struct list_head {
struct list_head *next, *prev;
};
struct hlist_head {
struct hlist_node *first;
};
struct hlist_node {
struct hlist_node *next, **pprev;
};
/************ originally in <include/linux/poison.h> */
# define POISON_POINTER_DELTA 0
/* These are non-NULL pointers that will result in page faults
* under normal circumstances, used to verify that nobody uses
* non-initialized list entries.
*/
#define LIST_POISON1 ((void *) 0x100 + POISON_POINTER_DELTA)
#define LIST_POISON2 ((void *) 0x200 + POISON_POINTER_DELTA)
/************ originally in <include/linux/kernel.h> */
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
((type *)(__mptr - offsetof(type, member))); })
/*
* Circular doubly linked list implementation.
*
* Some of the internal functions ("__xxx") are useful when
* manipulating whole lists rather than single entries, as
* sometimes we already know the next/prev entries and we can
* generate better code by using them directly rather than
* using the generic single-entry routines.
*/
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
/**
* INIT_LIST_HEAD - Initialize a list_head structure
* @list: list_head structure to be initialized.
*
* Initializes the list_head to point to itself. If it is a list header,
* the result is an empty list.
*/
static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
/**
* list_add - add a new entry
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
/**
* list_add_tail - add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
/*
* Delete a list entry and clear the 'prev' pointer.
*
* This is a special-purpose list clearing method used in the networking code
* for lists allocated as per-cpu, where we don't want to incur the extra
* WRITE_ONCE() overhead of a regular list_del_init(). The code that uses this
* needs to check the node 'prev' pointer instead of calling list_empty().
*/
static inline void __list_del_clearprev(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->prev = NULL;
}
static inline void __list_del_entry(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
}
/**
* list_del - deletes entry from list.
* @entry: the element to delete from the list.
* Note: list_empty() on entry does not return true after this, the entry is
* in an undefined state.
*/
static inline void list_del(struct list_head *entry)
{
__list_del_entry(entry);
entry->next = LIST_POISON1;
entry->prev = LIST_POISON2;
}
/**
* list_replace - replace old entry by new one
* @old : the element to be replaced
* @new : the new element to insert
*
* If @old was empty, it will be overwritten.
*/
static inline void list_replace(struct list_head *old,
struct list_head *new)
{
new->next = old->next;
new->next->prev = new;
new->prev = old->prev;
new->prev->next = new;
}
/**
* list_replace_init - replace old entry by new one and initialize the old one
* @old : the element to be replaced
* @new : the new element to insert
*
* If @old was empty, it will be overwritten.
*/
static inline void list_replace_init(struct list_head *old,
struct list_head *new)
{
list_replace(old, new);
INIT_LIST_HEAD(old);
}
/**
* list_swap - replace entry1 with entry2 and re-add entry1 at entry2's position
* @entry1: the location to place entry2
* @entry2: the location to place entry1
*/
static inline void list_swap(struct list_head *entry1,
struct list_head *entry2)
{
struct list_head *pos = entry2->prev;
list_del(entry2);
list_replace(entry1, entry2);
if (pos == entry1)
pos = entry2;
list_add(entry1, pos);
}
/**
* list_del_init - deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static inline void list_del_init(struct list_head *entry)
{
__list_del_entry(entry);
INIT_LIST_HEAD(entry);
}
/**
* list_move - delete from one list and add as another's head
* @list: the entry to move
* @head: the head that will precede our entry
*/
static inline void list_move(struct list_head *list, struct list_head *head)
{
__list_del_entry(list);
list_add(list, head);
}
/**
* list_move_tail - delete from one list and add as another's tail
* @list: the entry to move
* @head: the head that will follow our entry
*/
static inline void list_move_tail(struct list_head *list,
struct list_head *head)
{
__list_del_entry(list);
list_add_tail(list, head);
}
/**
* list_bulk_move_tail - move a subsection of a list to its tail
* @head: the head that will follow our entry
* @first: first entry to move
* @last: last entry to move, can be the same as first
*
* Move all entries between @first and including @last before @head.
* All three entries must belong to the same linked list.
*/
static inline void list_bulk_move_tail(struct list_head *head,
struct list_head *first,
struct list_head *last)
{
first->prev->next = last->next;
last->next->prev = first->prev;
head->prev->next = first;
first->prev = head->prev;
last->next = head;
head->prev = last;
}
/**
* list_is_first -- tests whether @list is the first entry in list @head
* @list: the entry to test
* @head: the head of the list
*/
static inline int list_is_first(const struct list_head *list,
const struct list_head *head)
{
return list->prev == head;
}
/**
* list_is_last - tests whether @list is the last entry in list @head
* @list: the entry to test
* @head: the head of the list
*/
static inline int list_is_last(const struct list_head *list,
const struct list_head *head)
{
return list->next == head;
}
/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
static inline int list_empty(const struct list_head *head)
{
return head->next == head;
}
/**
* list_rotate_left - rotate the list to the left
* @head: the head of the list
*/
static inline void list_rotate_left(struct list_head *head)
{
struct list_head *first;
if (!list_empty(head)) {
first = head->next;
list_move_tail(first, head);
}
}
/**
* list_rotate_to_front() - Rotate list to specific item.
* @list: The desired new front of the list.
* @head: The head of the list.
*
* Rotates list so that @list becomes the new front of the list.
*/
static inline void list_rotate_to_front(struct list_head *list,
struct list_head *head)
{
/*
* Deletes the list head from the list denoted by @head and
* places it as the tail of @list, this effectively rotates the
* list so that @list is at the front.
*/
list_move_tail(head, list);
}
/**
* list_is_singular - tests whether a list has just one entry.
* @head: the list to test.
*/
static inline int list_is_singular(const struct list_head *head)
{
return !list_empty(head) && (head->next == head->prev);
}
static inline void __list_cut_position(struct list_head *list,
struct list_head *head, struct list_head *entry)
{
struct list_head *new_first = entry->next;
list->next = head->next;
list->next->prev = list;
list->prev = entry;
entry->next = list;
head->next = new_first;
new_first->prev = head;
}
/**
* list_cut_position - cut a list into two
* @list: a new list to add all removed entries
* @head: a list with entries
* @entry: an entry within head, could be the head itself
* and if so we won't cut the list
*
* This helper moves the initial part of @head, up to and
* including @entry, from @head to @list. You should
* pass on @entry an element you know is on @head. @list
* should be an empty list or a list you do not care about
* losing its data.
*
*/
static inline void list_cut_position(struct list_head *list,
struct list_head *head, struct list_head *entry)
{
if (list_empty(head))
return;
if (list_is_singular(head) &&
(head->next != entry && head != entry))
return;
if (entry == head)
INIT_LIST_HEAD(list);
else
__list_cut_position(list, head, entry);
}
/**
* list_cut_before - cut a list into two, before given entry
* @list: a new list to add all removed entries
* @head: a list with entries
* @entry: an entry within head, could be the head itself
*
* This helper moves the initial part of @head, up to but
* excluding @entry, from @head to @list. You should pass
* in @entry an element you know is on @head. @list should
* be an empty list or a list you do not care about losing
* its data.
* If @entry == @head, all entries on @head are moved to
* @list.
*/
static inline void list_cut_before(struct list_head *list,
struct list_head *head,
struct list_head *entry)
{
if (head->next == entry) {
INIT_LIST_HEAD(list);
return;
}
list->next = head->next;
list->next->prev = list;
list->prev = entry->prev;
list->prev->next = list;
head->next = entry;
entry->prev = head;
}
static inline void __list_splice(const struct list_head *list,
struct list_head *prev,
struct list_head *next)
{
struct list_head *first = list->next;
struct list_head *last = list->prev;
first->prev = prev;
prev->next = first;
last->next = next;
next->prev = last;
}
/**
* list_splice - join two lists, this is designed for stacks
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice(const struct list_head *list,
struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head, head->next);
}
/**
* list_splice_tail - join two lists, each list being a queue
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice_tail(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head->prev, head);
}
/**
* list_splice_init - join two lists and reinitialise the emptied list.
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* The list at @list is reinitialised
*/
static inline void list_splice_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head, head->next);
INIT_LIST_HEAD(list);
}
}
/**
* list_splice_tail_init - join two lists and reinitialise the emptied list
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* Each of the lists is a queue.
* The list at @list is reinitialised
*/
static inline void list_splice_tail_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head->prev, head);
INIT_LIST_HEAD(list);
}
}
/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*/
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
/**
* list_first_entry - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note, that list is expected to be not empty.
*/
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
/**
* list_last_entry - get the last element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note, that list is expected to be not empty.
*/
#define list_last_entry(ptr, type, member) \
list_entry((ptr)->prev, type, member)
/**
* list_first_entry_or_null - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note that if the list is empty, it returns NULL.
*/
#define list_first_entry_or_null(ptr, type, member) ({ \
struct list_head *head__ = (ptr); \
struct list_head *pos__ = head__->next; \
pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
})
/**
* list_next_entry - get the next element in list
* @pos: the type * to cursor
* @member: the name of the list_head within the struct.
*/
#define list_next_entry(pos, member) \
list_entry((pos)->member.next, __typeof__(*(pos)), member)
/**
* list_prev_entry - get the prev element in list
* @pos: the type * to cursor
* @member: the name of the list_head within the struct.
*/
#define list_prev_entry(pos, member) \
list_entry((pos)->member.prev, __typeof__(*(pos)), member)
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
/**
* list_for_each_continue - continue iteration over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*
* Continue to iterate over a list, continuing after the current position.
*/
#define list_for_each_continue(pos, head) \
for (pos = pos->next; pos != (head); pos = pos->next)
/**
* list_for_each_prev - iterate over a list backwards
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; pos != (head); pos = pos->prev)
/**
* list_for_each_safe - iterate over a list safe against removal of list entry
* @pos: the &struct list_head to use as a loop cursor.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
/**
* list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
* @pos: the &struct list_head to use as a loop cursor.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#define list_for_each_prev_safe(pos, n, head) \
for (pos = (head)->prev, n = pos->prev; \
pos != (head); \
pos = n, n = pos->prev)
/**
* list_entry_is_head - test if the entry points to the head of the list
* @pos: the type * to cursor
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_entry_is_head(pos, head, member) \
(&pos->member == (head))
/**
* list_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_first_entry(head, __typeof__(*pos), member); \
!list_entry_is_head(pos, head, member); \
pos = list_next_entry(pos, member))
/**
* list_for_each_entry_reverse - iterate backwards over list of given type.
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry_reverse(pos, head, member) \
for (pos = list_last_entry(head, __typeof__(*pos), member); \
!list_entry_is_head(pos, head, member); \
pos = list_prev_entry(pos, member))
/**
* list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
* @pos: the type * to use as a start point
* @head: the head of the list
* @member: the name of the list_head within the struct.
*
* Prepares a pos entry for use as a start point in list_for_each_entry_continue().
*/
#define list_prepare_entry(pos, head, member) \
((pos) ? : list_entry(head, __typeof__(*pos), member))
/**
* list_for_each_entry_continue - continue iteration over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Continue to iterate over list of given type, continuing after
* the current position.
*/
#define list_for_each_entry_continue(pos, head, member) \
for (pos = list_next_entry(pos, member); \
!list_entry_is_head(pos, head, member); \
pos = list_next_entry(pos, member))
/**
* list_for_each_entry_continue_reverse - iterate backwards from the given point
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Start to iterate over list of given type backwards, continuing after
* the current position.
*/
#define list_for_each_entry_continue_reverse(pos, head, member) \
for (pos = list_prev_entry(pos, member); \
!list_entry_is_head(pos, head, member); \
pos = list_prev_entry(pos, member))
/**
* list_for_each_entry_from - iterate over list of given type from the current point
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate over list of given type, continuing from current position.
*/
#define list_for_each_entry_from(pos, head, member) \
for (; !list_entry_is_head(pos, head, member); \
pos = list_next_entry(pos, member))
/**
* list_for_each_entry_from_reverse - iterate backwards over list of given type
* from the current point
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate backwards over list of given type, continuing from current position.
*/
#define list_for_each_entry_from_reverse(pos, head, member) \
for (; !list_entry_is_head(pos, head, member); \
pos = list_prev_entry(pos, member))
/**
* list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_first_entry(head, __typeof__(*pos), member), \
n = list_next_entry(pos, member); \
!list_entry_is_head(pos, head, member); \
pos = n, n = list_next_entry(n, member))
/**
* list_for_each_entry_safe_continue - continue list iteration safe against removal
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate over list of given type, continuing after current point,
* safe against removal of list entry.
*/
#define list_for_each_entry_safe_continue(pos, n, head, member) \
for (pos = list_next_entry(pos, member), \
n = list_next_entry(pos, member); \
!list_entry_is_head(pos, head, member); \
pos = n, n = list_next_entry(n, member))
/**
* list_for_each_entry_safe_from - iterate over list from current point safe against removal
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate over list of given type from current point, safe against
* removal of list entry.
*/
#define list_for_each_entry_safe_from(pos, n, head, member) \
for (n = list_next_entry(pos, member); \
!list_entry_is_head(pos, head, member); \
pos = n, n = list_next_entry(n, member))
/**
* list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate backwards over list of given type, safe against removal
* of list entry.
*/
#define list_for_each_entry_safe_reverse(pos, n, head, member) \
for (pos = list_last_entry(head, __typeof__(*pos), member), \
n = list_prev_entry(pos, member); \
!list_entry_is_head(pos, head, member); \
pos = n, n = list_prev_entry(n, member))
/**
* list_safe_reset_next - reset a stale list_for_each_entry_safe loop
* @pos: the loop cursor used in the list_for_each_entry_safe loop
* @n: temporary storage used in list_for_each_entry_safe
* @member: the name of the list_head within the struct.
*
* list_safe_reset_next is not safe to use in general if the list may be
* modified concurrently (eg. the lock is dropped in the loop body). An
* exception to this is if the cursor element (pos) is pinned in the list,
* and list_safe_reset_next is called after re-taking the lock and before
* completing the current iteration of the loop body.
*/
#define list_safe_reset_next(pos, n, member) \
n = list_next_entry(pos, member)
/*
* Double linked lists with a single pointer list head.
* Mostly useful for hash tables where the two pointer list head is
* too wasteful.
* You lose the ability to access the tail in O(1).
*/
#define HLIST_HEAD_INIT { .first = NULL }
#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
static inline void INIT_HLIST_NODE(struct hlist_node *h)
{
h->next = NULL;
h->pprev = NULL;
}
/**
* hlist_unhashed - Has node been removed from list and reinitialized?
* @h: Node to be checked
*
* Not that not all removal functions will leave a node in unhashed
* state. For example, hlist_nulls_del_init_rcu() does leave the
* node in unhashed state, but hlist_nulls_del() does not.
*/
static inline int hlist_unhashed(const struct hlist_node *h)
{
return !h->pprev;
}
/**
* hlist_unhashed_lockless - Version of hlist_unhashed for lockless use
* @h: Node to be checked
*
* This variant of hlist_unhashed() must be used in lockless contexts
* to avoid potential load-tearing. The READ_ONCE() is paired with the
* various WRITE_ONCE() in hlist helpers that are defined below.
*/
static inline int hlist_unhashed_lockless(const struct hlist_node *h)
{
return !h->pprev;
}
/**
* hlist_empty - Is the specified hlist_head structure an empty hlist?
* @h: Structure to check.
*/
static inline int hlist_empty(const struct hlist_head *h)
{
return !h->first;
}
static inline void __hlist_del(struct hlist_node *n)
{
struct hlist_node *next = n->next;
struct hlist_node **pprev = n->pprev;
*pprev = next;
if (next)
next->pprev = pprev;
}
/**
* hlist_del - Delete the specified hlist_node from its list
* @n: Node to delete.
*
* Note that this function leaves the node in hashed state. Use
* hlist_del_init() or similar instead to unhash @n.
*/
static inline void hlist_del(struct hlist_node *n)
{
__hlist_del(n);
n->next = LIST_POISON1;
n->pprev = LIST_POISON2;
}
/**
* hlist_del_init - Delete the specified hlist_node from its list and initialize
* @n: Node to delete.
*
* Note that this function leaves the node in unhashed state.
*/
static inline void hlist_del_init(struct hlist_node *n)
{
if (!hlist_unhashed(n)) {
__hlist_del(n);
INIT_HLIST_NODE(n);
}
}
/**
* hlist_add_head - add a new entry at the beginning of the hlist
* @n: new entry to be added
* @h: hlist head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
{
struct hlist_node *first = h->first;
n->next = first;
if (first)
first->pprev = &n->next;
h->first = n;
n->pprev = &h->first;
}
/**
* hlist_add_before - add a new entry before the one specified
* @n: new entry to be added
* @next: hlist node to add it before, which must be non-NULL
*/
static inline void hlist_add_before(struct hlist_node *n,
struct hlist_node *next)
{
n->pprev = next->pprev;
n->next = next;
next->pprev = &n->next;
*(n->pprev) = n;
}
/**
* hlist_add_behind - add a new entry after the one specified
* @n: new entry to be added
* @prev: hlist node to add it after, which must be non-NULL
*/
static inline void hlist_add_behind(struct hlist_node *n,
struct hlist_node *prev)
{
n->next = prev->next;
prev->next = n;
n->pprev = &prev->next;
if (n->next)
n->next->pprev = &n->next;
}
/**
* hlist_add_fake - create a fake hlist consisting of a single headless node
* @n: Node to make a fake list out of
*
* This makes @n appear to be its own predecessor on a headless hlist.
* The point of this is to allow things like hlist_del() to work correctly
* in cases where there is no list.
*/
static inline void hlist_add_fake(struct hlist_node *n)
{
n->pprev = &n->next;
}
/**
* hlist_fake: Is this node a fake hlist?
* @h: Node to check for being a self-referential fake hlist.
*/
static inline bool hlist_fake(struct hlist_node *h)
{
return h->pprev == &h->next;
}
/**
* hlist_is_singular_node - is node the only element of the specified hlist?
* @n: Node to check for singularity.
* @h: Header for potentially singular list.
*
* Check whether the node is the only node of the head without
* accessing head, thus avoiding unnecessary cache misses.
*/
static inline bool
hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
{
return !n->next && n->pprev == &h->first;
}
/**
* hlist_move_list - Move an hlist
* @old: hlist_head for old list.
* @new: hlist_head for new list.
*
* Move a list from one list head to another. Fixup the pprev
* reference of the first entry if it exists.
*/
static inline void hlist_move_list(struct hlist_head *old,
struct hlist_head *new)
{
new->first = old->first;
if (new->first)
new->first->pprev = &new->first;
old->first = NULL;
}
#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
#define hlist_for_each(pos, head) \
for (pos = (head)->first; pos ; pos = pos->next)
#define hlist_for_each_safe(pos, n, head) \
for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
pos = n)
#define hlist_entry_safe(ptr, type, member) \
({ __typeof__(ptr) ____ptr = (ptr); \
____ptr ? hlist_entry(____ptr, type, member) : NULL; \
})
/**
* hlist_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry(pos, head, member) \
for (pos = hlist_entry_safe((head)->first, __typeof__(*(pos)), member); \
pos; \
pos = hlist_entry_safe((pos)->member.next, __typeof__(*(pos)), member))
/**
* hlist_for_each_entry_continue - iterate over a hlist continuing after current point
* @pos: the type * to use as a loop cursor.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry_continue(pos, member) \
for (pos = hlist_entry_safe((pos)->member.next, __typeof__(*(pos)), member); \
pos; \
pos = hlist_entry_safe((pos)->member.next, __typeof__(*(pos)), member))
/**
* hlist_for_each_entry_from - iterate over a hlist continuing from current point
* @pos: the type * to use as a loop cursor.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry_from(pos, member) \
for (; pos; \
pos = hlist_entry_safe((pos)->member.next, __typeof__(*(pos)), member))
/**
* hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop cursor.
* @n: a &struct hlist_node to use as temporary storage
* @head: the head for your list.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry_safe(pos, n, head, member) \
for (pos = hlist_entry_safe((head)->first, __typeof__(*pos), member); \
pos && ({ n = pos->member.next; 1; }); \
pos = hlist_entry_safe(n, __typeof__(*pos), member))
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -5,7 +5,7 @@
* 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>.
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
@@ -32,34 +32,48 @@ typedef unsigned char move_flags_t;
/* moves_print flags
*/
#define M_PR_SEPARATE 0x40 /* separate capture/non capture */
#define M_PR_LONG 0x80
#define M_PR_CAPT 0x01
#define M_PR_NCAPT 0x02
#define M_PR_NUM 0x04
#define M_PR_NL 0x08
#define M_PR_EVAL 0x20 /* separate captures */
#define M_PR_SEPARATE 0x40 /* separate captures */
#define M_PR_LONG 0x80
typedef struct move_s {
piece_t piece;
square_t from, to;
piece_t taken; /* removed piece */
piece_t capture; /* captured piece */
piece_t promotion; /* promoted piece */
move_flags_t flags;
eval_t negamax;
eval_t eval;
eval_t eval_simple;
pos_t *pos;
struct list_head list; /* next move */
struct pos_s *newpos; /* resulting position */
} move_t;
pool_t *moves_pool_init();
void moves_pool_stats();
int move_print(move_t *move, move_flags_t flags);
int move_print(int movenum, move_t *move, move_flags_t flags);
void moves_print(pos_t *move, move_flags_t flags);
int pseudo_moves_castle(pos_t *pos);
void move_del(struct list_head *ptr);
int moves_del(pos_t *pos);
int pseudo_moves_gen(pos_t *pos, piece_list_t *piece, bool doit);
int pseudo_moves_castle(pos_t *pos, bool color, bool doit, bool do_king);
int pseudo_moves_gen(pos_t *pos, piece_list_t *piece, bool doit, bool do_king);
int pseudo_moves_pawn(pos_t *pos, piece_list_t *piece, bool doit);
int moves_gen(pos_t *pos, bool color, bool doit);
int moves_gen(pos_t *pos, bool color, bool doit, bool do_king);
int moves_gen_king_moves(pos_t *pos, bool color, bool doit);
int move_doit(pos_t *pos, move_t *move);
void moves_sort(pos_t *pos);
void moves_gen_eval_sort(pos_t *pos);
#endif /* MODE_H */
void moves_gen_all(pos_t *pos);
void moves_gen_all_nomoves(pos_t *pos);
pos_t *move_do(pos_t *pos, move_t *move);
void move_undo(pos_t *pos, move_t *move);
#endif /* MOVE_H */

View File

@@ -5,7 +5,7 @@
* 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>.
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
@@ -14,10 +14,14 @@
#include <malloc.h>
#include <ctype.h>
#include <debug.h>
#include <pool.h>
#include <list.h>
#include "chessdefs.h"
#include "piece.h"
#include "board.h"
#include "debug.h"
#include "bitboard.h"
#include "position.h"
static pool_t *pieces_pool;
@@ -41,8 +45,8 @@ void piece_list_print(struct list_head *list)
piece = list_entry(p_cur, piece_list_t, list);
printf("%s%c%c ", P_SYM(piece->piece),
FILE2C(GET_F(piece->square)),
RANK2C(GET_R(piece->square)));
FILE2C(F88(piece->square)),
RANK2C(R88(piece->square)));
}
printf("\n");
}
@@ -50,7 +54,7 @@ void piece_list_print(struct list_head *list)
pool_t *piece_pool_init()
{
if (!pieces_pool)
pieces_pool = pool_init("pieces", 128, sizeof(piece_list_t));
pieces_pool = pool_create("pieces", 128, sizeof(piece_list_t));
return pieces_pool;
}
@@ -63,16 +67,19 @@ void piece_pool_stats()
piece_list_t *piece_add(pos_t *pos, piece_t piece, square_t square)
{
piece_list_t *new;
short color = VCOLOR(piece);
short color = COLOR(piece);
# ifdef DEBUG_PIECE
log_f(3, "piece=%02x square=%02x\n", piece, square);
log_f(5, "Adding %s %s on %c%c\n", color? "Black": "White",
P_NAME(piece), FILE2C(GET_F(square)), RANK2C(GET_R(square)));
P_NAME(piece), FILE2C(F88(square)), RANK2C(R88(square)));
# endif
if ((new = pool_get(pieces_pool))) {
list_add_tail(&new->list, &pos->pieces[color]);
//color? &pos->pieces_black: &pos->pieces_white);
/* first piece is always king */
if (PIECE(piece) == KING)
list_add(&new->list, &pos->pieces[color]);
else
list_add_tail(&new->list, &pos->pieces[color]);
new->piece = piece;
new->square = square;
new->castle = 0;
@@ -109,47 +116,3 @@ int pieces_del(pos_t *pos, short color)
# endif
return count;
}
#ifdef BIN_piece
#include "fen.h"
int main(int ac, char**av)
{
pos_t *pos;
debug_init(5);
pos_pool_init();
pos = pos_get();
piece_pool_init();
if (ac == 1) {
pos_startpos(pos);
} else {
fen2pos(pos, av[1]);
}
pos_print(pos);
pos_pieces_print(pos);
printf("0x1c:\n");
bitboard_print(0x1c);
printf("0x70:\n");
bitboard_print(0x70);
printf("0x0e:\n");
bitboard_print(0x0e);
printf("0x60:\n");
bitboard_print(0x60);
printf("A1:\n");
bitboard_print(A1);
printf("1:\n");
bitboard_print(1L);
printf("H1:\n");
bitboard_print(H1);
printf("C1:\n");
bitboard_print(C1);
printf("D1:\n");
bitboard_print(D1);
printf("C1|D1:\n");
bitboard_print(C1|D1);
printf("H8:\n");
bitboard_print(H8);
}
#endif

View File

@@ -5,7 +5,7 @@
* 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>.
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
@@ -34,7 +34,7 @@ typedef struct piece_list_s {
piece_t piece;
square_t square;
short castle;
int64_t value;
s64 value;
struct list_head list;
} piece_list_t;
@@ -46,7 +46,7 @@ extern struct piece_details {
char *symbol_w;
char *symbol_b; /* used for game notation */
char *name;
int64_t value;
s64 value;
} piece_details[];
#define P_NAME(p) piece_details[E_PIECE(p)].name
@@ -56,6 +56,7 @@ extern struct piece_details {
piece_details[E_PIECE(p)].abbrev_b)
#define P_CSYM(p) (IS_WHITE(p)? piece_details[E_PIECE(p)].symbol_w: \
piece_details[E_PIECE(p)].symbol_b)
#define P_VALUE(p) (piece_details[E_PIECE(p)].value)
/* use short name or symbol - no effect
*/
#define P_USE_UTF 1

View File

@@ -1,179 +0,0 @@
/* pool.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 <stdbool.h>
#include <ctype.h>
*/
#include <stddef.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include "list.h"
#include "pool.h"
#include "debug.h"
void pool_stats(pool_t *pool)
{
if (pool) {
# ifdef DEBUG_POOL
log_f(1, "[%s] pool [%p]: avail:%u alloc:%u grow:%u eltsize:%lu\n",
pool->name, (void *)pool, pool->available, pool->allocated,
pool->growsize, pool->eltsize);
# endif
}
}
pool_t *pool_init(const char *name, uint32_t growsize, size_t eltsize)
{
pool_t *pool;
# ifdef DEBUG_POOL
log_f(1, "name=[%s] growsize=%u eltsize=%lu\n",
name, growsize, eltsize);
# endif
/* we need at least this space in struct */
if (eltsize < sizeof (struct list_head))
return NULL;
if ((pool = malloc(sizeof (*pool)))) {
pool->name = strdup(name);
pool->growsize = growsize;
pool->eltsize = eltsize;
pool->available = 0;
pool->allocated = 0;
INIT_LIST_HEAD(&pool->head);
}
return pool;
}
static uint32_t _pool_add(pool_t *pool, struct list_head *elt)
{
# ifdef DEBUG_POOL
log_f(10, "pool=%p &head=%p elt=%p off1=%lu off2=%lu\n",
(void *)pool,
(void *)&pool->head,
(void *)elt,
(void *)&pool->head-(void *)pool,
offsetof(pool_t, head));
# endif
list_add(elt, &pool->head);
return ++pool->available;
}
uint32_t pool_add(pool_t *pool, void *elt)
{
return _pool_add(pool, elt);
}
static struct list_head *_pool_get(pool_t *pool)
{
struct list_head *res = pool->head.next;
pool->available--;
list_del(res);
return res;
}
void *pool_get(pool_t *pool)
{
if (!pool)
return NULL;
if (!pool->available) {
void *alloc = malloc(pool->eltsize * pool->growsize);
void *cur;
uint32_t i;
# ifdef DEBUG_POOL
log_f(1, "[%s]: growing pool from %u to %u elements.\n",
pool->name,
pool->allocated,
pool->allocated + pool->growsize);
# endif
if (!alloc)
return NULL;
# ifdef DEBUG_POOL
log_f(5, " (old=%u)\n", pool->allocated);
# endif
pool->allocated += pool->growsize;
# ifdef DEBUG_POOL
log_f(5, " (new=%u)\n", pool->allocated);
# endif
for (i = 0; i < pool->growsize; ++i) {
cur = alloc + i * pool->eltsize;
# ifdef DEBUG_POOL
log_f(5, "alloc=%p cur=%p\n", alloc, cur);
# endif
_pool_add(pool, (struct list_head *)cur);
}
pool_stats(pool);
}
/* this is the effective address if the object (and also the
* pool list_head address)
*/
return _pool_get(pool);
}
#ifdef BIN_pool
struct d {
uint16_t data1;
char c;
struct list_head list;
};
static LIST_HEAD (head);
int main(int ac, char**av)
{
pool_t *pool;
int total;
int action=0;
uint16_t icur=0;
char ccur='z';
struct d *elt;
debug_init(3);
log_f(1, "%s: sizeof(d)=%lu sizeof(*d)=%lu off=%lu\n", *av, sizeof(elt),
sizeof(*elt), offsetof(struct d, list));
if ((pool = pool_init("dummy", 3, sizeof(*elt)))) {
pool_stats(pool);
for (int cur=1; cur<ac; ++cur) {
total = atoi(av[cur]);
if (action == 0) { /* add elt to list */
log_f(2, "adding %d elements\n", total);
for (int i = 0; i < total; ++i) {
elt = pool_get(pool);
elt->data1 = icur++;
elt->c = ccur--;
list_add(&elt->list, &head);
}
pool_stats(pool);
action = 1;
} else { /* remove one elt from list */
log_f(2, "deleting %d elements\n", total);
for (int i = 0; i < total; ++i) {
if (!list_empty(&head)) {
elt = list_last_entry(&head, struct d, list);
printf("elt=[%d, %c]\n", elt->data1, elt->c);
list_del(&elt->list);
pool_add(pool, elt);
}
}
pool_stats(pool);
action = 0;
}
}
}
pool_stats(pool);
}
#endif

View File

@@ -1,35 +0,0 @@
/* pool.h - A simple memory 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>
*
*/
#ifndef POOL_H
#define POOL_H
#include <stdint.h>
#include <stddef.h>
#include "list.h"
typedef struct {
char *name;
uint32_t available;
uint32_t allocated;
uint32_t growsize;
size_t eltsize;
struct list_head head;
} pool_t;
void pool_stats(pool_t *pool);
pool_t *pool_init(const char *name, uint32_t grow, size_t size);
void *pool_get(pool_t *pool);
uint32_t pool_add(pool_t *pool, void *elt);
#endif

View File

@@ -5,7 +5,7 @@
* 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>.
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
@@ -22,6 +22,7 @@
#include "move.h"
#include "fen.h"
#include "piece.h"
#include "eval.h"
static pool_t *pos_pool;
@@ -54,6 +55,10 @@ inline void bitboard_print2(bitboard_t bb1, bitboard_t bb2)
BYTE2BIN(bb2>>i));
}
/**
* pos_pieces_print() - Print position pieces
* @pos: &position
*/
void pos_pieces_print(pos_t *pos)
{
printf("White pieces (%d): \t", popcount64(pos->occupied[WHITE]));
@@ -62,16 +67,31 @@ void pos_pieces_print(pos_t *pos)
piece_list_print(&pos->pieces[BLACK]);
}
/* void pos_print - Print position on stdout.
* @pos: Position address (pos_t * )
*
* Return: None.
/**
* pos_bitboards_print() - Print position bitboards
* @pos: &position
*/
void pos_bitboards_print(pos_t *pos)
{
printf("Bitboards occupied :\n");
bitboard_print2(pos->occupied[WHITE], pos->occupied[BLACK]);
printf("Bitboards controlled :\n");
bitboard_print2(pos->controlled[WHITE], pos->controlled[BLACK]);
}
/**
* pos_print() - Print position on stdout.
* @pos: &position
*/
void pos_print(pos_t *pos)
{
int rank, file;
piece_t piece;
board_t *board = pos->board;
piece_list_t *wk = list_first_entry(&pos->pieces[WHITE], piece_list_t, list),
*bk = list_first_entry(&pos->pieces[BLACK], piece_list_t, list);
printf(" +---+---+---+---+---+---+---+---+\n");
for (rank = 7; rank >= 0; --rank) {
@@ -85,18 +105,19 @@ void pos_print(pos_t *pos)
printf(" A B C D E F G H\n\n");
printf("Turn: %s.\n", IS_WHITE(pos->turn) ? "white" : "black");
printf("Kings: W:%c%c B:%c%c\n",
FILE2C(GET_F(pos->king[WHITE])),
RANK2C(GET_R(pos->king[WHITE])),
FILE2C(GET_F(pos->king[BLACK])),
RANK2C(GET_R(pos->king[BLACK])));
FILE2C(F88(wk->square)),
RANK2C(R88(wk->square)),
FILE2C(F88(bk->square)),
RANK2C(R88(bk->square)));
printf("Possible en-passant: [%#x] ", pos->en_passant);
if (pos->en_passant == 0)
printf("None.\n");
else
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("%d %d = %c%c\n",
F88(pos->en_passant),
R88(pos->en_passant),
FILE2C(F88(pos->en_passant)),
RANK2C(R88(pos->en_passant)));
printf("castle [%#x] : ", pos->castle);
@@ -115,10 +136,52 @@ void pos_print(pos_t *pos)
popcount64(pos->controlled[BLACK]));
printf("Mobility: W:%u B:%u\n", pos->mobility[WHITE],
pos->mobility[BLACK]);
printf("Bitboards occupied :\n");
bitboard_print2(pos->occupied[WHITE], pos->occupied[BLACK]);
printf("Bitboards controlled :\n");
bitboard_print2(pos->controlled[WHITE], pos->controlled[BLACK]);
}
/**
* pos_check() - extensive position consistenci check.
* @pos: &position
*/
void pos_check(pos_t *pos)
{
int rank, file;
piece_t piece;
board_t *board = pos->board;
/* check that board and bitboard reflect same information */
for (rank = 7; rank >= 0; --rank) {
for (file = 0; file < 8; ++file) {
piece_list_t *ppiece;
printf("checking %c%c ", file+'a', rank+'1');
piece = board[SQ88(file, rank)].piece;
ppiece= board[SQ88(file, rank)].s_piece;
printf("piece=%s ", P_CSYM(piece));
if (ppiece)
printf("ppiece=%s/sq=%#x ", P_CSYM(ppiece->piece), ppiece->square);
switch(PIECE(piece)) {
case PAWN:
printf("pawn" );
break;
case KNIGHT:
printf("knight ");
break;
case BISHOP:
printf("bishop ");
break;
case ROOK:
printf("rook ");
break;
case QUEEN:
printf("queen ");
break;
case KING:
printf("king ");
break;
}
printf("\n");
}
}
}
pos_t *pos_clear(pos_t *pos)
@@ -137,19 +200,23 @@ pos_t *pos_clear(pos_t *pos)
}
SET_WHITE(pos->turn);
pos->node_count = 0;
pos->castle = 0;
pos->clock_50 = 0;
pos->curmove = 0;
pos->eval = 0;
pos->en_passant = 0;
pos->king[WHITE] = 0;
pos->king[BLACK] = 0;
pos->occupied[WHITE] = 0;
pos->occupied[BLACK] = 0;
for (int color=0; color<2; ++color)
for (int piece = BB_ALL; piece < BB_END; ++piece)
pos->bb[color][piece] = 0;
pos->controlled[WHITE] = 0;
pos->controlled[BLACK] = 0;
pos->mobility[WHITE] = 0;
pos->mobility[BLACK] = 0;
pos->moves_generated = false;
pos->moves_counted = false;
/* remove pieces / moves */
pieces_del(pos, WHITE);
pieces_del(pos, BLACK);
@@ -158,6 +225,18 @@ pos_t *pos_clear(pos_t *pos)
return pos;
}
/**
* pos_del() - delete a position.
* @pos: &position.
*/
void pos_del(pos_t *pos)
{
pieces_del(pos, WHITE);
pieces_del(pos, BLACK);
moves_del(pos);
pool_add(pos_pool, pos);
}
pos_t *pos_startpos(pos_t *pos)
{
static char *startfen="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
@@ -171,16 +250,36 @@ pos_t *pos_get()
if (pos) {
INIT_LIST_HEAD(&pos->pieces[WHITE]);
INIT_LIST_HEAD(&pos->pieces[BLACK]);
INIT_LIST_HEAD(&pos->moves);
INIT_LIST_HEAD(&pos->moves[WHITE]);
INIT_LIST_HEAD(&pos->moves[BLACK]);
pos_clear(pos);
} else {
fprintf(stderr, "zobaaa\n");
}
return pos;
}
/* TODO: merge with pos_get - NULL for init, non null for duplicate */
/**
* pos_dup() - duplicate a position.
* @pos: &position to duplicate.
*
* New position is the same as source one (with duplicated pieces list),
* except:
* - moves list is empty
* - bestmove is NULL
* - nodecount is set to zero
* - eval is set to EVAL_INVALID
* - moves_generated ans moves_counted are unset
* - check is set to zero
*
* @return: The new position.
*
* TODO: merge with pos_get - NULL for init, non null for duplicate
*/
pos_t *pos_dup(pos_t *pos)
{
struct list_head *p_cur, *tmp, *piece_list;
struct list_head *p_cur, *piece_list;
piece_list_t *oldpiece;
board_t *board;
pos_t *new = pool_get(pos_pool);
@@ -188,21 +287,24 @@ pos_t *pos_dup(pos_t *pos)
if (new) {
board = new->board;
*new = *pos;
INIT_LIST_HEAD(&new->pieces[WHITE]);
INIT_LIST_HEAD(&new->pieces[BLACK]);
INIT_LIST_HEAD(&new->moves);
/* duplicate piece list */
for (int color=0; color<2; ++color) {
for (int color = 0; color < 2; ++color) {
INIT_LIST_HEAD(&new->pieces[color]);
INIT_LIST_HEAD(&new->moves[color]);
/* duplicate piece list */
piece_list = &pos->pieces[color]; /* white/black piece list */
list_for_each_safe(p_cur, tmp, piece_list) {
list_for_each(p_cur, piece_list) {
oldpiece = list_entry(p_cur, piece_list_t, list);
board[oldpiece->square].s_piece =
piece_add(new, oldpiece->piece, oldpiece->square);
}
}
new->bestmove = NULL;
new->node_count = 0;
new->eval = EVAL_INVALID;
new->moves_generated = false;
new->moves_counted = false;
new->check[WHITE] = new->check[BLACK] = 0;
}
return new;
}
@@ -210,7 +312,7 @@ pos_t *pos_dup(pos_t *pos)
pool_t *pos_pool_init()
{
if (!pos_pool)
pos_pool = pool_init("positions", 128, sizeof(pos_t));
pos_pool = pool_create("positions", 128, sizeof(pos_t));
return pos_pool;
}

View File

@@ -5,7 +5,7 @@
* 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>.
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
@@ -15,38 +15,51 @@
#define POSITION_H
#include <stdint.h>
#include "chessdefs.h"
#include <pool.h>
#include <list.h>
#include <bits.h>
#include "board.h"
#include "pool.h"
#include "list.h"
#include "chessdefs.h"
typedef struct pos_s {
u64 node_count; /* evaluated nodes */
piece_t turn; /* we use only color bit */
castle_t castle;
u16 clock_50;
u16 curmove;
u16 mobility[2];
eval_t eval;
int check[2];
int eval_simple_phase;
eval_t eval_simple;
move_t *bestmove;
bool moves_generated;
bool moves_counted;
board_t board[BOARDSIZE];
square_t en_passant;
square_t king[2];
bitboard_t occupied[2];
bitboard_t bb[2][BB_END]; /* use: pieces[BLACK][BB_PAWN] */
bitboard_t occupied[2]; /* OR of bb[COLOR][x] */
bitboard_t controlled[2];
struct list_head pieces[2];
struct list_head moves;
u16 mobility[2];
struct list_head pieces[2]; /* pieces list, King is first */
struct list_head moves[2];
} pos_t;
void bitboard_print(bitboard_t bb);
void bitboard_print2(bitboard_t bb1, bitboard_t bb2);
void pos_pieces_print(pos_t *pos);
void pos_bitboards_print(pos_t *pos);
void pos_print(pos_t *pos);
pos_t *pos_clear(pos_t *pos);
void pos_del(pos_t *pos);
pos_t *pos_startpos(pos_t *pos);
pos_t *pos_create();
pool_t *pos_pool_init();
void pos_pool_stats();
pos_t *pos_get();
pos_t *pos_dup(pos_t *pos);
void pos_check(pos_t *pos);
#endif /* POSITION_H */

237
src/search.c Normal file
View File

@@ -0,0 +1,237 @@
/* search.c - search good moves.
*
* Copyright (C) 2023 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.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#include <br.h>
#include <list.h>
#include "debug.h"
#include "move.h"
#include "eval.h"
#include "search.h"
/**
* negamax() - search position negamax.
* @pos: &position to search
* @depth: Wanted depth.
* @color: 1 for white, -1 for black.
*
* Calculate the negamax value of @pos. This is an extensive search, with
* absolutely no cutoff.
*
* @return: The @pos negamax evaluation.
*/
eval_t negamax(pos_t *pos, int depth, int color)
{
move_t *move;
pos_t *newpos;
eval_t best = EVAL_MIN, score;
pos->node_count++;
if (depth == 0) {
moves_gen_all_nomoves(pos);
score = eval(pos) * color;
return score;
}
moves_gen_all(pos);
list_for_each_entry(move, &pos->moves[pos->turn], list) {
newpos = move_do(pos, move);
score = -negamax(newpos, depth - 1, -color);
pos->node_count += newpos->node_count;
move->negamax = score;
if (score > best) {
best = score;
pos->bestmove = move;
}
move_undo(newpos, move);
}
return best;
}
/**
* pvs() - Principal Variation Search.
* @pos: &position to search
* @depth: wanted depth.
* @alpha: alpha value.
* @beta: beta value.
* @color: 1 for white, -1 for black.
*
* Calculate the PVS value of @pos.
* See https://en.wikipedia.org/wiki/Principal_variation_search
*
* Moves list should be first generated and evaluated/sorted.
*
* @return: The @pos PVS evaluation.
*/
eval_t pvs(pos_t *pos, int depth, int alpha, int beta, int color)
{
move_t *move;
pos_t *newpos;
eval_t score = EVAL_INVALID;
bool firstchild = true;
pos->node_count++;
if (depth == 0) {
//return quiesce(p, alpha, beta); /* leaf node */
moves_gen_all_nomoves(pos);
score = eval(pos) * color;
log_f(2, "Terminal: depth=%d ", depth);
log_f(2, "score=%d alpha=%d beta=%d\n", score, alpha, beta);
return score;
}
moves_gen_all(pos);
//moves_print(pos, M_PR_EVAL);
/* do the full search for first child */
//move = list_first_entry_or_null(&pos->moves[pos->turn], move_t, list);
list_for_each_entry(move, &pos->moves[pos->turn], list) {
newpos = move_do(pos, move);
log(2, "%.*s", 5 - depth, " ");
if (firstchild) { /* first child */
score = -pvs(newpos, depth - 1, -beta, -alpha, -color);
log_f(2, "First child depth=%d move=", depth);
//move_print(0, move, 0);
log(2, "score=%d alpha=%d beta=%d\n", score, alpha, beta);
pos->bestmove = move;
} else {
/* search with a null window */
score = -pvs(newpos, depth - 1, -alpha - 1, -alpha, -color);
log_f(2, "Other child depth=%d move=", depth);
//move_print(0, move, 0);
log_f(2, "score=%d alpha=%d beta=%d ", score, alpha, beta);
/* for fail-soft: if (score > alpha && score < beta) */
if (score > alpha) {
/* if failed high, do a full re-search */
log_f(2, "doing full search.");
score = -pvs(newpos, depth - 1, -beta, -alpha, -color);
}
log(2, "\n");
}
pos->node_count += newpos->node_count;
move_undo(newpos, move);
if (score >= beta) { /* fail-hard hard beta cut-off */
log(2, "%.*s", 5 - depth, " ");
log_f(2, "depth=%d score=%d alpha=%d beta=%d beta cut-off.\n",
depth, score, alpha, beta);
return beta;
}
if (score > alpha) {
log(2, "%.*s", 5 - depth, " ");
log_f(2, "depth=%d setting new alpha from %d to %d\n",
depth, alpha, score);
alpha = score;
pos->bestmove = move;
}
move->pos = NULL;
move->negamax = score;
firstchild = false;
}
return alpha;
}
/*
* int negascout (pos_t *pos, int depth, int alpha, int beta )
* { /\* compute minimax value of position p *\/
* move_t *move;
* pos_t *newpos;
* eval_t best = EVAL_MIN, score;
*
* int a, b, t, i;
*
* if (depth == 0) {
* //return quiesce(p, alpha, beta); /\* leaf node *\/
* moves_gen_all_nomoves(pos);
* score = eval(pos) * color;
* return score;
* }
* moves_gen_all(pos);
* a = alpha;
* b = beta;
* list_for_each_entry(move, &pos->moves[pos->turn], list) {
* log(1, "%.*s", 5 - depth, " ");
* newpos = move_do(pos, move);
* // for ( i = 1; i <= w; i++ ) {
* t = -negascout (newpos, depth - 1, -b, -alpha);
* if ( (t > a) && (t < beta) && (i > 1) )
* t = -NegaScout ( p_i, -beta, -alpha ); /\* re-search *\/
* alpha = max( alpha, t );
* if ( alpha >= beta )
* return alpha; /\* cut-off *\/
* b = alpha + 1; /\* set new null window *\/
* }
* return alpha;
* }
*/
/*
* int quiesce(pos_t *pos, int alpha, int beta)
* {
* int stand_pat = eval(pos);
*
* if( stand_pat >= beta )
* return beta;
* if( alpha < stand_pat )
* alpha = stand_pat;
*
* /\*
* * until( every_capture_has_been_examined ) {
* * MakeCapture();
* * score = -Quiesce( -beta, -alpha );
* * TakeBackMove();
* *
* * if( score >= beta )
* * return beta;
* * if( score > alpha )
* * alpha = score;
* * }
* *\/
* return alpha;
* }
*/
/**
* ab_negamax() - search position negamax with alpha-beta cutoff.
* @pos: &position to search
* @depth: Wanted depth.
* @color: 1 for white, -1 for black.
*
* Calculate the negamax value of @pos, with alpha-beta pruning.
*
* @return: The @pos negamax evaluation.
*/
/*int ab_negamax(pos_t *pos, int alpha, int beta, int depth)
{
move_t *move;
pos_t *newpos;
eval_t best = EVAL_MIN, score;
if(depth == 0) {
//return quiesce( alpha, beta );
moves_gen_all_nomoves(pos);
score = eval(pos) * color;
return score;
}
for ( all moves) {
score = -alphaBeta( -beta, -alpha, depthleft - 1 );
if( score >= beta )
return beta; // fail hard beta-cutoff
if( score > alpha )
alpha = score; // alpha acts like max in MiniMax
}
return alpha;
}
*/

22
src/search.h Normal file
View File

@@ -0,0 +1,22 @@
/* search.h - search for perfect move.
*
* 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.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#ifndef SEARCH_H
#define SEARCH_H
#include "position.h"
eval_t negamax(pos_t *pos, int depth, int color);
eval_t pvs(pos_t *pos, int depth, int alpha, int beta, int color);
#endif /* SEARCH_H */

35
test/eval.c Normal file
View File

@@ -0,0 +1,35 @@
#include "debug.h"
#include "../src/position.h"
#include "../src/eval.h"
#include "../src/fen.h"
#include "../src/move.h"
int main(int ac, char**av)
{
pos_t *pos;
eval_t res;
debug_init(5, stderr, true);
piece_pool_init();
moves_pool_init();
pos_pool_init();
pos = pos_get();
if (ac == 1) {
pos_startpos(pos);
} else {
fen2pos(pos, av[1]);
}
pos_print(pos);
pos_pieces_print(pos);
moves_gen_all(pos);
pos_print(pos);
moves_print(pos, M_PR_SEPARATE);
res = eval(pos);
printf("eval=%d centipawns)\n", res);
}

20
test/fen.c Normal file
View File

@@ -0,0 +1,20 @@
#include "debug.h"
#include "pool.h"
#include "../src/position.h"
#include "../src/fen.h"
int main(int ac, char**av)
{
pos_t *pos;
debug_init(5, stderr, true);
piece_pool_init();
pos_pool_init();
pos = pos_get();
if (ac == 1) {
pos_startpos(pos);
} else {
fen2pos(pos, av[1]);
}
pos_print(pos);
}

31
test/move.c Normal file
View File

@@ -0,0 +1,31 @@
#include <stdio.h>
#include "debug.h"
#include "../src/fen.h"
#include "../src/move.h"
int main(int ac, char**av)
{
pos_t *pos;
debug_init(5, stderr, true);
piece_pool_init();
moves_pool_init();
pos_pool_init();
pos = pos_get();
if (ac == 1) {
fen2pos(pos, "rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2");
//pos_startpos(pos);
} else {
fen2pos(pos, av[1]);
}
//printf("turn = %d opponent = %d\n", pos->turn, OPPONENT(pos->turn));
moves_gen_all(pos);
pos_print(pos);
pos_pieces_print(pos);
moves_print(pos, M_PR_SEPARATE);
//bitboard_print2(castle_squares[0].controlled, castle_squares[1].controlled);
//bitboard_print2(castle_squares[0].occupied, castle_squares[1].occupied);
}

55
test/piece.c Normal file
View File

@@ -0,0 +1,55 @@
#include <stdio.h>
#include "debug.h"
#include "../src/fen.h"
#include "../src/position.h"
#include "../src/bitboard.h"
int main(int ac, char**av)
{
pos_t *pos;
printf("zobi\n");fflush(stdout);
debug_init(6, stderr, true);
log_f(5, "kfsjdhg\n");
pos_pool_init();
pos = pos_get();
piece_pool_init();
if (ac == 1) {
printf("zoba\n");fflush(stdout);
pos_startpos(pos);
} else {
fen2pos(pos, av[1]);
}
pos_print(pos);
pos_pieces_print(pos);
printf("0x1c = 11100 = C1-E1:\n");
bitboard_print(0x1c);
printf("0x70 = 111 = A1-C1\n");
bitboard_print(0x70);
printf("0x0e = 1110 = B1-D1\n");
bitboard_print(0x0e);
printf("0x60 = 1100000 = F1-G1\n");
bitboard_print(0x60);
printf("A1:\n");
bitboard_print(A1);
printf("1:\n");
bitboard_print(1L);
printf("H1:\n");
bitboard_print(H1);
printf("C1:\n");
bitboard_print(C1);
printf("D1:\n");
bitboard_print(D1);
printf("C1|D1:\n");
bitboard_print(C1|D1);
printf("H8:\n");
bitboard_print(H8);
}