Last active
September 10, 2023 18:38
-
-
Save calebarg/e9da578d2877e443ffa794d022ed941d to your computer and use it in GitHub Desktop.
Chess engine written in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ##! Toy chess engine | |
| ##! Caleb Barger -- 09/10/23 | |
| from enum import Enum | |
| class PieceType(Enum): | |
| pawn = 0 | |
| night = 1 # Yes, this is the wrong knight but avoids having 2 'K's. | |
| bishop = 2 | |
| rook = 3 | |
| queen = 4 | |
| king = 5 | |
| none = 6 # Used for init. | |
| class Piece: | |
| def __init__(self, type: PieceType, pos: (int, int)): | |
| self.type = type | |
| self.pos = pos | |
| class SetColor(Enum): | |
| white = 0 | |
| black = 1 | |
| class PieceSet: | |
| def __init__(self): | |
| self.pieces = [Piece(PieceType.none, (-1, -1)) for _ in range(16)] | |
| self.piece_count = 16 | |
| class PieceHandle: | |
| def __init__(self, set_color: SetColor, piece_index: int): | |
| self.set_color = set_color | |
| self.piece_index = piece_index | |
| def initPieceSets(piece_sets: [PieceSet]) -> None: | |
| for col_index in range(8): | |
| piece_sets[SetColor.white.value].pieces[col_index].type = PieceType.pawn | |
| piece_sets[SetColor.white.value].pieces[col_index].pos = (6, col_index) | |
| piece_sets[SetColor.white.value].pieces[8].type = PieceType.rook | |
| piece_sets[SetColor.white.value].pieces[8].pos = (7, 0) | |
| piece_sets[SetColor.white.value].pieces[9].type = PieceType.night | |
| piece_sets[SetColor.white.value].pieces[9].pos = (7, 1) | |
| piece_sets[SetColor.white.value].pieces[10].type = PieceType.bishop | |
| piece_sets[SetColor.white.value].pieces[10].pos = (7, 2) | |
| piece_sets[SetColor.white.value].pieces[11].type = PieceType.queen | |
| piece_sets[SetColor.white.value].pieces[11].pos = (7, 3) | |
| piece_sets[SetColor.white.value].pieces[12].type = PieceType.king | |
| piece_sets[SetColor.white.value].pieces[12].pos = (7, 4) | |
| piece_sets[SetColor.white.value].pieces[13].type = PieceType.bishop | |
| piece_sets[SetColor.white.value].pieces[13].pos = (7, 5) | |
| piece_sets[SetColor.white.value].pieces[14].type = PieceType.night | |
| piece_sets[SetColor.white.value].pieces[14].pos = (7, 6) | |
| piece_sets[SetColor.white.value].pieces[15].type = PieceType.rook | |
| piece_sets[SetColor.white.value].pieces[15].pos = (7, 7) | |
| for col_index in range(8): | |
| piece_sets[SetColor.black.value].pieces[col_index].type = PieceType.pawn | |
| piece_sets[SetColor.black.value].pieces[col_index].pos = (1, col_index) | |
| piece_sets[SetColor.black.value].pieces[8].type = PieceType.rook | |
| piece_sets[SetColor.black.value].pieces[8].pos = (0, 0) | |
| piece_sets[SetColor.black.value].pieces[9].type = PieceType.night | |
| piece_sets[SetColor.black.value].pieces[9].pos = (0, 1) | |
| piece_sets[SetColor.black.value].pieces[10].type = PieceType.bishop | |
| piece_sets[SetColor.black.value].pieces[10].pos = (0, 2) | |
| piece_sets[SetColor.black.value].pieces[11].type = PieceType.king | |
| piece_sets[SetColor.black.value].pieces[11].pos = (0, 3) | |
| piece_sets[SetColor.black.value].pieces[12].type = PieceType.queen | |
| piece_sets[SetColor.black.value].pieces[12].pos = (0, 4) | |
| piece_sets[SetColor.black.value].pieces[13].type = PieceType.bishop | |
| piece_sets[SetColor.black.value].pieces[13].pos = (0, 5) | |
| piece_sets[SetColor.black.value].pieces[14].type = PieceType.night | |
| piece_sets[SetColor.black.value].pieces[14].pos = (0, 6) | |
| piece_sets[SetColor.black.value].pieces[15].type = PieceType.rook | |
| piece_sets[SetColor.black.value].pieces[15].pos = (0, 7) | |
| def initBoard(board: [PieceHandle], piece_sets: [PieceSet]) -> None: | |
| for set_color in SetColor: | |
| for p_index, p in enumerate(piece_sets[set_color.value].pieces): | |
| board[p.pos[0] * 8 + p.pos[1]].set_color = set_color | |
| board[p.pos[0] * 8 + p.pos[1]].piece_index = p_index | |
| def pieceFromHandle(piece_handle: PieceHandle, piece_sets: [PieceSet]) -> None | Piece: | |
| result: Piece | None = None | |
| if (piece_handle.piece_index != -1): | |
| result = piece_sets[piece_handle.set_color.value].pieces[piece_handle.piece_index] | |
| return result | |
| def boardToMemCoords(col_chr: chr, row: int) -> (int, int): | |
| col_index = ord(col_chr.lower()) - ord('a') | |
| if (col_index < 0 or col_index > 7 or row < 1 or row > 8): | |
| print('Bad pos ', (col_chr, row)) | |
| return (-1, -1) | |
| return (8 - row, col_index) | |
| def movePiece(board: [PieceHandle], piece_sets: [PieceSet], from_pos: (int, int), to_pos: (int, int)) -> None: | |
| piece_handle = board[from_pos[0] * 8 + from_pos[1]] | |
| piece = pieceFromHandle(piece_handle, piece_sets) | |
| if (piece == None): | |
| print('No piece to move at ', from_pos) | |
| return | |
| # TODO(caleb): logistics of moving a piece and all that nonsense | |
| # Update piece pos | |
| piece.pos = (to_pos[0], to_pos[1]) | |
| # Update piece handle(s) at start and end | |
| board[to_pos[0] * 8 + to_pos[1]].set_color = piece_handle.set_color | |
| board[to_pos[0] * 8 + to_pos[1]].piece_index = piece_handle.piece_index | |
| piece_handle.piece_index = -1 | |
| def drawBoard(board: [PieceHandle], piece_sets: [PieceSet]) -> None: | |
| print(' +', end='') | |
| for col_index in range(8 * 3): | |
| print('-', end='') | |
| print('+') | |
| for row_index in range(8): | |
| print(f'{8 - row_index} |', end='') | |
| for col_index in range(8): | |
| opt_piece = pieceFromHandle(board[row_index * 8 + col_index], piece_sets) | |
| print(' ', end='') | |
| if (opt_piece == None): | |
| print('.', end='') | |
| else: | |
| if (board[row_index * 8 + col_index].set_color == SetColor.white): | |
| print(opt_piece.type.name[0].upper(), end='') | |
| else: | |
| print(opt_piece.type.name[0], end='') | |
| print(' ', end='') | |
| print('|', end='') | |
| print() | |
| print(' +', end='') | |
| for col_index in range(8 * 3): | |
| print('-', end='') | |
| print('+') | |
| print(' ', end='') | |
| for col_index in range(8): | |
| print(' ' + chr(ord('a') + col_index) + ' ', end='') | |
| print() | |
| piece_sets = [PieceSet(), PieceSet()] | |
| initPieceSets(piece_sets) | |
| board = [PieceHandle(SetColor.white, -1) for _ in range(64)] | |
| initBoard(board, piece_sets) | |
| drawBoard(board, piece_sets) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment