Skip to content

Instantly share code, notes, and snippets.

@niklasf niklasf/tbdep.py
Created Jan 26, 2018

Embed
What would you like to do?
Create a Makefile for Syzygy tabebase generation
import sys
import chess.syzygy
import itertools
PCHR = chess.syzygy.PCHR
NORMAL = set(chess.syzygy.filenames())
def swap(egname):
w, b = egname.split("v")
return b + "v" + w
def normalize(egname):
if egname in NORMAL:
return egname
else:
return swap(egname)
def _ch(counts):
return "".join(ch * counts.get(ch, 0) for ch in PCHR)
def _counts(ch):
return {p: ch.count(p) for p in PCHR if ch.count(p)}
def _promotion_deps(egname):
w, b = egname.split("v")
wc, bc = _counts(w), _counts(b)
if wc.get("P", 0):
wc["P"] -= 1
for promotion in PCHR:
if promotion != "P" and promotion != "K":
wc[promotion] = wc.get(promotion, 0) + 1
subeg = _ch(wc) + "v" + b
yield normalize(subeg)
yield from _promotion_deps(subeg)
wc[promotion] -= 1
wc["P"] += 1
if bc.get("P", 0):
bc["P"] -= 1
for promotion in PCHR:
if promotion != "P" and promotion != "K":
bc[promotion] = bc.get(promotion, 0) + 1
subeg = w + "v" + _ch(bc)
yield normalize(subeg)
yield from _promotion_deps(subeg)
bc[promotion] -= 1
bc["P"] += 1
def _deps(egname):
w, b = egname.split("v")
wc, bc = _counts(w), _counts(b)
for p in PCHR:
if p != "K" and p in wc:
wc[p] -= 1
yield normalize(_ch(wc) + "v" + b)
wc[p] += 1
def deps(egname):
closed = set()
closed.add("KvK")
for eg in itertools.chain(_deps(egname), _deps(swap(egname)), _promotion_deps(egname)):
if not eg in closed:
closed.add(eg)
yield eg
def fname(egname):
return "%s.dtz" % (egname, )
def main(egnames):
print("TBGEN = ../tbgen -s -t 6")
print("TBGENP = ../tbgenp -s -t 6")
print()
closed = set()
closed.add("KvK")
open_list = list(egnames)
while open_list:
egname = normalize(open_list.pop(0))
if not egname in closed:
closed.add(egname)
dep_list = list(deps(egname))
if dep_list:
print("%s: %s" % (fname(egname), " ".join(fname(eg) for eg in dep_list)))
else:
print(fname(egname))
if "P" in egname:
print("\t$(TBGENP) %s" % (egname, ))
else:
print("\t$(TBGEN) %s" % (egname, ))
for dep in dep_list:
open_list.append(dep)
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.