-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pawn, knight and king attack tables (#5)
* Add pawn, knight and king attack tables * Bump version to 0.9
- Loading branch information
1 parent
e3485dd
commit c7c9d1f
Showing
12 changed files
with
687 additions
and
57 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
def index_to_rank_and_file(index): | ||
rank = index // 8 | ||
file = index % 8 | ||
return rank, file | ||
|
||
def rank_and_file_to_index(rank, file): | ||
return rank * 8 + file | ||
|
||
def bitboard_from_bit(index): | ||
return 1 << index | ||
|
||
def is_valid_position(rank, file): | ||
return rank >= 0 and rank < 8 and file >= 0 and file < 8 | ||
|
||
def format_bitboard(bitboard): | ||
print(" abcdefgh") | ||
for rank_number, rank_byte in reversed(list(enumerate(bitboard.to_bytes(8, 'little')))): | ||
print(f"{rank_number + 1} {rank_byte:08b}") | ||
|
||
print("pub const KING_ATTACK_TABLE: [u64; 64] = [") | ||
|
||
for source_index in range(64): | ||
source_rank, source_file = index_to_rank_and_file(source_index) | ||
|
||
moves = [ | ||
(-1, -1), | ||
(-1, 0), | ||
(-1, 1), | ||
(0, -1), | ||
(0, 1), | ||
(1, -1), | ||
(1, 0), | ||
(1, 1), | ||
] | ||
|
||
bitboard = 0 | ||
|
||
for move_ranks, move_files in moves: | ||
target_rank = source_rank + move_ranks | ||
target_file = source_file + move_files | ||
|
||
if not is_valid_position(target_rank, target_file): | ||
continue | ||
|
||
target_index = rank_and_file_to_index(target_rank, target_file) | ||
bitboard |= bitboard_from_bit(target_index) | ||
|
||
if bitboard == 0: | ||
print(" 0,") | ||
else: | ||
print(f" 0x{bitboard:016x},") | ||
# print(f"{format_bitboard(bitboard | bitboard_from_bit(source_index))}") | ||
|
||
|
||
print("];") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
def index_to_rank_and_file(index): | ||
rank = index // 8 | ||
file = index % 8 | ||
return rank, file | ||
|
||
def rank_and_file_to_index(rank, file): | ||
return rank * 8 + file | ||
|
||
def bitboard_from_bit(index): | ||
return 1 << index | ||
|
||
def is_valid_position(rank, file): | ||
return rank >= 0 and rank < 8 and file >= 0 and file < 8 | ||
|
||
def format_bitboard(bitboard): | ||
print(" abcdefgh") | ||
for rank_number, rank_byte in reversed(list(enumerate(bitboard.to_bytes(8, 'little')))): | ||
print(f"{rank_number + 1} {rank_byte:08b}") | ||
|
||
print("pub const KNIGHT_ATTACK_TABLE: [u64; 64] = [") | ||
|
||
for source_index in range(64): | ||
source_rank, source_file = index_to_rank_and_file(source_index) | ||
|
||
moves = [ | ||
(-1, -2), | ||
(-1, 2), | ||
(-2, -1), | ||
(-2, 1), | ||
(2, -1), | ||
(2, 1), | ||
(1, -2), | ||
(1, 2), | ||
] | ||
|
||
bitboard = 0 | ||
|
||
for move_ranks, move_files in moves: | ||
target_rank = source_rank + move_ranks | ||
target_file = source_file + move_files | ||
|
||
if not is_valid_position(target_rank, target_file): | ||
continue | ||
|
||
target_index = rank_and_file_to_index(target_rank, target_file) | ||
bitboard |= bitboard_from_bit(target_index) | ||
|
||
if bitboard == 0: | ||
print(" 0,") | ||
else: | ||
print(f" 0x{bitboard:016x},") | ||
# print(f"{format_bitboard(bitboard | bitboard_from_bit(source_index))}") | ||
|
||
|
||
print("];") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
def index_to_rank_and_file(index): | ||
rank = index // 8 | ||
file = index % 8 | ||
return rank, file | ||
|
||
def rank_and_file_to_index(rank, file): | ||
return rank * 8 + file | ||
|
||
def bitboard_from_bit(index): | ||
return 1 << index | ||
|
||
# A pawn can never be below its starting rank (1 for white or 8 for black) | ||
# Also when it arrives to the promotion rank, it promotes and cannot be a pawn anymore | ||
# So no possible moves for ranks 1 and 8 | ||
|
||
print("pub const WHITE_PAWN_ATTACK_TABLE: [u64; 64] = [") | ||
|
||
for source_index in range(64): | ||
source_rank, source_file = index_to_rank_and_file(source_index) | ||
|
||
# Possible captures, not including en passant | ||
|
||
if source_rank == 0 or source_rank == 7: | ||
print(" 0,") | ||
continue | ||
|
||
bitboard = 0 | ||
if source_file > 0: | ||
target_index = rank_and_file_to_index(source_rank + 1, source_file - 1) | ||
bitboard |= bitboard_from_bit(target_index) | ||
|
||
if source_file < 7: | ||
target_index = rank_and_file_to_index(source_rank + 1, source_file + 1) | ||
bitboard |= bitboard_from_bit(target_index) | ||
|
||
print(f" 0x{bitboard:016x},") | ||
|
||
print("];") | ||
|
||
print() | ||
|
||
print("pub const BLACK_PAWN_ATTACK_TABLE: [u64; 64] = [") | ||
|
||
for source_index in range(64): | ||
source_rank, source_file = index_to_rank_and_file(source_index) | ||
|
||
# Possible captures, not including en passant | ||
|
||
if source_rank == 0 or source_rank == 7: | ||
print(" 0,") | ||
continue | ||
|
||
bitboard = 0 | ||
if source_file > 0: | ||
target_index = rank_and_file_to_index(source_rank - 1, source_file - 1) | ||
bitboard |= bitboard_from_bit(target_index) | ||
|
||
if source_file < 7: | ||
target_index = rank_and_file_to_index(source_rank - 1, source_file + 1) | ||
bitboard |= bitboard_from_bit(target_index) | ||
|
||
print(f" 0x{bitboard:016x},") | ||
|
||
print("];") | ||
|
||
print("pub const WHITE_ATTACKED_BY_PAWN_TABLE: [u64; 64] = [") | ||
|
||
for source_index in range(64): | ||
source_rank, source_file = index_to_rank_and_file(source_index) | ||
|
||
# Possible captures, not including en passant | ||
|
||
if source_rank == 7: | ||
print(" 0,") | ||
continue | ||
|
||
bitboard = 0 | ||
if source_file > 0: | ||
target_index = rank_and_file_to_index(source_rank + 1, source_file - 1) | ||
bitboard |= bitboard_from_bit(target_index) | ||
|
||
if source_file < 7: | ||
target_index = rank_and_file_to_index(source_rank + 1, source_file + 1) | ||
bitboard |= bitboard_from_bit(target_index) | ||
|
||
print(f" 0x{bitboard:016x},") | ||
|
||
print("];") | ||
|
||
print("pub const BLACK_ATTACKED_BY_PAWN_TABLE: [u64; 64] = [") | ||
|
||
for source_index in range(64): | ||
source_rank, source_file = index_to_rank_and_file(source_index) | ||
|
||
# Possible captures, not including en passant | ||
|
||
if source_rank == 0: | ||
print(" 0,") | ||
continue | ||
|
||
bitboard = 0 | ||
if source_file > 0: | ||
target_index = rank_and_file_to_index(source_rank - 1, source_file - 1) | ||
bitboard |= bitboard_from_bit(target_index) | ||
|
||
if source_file < 7: | ||
target_index = rank_and_file_to_index(source_rank - 1, source_file + 1) | ||
bitboard |= bitboard_from_bit(target_index) | ||
|
||
print(f" 0x{bitboard:016x},") | ||
|
||
print("];") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.