Converting FairyGen to other Programming Languages

Endgame analysis using tablebases, EGTB generation, exchange, sharing, discussions, etc..
Post Reply
byakuugan
Posts: 44
Joined: Tue May 18, 2010 4:36 am
Sign-up code: 10159

Converting FairyGen to other Programming Languages

Post by byakuugan »

My computer doesn't seem to be able to run C anymore.
I was feeding ChatGPT the C code to FairyGen and asking it to convert it to python.
It gave me this and I thought I might as well post it on here to see if anyone could help.

import re

# Global variables
MEN = 3 # Define the value of MEN here
cc = [] # Define cc array
first = [] # Define first array
mode = [] # Define mode array
boardvec = [1, -1, 16, -16, 0,
14, -14, 18, -18, 31, -31, 33, -33, # 5
2, -2, 32, -32, 30, -30, 34, -34, 0, # 13
14, -14, 18, -18, 31, -31, 33, -33, # 22
1, -1, 16, -16, 15, -15, 17, -17, 0, # 30
1, -1, 16, -16, 2, -2, 32, -32, 0, # 39
13, -13, 19, -19, 47, -47, 49, -49, # 48
14, -14, 18, -18, 31, -31, 33, -33, # 56
29, -29, 35, -35, 46, -46, 50, -50, 0, # 64
29, -29, 35, -35, 46, -46, 50, -50, # 73
13, -13, 19, -19, 47, -47, 49, -49, 0, # 81
10, -10, 22, -22, 95, -95, 97, -97, 0, # 90
12, -12, 20, -20, 63, -63, 65, -65, 0, # 99
13, -13, 19, -19, 47, -47, 49, -49, # 108
14, -14, 18, -18, 31, -31, 33, -33, 0, # 116
15, -15, 17, -17, 0, # 125
15, -15, 17, -17, # 130
14, -14, 18, -18, 31, -31, 33, -33, 0, # 134
15, -15, 17, -17, # 143
1, -1, 16, -16, 0, # 147
1, -1, 16, -16, -2, 32, -32, 2, 0, # 152
1, -1, # 161
15, -15, 17, -17, 30, -30, 34, -34, 0, # 163
1, -1, 16, -16, 30, -30, 34, -34, 0, # 203
# 15, -15, 17, -17,
# 30, -30, 34, -34, 1, -1, 16, -16, 0, # 161
1, -1, 16, -16, 15, -15, 17, -17, 0] # Define boardvec array
bis = [] # Define bis array
piece = [K, N, K] # Define piece array
nblack = 0 # Define nblack variable
nullMove = 0 # Define nullMove variable
staleMate = 0 # Define staleMate variable
fname = None # Define fname variable
exch = False # Define exch variable
basevec = [1, -1, 8, -8, 0,
6, -6, 10, -10, 15, -15, 17, -17,
2, -2, 16, -16, 14, -14, 18, -18, 0,
6, -6, 10, -10, 15, -15, 17, -17,
1, -1, 8, -8, 7, -7, 9, -9, 0,
1, -1, 8, -8, 2, -2, 16, -16, 0,
5, -5, 11, -11, 23, -23, 25, -25,
6, -6, 10, -10, 15, -15, 17, -17,
13, -13, 19, -19, 22, -22, 26, -26, 0,
13, -13, 19, -19, 22, -22, 26, -26,
5, -5, 11, -11, 23, -23, 25, -25, 0,
2, -2, 14, -14, 47, -47, 49, -49, 0,
4, -4, 12, -12, 31, -31, 33, -33, 0,
5, -5, 11, -11, 23, -23, 25, -25,
6, -6, 10, -10, 15, -15, 17, -17, 0,
7, -7, 9, -9, 0,
7, -7, 9, -9,
6, -6, 10, -10, 15, -15, 17, -17, 0,
7, -7, 9, -9,
1, -1, 8, -8, 0,
1, -1, 8, -8, -2, 16, -16, 2, 0,
1, -1,
7, -7, 9, -9, 14, -14, 18, -18, 0,
1, -1, 8, -8, 7, -7, 9, -9, 0,
2, -2, 16, -16, 7, -7, 9, -9, 0,
2, -2, 16, -16,
7, -7, 9, -9, 14, -14, 18, -18, 0,
1, -1, 8, -8, 14, -14, 18, -18, 0,
# 7, -7, 9, -9,
# 14, -14, 18, -18, 1, -1, 8, -8, 0,
1, -1, 8, -8, 7, -7, 9, -9, 0] # Define basevec array
stride = [] # Define stride array
codevec = [] # Define codevec array
histo = [] # Define histo array
cs1 = cs2 = cs3 = inhom = 0 # Define cs1, cs2, cs3, inhom variables

