-
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
|
import os
|
||||||
# region Constants
|
# region Constants
|
||||||
|
|
||||||
# White Constants
|
# White Constants
|
||||||
W_KIN, W_QUE, W_BIS, W_NHT, W_ROO, W_PAW = range(6)
|
W_KIN, W_QUE, W_BIS, W_NHT, W_ROO, W_PAW = range(6)
|
||||||
# Black Constans
|
# 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)
|
A2, B2, C2, D2, E2, F2, G2, H2 = range(8, 16)
|
||||||
A1, B1, C1, D1, E1, F1, G1, H1 = range(8)
|
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
|
# Move Flags
|
||||||
QUIET = 0
|
QUIET = 0
|
||||||
DOUBLE_PAWN_PUSH = 1
|
DOUBLE_PAWN_PUSH = 1
|
||||||
@@ -57,6 +72,15 @@ RAYS_S = [0] * 64
|
|||||||
RAYS_SW = [0] * 64
|
RAYS_SW = [0] * 64
|
||||||
RAYS_W = [0] * 64
|
RAYS_W = [0] * 64
|
||||||
RAYS_NW = [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
|
# endregion
|
||||||
|
|
||||||
class Bitboard():
|
class Bitboard():
|
||||||
@@ -68,6 +92,7 @@ class Bitboard():
|
|||||||
0, 0, 0, 0, 0, 0]
|
0, 0, 0, 0, 0, 0]
|
||||||
|
|
||||||
self.side_to_move = WHITE
|
self.side_to_move = WHITE
|
||||||
|
self.playing = True
|
||||||
|
|
||||||
self.bitboards[W_KIN] = 1 << E1
|
self.bitboards[W_KIN] = 1 << E1
|
||||||
self.bitboards[W_QUE] = 1 << D1
|
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)
|
moves.append(to_sq | (bis_sq << 6) | ((bis + turn_offset) << 12) | (no_piece << 16) | quiet << 20)
|
||||||
# endregion
|
# endregion
|
||||||
# endregion Bishop Moves
|
# endregion Bishop Moves
|
||||||
|
return captures, moves
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -373,10 +399,40 @@ class Bitboard():
|
|||||||
self.bitboards[captured] ^= (1 << end)
|
self.bitboards[captured] ^= (1 << end)
|
||||||
self.all_pieces[turn ^ 1] ^= (1 << end)
|
self.all_pieces[turn ^ 1] ^= (1 << end)
|
||||||
|
|
||||||
|
self.move_list.append(move)
|
||||||
|
|
||||||
self.side_to_move ^= 1 # Seite Wechseln
|
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):
|
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):
|
for pce in range(12):
|
||||||
locations = self.bitboards[pce]
|
locations = self.bitboards[pce]
|
||||||
while locations:
|
while locations:
|
||||||
@@ -389,7 +445,7 @@ class Bitboard():
|
|||||||
for r in range(8):
|
for r in range(8):
|
||||||
print(board[r + (c * 8)], end=" ")
|
print(board[r + (c * 8)], end=" ")
|
||||||
print("")
|
print("")
|
||||||
print(" A B C D E F G H /n")
|
print(" A B C D E F G H \n")
|
||||||
|
|
||||||
def initialize():
|
def initialize():
|
||||||
'''
|
'''
|
||||||
@@ -468,18 +524,22 @@ def initialize():
|
|||||||
|
|
||||||
initialize()
|
initialize()
|
||||||
board = Bitboard()
|
board = Bitboard()
|
||||||
|
turn = -1
|
||||||
|
text = "Please input move: "
|
||||||
|
while board.playing:
|
||||||
|
clear()
|
||||||
|
print(f"\n -------- CHESS -------- Turn: {turn}")
|
||||||
board.print_board()
|
board.print_board()
|
||||||
board.make_move(1835036) #e2-e4
|
print(text, end="")
|
||||||
board.print_board()
|
move = input()
|
||||||
board.make_move(1837347) #(d7-d5)
|
if not board.encode_and_make_move(move):
|
||||||
board.print_board()
|
text = "Move not possible new move: "
|
||||||
board.make_move(4590371) #(exd5 - Capture!)
|
continue
|
||||||
board.print_board()
|
|
||||||
board.make_move(4245795) #(Qxd5 - Capture!)
|
turn += 1
|
||||||
board.print_board()
|
|
||||||
board.make_move(804882) #(Nc3)
|
|
||||||
board.print_board()
|
|
||||||
board.make_move(829216) #(Qa5)
|
|
||||||
|
|
||||||
''' Board Index:
|
''' Board Index:
|
||||||
a b c d e f g h
|
a b c d e f g h
|
||||||
|
|||||||
Reference in New Issue
Block a user