This commit is contained in:
VS-Code
2026-02-14 14:13:25 +01:00
parent 46d9d142e8
commit 0ba7a7c8a0

View File

@@ -1,5 +1,5 @@
import os
# region Constants
# White Constants
W_KIN, W_QUE, W_BIS, W_NHT, W_ROO, W_PAW = range(6)
# Black Constans
@@ -22,6 +22,21 @@ 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)
# Convert Piece to number
PCE_TO_NUM = {"K":0, "Q":1, "B":2, "N":3, "R":4, "P":5}
# Convert Field to index
STR_TO_SQ = {}
files = "ABCDEFGH" # Oder "abcdefgh" je nach UCI Standard
ranks = "12345678"
for r_idx, r in enumerate(ranks):
for f_idx, f in enumerate(files):
# Berechne Index (Little Endian: A1=0, B1=1 ... H8=63)
sq = r_idx * 8 + f_idx
key = f + r # z.B. "A1"
STR_TO_SQ[key] = sq
# Move Flags
QUIET = 0
DOUBLE_PAWN_PUSH = 1
@@ -57,6 +72,15 @@ RAYS_S = [0] * 64
RAYS_SW = [0] * 64
RAYS_W = [0] * 64
RAYS_NW = [0] * 64
clear = lambda: os.system('clear')
class COL():
def __init__(self) -> None:
self.W = "\033[97m"
self.B = "\033[30m"
self.BG_W = "\033[47m"
self.BW_B = "\033[40m"
# endregion
class Bitboard():
@@ -68,6 +92,7 @@ class Bitboard():
0, 0, 0, 0, 0, 0]
self.side_to_move = WHITE
self.playing = True
self.bitboards[W_KIN] = 1 << E1
self.bitboards[W_QUE] = 1 << D1
@@ -344,6 +369,7 @@ class Bitboard():
moves.append(to_sq | (bis_sq << 6) | ((bis + turn_offset) << 12) | (no_piece << 16) | quiet << 20)
# endregion
# endregion Bishop Moves
return captures, moves
@@ -373,10 +399,40 @@ class Bitboard():
self.bitboards[captured] ^= (1 << end)
self.all_pieces[turn ^ 1] ^= (1 << end)
self.move_list.append(move)
self.side_to_move ^= 1 # Seite Wechseln
def encode_and_make_move(self, move_str: str):
piece = move_str[0]
start = move_str[1:3]
end = move_str[3:5]
piece_int = (PCE_TO_NUM[piece.upper()] + 6) if self.side_to_move else PCE_TO_NUM[piece.upper()]
start_int = STR_TO_SQ[start.upper()]
end_int = STR_TO_SQ[end.upper()]
move_input = end_int | start_int << 6 | piece_int << 12
captures, moves = self.get_all_moves()
for move in captures:
if (move & 0xFFFF) == (move_input & 0xFFFF):
self.make_move(move)
return True
for move in moves:
if (move & 0xFFFF) == (move_input & 0xFFFF):
self.make_move(move)
return True
return False
def print_board(self):
board = [" ."] * 64
board = ["--"] * 64
if self.move_list:
move = self.move_list[-1]
last_sq = (move >> 6) & 0x3F
board[last_sq] = " "
for pce in range(12):
locations = self.bitboards[pce]
while locations:
@@ -389,7 +445,7 @@ class Bitboard():
for r in range(8):
print(board[r + (c * 8)], end=" ")
print("")
print(" A B C D E F G H /n")
print(" A B C D E F G H \n")
def initialize():
'''
@@ -468,18 +524,22 @@ def initialize():
initialize()
board = Bitboard()
board.print_board()
board.make_move(1835036) #e2-e4
board.print_board()
board.make_move(1837347) #(d7-d5)
board.print_board()
board.make_move(4590371) #(exd5 - Capture!)
board.print_board()
board.make_move(4245795) #(Qxd5 - Capture!)
board.print_board()
board.make_move(804882) #(Nc3)
board.print_board()
board.make_move(829216) #(Qa5)
turn = -1
text = "Please input move: "
while board.playing:
clear()
print(f"\n -------- CHESS -------- Turn: {turn}")
board.print_board()
print(text, end="")
move = input()
if not board.encode_and_make_move(move):
text = "Move not possible new move: "
continue
turn += 1
''' Board Index:
a b c d e f g h