import colorama # import copy colorama.init() # So colors work in CMD board = { # Chess board dictionary. Keys - coordinates, Values - chess piece. 'A': {'A1': 'C_l_b', 'A2': 'H_l_b', 'A3': 'B_l_b', 'A4': 'Q___b', 'A5': 'K___b', 'A6': 'B_r_b', 'A7': 'H_r_b', 'A8': 'C_r_b'}, 'B': {'B1': 'P_1_b', 'B2': 'P_2_b', 'B3': 'P_3_b', 'B4': 'P_4_b', 'B5': 'P_5_b', 'B6': 'P_6_b', 'B7': 'P_7_b', 'B8': 'P_8_b'}, 'C': {'C1': ' ', 'C2': ' ', 'C3': ' ', 'C4': ' ', 'C5': ' ', 'C6': ' ', 'C7': ' ', 'C8': ' '}, 'D': {'D1': ' ', 'D2': ' ', 'D3': ' ', 'D4': ' ', 'D5': ' ', 'D6': ' ', 'D7': ' ', 'D8': ' '}, 'E': {'E1': ' ', 'E2': ' ', 'E3': ' ', 'E4': ' ', 'E5': ' ', 'E6': ' ', 'E7': ' ', 'E8': ' '}, 'F': {'F1': ' ', 'F2': ' ', 'F3': ' ', 'F4': ' ', 'F5': ' ', 'F6': ' ', 'F7': ' ', 'F8': ' '}, 'G': {'G1': 'P_1_w', 'G2': 'P_2_w', 'G3': 'P_3_w', 'G4': 'P_4_w', 'G5': 'P_5_w', 'G6': 'P_6_w', 'G7': 'P_7_w', 'G8': 'P_8_w'}, 'H': {'H1': 'C_l_w', 'H2': 'H_l_w', 'H3': 'B_l_w', 'H4': 'Q___w', 'H5': 'K___w', 'H6': 'B_r_w', 'H7': 'H_r_w', 'H8': 'C_r_w'} } def check_valid_move(current_space, where_to): # Checks validity of move input by user in play() if current_space == ' ': # A player can't move an empty space return False if current_space == where_to: # Cannot move to the same place return False elif board[current_space[0]][current_space][0] == 'C': # Checks if its a castle piece if current_space[0] == where_to[0]: # Horizontal movement for i in range(min(int(current_space[1]) + 1, int(where_to[1])), max(int(current_space[1]), int(where_to[1]))): if board[current_space[0]][current_space[0] + str(i)] == ' ': # Checks that it is all empty spaces in between continue else: return False return True elif int(current_space[1]) == int(where_to[1]): # Vertical movement for i in range(min(ord(current_space[0]), ord(where_to[0])) - 63, min(ord(current_space[0]), ord(where_to[0])) - 63): if board[chr(i + 64)][chr(i + 64) + current_space[1]] == ' ': # Checks that it is all empty spaces in between continue else: return False return True else: return False elif board[current_space[0]][current_space][0] == 'H': # Checks for Horse piece if ((abs(ord(current_space[0]) - ord(where_to[0])) == 1) and (abs(int(current_space[1]) - current_space[2]) == 2)) or ( (abs(ord(current_space[0]) - ord(where_to[0])) == 2) and (abs(int(current_space[1]) - int(where_to[1])) == 1)): return True else: return False elif board[current_space[0]][current_space][0] == 'B': # Checks for bishop piece if abs(ord(current_space[0]) - ord(where_to[0])) == abs(int(current_space[1]) - int(where_to[1])): direction = (1 if ord(current_space[0]) < ord(where_to[0]) else -1, 1 if int(current_space[1]) < int(where_to[1]) else -1) for i in range(1, abs(int(current_space[1]) - int(where_to[1]))): check_space = str(chr((ord(current_space[0]) - 64) + (i * direction[0]) + 64)) \ + str(int(current_space[1]) + (i * direction[1])) if board[check_space[0]][check_space] == ' ': # Checks for empty spaces in between continue else: return False return True else: return False elif board[current_space[0]][current_space][0] == 'Q': # Checks for queen piece if current_space[0] == where_to[0]: # From here to king copied from castle and bishop for i in range(min(int(current_space[1]) + 1, int(where_to[1])), max(int(current_space[1]), int(where_to[1]))): if board[current_space[0]][current_space[0] + str(i)] == ' ': continue else: return False return True elif int(current_space[1]) == int(where_to[1]): for i in range(min(ord(current_space[0]), ord(where_to[0])) - 63, min(ord(current_space[0]), ord(where_to[0])) - 63): if board[chr(i + 64)][chr(i + 64) + current_space[1]] == ' ': continue else: return False return True elif abs((ord(current_space[0]) - 64) - (ord(where_to[0]) - 64)) == abs(int(current_space[1]) - int(where_to[1])): direction = (1 if ord(current_space[0]) < ord(where_to[0]) else -1, 1 if int(current_space[1]) < int(where_to[1]) else -1) for i in range(1, abs(int(current_space[1]) - int(where_to[1]))): check_space = str(chr((ord(current_space[0]) - 64) + (i * direction[0]) + 64)) \ + str(int(current_space[1]) + (i * direction[1])) if board[check_space[0]][check_space] == ' ': continue else: return False return True else: return False elif board[current_space[0]][current_space][0] == 'K': # Checks for king piece if ((abs(ord(current_space[0]) - ord(where_to[0])) == 1) and (current_space[1] == where_to[1])) or \ ((abs(int(current_space[1]) - int(where_to[1])) == 1) and (current_space[0] == where_to[0])) or \ ((abs(ord(current_space[0]) - ord(where_to[0])) == 1) and (abs(int(current_space[1]) - int(where_to[1])) == 1)): return True else: return False elif (board[current_space[0]][current_space][0] == 'P') and \ (board[current_space[0]][current_space][-1] == 'b'): # Checks for black pawn if (ord(current_space[0]) - ord(where_to[0]) == -1) and (current_space[1] == where_to[1]) and \ (board[where_to[0]][where_to] == ' '): # Regular move return True elif (current_space[0] == 'B') and (ord(current_space[0]) - ord(where_to[0]) == -2) and \ (current_space[1] == where_to[1]) and (board[where_to[0]][where_to] == ' '): # Two space jump from start return True elif (ord(current_space[0]) - ord(where_to[0]) == -1) and ( abs(int(current_space[1]) - int(where_to[1])) == 1) and (board[where_to[0]][where_to] != ' '): # Diagonal eating return True else: return False # Checks for white pawn elif (board[current_space[0]][current_space][0] == 'P') and (board[current_space[0]][current_space][-1] == 'w'): if (current_space[0] == 'G') and (ord(current_space[0]) - ord(where_to[0]) == 2) and \ (current_space[1] == where_to[1]) and (board[where_to[0]][where_to] == ' '): # Two space jump from start return True elif (ord(current_space[0]) - ord(where_to[0]) == 1) and (abs(int(current_space[1]) - int(where_to[1])) == 1): # Regular return True elif (ord(current_space[0]) - ord(where_to[0]) == 1) and (current_space[1] == where_to[1]) and \ (board[where_to[0]][where_to] == ' '): # Diagonal eating return True else: return False def print_board(the_board): # Print visual board print('\033[93;46m ||' + '-' * 54 + '||\033[0m') # Topping x = 2 # For checkering for i in range(8): # Loops through board print('\033[93;46m' + chr(i + 65) + '\033[0m', end='') # Prints letter for ii in range(8): # Loops through dictionaries in board if the_board[chr(i + 65)][chr(i + 65) + str(ii + 1)] == ' ': color = '\033[0;0;' # Color or empty space elif the_board[chr(i + 65)][chr(i + 65) + str(ii + 1)].strip()[-1] == 'w': color = '\033[37;1;' # Color for white piece else: color = '\033[30;' # Color for black piece print('\033[93;46m||\033[0m' + color + ('41m ' if x % 2 == 0 else '44m ') + the_board[chr(i + 65)][chr(i + 65) + str(ii + 1)][0] + ' \033[0m', end='') # Prints the actual space x += 1 x += 1 print('\033[93;46m||\033[0m') print('\033[93;46m ||' + '-' * 54 + '||\033[0m') # Bottom print('\033[93;46m || 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 ||\033[0m') # Prints numbers def play(): # The course of the game global board white_king = 'H5' # Tracks white king for checking black_king = 'A5' # Tracks black king for checking turn = 'white' # Tracks who's turn b_k_checked = 0 # 1 if checked 0 if not w_k_checked = 0 while True: # Turns print_board(board) if (turn == 'white') and (w_k_checked == 1): print('White king is checked') elif (turn == 'black') and (b_k_checked == 1): print('Black king is checked') print(turn.title(), 'player\'s turn') print('Space from:') from_ = input() print('Space to:') to = input() from_ = from_.upper() to = to.upper() # board_copy = copy.deepcopy(board) if (check_valid_move(from_, to)) and (board[from_[0]][from_].strip()[-1] == turn[0]): eater = board[from_[0]][from_][0] eaten = board[to[0]][to][0] if board[from_[0]][from_] == 'K___w': white_king = from_ elif board[from_[0]][from_] == 'K___b': black_king = from_ board[to[0]][to] = board[from_[0]][from_] board[from_[0]][from_] = ' ' if eaten != ' ': print(eater, 'has eaten', eaten + '.') if turn == 'white': if check_valid_move(to, black_king): b_k_checked = 1 else: if check_valid_move(to, white_king): w_k_checked = 1 else: print('Invalid move, try again.') continue if turn == 'white': turn = 'black' else: turn = 'white' print('To exit type \'X\'.') response = input() if response.upper() == 'X': break play()