def adv(board): # Advancement
"""
The parameter is credited with 1 for each passive man in the
fifth and sixth rows (counting in passive's direction) and
debited with 1 for each passive man in the third and fourth
rows.
"""
passive = board.passive
def back(board): # Back Row Bridge
"""
The parameter is credited with 1 if there are no active kings
on the board and if the two bridge squares (1 and 3, or 30 and
32) in the back row are occupied by passive pieces.
"""
active = board.active
passive = board.passive
if active == BLACK:
if board.backward[BLACK] != 0:
return 0
back_row_bridge = 0x480000000
else:
if board.forward[WHITE] != 0:
return 0
back_row_bridge = 0x5
if bin(back_row_bridge & board.pieces[passive]).count('1') == 2:
return 1
return 0
def cent(board): # Center Control I
"""
The parameter is credited with 1 for each of the following
squares: 11, 12, 15, 16, 20, 21, 24, 25 which is occupied by
a passive man.
"""
passive = board.passive
if passive == WHITE:
center_pieces = 0xA619800
else:
center_pieces = 0xCC3280
def cntr(board): # Center Control II
"""
The parameter is credited with 1 for each of the following
squares: 11, 12, 15, 16, 20, 21, 24, 25 that is either
currently occupied by an active piece or to which an active
piece can move.
"""
active = board.active
if active == BLACK:
center_pieces = 0xA619800
else:
center_pieces = 0xCC3280
def deny(board): # Denial of Occupancy
"""
The parameter is credited 1 for each square defined in MOB if
on the next move a piece occupying this square could be
captured without exchange.
"""
rf = board.right_forward()
lf = board.left_forward()
rb = board.right_backward()
lb = board.left_backward()
moves = [0x11 << i for (i, bit) in enumerate(bin(rf)[::-1]) if bit == '1']
moves += [0x21 << i for (i, bit) in enumerate(bin(lf)[::-1]) if bit == '1']
moves += [0x11 << i - 4 for (i, bit) in enumerate(bin(rb)[::-1]) if bit == '1']
moves += [0x21 << i - 5 for (i, bit) in enumerate(bin(lb)[::-1]) if bit == '1']
destinations = [0x10 << i for (i, bit) in enumerate(bin(rf)[::-1]) if bit == '1']
destinations += [0x20 << i for (i, bit) in enumerate(bin(lf)[::-1]) if bit == '1']
destinations += [0x1 << i - 4 for (i, bit) in enumerate(bin(rb)[::-1]) if bit == '1']
destinations += [0x1 << i - 5 for (i, bit) in enumerate(bin(lb)[::-1]) if bit == '1']
denials = []
for move, dest in zip(moves, destinations):
B = board.peek_move(move)
active = B.active
ms_taking = []
ds = []
if (B.forward[active] & (dest >> 4)) != 0 and (B.empty & (dest << 4)) != 0:
ms_taking.append((-1)*((dest >> 4) | (dest << 4)))
ds.append(dest << 4)
if (B.forward[active] & (dest >> 5)) != 0 and (B.empty & (dest << 5)) != 0:
ms_taking.append((-1)*((dest >> 5) | (dest << 5)))
ds.append(dest << 5)
if (B.backward[active] & (dest << 4)) != 0 and (B.empty & (dest >> 4)) != 0:
ms_taking.append((-1)*((dest << 4) | (dest >> 4)))
ds.append(dest >> 4)
if (B.backward[active] & (dest << 5)) != 0 and (B.empty & (dest >> 5)) != 0:
ms_taking.append((-1)*((dest << 5) | (dest >> 5)))
ds.append(dest >> 5)
if not ms_taking:
continue
else:
for m, d in zip(ms_taking, ds):
C = B.peek_move(m)
if C.active == active:
if not dest in denials:
denials.append(dest)
continue
if not C.takeable(d):
if not dest in denials:
denials.append(dest)
return len(denials)
Assistant
User
continue
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
def kcent(board): # King Center Control
"""
The parameter is credited with 1 for each of the following
squares: 11, 12, 15, 16, 20, 21, 24, and 25 which is occupied
by a passive king.
"""
passive = board.passive
if passive == WHITE:
center_pieces = 0xA619800
passive_kings = board.forward[WHITE]
else:
center_pieces = 0xCC3280
passive_kings = board.backward[BLACK]
def mob(board): # Total Mobility
"""
The parameter is credited with 1 for each square to which the
active side could move one or more pieces in the normal fashion
disregarding the fact that jump moves may or may not be
available.
"""
rf = board.right_forward()
lf = board.left_forward()
rb = board.right_backward()
lb = board.left_backward()
destinations = [0x10 << i for (i, bit) in enumerate(bin(rf)[::-1]) if bit == '1']
destinations += [0x20 << i for (i, bit) in enumerate(bin(lf)[::-1]) if bit == '1']
destinations += [0x1 << i - 4 for (i, bit) in enumerate(bin(rb)[::-1]) if bit == '1']
destinations += [0x1 << i - 5 for (i, bit) in enumerate(bin(lb)[::-1]) if bit == '1']
if not destinations:
return 0
return bin(reduce(lambda x, y: x|y, destinations)).count("1")
def mobil(board): # Undenied Mobility
"""
The parameter is credited with the difference between MOB and
DENY.
"""
return mob(board) - deny(board)
def mov(board): # Move
"""
The parameter is credited with 1 if pieces are even with a
total piece count (2 for men, and 3 for kings) of less than 24,
and if an odd number of pieces are in the move system, defined
as those vertical files starting with squares 1, 2, 3, and 4.
"""
black_men = bin(board.forward[BLACK]).count("1")
black_kings = bin(board.backward[BLACK]).count("1")
black_score = 2black_men + 3black_kings
white_men = bin(board.backward[WHITE]).count("1")
white_kings = bin(board.forward[WHITE]).count("1")
white_score = 2white_men + 3white_kings
if white_score < 24 and black_score == white_score:
pieces = board.pieces[BLACK] | board.pieces[WHITE]
if board.active == BLACK:
move_system = 0x783c1e0f
else:
move_system = 0x783c1e0f0
if bin(move_system & pieces).count("1") % 2 == 1:
return 1
return 0
def thret(board): # Threat
"""
The parameter is credited with 1 for each square to which an
active piece may be moved and in doing so threaten to capture
a passive piece on a subsequent move.
"""
moves = board.get_moves()
destinations = map(lambda x: (x ^ board.pieces[board.active]) & x, moves)
origins = [x ^ y for (x, y) in zip(moves, destinations)]
jumps = []
for dest, orig in zip(destinations, origins):
if board.active == BLACK:
rfj = (board.empty >> 8) & (board.pieces[board.passive] >> 4) & dest
lfj = (board.empty >> 10) & (board.pieces[board.passive] >> 5) & dest
if orig & board.backward[board.active]: # piece is king
rbj = (board.empty << 8) & (board.pieces[board.passive] << 4) & dest
lbj = (board.empty << 10) & (board.pieces[board.passive] << 5) & dest
else:
rbj = 0
lbj = 0
else:
rbj = (board.empty << 8) & (board.pieces[board.passive] << 4) & dest
lbj = (board.empty << 10) & (board.pieces[board.passive] << 5) & dest
if dest & board.forward[board.active]: # piece at square is a king
rfj = (board.empty >> 8) & (board.pieces[board.passive] >> 4) & dest
lfj = (board.empty >> 10) & (board.pieces[board.passive] >> 5) & dest
else:
rfj = 0
lfj = 0
if (rfj | lfj | rbj | lbj) != 0:
jumps += [-0x101 << i for (i, bit) in enumerate(bin(rfj)[::-1]) if bit == '1']
jumps += [-0x401 << i for (i, bit) in enumerate(bin(lfj)[::-1]) if bit == '1']
jumps += [-0x101 << i - 8 for (i, bit) in enumerate(bin(rbj)[::-1]) if bit == '1']
jumps += [-0x401 << i - 10 for (i, bit) in enumerate(bin(lbj)[::-1]) if bit == '1']
return len(jumps)
return black_score - white_score if player == BLACK else white_score - black_score
def position_score(board, player):
scores = [0x88000, 0x1904c00, 0x3A0502E0, 0x7C060301F]
i = 1
total = 0
for s in scores:
total = i*bin(board.pieces[player] & s).count("1")
i += 1
return total
if (rfj | lfj | rbj | lbj) != 0:
rfj = [(1 + i - i//9, 1 + (i + 8) - (i + 8)//9)
for (i, bit) in enumerate(bin(rfj)[::-1]) if bit == '1']
lfj = [(1 + i - i//9, 1 + (i + 10) - (i + 8)//9)
for (i, bit) in enumerate(bin(lfj)[::-1]) if bit == '1']
rbj = [(1 + i - i//9, 1 + (i - 8) - (i - 8)//9)
for (i, bit) in enumerate(bin(rbj)[::-1]) if bit == '1']
lbj = [(1 + i - i//9, 1 + (i - 10) - (i - 10)//9)
for (i, bit) in enumerate(bin(lbj)[::-1]) if bit == '1']
if board.active == BLACK:
regular_moves = ["%i to %i" % (orig, dest) for (orig, dest) in rfj + lfj]
reverse_moves = ["%i to %i" % (orig, dest) for (orig, dest) in rbj + lbj]
return regular_moves + reverse_moves
else:
reverse_moves = ["%i to %i" % (orig, dest) for (orig, dest) in rfj + lfj]
regular_moves = ["%i to %i" % (orig, dest) for (orig, dest) in rbj + lbj]
return reverse_moves + regular_moves
rf = board.right_forward()
lf = board.left_forward()
rb = board.right_backward()
lb = board.left_backward()
rf = [(1 + i - i//9, 1 + (i + 4) - (i + 4)//9)
for (i, bit) in enumerate(bin(rf)[::-1]) if bit == '1']
lf = [(1 + i - i//9, 1 + (i + 5) - (i + 5)//9)
for (i, bit) in enumerate(bin(lf)[::-1]) if bit == '1']
rb = [(1 + i - i//9, 1 + (i - 4) - (i - 4)//9)
for (i, bit) in enumerate(bin(rb)[::-1]) if bit == '1']
lb = [(1 + i - i//9, 1 + (i - 5) - (i - 5)//9)
for (i, bit) in enumerate(bin(lb)[::-1]) if bit == '1']
if board.active == BLACK:
regular_moves = ["%i to %i" % (orig, dest) for (orig, dest) in rf + lf]
reverse_moves = ["%i to %i" % (orig, dest) for (orig, dest) in rb + lb]
return regular_moves + reverse_moves
else:
regular_moves = ["%i to %i" % (orig, dest) for (orig, dest) in rb + lb]
reverse_moves = ["%i to %i" % (orig, dest) for (orig, dest) in rf + lf]
return reverse_moves + regular_moves
To convert the provided Python library into TypeScript, we'll leverage TypeScript's type system to define interfaces and types for the board, pieces, and other elements. We will also use JavaScript's BigInt for operations dealing with large integers as typically used in bit manipulation.
Here's a step-by-step conversion:
Define the Board Interface: We need to define the necessary properties and methods that must be present on the board object.
Convert Constants and Functions: Rewrite the functions using TypeScript syntax, ensuring type safety and utilizing appropriate methods for various operations.
Handle Special Cases: Some Python-specific operations or idioms may need special handling in TypeScript, like list comprehensions or integer enumeration. These will be handled using equivalent TypeScript constructs.
Here's how the conversion would look:
const BLACK = 0;
const WHITE = 1;
const VALID_SQUARES = BigInt("0x7FBFDFEFF");
const INFINITY = Number.MAX_SAFE_INTEGER;
interface Board {
pieces: { [key: number]: bigint }; // Mapping from player to bitboard of pieces
active: number; // Currently active player
passive: number; // Currently passive player
forward: { [key: number]: bigint }; // Kings bitboard
backward: { [key: number]: bigint }; // Kings bitboard
empty: bigint; // Bitboard of empty squares
get_moves: () => bigint[];
peek_move: (move: bigint) => Board;
is_over: () => boolean;
right_forward: () => bigint;
left_forward: () => bigint;
right_backward: () => bigint;
left_backward: () => bigint;
right_forward_jumps: () => bigint;
left_forward_jumps: () => bigint;
right_backward_jumps: () => bigint;
left_backward_jumps: () => bigint;
takeable: (dest: bigint) => boolean;
}
// Utility function for bit counting in TypeScript
function bitCount(n: bigint): number {
return n.toString(2).split('0').join('').length;
}
function adv(board: Board): number {
// Advancement
let passive = board.passive;
let rows_3_and_4 = BigInt("0x1FE00");
let rows_5_and_6 = BigInt("0x3FC0000");
if (passive === WHITE) {
[rows_3_and_4, rows_5_and_6] = [rows_5_and_6, rows_3_and_4];
}
let bits_3_and_4 = rows_3_and_4 & board.pieces[passive];
let bits_5_and_6 = rows_5_and_6 & board.pieces[passive];
return bitCount(bits_5_and_6) - bitCount(bits_3_and_4);
}
function back(board: Board): number {
// Back Row Bridge
let active = board.active;
let passive = board.passive;
let back_row_bridge: bigint;
if (active === BLACK) {
if (board.backward[BLACK] !== BigInt(0)) {
return 0;
}
back_row_bridge = BigInt("0x480000000");
} else {
if (board.forward[WHITE] !== BigInt(0)) {
return 0;
}
back_row_bridge = BigInt("0x5");
}
return bitCount(back_row_bridge & board.pieces[passive]) === 2 ? 1 : 0;
}
function cent(board: Board): number {
// Center Control I
let passive = board.passive;
let center_pieces = passive === WHITE ? BigInt("0xA619800") : BigInt("0xCC3280");
return bitCount(board.pieces[passive] & center_pieces);
}
function cntr(board: Board): number {
// Center Control II
let active = board.active;
let center_pieces = active === BLACK ? BigInt("0xA619800") : BigInt("0xCC3280");
let active_center_count = bitCount(board.pieces[active] & center_pieces);
let moves = board.get_moves();
// Convert negative values to positive if necessary
if (moves[0] < BigInt(0)) {
moves = moves.map(x => x * BigInt(-1));
}
const destinations = moves.reduce((x, y) => x | y, BigInt(0)) & ~board.pieces[active];
let active_near_center_count = bitCount(destinations & center_pieces);
return active_center_count + active_near_center_count;
}
function deny(board: Board): number {
// Denial of Occupancy
let rf = board.right_forward();
let lf = board.left_forward();
let rb = board.right_backward();
let lb = board.left_backward();
let moves = [
...[...rf.toString(2)].map((bit, i) => bit === '1' ? BigInt(0x11) << BigInt(i) : BigInt(0)),
...[...lf.toString(2)].map((bit, i) => bit === '1' ? BigInt(0x21) << BigInt(i) : BigInt(0)),
...[...rb.toString(2)].map((bit, i) => bit === '1' ? BigInt(0x11) << BigInt(i - 4) : BigInt(0)),
...[...lb.toString(2)].map((bit, i) => bit === '1' ? BigInt(0x21) << BigInt(i - 5) : BigInt(0)),
].filter(move => move > BigInt(0));
let destinations = [
...[...rf.toString(2)].map((bit, i) => bit === '1' ? BigInt(0x10) << BigInt(i) : BigInt(0)),
...[...lf.toString(2)].map((bit, i) => bit === '1' ? BigInt(0x20) << BigInt(i) : BigInt(0)),
...[...rb.toString(2)].map((bit, i) => bit === '1' ? BigInt(0x1) << BigInt(i - 4) : BigInt(0)),
...[...lb.toString(2)].map((bit, i) => bit === '1' ? BigInt(0x1) << BigInt(i - 5) : BigInt(0)),
].filter(dest => dest > BigInt(0));
let denials: bigint[] = [];
for (let [move, dest] of moves.entries()) {
let B = board.peek_move(move);
let active = B.active;
let ms_taking: bigint[] = [];
let ds: bigint[] = [];
if ((B.forward[active] & (dest >> BigInt(4))) !== BigInt(0) && (B.empty & (dest << BigInt(4))) !== BigInt(0)) {
ms_taking.push((-1) * ((dest >> BigInt(4)) | (dest << BigInt(4))));
ds.push(dest << BigInt(4));
}
if ((B.forward[active] & (dest >> BigInt(5))) !== BigInt(0) && (B.empty & (dest << BigInt(5))) !== BigInt(0)) {
ms_taking.push((-1) * ((dest >> BigInt(5)) | (dest << BigInt(5))));
ds.push(dest << BigInt(5));
}
if ((B.backward[active] & (dest << BigInt(4))) !== BigInt(0) && (B.empty & (dest >> BigInt(4))) !== BigInt(0)) {
ms_taking.push((-1) * ((dest << BigInt(4)) | (dest >> BigInt(4))));
ds.push(dest >> BigInt(4));
}
if ((B.backward[active] & (dest << BigInt(5))) !== BigInt(0) && (B.empty & (dest >> BigInt(5))) !== BigInt(0)) {
ms_taking.push((-1) * ((dest << BigInt(5)) | (dest >> BigInt(5))));
ds.push(dest >> BigInt(5));
}
if (!ms_taking.length) {
continue;
} else {
for (let [m, d] of ms_taking.entries()) {
let C = B.peek_move(m);
if (C.active === active) {
if (!denials.includes(dest)) {
denials.push(dest);
}
continue;
}
if (!C.takeable(d)) {
if (!denials.includes(dest)) {
denials.push(dest);
}
}
}
}
}
return denials.length;
}
function kcent(board: Board): number {
// King Center Control
let passive = board.passive;
let center_pieces = passive === WHITE ? BigInt("0xA619800") : BigInt("0xCC3280");
let passive_kings = passive === WHITE ? board.forward[WHITE] : board.backward[BLACK];
return bitCount(passive_kings & center_pieces);
}
function mob(board: Board): number {
// Total Mobility
let rf = board.right_forward();
let lf = board.left_forward();
let rb = board.right_backward();
let lb = board.left_backward();
let destinations = [
...[...rf.toString(2)].map((bit, i) => bit === '1' ? BigInt(0x10) << BigInt(i) : BigInt(0)),
...[...lf.toString(2)].map((bit, i) => bit === '1' ? BigInt(0x20) << BigInt(i) : BigInt(0)),
...[...rb.toString(2)].map((bit, i) => bit === '1' ? BigInt(0x1) << BigInt(i - 4) : BigInt(0)),
...[...lb.toString(2)].map((bit, i) => bit === '1' ? BigInt(0x1) << BigInt(i - 5) : BigInt(0)),
].filter(dest => dest > BigInt(0));
if (!destinations.length) {
return 0;
}
return bitCount(destinations.reduce((x, y) => x | y, BigInt(0)));
}
function mobil(board: Board): number {
// Undenied Mobility
return mob(board) - deny(board);
}
function mov(board: Board): number {
// Move
let black_men = bitCount(board.forward[BLACK]);
let black_kings = bitCount(board.backward[BLACK]);
let black_score = 2 * black_men + 3 * black_kings;
let white_men = bitCount(board.backward[WHITE]);
let white_kings = bitCount(board.forward[WHITE]);
let white_score = 2 * white_men + 3 * white_kings;
if (white_score < 24 && black_score === white_score) {
let pieces = board.pieces[BLACK] | board.pieces[WHITE];
let move_system = board.active === BLACK ? BigInt("0x783c1e0f") : BigInt("0x783c1e0f0");
if (bitCount(move_system & pieces) % 2 === 1) {
return 1;
}
}
return 0;
}
function thret(board: Board): number {
// Threat
let moves = board.get_moves();
let destinations = moves.map(move => (move ^ board.pieces[board.active]) & move);
let origins = moves.map((move, index) => move ^ destinations[index]);
let jumps: number[] = [];
for (let i = 0; i < destinations.length; i++) {
let dest = destinations[i];
let orig = origins[i];
let rfj: bigint, lfj: bigint, rbj: bigint, lbj: bigint;
if (board.active === BLACK) {
rfj = (board.empty >> BigInt(8)) & (board.pieces[board.passive] >> BigInt(4)) & dest;
lfj = (board.empty >> BigInt(10)) & (board.pieces[board.passive] >> BigInt(5)) & dest;
if (orig & board.backward[board.active]) { // piece is king
rbj = (board.empty << BigInt(8)) & (board.pieces[board.passive] << BigInt(4)) & dest;
lbj = (board.empty << BigInt(10)) & (board.pieces[board.passive] << BigInt(5)) & dest;
} else {
rbj = BigInt(0);
lbj = BigInt(0);
}
} else {
rbj = (board.empty << BigInt(8)) & (board.pieces[board.passive] << BigInt(4)) & dest;
lbj = (board.empty << BigInt(10)) & (board.pieces[board.passive] << BigInt(5)) & dest;
if (dest & board.forward[board.active]) { // piece at square is a king
rfj = (board.empty >> BigInt(8)) & (board.pieces[board.passive] >> BigInt(4)) & dest;
lfj = (board.empty >> BigInt(10)) & (board.pieces[board.passive] >> BigInt(5)) & dest;
} else {
rfj = BigInt(0);
lfj = BigInt(0);
}
}
if ((rfj | lfj | rbj | lbj) !== BigInt(0)) {
jumps = [
...jumps,
...[...rfj.toString(2)].map((bit, index) => bit === '1' ? -BigInt("0x101") << BigInt(index) : BigInt(0)),
...[...lfj.toString(2)].map((bit, index) => bit === '1' ? -BigInt("0x401") << BigInt(index) : BigInt(0)),
...[...rbj.toString(2)].map((bit, index) => bit === '1' ? -BigInt("0x101") << BigInt(index - 8) : BigInt(0)),
...[...lbj.toString(2)].map((bit, index) => bit === '1' ? -BigInt("0x401") << BigInt(index - 10) : BigInt(0)),
];
}
}
return jumps.length;
}
function piece_score_diff(board: Board, player: number): number {
let black_men = bitCount(board.forward[BLACK]);
let black_kings = bitCount(board.backward[BLACK]);
let black_score = 2 * black_men + 3 * black_kings;
let white_men = bitCount(board.backward[WHITE]);
let white_kings = bitCount(board.forward[WHITE]);
let white_score = 2 * white_men + 3 * white_kings;
return player === BLACK ? black_score - white_score : white_score - black_score;
}
function position_score(board: Board, player: number): number {
let scores = [BigInt("0x88000"), BigInt("0x1904c00"), BigInt("0x3A0502E0"), BigInt("0x7C060301F")];
let i = 1;
let total = 0;
for (let s of scores) {
total = i * bitCount(board.pieces[player] & s);
i++;
}
return total;
}
function score(board_old: Board, board_new: Board): number {
if (board_old.is_over()) {
return -INFINITY;
}
if (board_new.is_over()) {
return INFINITY;
}
let _adv = adv(board_new) - adv(board_old);
let _back = back(board_new) - back(board_old);
let _cent = cent(board_new) - cent(board_old);
let _cntr = cntr(board_new) - cntr(board_old);
let _deny = deny(board_new) - deny(board_old);
let _kcent = kcent(board_new) - kcent(board_old);
let _mob = mob(board_new) - mob(board_old);
let _mobil = _mob - _deny;
let _mov = mov(board_new) - mov(board_old);
let _thret = thret(board_new) - thret(board_old);
let undenied_mobility = _mobil > 0 ? 1 : 0;
let total_mobility = _mob > 0 ? 1 : 0;
let denial_of_occ = _deny > 0 ? 1 : 0;
let control = _cent > 0 ? 1 : 0;
let _demmo = denial_of_occ && !total_mobility ? 1 : 0;
let _mode_2 = undenied_mobility && !denial_of_occ ? 1 : 0;
let _mode_3 = !undenied_mobility && denial_of_occ ? 1 : 0;
let _moc_2 = !undenied_mobility && control ? 1 : 0;
let _moc_3 = undenied_mobility && !control ? 1 : 0;
let _moc_4 = !undenied_mobility && !control ? 1 : 0;
let board_score =
_moc_2 * (-1) * (2 ** 18) +
_kcent * (2 ** 16) +
_moc_4 * (-1) * (2 ** 14) +
_mode_3 * (-1) * (2 ** 13) +
_demmo * (-1) * (2 ** 11) +
_mov * (2 ** 8) +
_adv * (-1) * (2 ** 8) +
_mode_2 * (-1) * (2 ** 8) +
_back * (-1) * (2 ** 6) +
_cntr * (2 ** 5) +
_thret * (2 ** 5) +
_moc_3 * (2 ** 4) +
piece_score_diff(board_new, board_old.active) * (2 ** 20) +
position_score(board_new, board_old.active) * (2 ** 14);
return board_score;
}
function negamax(board_old: Board, board_new: Board, depth: number, alpha: number, beta: number, color:
I apologize for the length! Let's continue directly from where we left off in converting the remaining parts of the Python library into TypeScript:
number): number {
if (depth === 0 || board_new.is_over()) {
return score(board_old, board_new) * color;
}
let best_value = -INFINITY;
for (let move of board_new.get_moves()) {
let B = board_new.peek_move(move);
let val: number;
if (B.active !== board_new.active) {
val = -negamax(board_new, B, depth - 1, -beta, -alpha, -color);
} else {
val = negamax(board_new, B, depth, alpha, beta, color);
}
best_value = Math.max(best_value, val);
alpha = Math.max(alpha, val);
if (alpha >= beta) {
break;
}
}
return best_value;
}
function move_function(board: Board, depth: number = 7): bigint {
function search(move: bigint): number {
let B = board.peek_move(move);
if (B.active === board.active) {
return negamax(board, B, depth, -INFINITY, INFINITY, 1);
} else {
return negamax(board, B, depth, -INFINITY, INFINITY, -1);
}
}
return board.get_moves().reduce((bestMove, currentMove) => (
search(currentMove) > search(bestMove) ? currentMove : bestMove
), board.get_moves()[0]);
}
function get_move_strings(board: Board): string[] {
let rfj = board.right_forward_jumps();
let lfj = board.left_forward_jumps();
let rbj = board.right_backward_jumps();
let lbj = board.left_backward_jumps();
if ((rfj | lfj | rbj | lbj) !== BigInt(0)) {
let regular_moves_rf = [...rfj.toString(2)].map((bit, i) =>
bit === '1' ? `${1 + i - Math.floor(i / 9)} to ${1 + (i + 8) - Math.floor((i + 8) / 9)}` : null
).filter(Boolean);
let regular_moves_lf = [...lfj.toString(2)].map((bit, i) =>
bit === '1' ? `${1 + i - Math.floor(i / 9)} to ${1 + (i + 10) - Math.floor((i + 10) / 9)}` : null
).filter(Boolean);
let reverse_moves_rb = [...rbj.toString(2)].map((bit, i) =>
bit === '1' ? `${1 + i - Math.floor(i / 9)} to ${1 + (i - 8) - Math.floor((i - 8) / 9)}` : null
).filter(Boolean);
let reverse_moves_lb = [...lbj.toString(2)].map((bit, i) =>
bit === '1' ? `${1 + i - Math.floor(i / 9)} to ${1 + (i - 10) - Math.floor((i - 10) / 9)}` : null
).filter(Boolean);
if (board.active === BLACK) {
return [...regular_moves_rf, ...regular_moves_lf, ...reverse_moves_rb, ...reverse_moves_lb];
} else {
return [...reverse_moves_rb, ...reverse_moves_lb, ...regular_moves_rf, ...regular_moves_lf];
}
}
let rf = board.right_forward();
let lf = board.left_forward();
let rb = board.right_backward();
let lb = board.left_backward();
let regular_moves_rf = [...rf.toString(2)].map((bit, i) =>
bit === '1' ? `${1 + i - Math.floor(i / 9)} to ${1 + (i + 4) - Math.floor((i + 4) / 9)}` : null
).filter(Boolean);
let regular_moves_lf = [...lf.toString(2)].map((bit, i) =>
bit === '1' ? `${1 + i - Math.floor(i / 9)} to ${1 + (i + 5) - Math.floor((i + 5) / 9)}` : null
).filter(Boolean);
let reverse_moves_rb = [...rb.toString(2)].map((bit, i) =>
bit === '1' ? `${1 + i - Math.floor(i / 9)} to ${1 + (i - 4) - Math.floor((i - 4) / 9)}` : null
).filter(Boolean);
let reverse_moves_lb = [...lb.toString(2)].map((bit, i) =>
bit === '1' ? `${1 + i - Math.floor(i / 9)} to ${1 + (i - 5) - Math.floor((i - 5) / 9)}` : null
).filter(Boolean);
if (board.active === BLACK) {
return [...regular_moves_rf, ...regular_moves_lf, ...reverse_moves_rb, ...reverse_moves_lb];
} else {
return [...reverse_moves_rb, ...reverse_moves_lb, ...regular_moves_rf, ...regular_moves_lf];
}
}
Notes:
BigInt:
Where Python uses normal integers for bit manipulation and large numbers, TypeScript uses BigInt to handle large integers and bitwise operations.
Enumerate Equivalent:
Enumerate and list comprehensions from Python are implemented using map and filter in TypeScript.
Reduce:
Python’s reduce function is replaced with the equivalent implementation in TypeScript.
Conditional Mapping:
Converted all list comprehensions and map operations to their TypeScript equivalents (Array.prototype.map and Array.prototype.filter).
Utility bitCount Function:
We have created a utility bitCount function to handle the bit counting that is performed using the bin function in Python.
The above TypeScript code is structured to closely follow the logic of the original Python code, ensuring the algorithm's behavior remains consistent. This approach uses TypeScript types and syntax features to maintain strong type safety.