knucklebones/lut.py

92 lines
2.2 KiB
Python

s = set()
for a in range(7):
for b in range(7):
for c in range(7):
s.add(tuple(sorted((a, b, c))))
mapping = sorted(s)
n = 128
filler = 0
print(f"const MAPPING: [(u8, u8, u8); {len(mapping)}] = {list(mapping)};")
print()
################################################################################
def score(column):
values = {die: 0 for die in range(1, 7)}
for die in column:
if die == 0:
continue
values[die] += 1
multiplier = {0: 1, 1: 1, 2: 2, 3: 9}
return sum(value * count * multiplier[count] for (value, count) in values.items())
score_lut = [filler for _ in range(n)]
for i, column in enumerate(mapping):
score_lut[i] = score(column)
print(f"const SCORE_LUT: [u8; {n}] = {score_lut};")
print()
################################################################################
def add(die, column):
if 0 not in column:
raise ValueError("Invalid Move")
new_column = list(column)
new_column[0] = die # sorted and a zero is present -> first index is 0
return tuple(sorted(new_column))
add_lut = {die: [filler for _ in range(32)] for die in range(1, 7)}
for die in range(1, 7):
for i, column in enumerate(mapping):
try:
new_column = add(die, column)
except ValueError:
continue
add_lut[die][i] = mapping.index(new_column)
print(f"const ADD_LUT: [[u8; 32]; 8] = [")
print(f" [{filler}; 32],")
for die in range(1, 7):
print(f" {add_lut[die]},")
print(f" [{filler}; 32],")
print("];");
print()
################################################################################
def remove(die, opposite):
new_opposite = list(opposite)
for i in range(3):
if new_opposite[i] == die:
new_opposite[i] = 0
return tuple(sorted(new_opposite))
remove_lut = {die: [filler for _ in range(n)] for die in range(1, 7)}
for die in range(1, 7):
for i, column in enumerate(mapping):
new_column = remove(die, column)
remove_lut[die][i] = mapping.index(new_column)
print(f"const REMOVE_LUT: [[u8; {n}]; 8] = [")
print(f" [{filler}; {n}],")
for die in range(1, 7):
print(f" {remove_lut[die]},")
print(f" [{filler}; {n}],")
print("];");
print()