This commit is contained in:
VS-Code
2026-02-06 10:17:19 +01:00
parent 229eb4db1d
commit 7931a95840
2 changed files with 40 additions and 32 deletions

151
chess/chess_64.py Normal file
View File

@@ -0,0 +1,151 @@
type_to_name = []
PIECE_TO_UNICODE = {"wk": "\u2654 ", "wq": "\u2655 ", "wr": "\u2656 ", "wb": "\u2657 ", "wn": "\u2658 ", "wp": "\u2659 ",
"bk": "\u265A ", "bq": "\u265B ", "br": "\u265C ", "bb": "\u265D ", "bn": "\u265E ", "bp": "\u265F "}
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",
]
''' 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]
'''
class Piece():
def __init__(self, pce_type, is_white, index):
self.type = pce_type
self.is_white = is_white
self.has_castled = False if type == 'K' else True
self.index = index
self.bit_mask = 1 << index
class Move():
def __init__(self, start_index, end_index, piece_moved, piece_captured=None):
self.piece_moved = piece_moved
self.piece_captured = piece_captured
self.index_start = start_index
self.index_end = end_index
self.bitmask_start: int = 1 << start_index
self.bitmask_end = 1 << end_index
self.chess_notation = f"{self.piece_moved[1].upper()}{INDEX_TO_FIELD[self.index_start]}{INDEX_TO_FIELD[self.index_end]}"
class Board():
def __init__(self):
self.bit_mask_w = 0x0000000000000001FE7F800
self.bit_mask_b = 0x1FE7F800000000000000000
self.bit_mask_all=0x1FE7F80000000001FE7F800
self.white_to_move = True
# self.wr1 = Piece('R', True, 11)
# self.wn1 = Piece('N', True, 12)
# self.wb1 = Piece('B', True, 13)
# self.wk = Piece('K', True, 14)
# self.wq = Piece('Q', True, 15)
# self.wb2 = Piece('B', True, 16)
# self.wn2 = Piece('N', True, 17)
# self.wr2 = Piece('R', True, 18)
# self.wp = [Piece('P', True, 21),
# Piece('P', True, 22),
# Piece('P', True, 23),
# Piece('P', True, 24),
# Piece('P', True, 25),
# Piece('P', True, 26),
# Piece('P', True, 27),
# Piece('P', True, 28)]
self.piece_dict = {
'br1':81, 'bn1':82, 'bb1':83, 'bk' :84, 'bq' :85, 'bb2':86, 'bn2':87, 'br2':88,
'bp1':71, 'bp2':72, 'bp3':73, 'bp4':74, 'bp5':75, 'bp6':76, 'bp7':77, 'bp8':78,
'wp1':21, 'wp2':22, 'wp3':23, 'wp4':24, 'wp5':25, 'wp6':26, 'wp7':27, 'wp8':28,
'wr1':11, 'wn1':12, 'wb1':13, 'wk' :14, 'wq' :15, 'wb2':16, 'wn2':17, 'wr2':18,
}
self.move_log = []
def print_board(self):
for x in range(100):
if OUT_OF_BOARD & 1 << x != 0:
if (x+1) % 10 == 0:
print("")
continue
continue
if self.bit_mask_all & 1 << x != 0:
for p in self.piece_dict:
if self.piece_dict[p] == x:
print(f"{PIECE_TO_UNICODE[p[:2]]} ", end="")
else:
print("\u25FB ", end="")
def notation_to_move(self, notation_str): # Kb1a5
if len(notation_str) < 6:
start = INDEX_TO_FIELD.index(notation_str[1]+ notation_str[2])
end = INDEX_TO_FIELD.index(notation_str[3]+ notation_str[4])
for p in self.piece_dict:
if self.piece_dict[p] == start:
piece_moved = p
if self.piece_dict[p] == end:
piece_captured = p
if piece_moved and piece_captured:
return Move(start, end, piece_moved, piece_captured)
elif piece_moved:
return Move(start, end, piece_moved)
else:
print("No Piece to Move")
return
else:
print("Move to long")
return
def make_move(self, move):
self.piece_dict[move.piece_moved] = move.index_start
if self.white_to_move:
self.bit_mask_w = self.bit_mask_w & ~move.bitmask_start
self.bit_mask_w = self.bit_mask_w | move.bitmask_end
if move.piece_captured:
self.piece_dict[move.piece_captured] = -1
self.bit_mask_b = self.bit_mask_b & ~move.bitmask_end
else:
self.bit_mask_b = self.bit_mask_b & ~move.bitmask_start
self.bit_mask_b = self.bit_mask_b | move.bitmask_end
if move.piece_captured:
self.piece_dict[move.piece_captured] = -1
self.bit_mask_b = self.bit_mask_b & ~move.bitmask_end
self.bit_mask_all = self.bit_mask_w | self.bit_mask_b
self.move_log.append(move)
board = Board()
board.print_board()
while True:
print("Your Move, Please: ")
move_str = input()
if len(move_str) != 4:
print("Wrong Syntax")
continue
move = board.notation_to_move(move_str)
board.make_move(move)