diff --git a/chess/chess.py b/chess/chess.py new file mode 100644 index 0000000..89f915f --- /dev/null +++ b/chess/chess.py @@ -0,0 +1,146 @@ +type_to_name = [] +OUT_OF_BOARD = 0xFFE01806018060180601807FF + +INDEX_TO_FIELD = ["XX", "XX", "XX", "XX", "XX", "XX", "XX", "XX", "XX", "XX", + "XX", "a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1", "XX", + "XX", "a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2", "XX", + "XX", "a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3", "XX", + "XX", "a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4", "XX", + "XX", "a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5", "XX", + "XX", "a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6", "XX", + "XX", "a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7", "XX", + "XX", "a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8", "XX", + "XX", "XX", "XX", "XX", "XX", "XX", "XX", "XX", "XX", "XX"] + +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 "} + +''' Board Index: + a b c d e f g h + (90) (91) (92) (93) (94) (95) (96) (97) (98) (99) + 8 (80) [81] [82] [83] [84] [85] [86] [87] [88] (89) + 7 (70) [71] [72] [73] [74] [75] [76] [77] [78] (79) + 6 (60) [61] [62] [63] [64] [65] [66] [67] [68] (69) + 5 (50) [51] [52] [53] [54] [55] [56] [57] [58] (59) + 4 (40) [41] [42] [43] [44] [45] [46] [47] [48] (49) + 3 (30) [31] [32] [33] [34] [35] [36] [37] [38] (39) + 2 (20) [21] [22] [23] [24] [25] [26] [27] [28] (29) + 1 (10) [11] [12] [13] [14] [15] [16] [17] [18] (19) + (00) (01) (02) (03) (04) (05) (06) (07) (08) (09) +''' + +class Piece(): + def __init__(self, type, is_white, index): + + self.type = 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 = 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() + diff --git a/chess/output.py b/chess/output.py new file mode 100644 index 0000000..379831a --- /dev/null +++ b/chess/output.py @@ -0,0 +1,33 @@ +width = 4 + +TOP_L, TOP_F, TOP_R = "\u2598", "\u2580", "\u259D" +MID_L, MID_F, MID_R = "\u258C", "\u2588", "\u2590" +BOT_L, BOT_F, BOT_R = "\u2596", "\u2584", "\u2597" + +print(TOP_L, TOP_F, TOP_R) +print(MID_L, MID_F, MID_R) +print(BOT_L, BOT_F, BOT_R) + +BL_TR, TL_BR = "\u259E", "\u259A" + + +print((BOT_R + BOT_F * width + BOT_L + " " * (2+width))*4) # Top Row +for row in range(8): + line1 = "" + line2 = "" + line3 = "" + for i in range(2): + for col in range(8): + if i == 0: # inner row + if (row + col) % 2 == 0: + line2 += MID_R + MID_F * width + MID_L + else: + line2 += " " * (width + 2) + elif i == 1: # outer row + if (row + col) % 2 == 0: + line1 +=BOT_R + BOT_F * width + BOT_L + else: + line1 += " " * (width + 2) + # print(line1) + # print(line2) + # print(line3)