Files
landingpage/chess/chess_64.py
2026-02-09 16:21:00 +01:00

150 lines
5.0 KiB
Python

# White Constants
W_KIN, W_QUE, W_BIS, W_NHT, W_ROO, W_PAW = range(6)
# Black Constans
B_KIN, B_QUE, B_BIS, B_NHT, B_ROO, B_PAW = range(6,12)
# Turn Constants
WHITE, BLACK = 0, 1
# Piece Constants
KIN, QUE, BIS, NHT, ROO, PAW = range(6)
# Square Constants
A8, B8, C8, D8, E8, F8, G8, H8 = range(56, 64)
A7, B7, C7, D7, E7, F7, G7, H7 = range(48, 56)
A6, B6, C6, D6, E6, F6, G6, H6 = range(40, 48)
A5, B5, C5, D5, E5, F5, G5, H5 = range(32, 40)
A4, B4, C4, D4, E4, F4, G4, H4 = range(24, 32)
A3, B3, C3, D3, E3, F3, G3, H3 = range(16, 24)
A2, B2, C2, D2, E2, F2, G2, H2 = range(8, 16)
A1, B1, C1, D1, E1, F1, G1, H1 = range(8)
NOT_A = 0xfefefefefefefefe
NOT_H = 0x7f7f7f7f7f7f7f7f
NOT_AB = 0xfcfcfcfcfcfcfcfc
NOT_GH = 0x3f3f3f3f3f3f3f3f
FULL_MASK = 0xffffffffffffffff
INDEX_TO_FIELD = [
"a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1",
"a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2",
"a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3",
"a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4",
"a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5",
"a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6",
"a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7",
"a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8",
]
class Bitboard():
def __init__(self) -> None:
# WHITE 0:King, 1:Queen, 2:Bishop, 3:Knight, 4:Rook, 5:Pawn
# BLACK 6:King, 7:Queen, 8:Bishop, 9:Knight, 10:Rook, 11:Pawn
self.bitboards = [0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0]
self.side_to_move = WHITE
self.bitboards[W_KIN] = 1 << E1
self.bitboards[W_QUE] = 1 << D1
self.bitboards[W_BIS] = (1 << C1) | (1 << F1)
self.bitboards[W_NHT] = (1 << B1) | (1 << G1)
self.bitboards[W_ROO] = (1 << A1) | (1 << H1)
self.bitboards[W_PAW] = 0xFF << 8
self.bitboards[B_KIN] = 1 << E8
self.bitboards[B_QUE] = 1 << D8
self.bitboards[B_BIS] = (1 << C8) | (1 << F8)
self.bitboards[B_NHT] = (1 << B8) | (1 << G8)
self.bitboards[B_ROO] = (1 << A8) | (1 << H8)
self.bitboards[B_PAW] = 0xFF << 48
self.all_white = self.bitboards[W_KIN] | self.bitboards[W_QUE] | self.bitboards[W_BIS] | self.bitboards[W_NHT] | self.bitboards[W_ROO] | self.bitboards[W_PAW]
self.all_black = self.bitboards[W_KIN] | self.bitboards[W_QUE] | self.bitboards[W_BIS] | self.bitboards[W_NHT] | self.bitboards[W_ROO] | self.bitboards[W_PAW]
self.all_piece = self.all_white | self.all_black
self.move_list = []
def get_all_moves(self):
turn_offset = self.side_to_move * 6
move_list = []
# King Moves
king = self.bitboards[KIN]
king_moves = 0
king_moves = (king & NOT_A) >> 1 | (king & NOT_A) << 7 | king << 8 | (king & NOT_H) << 9 | (king & NOT_H) << 1 | (king & NOT_H) >> 7 | king >> 8 | (king & NOT_A) >> 9
king_moves &= FULL_MASK
pass
def make_move(self, move):
# Setup
# Bit 0-5: Target (6 Bit, Index: 0-63)
# Bit 6-11: Source (6 Bit, Index: 0-63)
# Bit 12-15: Flags (4 Bit)
# 0000: 0 Quiet 0100: 4 Capture 1000: 8 Promo NHT
# 0001: 1 Double Pawn Push 0101: 5 En Passant 1001: 9 Promo BIS
# 0010: 2 King Castle 0110: 1010: 10 Promo ROO
# 0011: 3 Queen Castle 0111: 1011: 11 Promo QUE
pass
KIN_ATTACKS = [0] * 64
NHT_ATTACKS = [0] * 64
PAW_ATTACKS = [0] * 128 # 0-63 für Weiße Bauern, 64-127 für Schwarze Bauern
RAY_N = [0] * 64
RAY_NE = [0] * 64
RAY_E = [0] * 64
RAY_SE = [0] * 64
RAY_S = [0] * 64
RAY_SW = [0] * 64
RAY_W = [0] * 64
RAY_NW = [0] * 64
def initialize():
for sq in range(64):
# King
kin = 1 << sq
kin_attacks = (kin & NOT_A) << 7 | kin << 8 | (kin & NOT_H) << 9 | (kin & NOT_H) << 1 | \
(kin & NOT_H) >> 7 | kin >> 8 | (kin & NOT_A) >> 9 | (kin & NOT_A) >> 1
KIN_ATTACKS[sq] = kin_attacks & FULL_MASK
# Knight
nht = 1 << sq
nht_attacks = (nht & NOT_AB) << 6 | (nht & NOT_A) << 15 | (nht & NOT_H) << 17 | (nht & NOT_GH) << 10 | \
(nht & NOT_GH) >> 6 | (nht & NOT_H) >> 15 | (nht & NOT_A) >> 17 | (nht & NOT_AB) >> 10
NHT_ATTACKS[sq] = nht_attacks & FULL_MASK
# Pawns
paw = 1 << sq
w_attacks = (paw & NOT_A) << 7 | (paw & NOT_H) << 9
PAW_ATTACKS[sq] = w_attacks & FULL_MASK
b_attacks = (paw & NOT_A) >> 9 | (paw & NOT_H) >> 7
PAW_ATTACKS[sq + 64] = b_attacks & FULL_MASK
# Rays
# North
''' Board Index:
a b c d e f g h
8 [56] [57] [58] [59] [60] [61] [62] [63]
7 [48] [49] [50] [51] [52] [53] [54] [55]
6 [40] [41] [42] [43] [44] [45] [46] [47]
5 [32] [33] [34] [35] [36] [37] [38] [39]
4 [24] [25] [26] [27] [28] [29] [30] [31]
3 [16] [17] [18] [19] [20] [21] [22] [23]
2 [08] [09] [10] [11] [12] [13] [14] [15]
1 [00] [01] [02] [03] [03] [05] [06] [07]
'''