def main():
# Parse command line arguments
argc = len(sys.argv)
argv = sys.argv

if argc > 2 and argv[1] == "-f":
global fname
fname = argv[2]
argc -= 2
argv = argv[2:]

# Read piece definitions from ini file
if f := open("piecedef.ini", "r"):
i = j = 0
for line in f:
match = re.match(r"(\S): (\S)", line)
if match:
p, c = match.group(1), match.group(2)
cc = p
first = j
m = 1 # start color
while True:
line = f.readline().strip()
if not line:
break
match = re.match(r"(\d+),(\d+)", line)
if match:
k, l = int(match.group(1)), int(match.group(2))
mode[j] = 3 # assume leaper
rights = re.findall(r"\S+", line)[2:]
if rights:
if 'n' in rights:
mode[j] = 2
if 'c' in rights:
mode[j] = 1
mode[j] |= 4 * ('s' in rights)
if mode[j] & 2:
m |= 1 + ((k ^ l) & 1) # color of non-capt target square
boardvec[j] = 16 * k + l
if '*' in rights or '+' in rights:
if l:
mode[j] = mode[j - 1]
boardvec[j] = 16 * k - l
j += 1
if k:
mode[j] = mode[j - 1]
boardvec[j] = -16 * k + l
j += 1
if k and l:
mode[j] = mode[j - 1]
boardvec[j] = -16 * k - l
j += 1
if k != l and ('*' in rights or '+' in rights):
mode[j] = mode[j - 1]
boardvec[j] = 16 * l + k
j += 1
if k:
mode[j] = mode[j - 1]
boardvec[j] = 16 * l - k
j += 1
if l:
mode[j] = mode[j - 1]
boardvec[j] = -16 * l + k
j += 1
if k and l:
mode[j] = mode[j - 1]
boardvec[j] = -16 * l - k
j += 1
elif '+' in rights:
if k:
mode[j] = mode[j - 1]
boardvec[j] = 16 * l - k
j += 1
if l:
mode[j] = mode[j - 1]
boardvec[j] = -16 * k + l
j += 1
if k and l:
mode[j] = mode[j - 1]
boardvec[j] = -16 * l - k
j += 1
elif line == "#":
break
boardvec[j] = mode[j] = 0
j += 1
bis = m != 3 # color-bound
i += 1
cc = 0
f.close()

for i in range(len(cc)):
print("#", cc, end=": ")
j = first
while boardvec[j]:
print(boardvec[j], mode[j], end=" ")
j += 1
print()

j = MEN - 1
k = -1
if argc > 1:
for i in range(len(argv[1])):
if argv[1] == '.':
k = 1
j = 0
continue
piece[j] = cc.index(argv[1])
nblack = j + k

f = open("rep2.txt", "w")

stride[0] = 1 # define TB mapping
for i in range(1, MEN):
stride = 64 * stride[i-1] # basically all 6-bit square numbers packed next to each other

scan(-1, 0, 0) # clear TB

# initialize stepvectors in TB for each piece, by pre-scaling
for i in range(MEN):
j = first[piece] - 1
m = MAXDIR * i
while (k := basevec[++j]):
codevec[m] = stride[i] * k # scaled 077 vectors
m += 1

for i in range(MEN):
if bis[piece[i]]:
cs3 = cs2
cs2 = cs1
cs1 = i
if cs2 < 0:
cs1 = -1
else:
if cs3 >= 0:
if cs2 < nblack: # two black bishops
i = cs1
cs1 = cs3
cs3 = i # cs1&cs2 same side
inhom = piece[cs1] != piece[cs2]

print("bishops:", cs1, cs2, cs3, "(", inhom, ")")

build()
print(xh, sample1, sample2)

for i in range(149):
histo[i] = histo[150 + i] = histo[300 + i] = histo[450 + i] = 0
scan(-2, 1, 0)
f.write("\nWON.wtm %10d" % histo[128])
if cs1 >= 0:
f.write(" %10d" % histo[278])
if cs3 >= 0:
f.write(" %10d %10d" % (histo[428], histo[578]))
f.write("\nK capture %8d" % histo[149])
if cs1 >= 0:
f.write(" %10d" % histo[299])
if cs3 >= 0:
f.write(" %10d %10d" % (histo[449], histo[599]))
f.write("\nother %10d" % (histo[128] - histo[149]))
if cs1 >= 0:
f.write(" %10d" % (histo[278] - histo[299]))
if cs3 >= 0:
f.write(" %10d %10d" % (histo[428] - histo[449], histo[578] - histo[599]))
for i in range(126):
if histo[i] + histo[150 + i] + histo[300 + i] + histo[450 + i]:
f.write("\n%3d. %10d" % (i, histo[i]))
if cs1 >= 0:
f.write(" %10d" % histo[150 + i])
if cs3 >= 0:
f.write(" %10d %10d" % (histo[300 + i], histo[450 + i]))
for j in range(0, 600, 150):
histo[j + 148] += histo[j + i]
f.write("\nWON.btm %10d" % (histo[148] - histo[0]))
if cs1 >= 0:
f.write(" %10d" % (histo[298] - histo[150]))
if cs3 >= 0:
f.write(" %10d %10d" % (histo[448] - histo[300], histo[598] - histo[450]))
f.write("\nstalemate %8d" % histo[127])
if cs1 >= 0:
f.write(" %10d" % histo[277])
if cs3 >= 0:
f.write(" %10d %10d" % (histo[427], histo[577]))
f.write("\nW check %10d" % histo[126])
if cs1 >= 0:
f.write(" %10d" % histo[276])
if cs3 >= 0:
f.write(" %10d %10d" % (histo[426], histo[576]))
f.write("\nLEGAL %10d" % (histo[148] + histo[127]))
if cs1 >= 0:
f.write(" %10d" % (histo[298] + histo[277]))
if cs3 >= 0:
f.write(" %10d %10d" % (histo[448] + histo[427], histo[598] + histo[577]))
f.write("\nTOTAL %10d" % (histo[148] + histo[126] + histo[127]))
if cs1 >= 0:
f.write(" %10d" % (histo[298] + histo[276] + histo[277]))
if cs3 >= 0:
f.write(" %10d %10d" % (histo[448] + histo[426] + histo[427], histo[598] + histo[576] + histo[577]))
f.write("\n")
f.close()
play()
return 0

if __name__ == "__main__":
main()
h.g.muller
Posts: 223
Joined: Mon Feb 19, 2007 8:24 am
Sign-up code: 0
Location: Amsterdam
Contact:

Re: Converting FairyGen to other Programming Languages

Post by h.g.muller »

And, does it run properly?

You might be interest in the checkmating applets I wrote in JavaScript for 2-vs-1 and 3-vs-1 checkmating:

https://www.chessvariants.com/page/MScheckmating-applet

There also is a version that can handle bent sliders and hoppers:

https://www.chessvariants.com/membergra ... rking=true
Post Reply