├── LICENSE.md ├── chess.py ├── chess_GUI.py ├── chess_v2.py ├── chess_v2_GUI.py ├── gameGUI.png └── readme.md /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2021 j00nas 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute and/or sublicense of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /chess.py: -------------------------------------------------------------------------------- 1 | class Figure(object): 2 | 3 | def __init__(self, name, object_name, color, start_position, possible_moves): 4 | self.object_name = object_name 5 | self.name = name 6 | self.color = color 7 | self.position = start_position 8 | self.possible_moves = possible_moves 9 | 10 | def update_position(self, ps, pe, all_positions_enemy, all_positions_own_team, all_possible_moves_enemy, turn): 11 | self.position = pe #neue Position 12 | self.check_if_move_legit(pe, (), all_positions_enemy, all_positions_own_team, all_possible_moves_enemy, turn) 13 | 14 | def return_position(self, ps, pe, all_positions_enemy, all_positions_own_team, all_possible_moves_enemy, turn): 15 | self.position = ps #alte Position 16 | self.check_if_move_legit(ps, (), all_positions_enemy, all_positions_own_team, all_possible_moves_enemy, turn) 17 | 18 | def update_possible_moves(self, all_positions_enemy, all_positions_own_team, all_possible_moves_enemy, turn): 19 | self.check_if_move_legit(self.position, (), all_positions_enemy, all_positions_own_team, all_possible_moves_enemy, turn) 20 | 21 | def check_if_move_legit(self, ps, pe, all_positions_enemy, all_positions_own_team, all_possible_moves_enemy, turn): 22 | 23 | if self.name == 'WB' or self.name == 'BB': ### rooks ### 24 | direction_1 = 1 if self.name == 'BB' else -1 25 | direction_2 = 2 if self.name == 'BB' else -2 26 | B_possible_moves = [] 27 | if (self.name == 'BB' and ps in [(1,i) for i in range(8)]) or (self.name == 'WB' and ps in [(6,i) for i in range(8)]): #Two steps possible at the beginning 28 | if ((ps[0] + direction_1,ps[1]) not in all_positions_enemy) and ((ps[0] + direction_1,ps[1]) not in all_positions_own_team): 29 | B_possible_moves.append((ps[0] + direction_1,ps[1])) 30 | if ((ps[0] + direction_2,ps[1]) not in all_positions_enemy) and ((ps[0] + direction_2,ps[1]) not in all_positions_own_team): 31 | B_possible_moves.append((ps[0] + direction_2,ps[1])) 32 | if (ps[0] + direction_1,ps[1]+1) in all_positions_enemy: 33 | B_possible_moves.append((ps[0]+direction_1,ps[1]+1)) 34 | if (ps[0] + direction_1,ps[1]-1) in all_positions_enemy: 35 | B_possible_moves.append((ps[0]+direction_1,ps[1]-1)) 36 | self.possible_moves = B_possible_moves 37 | if pe in B_possible_moves: 38 | return True 39 | else: 40 | return False 41 | else: #normally one step 42 | if ((ps[0] + direction_1,ps[1]) not in all_positions_enemy) and ((ps[0] + direction_1,ps[1]) not in all_positions_own_team): 43 | B_possible_moves.append((ps[0] + direction_1,ps[1])) 44 | if (ps[0] + direction_1,ps[1]+1) in all_positions_enemy: 45 | B_possible_moves.append((ps[0]+direction_1,ps[1]+1)) 46 | if (ps[0] + direction_1,ps[1]-1) in all_positions_enemy: 47 | B_possible_moves.append((ps[0]+direction_1,ps[1]-1)) 48 | self.possible_moves = B_possible_moves 49 | if pe in B_possible_moves: 50 | return True 51 | else: 52 | return False 53 | 54 | if self.name == 'WT' or self.name == 'BT' or self.name == 'WD' or self.name == 'WK' or self.name == 'BD' or self.name == 'BK': ### pawns / king / queen ### 55 | T_possible_moves = [] 56 | i = 1 57 | if ps[0] + i != 8 and ps[0] + i != 0: 58 | if (ps[0]+ i,ps[1]) not in all_positions_own_team: 59 | T_possible_moves.append((ps[0]+ i,ps[1])) 60 | if self.name != 'WK' and self.name != 'BK': 61 | while (ps[0]+ i,ps[1]) not in all_positions_enemy and (ps[0]+ i, ps[1]) not in all_positions_own_team: #bot 62 | T_possible_moves.append((ps[0]+ i,ps[1])) 63 | i += 1 64 | if (ps[0]+ i,ps[1]) in all_positions_enemy: 65 | T_possible_moves.append((ps[0]+ i,ps[1])) 66 | break 67 | if ps[0]+ i == 8: 68 | break 69 | i = 1 70 | if ps[0]-i != -1 and ps[0]-i != -2: #if a figure is beaten it gets position (-1,-1) 71 | if (ps[0]-i,ps[1]) not in all_positions_own_team: 72 | T_possible_moves.append((ps[0]-i,ps[1])) 73 | if self.name != 'WK' and self.name != 'BK': 74 | while (ps[0]-i,ps[1]) not in all_positions_enemy and (ps[0]-i, ps[1]) not in all_positions_own_team: #top 75 | T_possible_moves.append((ps[0]-i,ps[1])) 76 | i += 1 77 | if (ps[0]-i,ps[1]) in all_positions_enemy: 78 | T_possible_moves.append((ps[0]-i,ps[1])) 79 | break 80 | if ps[0]-i == -1: 81 | break 82 | i = 1 83 | if ps[1] + i != 8 and ps[1] + 1 != 0: 84 | if (ps[0], ps[1] + i) not in all_positions_own_team: 85 | T_possible_moves.append((ps[0], ps[1] + i)) 86 | if self.name != 'WK' and self.name != 'BK': 87 | while (ps[0], ps[1] + i) not in all_positions_enemy and (ps[0], ps[1] + i) not in all_positions_own_team: #right 88 | T_possible_moves.append((ps[0], ps[1] + i)) 89 | i += 1 90 | if (ps[0], ps[1] + i) in all_positions_enemy: 91 | T_possible_moves.append((ps[0], ps[1] + i)) 92 | break 93 | if ps[1]+ i == 8: 94 | break 95 | i = 1 96 | if ps[1] - i != -1 and ps[1] -i != -2: #if a figure is beaten it gets position (-1,-1) 97 | if (ps[0], ps[1] - i) not in all_positions_own_team: 98 | T_possible_moves.append((ps[0], ps[1] - i)) 99 | if self.name != 'WK' and self.name != 'BK': 100 | while (ps[0], ps[1] - i) not in all_positions_enemy and (ps[0], ps[1] - i) not in all_positions_own_team: #left 101 | T_possible_moves.append((ps[0], ps[1] - i)) 102 | i += 1 103 | if (ps[0], ps[1] - i) in all_positions_enemy: 104 | T_possible_moves.append((ps[0], ps[1] - i)) 105 | break 106 | if ps[1]- i == -1: 107 | break 108 | self.possible_moves = [] 109 | for i in T_possible_moves: 110 | self.possible_moves.append(i) 111 | if self.name == 'WK' or self.name == 'BK' or self.name == 'WD' or self.name == 'BD': 112 | pass 113 | elif pe in T_possible_moves: 114 | return True 115 | else: 116 | return False 117 | 118 | if self.name == 'WS' or self.name == 'BS': ### knight ### 119 | S_possible_moves = [] 120 | if (ps[0] + 2, ps[1] + 1) not in all_positions_own_team and ps[0] + 2 in range(0,8) and ps[1] + 1 in range(0,8): 121 | S_possible_moves.append((ps[0] + 2, ps[1] + 1)) 122 | if (ps[0] + 2, ps[1] - 1) not in all_positions_own_team and ps[0] + 2 in range(0,8) and ps[1] - 1 in range(0,8): 123 | S_possible_moves.append((ps[0] + 2, ps[1] - 1)) 124 | if (ps[0] - 2, ps[1] + 1) not in all_positions_own_team and ps[0] - 2 in range(0,8) and ps[1] + 1 in range(0,8): 125 | S_possible_moves.append((ps[0] - 2, ps[1] + 1)) 126 | if (ps[0] - 2, ps[1] - 1) not in all_positions_own_team and ps[0] - 2 in range(0,8) and ps[1] - 1 in range(0,8): 127 | S_possible_moves.append((ps[0] - 2, ps[1] - 1)) 128 | if (ps[0] + 1, ps[1] + 2) not in all_positions_own_team and ps[0] + 1 in range(0,8) and ps[1] + 2 in range(0,8): 129 | S_possible_moves.append((ps[0] + 1, ps[1] + 2)) 130 | if (ps[0] + 1, ps[1] - 2) not in all_positions_own_team and ps[0] + 1 in range(0,8) and ps[1] - 2 in range(0,8): 131 | S_possible_moves.append((ps[0] + 1, ps[1] - 2)) 132 | if (ps[0] - 1, ps[1] + 2) not in all_positions_own_team and ps[0] - 1 in range(0,8) and ps[1] + 2 in range(0,8): 133 | S_possible_moves.append((ps[0] - 1, ps[1] + 2)) 134 | if (ps[0] - 1, ps[1] - 2) not in all_positions_own_team and ps[0] - 1 in range(0,8) and ps[1] - 2 in range(0,8): 135 | S_possible_moves.append((ps[0] - 1, ps[1] - 2)) 136 | 137 | self.possible_moves = S_possible_moves 138 | if pe in S_possible_moves: 139 | return True 140 | else: 141 | return False 142 | 143 | if self.name == 'WL' or self.name == 'BL' or self.name == 'WD' or self.name == 'WK' or self.name == 'BD' or self.name == 'BK': ### bishop / queen / king ### 144 | L_possible_moves = [] 145 | i = 1 146 | j = 1 147 | if (ps[0]+i, ps[1]+j) not in all_positions_own_team and ps[0]+i in range(0,8) and ps[1]+j in range(0,8): 148 | L_possible_moves.append((ps[0]+i, ps[1]+j)) 149 | if self.name != 'BK' and self.name != 'WK': 150 | while (ps[0]+i, ps[1]+j) not in all_positions_own_team and (ps[0]+i, ps[1]+j) not in all_positions_enemy and ps[0]+i in range(0,8) and ps[1]+j in range(0,8): 151 | L_possible_moves.append((ps[0]+i, ps[1]+j)) 152 | i += 1 153 | j += 1 154 | if (ps[0]+i, ps[1]+j) in all_positions_enemy: 155 | L_possible_moves.append((ps[0]+i, ps[1]+j)) 156 | break 157 | i = 1 158 | j = 1 159 | if (ps[0]-i, ps[1]+j) not in all_positions_own_team and ps[0]-i in range(0,8) and ps[1]+j in range(0,8): 160 | L_possible_moves.append((ps[0]-i, ps[1]+j)) 161 | if self.name != 'BK' and self.name != 'WK': 162 | while (ps[0]-i, ps[1]+j) not in all_positions_own_team and (ps[0]-i, ps[1]+j) not in all_positions_enemy and ps[0]-i in range(0,8) and ps[1]+j in range(0,8): 163 | L_possible_moves.append((ps[0]-i, ps[1]+j)) 164 | i += 1 165 | j += 1 166 | if (ps[0]-i, ps[1]+j) in all_positions_enemy: 167 | L_possible_moves.append((ps[0]-i, ps[1]+j)) 168 | break 169 | i = 1 170 | j = 1 171 | if (ps[0]-i, ps[1]-j) not in all_positions_own_team and ps[0]-i in range(0,8) and ps[1]-j in range(0,8): 172 | L_possible_moves.append((ps[0]-i, ps[1]-j)) 173 | if self.name != 'BK' and self.name != 'WK': 174 | while (ps[0]-i, ps[1]-j) not in all_positions_own_team and (ps[0]-i, ps[1]-j) not in all_positions_enemy and ps[0]-i in range(0,8) and ps[1]-j in range(0,8): 175 | L_possible_moves.append((ps[0]-i, ps[1]-j)) 176 | i += 1 177 | j += 1 178 | if (ps[0]-i, ps[1]-j) in all_positions_enemy: 179 | L_possible_moves.append((ps[0]-i, ps[1]-j)) 180 | break 181 | i = 1 182 | j = 1 183 | if (ps[0]+i, ps[1]-j) not in all_positions_own_team and ps[0]+i in range(0,8) and ps[1]-j in range(0,8): #necessary for the king, but generates double positions 184 | L_possible_moves.append((ps[0]+i, ps[1]-j)) 185 | if self.name != 'BK' and self.name != 'WK': 186 | while (ps[0]+i, ps[1]-j) not in all_positions_own_team and (ps[0]+i, ps[1]-j) not in all_positions_enemy and ps[0]+i in range(0,8) and ps[1]-j in range(0,8): 187 | L_possible_moves.append((ps[0]+i, ps[1]-j)) 188 | i += 1 189 | j += 1 190 | if (ps[0]+i, ps[1]-j) in all_positions_enemy: 191 | L_possible_moves.append((ps[0]+i, ps[1]-j)) 192 | break 193 | if self.name != 'WK' and self.name != 'BK' and self.name != 'WD' and self.name != 'BD': #already emptied in Tower-Query for this 4 figures 194 | self.possible_moves = [] 195 | for i in L_possible_moves: 196 | self.possible_moves.append(i) 197 | self.possible_moves = list(set(self.possible_moves)) #sorts out doubles (will give unsorted list without doubles) 198 | self.possible_moves.sort() 199 | 200 | if self.name == 'WK' or self.name == 'BK': 201 | for i in all_possible_moves_enemy: 202 | self.possible_moves = [j for j in self.possible_moves if j not in i] 203 | 204 | if pe in self.possible_moves: 205 | return True 206 | else: 207 | return False 208 | 209 | 210 | 211 | class Board(object): 212 | 213 | def __init__(self): 214 | self.B1 = Figure('WB', 'B1', 'WR', (6,0), [(5,0),(4,0)]) 215 | self.B2 = Figure('WB', 'B2', 'WR', (6,1), [(5,1),(4,1)]) 216 | self.B3 = Figure('WB', 'B3', 'WR', (6,2), [(5,2),(4,2)]) 217 | self.B4 = Figure('WB', 'B4', 'WR', (6,3), [(5,3),(4,3)]) 218 | self.B5 = Figure('WB', 'B5', 'WR', (6,4), [(5,4),(4,4)]) 219 | self.B6 = Figure('WB', 'B6', 'WR', (6,5), [(5,5),(4,5)]) 220 | self.B7 = Figure('WB', 'B7', 'WR', (6,6), [(5,6),(4,6)]) 221 | self.B8 = Figure('WB', 'B8', 'WR', (6,7), [(5,7),(4,7)]) 222 | self.T1 = Figure('WT', 'T1', 'WP', (7,0), []) 223 | self.T2 = Figure('WT', 'T2', 'WP', (7,7), []) 224 | self.S1 = Figure('WS', 'S1', 'WN', (7,1), [(5,0),(5,2)]) 225 | self.S2 = Figure('WS', 'S2', 'WN', (7,6), [(5,5),(5,7)]) 226 | self.L1 = Figure('WL', 'L1', 'WB', (7,2), []) 227 | self.L2 = Figure('WL', 'L2', 'WB', (7,5), []) 228 | self.D1 = Figure('WD', 'D1', 'WQ', (7,4), []) 229 | self.K1 = Figure('WK', 'K1', 'WK', (7,3), []) 230 | 231 | self.B9 = Figure('BB', 'B9', 'BR', (1,0), [(2,0),(3,0)]) 232 | self.B10 = Figure('BB', 'B10', 'BR', (1,1), [(2,1),(3,1)]) 233 | self.B11 = Figure('BB', 'B11', 'BR', (1,2), [(2,2),(3,2)]) 234 | self.B12 = Figure('BB', 'B12', 'BR', (1,3), [(2,3),(3,3)]) 235 | self.B13 = Figure('BB', 'B13', 'BR', (1,4), [(2,4),(3,4)]) 236 | self.B14 = Figure('BB', 'B14', 'BR', (1,5), [(2,5),(3,5)]) 237 | self.B15 = Figure('BB', 'B15', 'BR', (1,6), [(2,6),(3,6)]) 238 | self.B16 = Figure('BB', 'B16', 'BR', (1,7), [(2,7),(3,7)]) 239 | self.T3 = Figure('BT', 'T3', 'BP', (0,0), []) 240 | self.T4 = Figure('BT', 'T4', 'BP', (0,7), []) 241 | self.S3 = Figure('BS', 'S3', 'BN', (0,1), [(2,0),(2,2)]) 242 | self.S4 = Figure('BS', 'S4', 'BN', (0,6), [(2,5),(2,7)]) 243 | self.L3 = Figure('BL', 'L3', 'BB', (0,2), []) 244 | self.L4 = Figure('BL', 'L4', 'BB', (0,5), []) 245 | self.D2 = Figure('BD', 'D2', 'BQ', (0,4), []) 246 | self.K2 = Figure('BK', 'K2', 'BK', (0,3), []) 247 | 248 | self.run = True 249 | self.turn = 'W' 250 | self.destroyed_unit = '' 251 | self.main() 252 | 253 | def main(self): 254 | print('Welcome to this Python Chess Game. You can move by typing eg. "b2 b4"') 255 | while self.run == True: 256 | self.get_positions() 257 | self.print_board() 258 | self.destroyed_unit = '' 259 | start, end = self.checkinput() 260 | if(start, end) == (-1,-1): 261 | print('[0] invalid input') 262 | continue 263 | if(start, end) == (-2,-2): 264 | print('Game over.') 265 | break 266 | 267 | def checkinput(self): 268 | colors_turn = 'white' if self.turn == 'W' else 'black' 269 | print(f'\nIts {colors_turn} turn. Your move:') 270 | try: 271 | a,b,c,d,e = input() 272 | if a+b+c+d+e == 'exit!': 273 | self.run = False 274 | return(-2,-2) 275 | alphabet = ['a','b','c','d','e','f','g','h'] 276 | numbers = [1,2,3,4,5,6,7,8] 277 | if str(a) in alphabet and int(b) in numbers and c == ' ' and str(d) in alphabet and int(e) in numbers: 278 | translate_a = {'a' : 0, 'b' : 1, 'c' : 2, 'd' : 3, 'e' : 4, 'f' : 5, 'g' : 6, 'h' : 7} 279 | translate_b = {'8' : 0, '7' : 1, '6' : 2, '5' : 3, '4' : 4, '3' : 5, '2' : 6, '1' : 7} 280 | start_position = (translate_b[b], translate_a[a]) 281 | end_position = (translate_b[e], translate_a[d]) 282 | if self.check_chosen_move(start_position, end_position): 283 | if self.turn == 'W': #whites turn 284 | #print('All possible Moves of White before: ' + str(self.possible_moves_white)) 285 | #print('All possible Moves of Black before: ' + str(self.possible_moves_black)) 286 | for i in self.white_players: 287 | if i.position == start_position: 288 | i.update_position(start_position, end_position, self.position_black_players, self.position_white_players, self.possible_moves_black, self.turn) 289 | for i in self.black_players: 290 | if i.position == end_position: 291 | self.destroyed_unit = i.object_name 292 | i.position = (-1,-1) 293 | self.get_positions() 294 | for i in self.black_players: #Update all possible moves of the enemy team 295 | i.update_possible_moves(self.position_white_players, self.position_black_players, self.possible_moves_white, self.turn) 296 | for i in self.white_players: #Update all possible moves of the own team 297 | i.update_possible_moves(self.position_black_players, self.position_white_players, self.possible_moves_black, self.turn) 298 | self.get_positions() 299 | for i in self.possible_moves_black: 300 | if self.position_king_white in i: #If the own king is check 301 | for i in self.white_players: 302 | if i.position == end_position: 303 | i.return_position(start_position, end_position, self.position_black_players, self.position_white_players, self.possible_moves_black, self.turn) 304 | for i in self.black_players: 305 | try: 306 | if i.object_name == self.destroyed_unit: 307 | i.position = end_position 308 | self.destroyed_unit = '' 309 | except: 310 | pass 311 | print('Not allowed to bring own King into chess position') 312 | return(0,0) 313 | #print('All possible Moves of White after: ' + str(self.possible_moves_white)) 314 | #print('All possible Moves of Black after: ' + str(self.possible_moves_black)) 315 | for i in self.possible_moves_white: 316 | if self.position_king_black in i: 317 | if self.K2.possible_moves != []: 318 | print('check!') 319 | else: 320 | can_reach_king = [] 321 | can_reach_king_position = [] 322 | helpfull_positions = [] 323 | who_can_help = {} #If the king is in danger, can he escape or can some figure of the own team come to help? 324 | for i in self.white_players: 325 | if self.position_king_black in i.possible_moves or self.possible_moves_black in i.possible_moves or self.possible_moves_black in i.position: #Can the enemy jump in between? 326 | possible_positions_to_cover = [] 327 | for m in self.possible_moves_black: 328 | for u in m: 329 | if u in i.possible_moves or u in i.position: 330 | possible_positions_to_cover.append(u) 331 | for k in possible_positions_to_cover: 332 | for n in self.black_players: 333 | if k in n.possible_moves: 334 | who_can_help[(n.object_name, k)] = 0 335 | current_object = n.object_name 336 | current_position = n.position 337 | n.update_position(n.position, k, self.position_black_players, self.position_white_players, self.possible_moves_black, self.turn) 338 | for q in self.white_players: 339 | if q.position == k: 340 | self.destroyed_unit = q.object_name 341 | q.position = (-1,-1) 342 | self.get_positions() 343 | for x in self.white_players: 344 | x.update_possible_moves(self.position_black_players, self.position_white_players, self.possible_moves_black, self.turn) 345 | for y in self.black_players: 346 | y.update_possible_moves(self.position_white_players, self.position_black_players, self.possible_moves_white, self.turn) 347 | self.get_positions() 348 | for p in self.possible_moves_white: 349 | if self.position_king_black in p: 350 | who_can_help[(n.object_name, k)] = 1 351 | n.return_position(current_position, k, self.position_black_players, self.position_white_players, self.possible_moves_black, self.turn) 352 | for h in self.white_players: 353 | try: 354 | if h.object_name == self.destroyed_unit: 355 | h.position = k 356 | self.destroyed_unit = '' 357 | except: 358 | pass 359 | self.get_positions() 360 | for z in self.white_players: 361 | z.update_possible_moves(self.position_black_players, self.position_white_players, self.possible_moves_black, self.turn) 362 | for w in self.black_players: 363 | w.update_possible_moves(self.position_white_players, self.position_black_players, self.possible_moves_white, self.turn) 364 | self.get_positions() 365 | for key, item in who_can_help.items(): 366 | if item == 0: 367 | helpfull_positions.append(key[1]) 368 | if helpfull_positions != []: 369 | print('check!') 370 | pass 371 | else: 372 | for i in self.white_players: 373 | if i.position == start_position: 374 | i.update_position(start_position, end_position, self.position_black_players, self.position_white_players, self.possible_moves_black, self.turn) 375 | for i in self.black_players: 376 | if i.position == end_position: 377 | self.destroyed_unit = i.object_name 378 | i.position = (-1,-1) 379 | self.get_positions() 380 | self.print_board() 381 | print('check mate!') 382 | return(-2,-2) 383 | self.turn = 'B' 384 | else: #blacks turn 385 | #print('All possible Moves of White before: ' + str(self.possible_moves_white)) 386 | #print('All possible Moves of Black before: ' + str(self.possible_moves_black)) 387 | for i in self.black_players: 388 | if i.position == start_position: 389 | i.update_position(start_position, end_position, self.position_white_players, self.position_black_players, self.possible_moves_white, self.turn) 390 | for i in self.white_players: 391 | if i.position == end_position: 392 | self.destroyed_unit = i.object_name 393 | i.position = (-1,-1) 394 | self.get_positions() 395 | for i in self.black_players: 396 | i.update_possible_moves(self.position_white_players, self.position_black_players, self.possible_moves_white, self.turn) 397 | for i in self.white_players: 398 | i.update_possible_moves(self.position_black_players, self.position_white_players, self.possible_moves_black, self.turn) 399 | self.get_positions() 400 | for i in self.possible_moves_white: 401 | if self.position_king_black in i: 402 | for i in self.black_players: 403 | if i.position == end_position: 404 | i.return_position(start_position, end_position, self.position_white_players, self.position_black_players, self.possible_moves_white, self.turn) 405 | for i in self.white_players: 406 | try: 407 | if i.object_name == self.destroyed_unit: 408 | i.position = end_position 409 | self.destroyed_unit = '' 410 | except: 411 | pass 412 | print('Not allowed to bring own King into chess position') 413 | return(0,0) 414 | #print('All possible Moves of White after: ' + str(self.possible_moves_white)) 415 | #print('All possible Moves of Black after: ' + str(self.possible_moves_black)) 416 | for i in self.possible_moves_black: 417 | if self.position_king_white in i: 418 | if self.K1.possible_moves != []: 419 | print('check!') 420 | else: 421 | can_reach_king = [] 422 | can_reach_king_position = [] 423 | helpfull_positions = [] 424 | who_can_help = {} 425 | for i in self.black_players: 426 | if self.position_king_white in i.possible_moves or self.possible_moves_white in i.possible_moves or self.possible_moves_white in i.position: #Can the enemy jump in between? 427 | possible_positions_to_cover = [] 428 | for m in self.possible_moves_white: 429 | for u in m: 430 | if u in i.possible_moves or u in i.position: 431 | possible_positions_to_cover.append(u) 432 | for k in possible_positions_to_cover: # Würde der Zug in dieses Feld den König aus dem Schach befreien? 433 | for n in self.white_players: 434 | if k in n.possible_moves: 435 | who_can_help[(n.object_name, k)] = 0 436 | current_object = n.object_name 437 | current_position = n.position 438 | n.update_position(n.position, k, self.position_white_players, self.position_black_players, self.possible_moves_white, self.turn) 439 | for q in self.black_players: 440 | if q.position == k: 441 | self.destroyed_unit = q.object_name 442 | q.position = (-1,-1) 443 | self.get_positions() 444 | for x in self.white_players: 445 | x.update_possible_moves(self.position_black_players, self.position_white_players, self.possible_moves_black, self.turn) 446 | for y in self.black_players: 447 | y.update_possible_moves(self.position_white_players, self.position_black_players, self.possible_moves_white, self.turn) 448 | self.get_positions() 449 | 450 | for p in self.possible_moves_black: 451 | if self.position_king_white in p: 452 | who_can_help[(n.object_name, k)] = 1 453 | n.return_position(current_position, k, self.position_white_players, self.position_black_players, self.possible_moves_white, self.turn) 454 | for h in self.black_players: 455 | try: 456 | if h.object_name == self.destroyed_unit: 457 | h.position = k 458 | self.destroyed_unit = '' 459 | except: 460 | pass 461 | self.get_positions() 462 | for z in self.white_players: 463 | z.update_possible_moves(self.position_black_players, self.position_white_players, self.possible_moves_black, self.turn) 464 | for w in self.black_players: 465 | w.update_possible_moves(self.position_white_players, self.position_black_players, self.possible_moves_white, self.turn) 466 | self.get_positions() 467 | for key, item in who_can_help.items(): 468 | if item == 0: 469 | helpfull_positions.append(key[1]) 470 | if helpfull_positions != []: 471 | print('check!') 472 | pass 473 | else: 474 | for i in self.black_players: 475 | if i.position == start_position: 476 | i.update_position(start_position, end_position, self.position_white_players, self.position_black_players, self.possible_moves_white, self.turn) 477 | for i in self.white_players: 478 | if i.position == end_position: 479 | self.destroyed_unit = i.object_name 480 | i.position = (-1,-1) 481 | self.get_positions() 482 | self.print_board() 483 | print('check mate!') 484 | return(-2,-2) 485 | self.turn = 'W' 486 | return(0,0) 487 | else: 488 | return(0,0) 489 | else: 490 | return(-1,-1) 491 | except: 492 | return(-1,-1) 493 | 494 | def check_chosen_move(self, ps, pe): 495 | if self.turn == 'W' and ps in self.position_white_players: 496 | try: 497 | for i in self.white_players: 498 | if i.position == ps: 499 | if i.check_if_move_legit(ps, pe, self.position_black_players, self.position_white_players, self.possible_moves_black, self.turn): 500 | return True 501 | else: 502 | print('move not legit') 503 | return False 504 | except: 505 | print('Choose a legit player') 506 | return False 507 | elif self.turn == 'B' and ps in self.position_black_players: 508 | try: 509 | for i in self.black_players: 510 | if i.position == ps: 511 | if i.check_if_move_legit(ps, pe, self.position_white_players, self.position_black_players, self.possible_moves_white, self.turn): 512 | return True 513 | else: 514 | print('move not legit') 515 | return False 516 | except: 517 | print('Choose a legit player') 518 | return False 519 | else: 520 | print(f'Its {self.turn} turn, pick a valid figure!') 521 | return False 522 | 523 | 524 | def print_board(self): 525 | board_side = [' 8 \u2502',' 7 \u2502',' 6 \u2502',' 5 \u2502',' 4 \u2502',' 3 \u2502',' 2 \u2502',' 1 \u2502'] 526 | print('\n\n a b c d e f g h' + '\n \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n \u2502 \u2502') 527 | k = 8 528 | for i in range(8): 529 | print(board_side[i], end = ' ') 530 | for j in range(8): 531 | if (i,j) in self.position_white_players or (i,j) in self.position_black_players: 532 | for x in self.white_players: 533 | if x.position == (i,j): 534 | print(x.color, end = ' ') 535 | for x in self.black_players: 536 | if x.position == (i,j): 537 | print(x.color, end = ' ') 538 | else: 539 | print(' \u2022 ', end = ' ') 540 | print(' \u2502 ' + str(k) + '\n \u2502 \u2502') 541 | k -= 1 542 | print(' \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n a b c d e f g h' + '\n') 543 | 544 | def get_positions(self): 545 | self.white_players = white_players = [self.B1,self.B2,self.B3,self.B4,self.B5,self.B6,self.B7,self.B8,self.T1,self.T2,self.S1,self.S2,self.L1,self.L2,self.D1,self.K1] 546 | self.position_white_players = [i.position for i in white_players] 547 | self.possible_moves_white = [i.possible_moves for i in white_players] 548 | self.position_king_white = self.K1.position 549 | self.black_players = black_players = [self.B9,self.B10,self.B11,self.B12,self.B13,self.B14,self.B15,self.B16,self.T3,self.T4,self.S3,self.S4,self.L3,self.L4,self.D2,self.K2] 550 | self.position_black_players = [i.position for i in black_players] 551 | self.possible_moves_black = [i.possible_moves for i in black_players] 552 | self.position_king_black = self.K2.position 553 | return True 554 | 555 | Board() -------------------------------------------------------------------------------- /chess_GUI.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | import tkinter.messagebox 3 | 4 | tk = Tk() 5 | tk.title('Chess') 6 | tkstart = '' 7 | tkend = '' 8 | startbutton = '' 9 | endbutton = '' 10 | endbutton_color = '' 11 | startbutton_color = '' 12 | error = '' 13 | 14 | def btnClick(buttons): 15 | global startbutton, endbutton, endbutton_color, startbutton_color, startbutton_text, endbutton_text 16 | if tkstart != '': 17 | endbutton = buttons 18 | endbutton_color = buttons['bg'] 19 | endbutton_text = buttons['text'] 20 | if tkend == '' and tkstart == '': 21 | startbutton = buttons 22 | startbutton_color = buttons['bg'] 23 | startbutton_text = buttons['text'] 24 | 25 | 26 | def undo_coloring(): 27 | for i in fields_dic: 28 | if fields_dic[i]['fg'] == 'lightgreen': 29 | fields_dic[i]['fg'] = 'black' 30 | if fields_dic[i]['text'] == '\u0E4F': 31 | fields_dic[i]['text'] = '' 32 | 33 | def btnID(id): 34 | global tkstart, tkend, startbutton, endbutton, error, white_players, black_players, fields_dic, turn, error 35 | colored_fields = [] 36 | fields_dic = {'a1':a1,'a2': a2,'a3': a3,'a4': a4,'a5':a5,'a6':a6,'a7':a7,'a8':a8,'b1':b1,'b2':b2,'b3':b3,'b4':b4,'b5':b5,'b6':b6,'b7':b7,'b8':b8,'c2':c1,'c2':c2,'c3':c3,'c4':c4,'c5':c5,'c6':c6,'c7':c7,'c8':c8,'d1':d1,'d2':d2,'d3':d3,'d4':d4,'d5':d5,'d6':d6,'d7':d7,'d8':d8,'e1':e1,'e2':e2,'e3':e3,'e4':e4,'e5':e5,'e6':e6,'e7':e7,'e8':e8,'f1':f1,'f2':f2,'f3':f3,'f4':f4,'f5':f5,'f6':f6,'f7':f7,'f8':f8,'g1':g1,'g2':g2,'g3':g3,'g4':g4,'g5':g5,'g6':g6,'g7':g7,'g8':g8,'h1':h1,'h2':h2,'h3':h3,'h4':h4,'h5':h5,'h6':h6,'h7':h7,'h8':h8} 37 | if error != 4: 38 | if tkstart != '': 39 | tkend = id 40 | if turn == 'W' and (startbutton['text'] in [WR, WN, WP, WQ, WK, WB] or startbutton['text'] == WK + '\ncheck!'): 41 | if tkend == '' and tkstart == '' and startbutton['text'] != '': 42 | tkstart = id 43 | startbutton['bg'] = 'lightgreen' 44 | elif turn == 'B' and (startbutton['text'] in [BR, BN, BP, BQ, BK, BB] or startbutton['text'] == BK + '\ncheck!'): 45 | if tkend == '' and tkstart == '' and startbutton['text'] != '': 46 | tkstart = id 47 | startbutton['bg'] = 'lightgreen' 48 | 49 | if tkstart != '' and tkend == '': 50 | get_positions() 51 | translate_a = {'a' : 0, 'b' : 1, 'c' : 2, 'd' : 3, 'e' : 4, 'f' : 5, 'g' : 6, 'h' : 7} 52 | translate_b = {'8' : 0, '7' : 1, '6' : 2, '5' : 3, '4' : 4, '3' : 5, '2' : 6, '1' : 7} 53 | start_position = (translate_b[tkstart[1]], translate_a[tkstart[0]]) 54 | translate_a_back = {0 : 'a', 1 : 'b' , 2 : 'c', 3 : 'd', 4 : 'e', 5 : 'f', 6 : 'g', 7 : 'h'} 55 | translate_b_back = {0 : '8', 1 : '7', 2 : '6', 3 : '5', 4 : '4', 5 : '3', 6 : '2', 7 : '1'} 56 | if turn == 'W': 57 | for i in white_players: 58 | if i.position == start_position: 59 | for i in i.possible_moves: 60 | fn = translate_a_back[i[1]] 61 | sn = translate_b_back[i[0]] 62 | fld = fn+sn 63 | for item in fields_dic: 64 | if item == fld: 65 | colored_fields.append(item) 66 | fields_dic[item]['fg'] = 'lightgreen' 67 | if fields_dic[item]['text'] == '': 68 | fields_dic[item]['text'] = '\u0E4F' 69 | else: 70 | for i in black_players: 71 | if i.position == start_position: 72 | for i in i.possible_moves: 73 | fn = translate_a_back[i[1]] 74 | sn = translate_b_back[i[0]] 75 | fld = fn+sn 76 | for item in fields_dic: 77 | if item == fld: 78 | colored_fields.append(item) 79 | fields_dic[item]['fg'] = 'lightgreen' 80 | if fields_dic[item]['text'] == '': 81 | fields_dic[item]['text'] = '\u0E4F' 82 | 83 | 84 | 85 | if tkstart != '' and tkend != '': 86 | main() 87 | if error == '': 88 | tkstart = '' 89 | tkend = '' 90 | startbutton['bg'] = startbutton_color 91 | if endbutton_color == 'lightgreen': 92 | endbutton['bg'] = startbutton_color 93 | else: 94 | endbutton['bg'] = endbutton_color 95 | startbutton['text'] = '' 96 | endbutton['text'] = startbutton_text 97 | fields = [a1,a2,a3,a4,a5,a6,a7,a8,b1,b2,b3,b4,b5,b6,b7,b8,c1,c2,c3,c4,c5,c5,c6,c7,c8,d1,d2,d3,d4,d5,d6,d7,d8,e1,e2,e3,e4,e5,e6,e7,e8,f1,f2,f3,f4,f5,f6,f7,f8,g1,g2,g3,g4,g5,g6,g7,g8,h1,h2,h3,h4,h5,h6,h7,h8] 98 | for i in fields: 99 | if i['text'] == BK + '\ncheck!': 100 | i['text'] = BK 101 | if i['text'] == WK + '\ncheck!': 102 | i['text'] = WK 103 | error = '' 104 | undo_coloring() 105 | elif error == 3: 106 | tkstart = '' 107 | tkend = '' 108 | startbutton['bg'] = startbutton_color 109 | if endbutton_color == 'lightgreen': 110 | endbutton['bg'] = startbutton_color 111 | else: 112 | endbutton['bg'] = endbutton_color 113 | startbutton['text'] = '' 114 | endbutton['text'] = startbutton_text 115 | fields = [a1,a2,a3,a4,a5,a6,a7,a8,b1,b2,b3,b4,b5,b6,b7,b8,c1,c2,c3,c4,c5,c5,c6,c7,c8,d1,d2,d3,d4,d5,d6,d7,d8,e1,e2,e3,e4,e5,e6,e7,e8,f1,f2,f3,f4,f5,f6,f7,f8,g1,g2,g3,g4,g5,g6,g7,g8,h1,h2,h3,h4,h5,h6,h7,h8] 116 | if turn == 'B': 117 | for i in fields: 118 | if i['text'] == BK: 119 | i['text'] = BK + '\ncheck!' 120 | elif turn == 'W': 121 | for i in fields: 122 | if i['text'] == WK: 123 | i['text'] = WK + '\ncheck!' 124 | tkstart = '' 125 | tkend = '' 126 | startbutton['bg'] = startbutton_color 127 | if endbutton_color == 'lightgreen': 128 | endbutton['bg'] = startbutton_color 129 | else: 130 | endbutton['bg'] = endbutton_color 131 | error = '' 132 | undo_coloring() 133 | elif error == 2: 134 | tkstart = '' 135 | tkend = '' 136 | startbutton['bg'] = startbutton_color 137 | if endbutton_color == 'lightgreen': 138 | endbutton['bg'] = startbutton_color 139 | else: 140 | endbutton['bg'] = endbutton_color 141 | startbutton['text'] = '' 142 | endbutton['text'] = startbutton_text 143 | fields = [a1,a2,a3,a4,a5,a6,a7,a8,b1,b2,b3,b4,b5,b6,b7,b8,c1,c2,c3,c4,c5,c5,c6,c7,c8,d1,d2,d3,d4,d5,d6,d7,d8,e1,e2,e3,e4,e5,e6,e7,e8,f1,f2,f3,f4,f5,f6,f7,f8,g1,g2,g3,g4,g5,g6,g7,g8,h1,h2,h3,h4,h5,h6,h7,h8] 144 | if turn == 'W': 145 | for i in fields: 146 | if i['text'] == BK: 147 | i['text'] = BK + '\ncheck \nmate!' 148 | elif turn == 'B': 149 | for i in fields: 150 | if i['text'] == WK: 151 | i['text'] = WK + '\ncheck \nmate!' 152 | tkstart = '' 153 | tkend = '' 154 | startbutton['bg'] = startbutton_color 155 | if endbutton_color == 'lightgreen': 156 | endbutton['bg'] = startbutton_color 157 | else: 158 | endbutton['bg'] = endbutton_color 159 | error = 4 160 | undo_coloring() 161 | else: 162 | tkstart = '' 163 | tkend = '' 164 | startbutton['bg'] = startbutton_color 165 | if endbutton_color == 'lightgreen': 166 | endbutton['bg'] = startbutton_color 167 | else: 168 | endbutton['bg'] = endbutton_color 169 | error = '' 170 | undo_coloring() 171 | 172 | 173 | 174 | BR = '\u265C' 175 | BN = '\u265E' 176 | BB = '\u265D' 177 | BQ = '\u265B' 178 | BK = '\u265A' 179 | BP = '\u265F' 180 | 181 | WR = '\u2656' 182 | WN = '\u2658' 183 | WP = '\u2659' 184 | WQ = '\u2655' 185 | WK = '\u2654' 186 | WB = '\u2657' 187 | 188 | a8 = Button(tk, text=BR, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(a8), btnID('a8')]) 189 | a8.grid(row=1, column=1) 190 | b8 = Button(tk, text=BN, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(b8), btnID('b8')]) 191 | b8.grid(row=1, column=2) 192 | c8 = Button(tk, text=BB, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(c8), btnID('c8')]) 193 | c8.grid(row=1, column=3) 194 | d8 = Button(tk, text=BQ, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(d8), btnID('d8')]) 195 | d8.grid(row=1, column=4) 196 | e8 = Button(tk, text=BK, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(e8), btnID('e8')]) 197 | e8.grid(row=1, column=5) 198 | f8 = Button(tk, text=BB, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(f8), btnID('f8')]) 199 | f8.grid(row=1, column=6) 200 | g8 = Button(tk, text=BN, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(g8), btnID('g8')]) 201 | g8.grid(row=1, column=7) 202 | h8 = Button(tk, text=BR, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(h8), btnID('h8')]) 203 | h8.grid(row=1, column=8) 204 | a7 = Button(tk, text=BP, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(a7), btnID('a7')]) 205 | a7.grid(row=2, column=1) 206 | b7 = Button(tk, text=BP, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(b7), btnID('b7')]) 207 | b7.grid(row=2, column=2) 208 | c7 = Button(tk, text=BP, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(c7), btnID('c7')]) 209 | c7.grid(row=2, column=3) 210 | d7 = Button(tk, text=BP, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(d7), btnID('d7')]) 211 | d7.grid(row=2, column=4) 212 | e7 = Button(tk, text=BP, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(e7), btnID('e7')]) 213 | e7.grid(row=2, column=5) 214 | f7 = Button(tk, text=BP, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(f7), btnID('f7')]) 215 | f7.grid(row=2, column=6) 216 | g7 = Button(tk, text=BP, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(g7), btnID('g7')]) 217 | g7.grid(row=2, column=7) 218 | h7 = Button(tk, text=BP, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(h7), btnID('h7')]) 219 | h7.grid(row=2, column=8) 220 | a6 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(a6), btnID('a6')]) 221 | a6.grid(row=3, column=1) 222 | b6 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(b6), btnID('b6')]) 223 | b6.grid(row=3, column=2) 224 | c6 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(c6), btnID('c6')]) 225 | c6.grid(row=3, column=3) 226 | d6 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(d6), btnID('d6')]) 227 | d6.grid(row=3, column=4) 228 | e6 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(e6), btnID('e6')]) 229 | e6.grid(row=3, column=5) 230 | f6 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(f6), btnID('f6')]) 231 | f6.grid(row=3, column=6) 232 | g6 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(g6), btnID('g6')]) 233 | g6.grid(row=3, column=7) 234 | h6 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(h6), btnID('h6')]) 235 | h6.grid(row=3, column=8) 236 | a5 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(a5), btnID('a5')]) 237 | a5.grid(row=4, column=1) 238 | b5 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(b5), btnID('b5')]) 239 | b5.grid(row=4, column=2) 240 | c5 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(c5), btnID('c5')]) 241 | c5.grid(row=4, column=3) 242 | d5 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(d5), btnID('d5')]) 243 | d5.grid(row=4, column=4) 244 | e5 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(e5), btnID('e5')]) 245 | e5.grid(row=4, column=5) 246 | f5 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(f5), btnID('f5')]) 247 | f5.grid(row=4, column=6) 248 | g5 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(g5), btnID('g5')]) 249 | g5.grid(row=4, column=7) 250 | h5 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(h5), btnID('h5')]) 251 | h5.grid(row=4, column=8) 252 | a4 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(a4), btnID('a4')]) 253 | a4.grid(row=5, column=1) 254 | b4 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(b4), btnID('b4')]) 255 | b4.grid(row=5, column=2) 256 | c4 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(c4), btnID('c4')]) 257 | c4.grid(row=5, column=3) 258 | d4 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(d4), btnID('d4')]) 259 | d4.grid(row=5, column=4) 260 | e4 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(e4), btnID('e4')]) 261 | e4.grid(row=5, column=5) 262 | f4 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(f4), btnID('f4')]) 263 | f4.grid(row=5, column=6) 264 | g4 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(g4), btnID('g4')]) 265 | g4.grid(row=5, column=7) 266 | h4 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(h4), btnID('h4')]) 267 | h4.grid(row=5, column=8) 268 | a3 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(a3), btnID('a3')]) 269 | a3.grid(row=6, column=1) 270 | b3 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(b3), btnID('b3')]) 271 | b3.grid(row=6, column=2) 272 | c3 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(c3), btnID('c3')]) 273 | c3.grid(row=6, column=3) 274 | d3 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(d3), btnID('d3')]) 275 | d3.grid(row=6, column=4) 276 | e3 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(e3), btnID('e3')]) 277 | e3.grid(row=6, column=5) 278 | f3 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(f3), btnID('f3')]) 279 | f3.grid(row=6, column=6) 280 | g3 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(g3), btnID('g3')]) 281 | g3.grid(row=6, column=7) 282 | h3 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(h3), btnID('h3')]) 283 | h3.grid(row=6, column=8) 284 | a2 = Button(tk, text=WP, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(a2), btnID('a2')]) 285 | a2.grid(row=7, column=1) 286 | b2 = Button(tk, text=WP, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(b2), btnID('b2')]) 287 | b2.grid(row=7, column=2) 288 | c2 = Button(tk, text=WP, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(c2), btnID('c2')]) 289 | c2.grid(row=7, column=3) 290 | d2 = Button(tk, text=WP, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(d2), btnID('d2')]) 291 | d2.grid(row=7, column=4) 292 | e2 = Button(tk, text=WP, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(e2), btnID('e2')]) 293 | e2.grid(row=7, column=5) 294 | f2 = Button(tk, text=WP, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(f2), btnID('f2')]) 295 | f2.grid(row=7, column=6) 296 | g2 = Button(tk, text=WP, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(g2), btnID('g2')]) 297 | g2.grid(row=7, column=7) 298 | h2 = Button(tk, text=WP, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(h2), btnID('h2')]) 299 | h2.grid(row=7, column=8) 300 | a1 = Button(tk, text=WR, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(a1), btnID('a1')]) 301 | a1.grid(row=8, column=1) 302 | b1 = Button(tk, text=WN, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(b1), btnID('b1')]) 303 | b1.grid(row=8, column=2) 304 | c1 = Button(tk, text=WB, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(c1), btnID('c1')]) 305 | c1.grid(row=8, column=3) 306 | d1 = Button(tk, text=WQ, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(d1), btnID('d1')]) 307 | d1.grid(row=8, column=4) 308 | e1 = Button(tk, text=WK, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(e1), btnID('e1')]) 309 | e1.grid(row=8, column=5) 310 | f1 = Button(tk, text=WB, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(f1), btnID('f1')]) 311 | f1.grid(row=8, column=6) 312 | g1 = Button(tk, text=WN, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick(g1), btnID('g1')]) 313 | g1.grid(row=8, column=7) 314 | h1 = Button(tk, text=WR, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick(h1), btnID('h1')]) 315 | h1.grid(row=8, column=8) 316 | 317 | 318 | class Figure(object): 319 | 320 | def __init__(self, name, object_name, color, start_position, possible_moves): 321 | self.object_name = object_name 322 | self.name = name 323 | self.color = color 324 | self.position = start_position 325 | self.possible_moves = possible_moves 326 | 327 | def update_position(self, ps, pe, all_positions_enemy, all_positions_own_team, all_possible_moves_enemy, turn): 328 | self.position = pe #neue Position 329 | self.check_if_move_legit(pe, (), all_positions_enemy, all_positions_own_team, all_possible_moves_enemy, turn) 330 | 331 | def return_position(self, ps, pe, all_positions_enemy, all_positions_own_team, all_possible_moves_enemy, turn): 332 | self.position = ps #alte Position 333 | self.check_if_move_legit(ps, (), all_positions_enemy, all_positions_own_team, all_possible_moves_enemy, turn) 334 | 335 | def update_possible_moves(self, all_positions_enemy, all_positions_own_team, all_possible_moves_enemy, turn): 336 | self.check_if_move_legit(self.position, (), all_positions_enemy, all_positions_own_team, all_possible_moves_enemy, turn) 337 | 338 | def check_if_move_legit(self, ps, pe, all_positions_enemy, all_positions_own_team, all_possible_moves_enemy, turn): 339 | 340 | if self.name == 'WB' or self.name == 'BB': ### rooks ### 341 | direction_1 = 1 if self.name == 'BB' else -1 342 | direction_2 = 2 if self.name == 'BB' else -2 343 | B_possible_moves = [] 344 | if (self.name == 'BB' and ps in [(1,i) for i in range(8)]) or (self.name == 'WB' and ps in [(6,i) for i in range(8)]): #Two steps possible at the beginning 345 | if ((ps[0] + direction_1,ps[1]) not in all_positions_enemy) and ((ps[0] + direction_1,ps[1]) not in all_positions_own_team): 346 | B_possible_moves.append((ps[0] + direction_1,ps[1])) 347 | if ((ps[0] + direction_2,ps[1]) not in all_positions_enemy) and ((ps[0] + direction_2,ps[1]) not in all_positions_own_team): 348 | B_possible_moves.append((ps[0] + direction_2,ps[1])) 349 | if (ps[0] + direction_1,ps[1]+1) in all_positions_enemy: 350 | B_possible_moves.append((ps[0]+direction_1,ps[1]+1)) 351 | if (ps[0] + direction_1,ps[1]-1) in all_positions_enemy: 352 | B_possible_moves.append((ps[0]+direction_1,ps[1]-1)) 353 | self.possible_moves = B_possible_moves 354 | if pe in B_possible_moves: 355 | return True 356 | else: 357 | return False 358 | else: #normally one step 359 | if ((ps[0] + direction_1,ps[1]) not in all_positions_enemy) and ((ps[0] + direction_1,ps[1]) not in all_positions_own_team): 360 | B_possible_moves.append((ps[0] + direction_1,ps[1])) 361 | if (ps[0] + direction_1,ps[1]+1) in all_positions_enemy: 362 | B_possible_moves.append((ps[0]+direction_1,ps[1]+1)) 363 | if (ps[0] + direction_1,ps[1]-1) in all_positions_enemy: 364 | B_possible_moves.append((ps[0]+direction_1,ps[1]-1)) 365 | self.possible_moves = B_possible_moves 366 | if pe in B_possible_moves: 367 | return True 368 | else: 369 | return False 370 | 371 | if self.name == 'WT' or self.name == 'BT' or self.name == 'WD' or self.name == 'WK' or self.name == 'BD' or self.name == 'BK': ### pawns / king / queen ### 372 | T_possible_moves = [] 373 | i = 1 374 | if ps[0] + i != 8 and ps[0] + i != 0: 375 | if (ps[0]+ i,ps[1]) not in all_positions_own_team: 376 | T_possible_moves.append((ps[0]+ i,ps[1])) 377 | if self.name != 'WK' and self.name != 'BK': 378 | while (ps[0]+ i,ps[1]) not in all_positions_enemy and (ps[0]+ i, ps[1]) not in all_positions_own_team: #bot 379 | T_possible_moves.append((ps[0]+ i,ps[1])) 380 | i += 1 381 | if (ps[0]+ i,ps[1]) in all_positions_enemy: 382 | T_possible_moves.append((ps[0]+ i,ps[1])) 383 | break 384 | if ps[0]+ i == 8: 385 | break 386 | i = 1 387 | if ps[0]-i != -1 and ps[0]-i != -2: #if a figure is beaten it gets position (-1,-1) 388 | if (ps[0]-i,ps[1]) not in all_positions_own_team: 389 | T_possible_moves.append((ps[0]-i,ps[1])) 390 | if self.name != 'WK' and self.name != 'BK': 391 | while (ps[0]-i,ps[1]) not in all_positions_enemy and (ps[0]-i, ps[1]) not in all_positions_own_team: #top 392 | T_possible_moves.append((ps[0]-i,ps[1])) 393 | i += 1 394 | if (ps[0]-i,ps[1]) in all_positions_enemy: 395 | T_possible_moves.append((ps[0]-i,ps[1])) 396 | break 397 | if ps[0]-i == -1: 398 | break 399 | i = 1 400 | if ps[1] + i != 8 and ps[1] + 1 != 0: 401 | if (ps[0], ps[1] + i) not in all_positions_own_team: 402 | T_possible_moves.append((ps[0], ps[1] + i)) 403 | if self.name != 'WK' and self.name != 'BK': 404 | while (ps[0], ps[1] + i) not in all_positions_enemy and (ps[0], ps[1] + i) not in all_positions_own_team: #right 405 | T_possible_moves.append((ps[0], ps[1] + i)) 406 | i += 1 407 | if (ps[0], ps[1] + i) in all_positions_enemy: 408 | T_possible_moves.append((ps[0], ps[1] + i)) 409 | break 410 | if ps[1]+ i == 8: 411 | break 412 | i = 1 413 | if ps[1] - i != -1 and ps[1] -i != -2: #if a figure is beaten it gets position (-1,-1) 414 | if (ps[0], ps[1] - i) not in all_positions_own_team: 415 | T_possible_moves.append((ps[0], ps[1] - i)) 416 | if self.name != 'WK' and self.name != 'BK': 417 | while (ps[0], ps[1] - i) not in all_positions_enemy and (ps[0], ps[1] - i) not in all_positions_own_team: #left 418 | T_possible_moves.append((ps[0], ps[1] - i)) 419 | i += 1 420 | if (ps[0], ps[1] - i) in all_positions_enemy: 421 | T_possible_moves.append((ps[0], ps[1] - i)) 422 | break 423 | if ps[1]- i == -1: 424 | break 425 | self.possible_moves = [] 426 | for i in T_possible_moves: 427 | self.possible_moves.append(i) 428 | if self.name == 'WK' or self.name == 'BK' or self.name == 'WD' or self.name == 'BD': 429 | pass 430 | elif pe in T_possible_moves: 431 | return True 432 | else: 433 | return False 434 | 435 | if self.name == 'WS' or self.name == 'BS': ### knight ### 436 | S_possible_moves = [] 437 | if (ps[0] + 2, ps[1] + 1) not in all_positions_own_team and ps[0] + 2 in range(0,8) and ps[1] + 1 in range(0,8): 438 | S_possible_moves.append((ps[0] + 2, ps[1] + 1)) 439 | if (ps[0] + 2, ps[1] - 1) not in all_positions_own_team and ps[0] + 2 in range(0,8) and ps[1] - 1 in range(0,8): 440 | S_possible_moves.append((ps[0] + 2, ps[1] - 1)) 441 | if (ps[0] - 2, ps[1] + 1) not in all_positions_own_team and ps[0] - 2 in range(0,8) and ps[1] + 1 in range(0,8): 442 | S_possible_moves.append((ps[0] - 2, ps[1] + 1)) 443 | if (ps[0] - 2, ps[1] - 1) not in all_positions_own_team and ps[0] - 2 in range(0,8) and ps[1] - 1 in range(0,8): 444 | S_possible_moves.append((ps[0] - 2, ps[1] - 1)) 445 | if (ps[0] + 1, ps[1] + 2) not in all_positions_own_team and ps[0] + 1 in range(0,8) and ps[1] + 2 in range(0,8): 446 | S_possible_moves.append((ps[0] + 1, ps[1] + 2)) 447 | if (ps[0] + 1, ps[1] - 2) not in all_positions_own_team and ps[0] + 1 in range(0,8) and ps[1] - 2 in range(0,8): 448 | S_possible_moves.append((ps[0] + 1, ps[1] - 2)) 449 | if (ps[0] - 1, ps[1] + 2) not in all_positions_own_team and ps[0] - 1 in range(0,8) and ps[1] + 2 in range(0,8): 450 | S_possible_moves.append((ps[0] - 1, ps[1] + 2)) 451 | if (ps[0] - 1, ps[1] - 2) not in all_positions_own_team and ps[0] - 1 in range(0,8) and ps[1] - 2 in range(0,8): 452 | S_possible_moves.append((ps[0] - 1, ps[1] - 2)) 453 | 454 | self.possible_moves = S_possible_moves 455 | if pe in S_possible_moves: 456 | return True 457 | else: 458 | return False 459 | 460 | if self.name == 'WL' or self.name == 'BL' or self.name == 'WD' or self.name == 'WK' or self.name == 'BD' or self.name == 'BK': ### bishop / queen / king ### 461 | L_possible_moves = [] 462 | i = 1 463 | j = 1 464 | if (ps[0]+i, ps[1]+j) not in all_positions_own_team and ps[0]+i in range(0,8) and ps[1]+j in range(0,8): 465 | L_possible_moves.append((ps[0]+i, ps[1]+j)) 466 | if self.name != 'BK' and self.name != 'WK': 467 | while (ps[0]+i, ps[1]+j) not in all_positions_own_team and (ps[0]+i, ps[1]+j) not in all_positions_enemy and ps[0]+i in range(0,8) and ps[1]+j in range(0,8): 468 | L_possible_moves.append((ps[0]+i, ps[1]+j)) 469 | i += 1 470 | j += 1 471 | if (ps[0]+i, ps[1]+j) in all_positions_enemy: 472 | L_possible_moves.append((ps[0]+i, ps[1]+j)) 473 | break 474 | i = 1 475 | j = 1 476 | if (ps[0]-i, ps[1]+j) not in all_positions_own_team and ps[0]-i in range(0,8) and ps[1]+j in range(0,8): 477 | L_possible_moves.append((ps[0]-i, ps[1]+j)) 478 | if self.name != 'BK' and self.name != 'WK': 479 | while (ps[0]-i, ps[1]+j) not in all_positions_own_team and (ps[0]-i, ps[1]+j) not in all_positions_enemy and ps[0]-i in range(0,8) and ps[1]+j in range(0,8): 480 | L_possible_moves.append((ps[0]-i, ps[1]+j)) 481 | i += 1 482 | j += 1 483 | if (ps[0]-i, ps[1]+j) in all_positions_enemy: 484 | L_possible_moves.append((ps[0]-i, ps[1]+j)) 485 | break 486 | i = 1 487 | j = 1 488 | if (ps[0]-i, ps[1]-j) not in all_positions_own_team and ps[0]-i in range(0,8) and ps[1]-j in range(0,8): 489 | L_possible_moves.append((ps[0]-i, ps[1]-j)) 490 | if self.name != 'BK' and self.name != 'WK': 491 | while (ps[0]-i, ps[1]-j) not in all_positions_own_team and (ps[0]-i, ps[1]-j) not in all_positions_enemy and ps[0]-i in range(0,8) and ps[1]-j in range(0,8): 492 | L_possible_moves.append((ps[0]-i, ps[1]-j)) 493 | i += 1 494 | j += 1 495 | if (ps[0]-i, ps[1]-j) in all_positions_enemy: 496 | L_possible_moves.append((ps[0]-i, ps[1]-j)) 497 | break 498 | i = 1 499 | j = 1 500 | if (ps[0]+i, ps[1]-j) not in all_positions_own_team and ps[0]+i in range(0,8) and ps[1]-j in range(0,8): #necessary for the king, but generates double positions 501 | L_possible_moves.append((ps[0]+i, ps[1]-j)) 502 | if self.name != 'BK' and self.name != 'WK': 503 | while (ps[0]+i, ps[1]-j) not in all_positions_own_team and (ps[0]+i, ps[1]-j) not in all_positions_enemy and ps[0]+i in range(0,8) and ps[1]-j in range(0,8): 504 | L_possible_moves.append((ps[0]+i, ps[1]-j)) 505 | i += 1 506 | j += 1 507 | if (ps[0]+i, ps[1]-j) in all_positions_enemy: 508 | L_possible_moves.append((ps[0]+i, ps[1]-j)) 509 | break 510 | if self.name != 'WK' and self.name != 'BK' and self.name != 'WD' and self.name != 'BD': #already emptied in Tower-Query for this 4 figures 511 | self.possible_moves = [] 512 | for i in L_possible_moves: 513 | self.possible_moves.append(i) 514 | self.possible_moves = list(set(self.possible_moves)) #sorts out doubles (will give unsorted list without doubles) 515 | self.possible_moves.sort() 516 | 517 | if self.name == 'WK' or self.name == 'BK': 518 | for i in all_possible_moves_enemy: 519 | self.possible_moves = [j for j in self.possible_moves if j not in i] 520 | 521 | if pe in self.possible_moves: 522 | return True 523 | else: 524 | return False 525 | 526 | B1 = Figure('WB', 'B1', 'WR', (6,0), [(5,0),(4,0)]) 527 | B2 = Figure('WB', 'B2', 'WR', (6,1), [(5,1),(4,1)]) 528 | B3 = Figure('WB', 'B3', 'WR', (6,2), [(5,2),(4,2)]) 529 | B4 = Figure('WB', 'B4', 'WR', (6,3), [(5,3),(4,3)]) 530 | B5 = Figure('WB', 'B5', 'WR', (6,4), [(5,4),(4,4)]) 531 | B6 = Figure('WB', 'B6', 'WR', (6,5), [(5,5),(4,5)]) 532 | B7 = Figure('WB', 'B7', 'WR', (6,6), [(5,6),(4,6)]) 533 | B8 = Figure('WB', 'B8', 'WR', (6,7), [(5,7),(4,7)]) 534 | T1 = Figure('WT', 'T1', 'WP', (7,0), []) 535 | T2 = Figure('WT', 'T2', 'WP', (7,7), []) 536 | S1 = Figure('WS', 'S1', 'WN', (7,1), [(5,0),(5,2)]) 537 | S2 = Figure('WS', 'S2', 'WN', (7,6), [(5,5),(5,7)]) 538 | L1 = Figure('WL', 'L1', 'WB', (7,2), []) 539 | L2 = Figure('WL', 'L2', 'WB', (7,5), []) 540 | D1 = Figure('WD', 'D1', 'WQ', (7,3), []) 541 | K1 = Figure('WK', 'K1', 'WK', (7,4), []) 542 | 543 | B9 = Figure('BB', 'B9', 'BR', (1,0), [(2,0),(3,0)]) 544 | B10 = Figure('BB', 'B10', 'BR', (1,1), [(2,1),(3,1)]) 545 | B11 = Figure('BB', 'B11', 'BR', (1,2), [(2,2),(3,2)]) 546 | B12 = Figure('BB', 'B12', 'BR', (1,3), [(2,3),(3,3)]) 547 | B13 = Figure('BB', 'B13', 'BR', (1,4), [(2,4),(3,4)]) 548 | B14 = Figure('BB', 'B14', 'BR', (1,5), [(2,5),(3,5)]) 549 | B15 = Figure('BB', 'B15', 'BR', (1,6), [(2,6),(3,6)]) 550 | B16 = Figure('BB', 'B16', 'BR', (1,7), [(2,7),(3,7)]) 551 | T3 = Figure('BT', 'T3', 'BP', (0,0), []) 552 | T4 = Figure('BT', 'T4', 'BP', (0,7), []) 553 | S3 = Figure('BS', 'S3', 'BN', (0,1), [(2,0),(2,2)]) 554 | S4 = Figure('BS', 'S4', 'BN', (0,6), [(2,5),(2,7)]) 555 | L3 = Figure('BL', 'L3', 'BB', (0,2), []) 556 | L4 = Figure('BL', 'L4', 'BB', (0,5), []) 557 | D2 = Figure('BD', 'D2', 'BQ', (0,3), []) 558 | K2 = Figure('BK', 'K2', 'BK', (0,4), []) 559 | 560 | run = True 561 | destroyed_unit = '' 562 | 563 | global turn 564 | turn = 'W' 565 | 566 | def main(): 567 | global error 568 | get_positions() 569 | destroyed_unit = '' 570 | start, end = checkinput() 571 | if(start, end) == (-1,-1): 572 | error = 1 573 | print('[0] invalid input') 574 | if(start, end) == (-2,-2): 575 | error = 2 576 | print('Game over.') 577 | print_board() 578 | 579 | 580 | def checkinput(): 581 | global turn, error 582 | 583 | colors_turn = 'white' if turn == 'W' else 'black' 584 | print(f'\nIts {colors_turn} turn. Your move:') 585 | print('start = ' + tkstart) 586 | print('end = ' + tkend) 587 | 588 | translate_a = {'a' : 0, 'b' : 1, 'c' : 2, 'd' : 3, 'e' : 4, 'f' : 5, 'g' : 6, 'h' : 7} 589 | translate_b = {'8' : 0, '7' : 1, '6' : 2, '5' : 3, '4' : 4, '3' : 5, '2' : 6, '1' : 7} 590 | a = tkstart[0] 591 | b = tkstart[1] 592 | d = tkend[0] 593 | e = tkend[1] 594 | 595 | start_position = (translate_b[b], translate_a[a]) 596 | end_position = (translate_b[e], translate_a[d]) 597 | if check_chosen_move(start_position, end_position): 598 | if turn == 'W': #whites turn 599 | #print('All possible Moves of White before: ' + str(possible_moves_white)) 600 | #print('All possible Moves of Black before: ' + str(possible_moves_black)) 601 | for i in white_players: 602 | if i.position == start_position: 603 | i.update_position(start_position, end_position, position_black_players, position_white_players, possible_moves_black, turn) 604 | for i in black_players: 605 | if i.position == end_position: 606 | destroyed_unit = i.object_name 607 | i.position = (-1,-1) 608 | get_positions() 609 | for i in black_players: #Update all possible moves of the enemy team 610 | i.update_possible_moves(position_white_players, position_black_players, possible_moves_white, turn) 611 | for i in white_players: #Update all possible moves of the own team 612 | i.update_possible_moves(position_black_players, position_white_players, possible_moves_black, turn) 613 | get_positions() 614 | for i in possible_moves_black: 615 | if position_king_white in i: #If the own king is check 616 | for i in white_players: 617 | if i.position == end_position: 618 | i.return_position(start_position, end_position, position_black_players, position_white_players, possible_moves_black, turn) 619 | for i in black_players: 620 | try: 621 | if i.object_name == destroyed_unit: 622 | i.position = end_position 623 | destroyed_unit = '' 624 | except: 625 | pass 626 | error = 1 627 | print('Not allowed to bring own King into chess position') 628 | return(0,0) 629 | #print('All possible Moves of White after: ' + str(possible_moves_white)) 630 | #print('All possible Moves of Black after: ' + str(possible_moves_black)) 631 | for i in possible_moves_white: 632 | if position_king_black in i: 633 | if K2.possible_moves != []: 634 | error = 3 635 | print('check!') 636 | else: 637 | can_reach_king = [] 638 | can_reach_king_position = [] 639 | helpfull_positions = [] 640 | who_can_help = {} #If the king is in danger, can he escape or can some figure of the own team come to help? 641 | for i in white_players: 642 | if position_king_black in i.possible_moves or possible_moves_black in i.possible_moves or possible_moves_black in i.position: #Can the enemy jump in between? 643 | possible_positions_to_cover = [] 644 | for m in possible_moves_black: 645 | for u in m: 646 | if u in i.possible_moves or u in i.position: 647 | possible_positions_to_cover.append(u) 648 | for k in possible_positions_to_cover: 649 | for n in black_players: 650 | if k in n.possible_moves: 651 | who_can_help[(n.object_name, k)] = 0 652 | current_object = n.object_name 653 | current_position = n.position 654 | n.update_position(n.position, k, position_black_players, position_white_players, possible_moves_black, turn) 655 | for q in white_players: 656 | if q.position == k: 657 | destroyed_unit = q.object_name 658 | q.position = (-1,-1) 659 | get_positions() 660 | for x in white_players: 661 | x.update_possible_moves(position_black_players, position_white_players, possible_moves_black, turn) 662 | for y in black_players: 663 | y.update_possible_moves(position_white_players, position_black_players, possible_moves_white, turn) 664 | get_positions() 665 | for p in possible_moves_white: 666 | if position_king_black in p: 667 | who_can_help[(n.object_name, k)] = 1 668 | n.return_position(current_position, k, position_black_players, position_white_players, possible_moves_black, turn) 669 | for h in white_players: 670 | try: 671 | if h.object_name == destroyed_unit: 672 | h.position = k 673 | destroyed_unit = '' 674 | except: 675 | pass 676 | get_positions() 677 | for z in white_players: 678 | z.update_possible_moves(position_black_players, position_white_players, possible_moves_black, turn) 679 | for w in black_players: 680 | w.update_possible_moves(position_white_players, position_black_players, possible_moves_white, turn) 681 | get_positions() 682 | for key, item in who_can_help.items(): 683 | if item == 0: 684 | helpfull_positions.append(key[1]) 685 | if helpfull_positions != []: 686 | error = 3 687 | print('check!') 688 | pass 689 | else: 690 | for i in white_players: 691 | if i.position == start_position: 692 | i.update_position(start_position, end_position, position_black_players, position_white_players, possible_moves_black, turn) 693 | for i in black_players: 694 | if i.position == end_position: 695 | destroyed_unit = i.object_name 696 | i.position = (-1,-1) 697 | get_positions() 698 | print_board() 699 | print('check mate!') 700 | return(-2,-2) 701 | turn = 'B' 702 | else: #blacks turn 703 | #print('All possible Moves of White before: ' + str(possible_moves_white)) 704 | #print('All possible Moves of Black before: ' + str(possible_moves_black)) 705 | for i in black_players: 706 | if i.position == start_position: 707 | i.update_position(start_position, end_position, position_white_players, position_black_players, possible_moves_white, turn) 708 | for i in white_players: 709 | if i.position == end_position: 710 | destroyed_unit = i.object_name 711 | i.position = (-1,-1) 712 | get_positions() 713 | for i in black_players: 714 | i.update_possible_moves(position_white_players, position_black_players, possible_moves_white, turn) 715 | for i in white_players: 716 | i.update_possible_moves(position_black_players, position_white_players, possible_moves_black, turn) 717 | get_positions() 718 | for i in possible_moves_white: 719 | if position_king_black in i: 720 | for i in black_players: 721 | if i.position == end_position: 722 | i.return_position(start_position, end_position, position_white_players, position_black_players, possible_moves_white, turn) 723 | for i in white_players: 724 | try: 725 | if i.object_name == destroyed_unit: 726 | i.position = end_position 727 | destroyed_unit = '' 728 | except: 729 | pass 730 | error = 1 731 | print('Not allowed to bring own King into chess position') 732 | return(0,0) 733 | #print('All possible Moves of White after: ' + str(possible_moves_white)) 734 | #print('All possible Moves of Black after: ' + str(possible_moves_black)) 735 | for i in possible_moves_black: 736 | if position_king_white in i: 737 | if K1.possible_moves != []: 738 | error = 3 739 | print('check!') 740 | else: 741 | can_reach_king = [] 742 | can_reach_king_position = [] 743 | helpfull_positions = [] 744 | who_can_help = {} 745 | for i in black_players: 746 | if position_king_white in i.possible_moves or possible_moves_white in i.possible_moves or possible_moves_white in i.position: #Can the enemy jump in between? 747 | possible_positions_to_cover = [] 748 | for m in possible_moves_white: 749 | for u in m: 750 | if u in i.possible_moves or u in i.position: 751 | possible_positions_to_cover.append(u) 752 | for k in possible_positions_to_cover: # Würde der Zug in dieses Feld den König aus dem Schach befreien? 753 | for n in white_players: 754 | if k in n.possible_moves: 755 | who_can_help[(n.object_name, k)] = 0 756 | current_object = n.object_name 757 | current_position = n.position 758 | n.update_position(n.position, k, position_white_players, position_black_players, possible_moves_white, turn) 759 | for q in black_players: 760 | if q.position == k: 761 | destroyed_unit = q.object_name 762 | q.position = (-1,-1) 763 | get_positions() 764 | for x in white_players: 765 | x.update_possible_moves(position_black_players, position_white_players, possible_moves_black, turn) 766 | for y in black_players: 767 | y.update_possible_moves(position_white_players, position_black_players, possible_moves_white, turn) 768 | get_positions() 769 | 770 | for p in possible_moves_black: 771 | if position_king_white in p: 772 | who_can_help[(n.object_name, k)] = 1 773 | n.return_position(current_position, k, position_white_players, position_black_players, possible_moves_white, turn) 774 | for h in black_players: 775 | try: 776 | if h.object_name == destroyed_unit: 777 | h.position = k 778 | destroyed_unit = '' 779 | except: 780 | pass 781 | get_positions() 782 | for z in white_players: 783 | z.update_possible_moves(position_black_players, position_white_players, possible_moves_black, turn) 784 | for w in black_players: 785 | w.update_possible_moves(position_white_players, position_black_players, possible_moves_white, turn) 786 | get_positions() 787 | for key, item in who_can_help.items(): 788 | if item == 0: 789 | helpfull_positions.append(key[1]) 790 | if helpfull_positions != []: 791 | error = 3 792 | print('check!') 793 | pass 794 | else: 795 | for i in black_players: 796 | if i.position == start_position: 797 | i.update_position(start_position, end_position, position_white_players, position_black_players, possible_moves_white, turn) 798 | for i in white_players: 799 | if i.position == end_position: 800 | destroyed_unit = i.object_name 801 | i.position = (-1,-1) 802 | get_positions() 803 | print_board() 804 | print('check mate!') 805 | return(-2,-2) 806 | turn = 'W' 807 | return(0,0) 808 | else: 809 | return(0,0) 810 | 811 | 812 | def check_chosen_move(ps, pe): 813 | global error 814 | if turn == 'W' and ps in position_white_players: 815 | try: 816 | for i in white_players: 817 | if i.position == ps: 818 | if i.check_if_move_legit(ps, pe, position_black_players, position_white_players, possible_moves_black, turn): 819 | return True 820 | else: 821 | error = 1 822 | print('move not legit') 823 | return False 824 | except: 825 | print('Choose a legit player') 826 | return False 827 | elif turn == 'B' and ps in position_black_players: 828 | try: 829 | for i in black_players: 830 | if i.position == ps: 831 | if i.check_if_move_legit(ps, pe, position_white_players, position_black_players, possible_moves_white, turn): 832 | return True 833 | else: 834 | error = 1 835 | print('move not legit') 836 | return False 837 | except: 838 | error = 1 839 | print('Choose a legit player') 840 | return False 841 | else: 842 | error = 1 843 | print(f'Its {turn} turn, pick a valid figure!') 844 | return False 845 | 846 | 847 | def print_board(): 848 | board_side = [' 8 \u2502',' 7 \u2502',' 6 \u2502',' 5 \u2502',' 4 \u2502',' 3 \u2502',' 2 \u2502',' 1 \u2502'] 849 | print('\n\n a b c d e f g h' + '\n \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n \u2502 \u2502') 850 | k = 8 851 | for i in range(8): 852 | print(board_side[i], end = ' ') 853 | for j in range(8): 854 | if (i,j) in position_white_players or (i,j) in position_black_players: 855 | for x in white_players: 856 | if x.position == (i,j): 857 | print(x.color, end = ' ') 858 | for x in black_players: 859 | if x.position == (i,j): 860 | print(x.color, end = ' ') 861 | else: 862 | print(' \u2022 ', end = ' ') 863 | print(' \u2502 ' + str(k) + '\n \u2502 \u2502') 864 | k -= 1 865 | print(' \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n a b c d e f g h' + '\n') 866 | 867 | def get_positions(): 868 | global white_players, position_white_players, possible_moves_white, position_king_white, black_players, position_black_players, possible_moves_black, position_king_black 869 | white_players = [B1,B2,B3,B4,B5,B6,B7,B8,T1,T2,S1,S2,L1,L2,D1,K1] 870 | position_white_players = [i.position for i in white_players] 871 | possible_moves_white = [i.possible_moves for i in white_players] 872 | position_king_white = K1.position 873 | black_players = [B9,B10,B11,B12,B13,B14,B15,B16,T3,T4,S3,S4,L3,L4,D2,K2] 874 | position_black_players = [i.position for i in black_players] 875 | possible_moves_black = [i.possible_moves for i in black_players] 876 | position_king_black = K2.position 877 | return True 878 | 879 | 880 | tk.mainloop() 881 | 882 | 883 | 884 | 885 | -------------------------------------------------------------------------------- /chess_v2.py: -------------------------------------------------------------------------------- 1 | class Board(object): 2 | def __init__(self): 3 | self.Fig_Pos = { 4 | (0,0) : Rook(0, (0,0), []), 5 | (0,1) : Knight(0, (0,1), []), 6 | (0,2) : Bishop(0, (0,2), []), 7 | (0,3) : Queen(0, (0,3), []), 8 | (0,4) : King(0, (0,4), []), 9 | (0,5) : Bishop(0, (0,5), []), 10 | (0,6) : Knight(0, (0,6), []), 11 | (0,7) : Rook(0, (0,7), []), 12 | (1,0) : Pawn(0, (1,0), []), 13 | (1,1) : Pawn(0, (1,1), []), 14 | (1,2) : Pawn(0, (1,2), []), 15 | (1,3) : Pawn(0, (1,3), []), 16 | (1,4) : Pawn(0, (1,4), []), 17 | (1,5) : Pawn(0, (1,5), []), 18 | (1,6) : Pawn(0, (1,6), []), 19 | (1,7) : Pawn(0, (1,7), []), 20 | (7,0) : Rook(1, (7,0), []), 21 | (7,1) : Knight(1, (7,1), []), 22 | (7,2) : Bishop(1, (7,2), []), 23 | (7,3) : Queen(1, (7,3), []), 24 | (7,4) : King(1, (7,4), []), 25 | (7,5) : Bishop(1, (7,5), []), 26 | (7,6) : Knight(1, (7,6), []), 27 | (7,7) : Rook(1, (7,7), []), 28 | (6,0) : Pawn(1, (6,0), []), 29 | (6,1) : Pawn(1, (6,1), []), 30 | (6,2) : Pawn(1, (6,2), []), 31 | (6,3) : Pawn(1, (6,3), []), 32 | (6,4) : Pawn(1, (6,4), []), 33 | (6,5) : Pawn(1, (6,5), []), 34 | (6,6) : Pawn(1, (6,6), []), 35 | (6,7) : Pawn(1, (6,7), []) 36 | } 37 | 38 | def draw_Board(self): 39 | board_side = [' 8 \u2502',' 7 \u2502',' 6 \u2502',' 5 \u2502',' 4 \u2502',' 3 \u2502',' 2 \u2502',' 1 \u2502'] 40 | print('\n\n a b c d e f g h' + '\n \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n \u2502 \u2502') 41 | k = 8 42 | for i in range(8): 43 | print(board_side[i], end = ' ') 44 | for j in range(8): 45 | if (i,j) in self.Fig_Pos: 46 | print(self.Fig_Pos[(i,j)]._figure[0] + str(self.Fig_Pos[(i,j)]._color), end = ' ') 47 | else: 48 | print(' \u2022 ', end = ' ') 49 | print(' \u2502 ' + str(k) + '\n \u2502 \u2502') 50 | k -= 1 51 | print(' \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n a b c d e f g h' + '\n') 52 | 53 | def get_positions(self): 54 | return self.Fig_Pos 55 | 56 | def update_positions(self, old_position, new_position): 57 | self.Fig_Pos[new_position] = self.Fig_Pos[old_position] 58 | del self.Fig_Pos[old_position] 59 | 60 | def update_position_rochade(self, old_position, new_position): 61 | p1 = self.Fig_Pos[old_position] 62 | p2 = self.Fig_Pos[new_position] 63 | 64 | self.Fig_Pos[old_position] = p2 65 | self.Fig_Pos[new_position] = p1 66 | 67 | 68 | 69 | class Figure(object): 70 | def __init__(self, color, position, poss_moves): 71 | self._color = color 72 | self.position = position 73 | self.poss_moves = poss_moves 74 | 75 | def get_poss_moves(self): 76 | return self.poss_moves 77 | 78 | def get_position(self): 79 | return self.position 80 | 81 | def get_color(self): 82 | return self._color 83 | 84 | 85 | class King(Figure): 86 | def __init__(self, color, position, poss_moves, figure = 'King'): 87 | Figure.__init__(self, color, position, poss_moves) 88 | self._figure = figure 89 | self.already_moved = False 90 | 91 | def get_sourrounding(self): 92 | i,j = self.position[0], self.position[1] 93 | sur = [(i-1, j-1), (i-1, j), (i-1, j+1), (i, j-1), (i, j+1), (i+1, j-1), (i+1, j), (i+1, j+1)] 94 | sur = [(i[0], i[1]) for i in sur if -1 < i[0] < 8 and -1 < i[1] < 8] 95 | return sur 96 | 97 | def check_move(self, end_position): 98 | if not self.poss_moves: 99 | return False 100 | 101 | cur_position = self.get_position() 102 | cur_Board = Chess_Board.get_positions() 103 | sur = self.get_sourrounding() 104 | 105 | if end_position not in sur: 106 | return False 107 | 108 | if end_position not in cur_Board: 109 | return True 110 | 111 | if end_position in cur_Board and cur_Board[end_position]._color != self._color: 112 | return True 113 | 114 | return False 115 | 116 | def update_current_pos(self, new_position): 117 | self.position = new_position 118 | 119 | def update_poss_moves(self): 120 | sur = self.get_sourrounding() 121 | cur_Board = Chess_Board.get_positions() 122 | self.poss_moves = [(i[0], i[1]) for i in sur if i not in cur_Board or Chess_Board.Fig_Pos[(i[0], i[1])]._color != self._color] 123 | 124 | 125 | class Queen(Figure): 126 | def __init__(self, color, position, poss_moves, figure = 'Queen'): 127 | Figure.__init__(self, color, position, poss_moves) 128 | self._figure = figure 129 | 130 | def get_sourrounding(self): 131 | cur_Board = Chess_Board.get_positions() 132 | i,j = self.position[0], self.position[1] 133 | sur = [] 134 | for count, m in enumerate([(i+1, 8, 1), (i-1, -1, -1), (j+1, 8, 1), (j-1, -1, -1)]): 135 | for k in range(m[0], m[1], m[2]): 136 | udrl = (k,j) if count < 2 else (i,k) 137 | if udrl not in cur_Board: 138 | sur.append(udrl) 139 | elif udrl in cur_Board and Chess_Board.Fig_Pos[udrl]._color != self._color: 140 | sur.append(udrl) 141 | break 142 | else: 143 | break 144 | 145 | for pos in ([1, 1], [1, -1], [-1, 1], [-1, -1]): 146 | i_m = pos[0] 147 | j_m = pos[1] 148 | while -1 < i+i_m < 8 and -1 < j+j_m < 8: 149 | if (i+i_m, j+j_m) in cur_Board and Chess_Board.Fig_Pos[(i+i_m, j+j_m)]._color != self._color: 150 | sur.append((i+i_m, j+j_m)) 151 | break 152 | elif (i+i_m, j+j_m) in cur_Board and Chess_Board.Fig_Pos[(i+i_m, j+j_m)]._color == self._color: 153 | break 154 | else: 155 | sur.append((i+i_m, j+j_m)) 156 | i_m += pos[0] 157 | j_m += pos[1] 158 | return sur 159 | 160 | def update_poss_moves(self): 161 | self.poss_moves = self.get_sourrounding() 162 | 163 | 164 | class Bishop(Figure): 165 | def __init__(self, color, position, poss_moves, figure = 'Bishop'): 166 | Figure.__init__(self, color, position, poss_moves) 167 | self._figure = figure 168 | 169 | def get_sourrounding(self): 170 | cur_Board = Chess_Board.get_positions() 171 | i,j = self.position[0], self.position[1] 172 | sur = [] 173 | for pos in ([1, 1], [1, -1], [-1, 1], [-1, -1]): 174 | i_m = pos[0] 175 | j_m = pos[1] 176 | while -1 < i+i_m < 8 and -1 < j+j_m < 8: 177 | if (i+i_m, j+j_m) in cur_Board and Chess_Board.Fig_Pos[(i+i_m, j+j_m)]._color != self._color: 178 | sur.append((i+i_m, j+j_m)) 179 | break 180 | elif (i+i_m, j+j_m) in cur_Board and Chess_Board.Fig_Pos[(i+i_m, j+j_m)]._color == self._color: 181 | break 182 | else: 183 | sur.append((i+i_m, j+j_m)) 184 | i_m += pos[0] 185 | j_m += pos[1] 186 | return sur 187 | 188 | def update_poss_moves(self): 189 | self.poss_moves = self.get_sourrounding() 190 | 191 | 192 | class Rook(Figure): 193 | def __init__(self, color, position, poss_moves, figure = 'Rook'): 194 | Figure.__init__(self, color, position, poss_moves) 195 | self._figure = figure 196 | self.already_moved = False 197 | 198 | def get_sourrounding(self): 199 | cur_Board = Chess_Board.get_positions() 200 | i,j = self.position[0], self.position[1] 201 | sur = [] 202 | for count, m in enumerate([(i+1, 8, 1), (i-1, -1, -1), (j+1, 8, 1), (j-1, -1, -1)]): 203 | for k in range(m[0], m[1], m[2]): 204 | udrl = (k,j) if count < 2 else (i,k) 205 | if udrl not in cur_Board: 206 | sur.append(udrl) 207 | elif udrl in cur_Board and Chess_Board.Fig_Pos[udrl]._color != self._color: 208 | sur.append(udrl) 209 | break 210 | else: 211 | break 212 | return sur 213 | 214 | def update_poss_moves(self): 215 | self.poss_moves = self.get_sourrounding() 216 | 217 | 218 | class Knight(Figure): 219 | def __init__(self, color, position, poss_moves, figure = 'Knight'): 220 | Figure.__init__(self, color, position, poss_moves) 221 | self._figure = figure 222 | 223 | def get_sourrounding(self): 224 | sur = [] 225 | cur_Board = Chess_Board.get_positions() 226 | i = self.position[0] 227 | j = self.position[1] 228 | 229 | for posspos in [(i-2, j-1), (i-2, j+1), (i-1, j-2), (i-1, j+2), (i+2, j-1), (i+2, j+1), (i+1, j-2), (i+1, j+2)]: 230 | i_k = posspos[0] 231 | j_k = posspos[1] 232 | if 0 <= i_k < 8 and 0 <= j_k < 8 and (i_k, j_k) not in cur_Board: 233 | sur.append((i_k, j_k)) 234 | if 0 <= i_k < 8 and 0 <= j_k < 8 and (i_k, j_k) in cur_Board and Chess_Board.Fig_Pos[(i_k, j_k)]._color != self._color: 235 | sur.append((i_k, j_k)) 236 | return sur 237 | 238 | def update_poss_moves(self): 239 | self.poss_moves = self.get_sourrounding() 240 | 241 | 242 | class Pawn(Figure): 243 | def __init__(self, color, position, poss_moves, figure = 'Pawn'): 244 | Figure.__init__(self, color, position, poss_moves) 245 | self._figure = figure 246 | 247 | def get_sourrounding(self): 248 | sur = [] 249 | cur_Board = Chess_Board.get_positions() 250 | i = self.position[0] 251 | j = self.position[1] 252 | 253 | if self._color == 0: 254 | if (i+1, j) not in cur_Board: 255 | sur.append((i+1, j)) 256 | if i == 1 and (i+2, j) not in cur_Board: 257 | sur.append((i+2, j)) 258 | if (i+1, j+1) in cur_Board and cur_Board[(i+1, j+1)]._color != 0: 259 | sur.append((i+1, j+1)) 260 | if (i+1, j-1) in cur_Board and cur_Board[(i+1, j-1)]._color != 0: 261 | sur.append((i+1, j-1)) 262 | else: 263 | if (i-1, j) not in cur_Board: 264 | sur.append((i-1, j)) 265 | if i == 6 and (i-2, j) not in cur_Board: 266 | sur.append((i-2, j)) 267 | if (i-1, j+1) in cur_Board and cur_Board[(i-1, j+1)]._color != 1: 268 | sur.append((i-1, j+1)) 269 | if (i-1, j-1) in cur_Board and cur_Board[(i-1, j-1)]._color != 1: 270 | sur.append((i-1, j-1)) 271 | return sur 272 | 273 | def update_poss_moves(self): 274 | self.poss_moves = self.get_sourrounding() 275 | 276 | 277 | def check_if_move_is_possible(start_position, end_position): 278 | print('first check...') 279 | cur_Board = Chess_Board.get_positions() 280 | print(Chess_Board.Fig_Pos[(start_position)]._color, Chess_Board.Fig_Pos[(start_position)]._figure, Chess_Board.Fig_Pos[(start_position)].poss_moves) 281 | if end_position in Chess_Board.Fig_Pos[(start_position)].poss_moves: 282 | return True 283 | else: 284 | return False 285 | 286 | 287 | def check_if_own_king_in_danger(start_position, end_position): 288 | print('second check...') 289 | current_color = Chess_Board.Fig_Pos[start_position]._color 290 | position_own_king = () 291 | 292 | Chess_Board.Fig_Pos[start_position].position = end_position 293 | Chess_Board.update_positions(start_position, end_position) 294 | 295 | for to_update in Chess_Board.Fig_Pos: 296 | Chess_Board.Fig_Pos[to_update].update_poss_moves() 297 | if Chess_Board.Fig_Pos[to_update]._figure == 'King' and Chess_Board.Fig_Pos[to_update]._color == current_color: 298 | position_own_king = Chess_Board.Fig_Pos[to_update].position 299 | 300 | print('test6') 301 | for figures in Chess_Board.Fig_Pos: 302 | if Chess_Board.Fig_Pos[figures]._color != current_color and position_own_king in Chess_Board.Fig_Pos[figures].poss_moves: 303 | print('Move not possible. Own King would be in chess.') 304 | Chess_Board.Fig_Pos[end_position].position = start_position 305 | Chess_Board.update_positions(end_position, start_position) 306 | for to_update in Chess_Board.Fig_Pos: 307 | Chess_Board.Fig_Pos[to_update].update_poss_moves() 308 | return False 309 | print('test7') 310 | return True 311 | 312 | 313 | def check_if_checkmate(current_color, start_position, end_position): 314 | enemy_king_pos = () 315 | 316 | for find_king in Chess_Board.Fig_Pos: 317 | if Chess_Board.Fig_Pos[find_king]._figure == 'King' and Chess_Board.Fig_Pos[find_king]._color != current_color: 318 | enemy_king_pos = Chess_Board.Fig_Pos[find_king].position 319 | 320 | who_attacks = [] 321 | attackers_moves = [] 322 | 323 | print('test9') 324 | for find_attacker in Chess_Board.Fig_Pos: 325 | if Chess_Board.Fig_Pos[find_attacker]._color == current_color and enemy_king_pos in Chess_Board.Fig_Pos[find_attacker].poss_moves: 326 | who_attacks.append(Chess_Board.Fig_Pos[find_attacker].position) 327 | attackers_moves.extend(Chess_Board.Fig_Pos[find_attacker].poss_moves) 328 | print('test10') 329 | if not who_attacks: 330 | print('test11') 331 | return True 332 | 333 | for find_helpers in Chess_Board.Fig_Pos: 334 | if Chess_Board.Fig_Pos[find_helpers]._color != current_color and Chess_Board.Fig_Pos[find_helpers]._figure != 'King': 335 | for helpers_poss_move in Chess_Board.Fig_Pos[find_helpers].poss_moves: 336 | if helpers_poss_move in attackers_moves: 337 | print('poss_helper:', Chess_Board.Fig_Pos[find_helpers].position) 338 | old_position = Chess_Board.Fig_Pos[find_helpers].position 339 | new_position = helpers_poss_move 340 | 341 | Chess_Board.Fig_Pos[old_position].position = new_position 342 | Chess_Board.update_positions(old_position, new_position) 343 | 344 | for to_update in Chess_Board.Fig_Pos: 345 | Chess_Board.Fig_Pos[to_update].update_poss_moves() 346 | print(enemy_king_pos) 347 | print(Chess_Board.Fig_Pos[end_position].poss_moves) 348 | if enemy_king_pos not in Chess_Board.Fig_Pos[end_position].poss_moves: 349 | print(f'helper is: {Chess_Board.Fig_Pos[new_position]._figure}') 350 | print('check!') 351 | Chess_Board.Fig_Pos[new_position].position = old_position 352 | Chess_Board.update_positions(new_position, old_position) 353 | 354 | for to_update in Chess_Board.Fig_Pos: 355 | Chess_Board.Fig_Pos[to_update].update_poss_moves() 356 | return True 357 | 358 | Chess_Board.Fig_Pos[new_position].position = old_position 359 | Chess_Board.update_positions(new_position, old_position) 360 | 361 | for to_update in Chess_Board.Fig_Pos: 362 | Chess_Board.Fig_Pos[to_update].update_poss_moves() 363 | 364 | 365 | 366 | for king_move in Chess_Board.Fig_Pos[enemy_king_pos].poss_moves: 367 | if king_move not in attackers_moves: 368 | print('check!') 369 | return True 370 | 371 | print('check mate!') 372 | global playing 373 | playing = False 374 | return False 375 | 376 | 377 | def check_rochade(current_color, start_position, end_position): 378 | print('test') 379 | 380 | cur_Board = Chess_Board.get_positions() 381 | if start_position not in cur_Board or end_position not in cur_Board: 382 | return False 383 | p1 = cur_Board[start_position] 384 | p2 = cur_Board[end_position] 385 | p1_position = p1.position 386 | p2_position = p2.position 387 | 388 | if p1._color != p2._color or p1._figure not in ('King', 'Rook') or p2._figure not in ('King', 'Rook'): 389 | return False 390 | 391 | if p1.already_moved or p2.already_moved: 392 | print('Figures already moved.') 393 | return False 394 | 395 | print('test2') 396 | pos_between = [] 397 | if p1.position == (0,0) or p2.position == (0,0): 398 | pos_between = [(0,1), (0,2), (0,3)] 399 | elif p1.position == (0,7) or p2.position == (0,7): 400 | pos_between = [(0,5), (0,6)] 401 | elif p1.position == (7,0) or p2.position == (7,0): 402 | pos_between = [(7,1), (7,2), (7,3)] 403 | elif p1.position == (7,7) or p2.position == (7,7): 404 | pos_between = [(7,5), (7,6)] 405 | 406 | print(pos_between) 407 | for check_poss_attack in Chess_Board.Fig_Pos: 408 | if Chess_Board.Fig_Pos[check_poss_attack]._color != current_color: 409 | for positions_between in pos_between: 410 | if positions_between in Chess_Board.Fig_Pos[check_poss_attack].poss_moves: 411 | print('test5') 412 | return False 413 | 414 | print('test3') 415 | Chess_Board.Fig_Pos[start_position].position = end_position 416 | Chess_Board.Fig_Pos[end_position].position = start_position 417 | Chess_Board.update_position_rochade(start_position, end_position) 418 | 419 | for to_update in Chess_Board.Fig_Pos: 420 | Chess_Board.Fig_Pos[to_update].update_poss_moves() 421 | 422 | position_own_king = () 423 | for to_update in Chess_Board.Fig_Pos: 424 | if Chess_Board.Fig_Pos[to_update]._figure == 'King' and Chess_Board.Fig_Pos[to_update]._color == current_color: 425 | position_own_king = Chess_Board.Fig_Pos[to_update].position 426 | 427 | for figures in Chess_Board.Fig_Pos: 428 | if Chess_Board.Fig_Pos[figures]._color != current_color and position_own_king in Chess_Board.Fig_Pos[figures].poss_moves: 429 | Chess_Board.Fig_Pos[end_position].position = start_position 430 | Chess_Board.Fig_Pos[start_position].position = end_position 431 | Chess_Board.update_position_rochade(end_position, start_position) 432 | 433 | for to_update in Chess_Board.Fig_Pos: 434 | Chess_Board.Fig_Pos[to_update].update_poss_moves() 435 | return False 436 | p1.already_moved = True 437 | p2.already_moved = True 438 | return True 439 | 440 | def process_input(current_color, start_position, end_position): 441 | print('process_input...', current_color, start_position, end_position) 442 | cur_Board = Chess_Board.get_positions() 443 | print() 444 | if start_position not in cur_Board: 445 | print('Empty field chosen.') 446 | return False 447 | 448 | if current_color != Chess_Board.Fig_Pos[start_position]._color: 449 | colors_turn = 'white' if current_color == 1 else 'black' 450 | print(f'It´s {colors_turn} turn.') 451 | return False 452 | 453 | print('comes to check...') 454 | 455 | if check_rochade(current_color, start_position, end_position): 456 | return True 457 | else: 458 | pass 459 | 460 | if check_if_move_is_possible(start_position, end_position) and check_if_own_king_in_danger(start_position, end_position) and check_if_checkmate(current_color, start_position, end_position): 461 | print('test12') 462 | print(Chess_Board.Fig_Pos[(end_position)]._figure) 463 | print(Chess_Board.Fig_Pos[(end_position)].position) 464 | print(Chess_Board.Fig_Pos[(end_position)].poss_moves) 465 | if Chess_Board.Fig_Pos[(end_position)]._figure in ('King', 'Rook'): 466 | print('test13') 467 | Chess_Board.Fig_Pos[(end_position)].already_moved = True 468 | return True 469 | else: 470 | return False 471 | 472 | 473 | def play_game(): 474 | global playing 475 | playing = True 476 | current_color = 1 477 | while playing: 478 | colors_turn = 'white' if current_color == 1 else 'black' 479 | print(f'\nIts {colors_turn} turn. Your move: ') 480 | try: 481 | a,b,c,d,e = input() 482 | if a+b+c+d+e == 'exit!': 483 | playing = False 484 | return 485 | alphabet = ['a','b','c','d','e','f','g','h'] 486 | numbers = [1,2,3,4,5,6,7,8] 487 | if str(a) in alphabet and int(b) in numbers and c == ' ' and str(d) in alphabet and int(e) in numbers: 488 | translate_a = {'a' : 0, 'b' : 1, 'c' : 2, 'd' : 3, 'e' : 4, 'f' : 5, 'g' : 6, 'h' : 7} 489 | translate_b = {'8' : 0, '7' : 1, '6' : 2, '5' : 3, '4' : 4, '3' : 5, '2' : 6, '1' : 7} 490 | start_position = (translate_b[b], translate_a[a]) 491 | end_position = (translate_b[e], translate_a[d]) 492 | 493 | if process_input(current_color, start_position, end_position): 494 | current_color = 0 if current_color == 1 else 1 495 | Chess_Board.draw_Board() 496 | 497 | except: 498 | print('input not valid!') 499 | 500 | 501 | 502 | if __name__ == '__main__': 503 | Chess_Board = Board() 504 | print('Welcome to this Chess Game. Have fun!') 505 | Chess_Board.draw_Board() 506 | for to_update in Chess_Board.Fig_Pos: 507 | Chess_Board.Fig_Pos[to_update].update_poss_moves() 508 | play_game() -------------------------------------------------------------------------------- /chess_v2_GUI.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | import tkinter.messagebox 3 | 4 | 5 | 6 | ###------------------------------------------------------------ GUI ------------------------------------------------------------### 7 | 8 | 9 | tk = Tk() 10 | tk.title('Chess 2.0') 11 | tk.configure(background='white') 12 | game_started = False 13 | tkstart = () 14 | tkend = () 15 | startbutton = '' 16 | endbutton = '' 17 | endbutton_color = '' 18 | startbutton_color = '' 19 | error = -1 20 | last_movement = () 21 | 22 | 23 | def restart_game(): 24 | main() 25 | undo_coloring(restartgame = True) 26 | 27 | history_gui.configure(state='normal') 28 | history_gui.delete('1.0' ,END) 29 | history_gui.configure(state='disabled') 30 | 31 | labelZahl1['text'] = "White's turn" 32 | 33 | black_fig = [BR, BN, BB, BQ, BK, BB, BN, BR, BP, BP, BP, BP, BP, BP, BP, BP] 34 | white_fig = [WP, WP, WP, WP, WP, WP, WP, WP, WR, WN, WB, WQ, WK, WB, WN, WR] 35 | board_fig = black_fig + ['' for i in range(32)] + white_fig 36 | 37 | for count, i in enumerate(fields_dic): 38 | fields_dic[i]['text'] = board_fig[count] 39 | 40 | 41 | def undo_coloring(restartgame = False): 42 | positions = [i for i in fields_dic] 43 | white = [0,2,4,6,9,11,13,15,16,18,20,22,25,27,29,31,32,34,36,38,41,43,45,47,48,50,52,54,57,59,61,63] 44 | black = [i for i in range(63) if i not in white] 45 | 46 | for i in white: 47 | fields_dic[positions[i]]['bg'] = 'white' 48 | for i in black: 49 | fields_dic[positions[i]]['bg'] = 'grey' 50 | 51 | if last_movement and not restartgame: 52 | fields_dic[last_movement[0]]['bg'] = '#ccffcc' 53 | fields_dic[last_movement[1]]['bg'] = '#ccffcc' 54 | 55 | for i in fields_dic: 56 | if fields_dic[i]['fg'] == 'lightblue': 57 | fields_dic[i]['fg'] = 'black' 58 | if fields_dic[i]['text'] == '\u0E4F': 59 | fields_dic[i]['text'] = '' 60 | if 'check' in fields_dic[i]['text']: 61 | if not history: 62 | fields_dic[i]['text'] = fields_dic[i]['text'][:1] 63 | elif not history[-1][-1] and not check_mate: 64 | fields_dic[i]['text'] = fields_dic[i]['text'][:1] 65 | 66 | 67 | def display_moves(coord): 68 | global fields_dic 69 | 70 | if Chess_Board.Fig_Pos[coord]._color == current_color: 71 | for fields in Chess_Board.Fig_Pos[coord].poss_moves: 72 | if fields_dic[fields]['text'] == '': 73 | fields_dic[fields]['fg'] = 'lightblue' 74 | fields_dic[fields]['text'] = '\u0E4F' 75 | else: 76 | fields_dic[fields]['fg'] = 'lightblue' 77 | 78 | 79 | def enemy_king_position(current_color): 80 | enemy_king_pos = () 81 | for find_king_pos in Chess_Board.Fig_Pos: 82 | if Chess_Board.Fig_Pos[find_king_pos]._color == current_color and Chess_Board.Fig_Pos[find_king_pos]._figure == 'King': 83 | enemy_king_pos = Chess_Board.Fig_Pos[find_king_pos].position 84 | break 85 | return enemy_king_pos 86 | 87 | def btnClick(id): 88 | global game_started, tkstart, tkend, error, fields_dic, startbutton_color, endbutton_color, is_check, check_mate, playing, last_movement 89 | 90 | fields_dic = {(0,0):a8, (0,1):b8, (0,2):c8, (0,3):d8, (0,4):e8, (0,5):f8, (0,6):g8, (0,7):h8, 91 | (1,0):a7, (1,1):b7, (1,2):c7, (1,3):d7, (1,4):e7, (1,5):f7, (1,6):g7, (1,7):h7, 92 | (2,0):a6, (2,1):b6, (2,2):c6, (2,3):d6, (2,4):e6, (2,5):f6, (2,6):g6, (2,7):h6, 93 | (3,0):a5, (3,1):b5, (3,2):c5, (3,3):d5, (3,4):e5, (3,5):f5, (3,6):g5, (3,7):h5, 94 | (4,0):a4, (4,1):b4, (4,2):c4, (4,3):d4, (4,4):e4, (4,5):f4, (4,6):g4, (4,7):h4, 95 | (5,0):a3, (5,1):b3, (5,2):c3, (5,3):d3, (5,4):e3, (5,5):f3, (5,6):g3, (5,7):h3, 96 | (6,0):a2, (6,1):b2, (6,2):c2, (6,3):d2, (6,4):e2, (6,5):f2, (6,6):g2, (6,7):h2, 97 | (7,0):a1, (7,1):b1, (7,2):c1, (7,3):d1, (7,4):e1, (7,5):f1, (7,6):g1, (7,7):h1} 98 | 99 | if not game_started: 100 | main() 101 | game_started = True 102 | 103 | if id == tkstart: 104 | undo_coloring() 105 | startbutton_color = '' 106 | endbutton_color = '' 107 | tkstart = () 108 | tkend = () 109 | 110 | elif tkstart != (): 111 | tkend = id 112 | if tkend in Chess_Board.Fig_Pos and Chess_Board.Fig_Pos[tkend]._color == current_color: 113 | con = True 114 | if tkstart in Chess_Board.Fig_Pos: #Rochade 115 | if (Chess_Board.Fig_Pos[tkstart]._figure == 'King' and Chess_Board.Fig_Pos[tkend]._figure == 'Rook') or (Chess_Board.Fig_Pos[tkstart]._figure == 'Rook' and Chess_Board.Fig_Pos[tkend]._figure == 'King'): 116 | con = False 117 | if con: 118 | fields_dic[tkstart]['bg'] = startbutton_color 119 | undo_coloring() 120 | tkstart = tkend 121 | startbutton_color = fields_dic[tkstart]['bg'] 122 | fields_dic[tkstart]['bg'] = 'lightblue' 123 | display_moves(tkstart) 124 | tkend = () 125 | if tkstart not in Chess_Board.Fig_Pos and tkend not in Chess_Board.Fig_Pos: 126 | tkstart = () 127 | tkend = () 128 | 129 | else: 130 | tkstart = id 131 | startbutton_color = fields_dic[tkstart]['bg'] 132 | if tkstart in Chess_Board.Fig_Pos and Chess_Board.Fig_Pos[tkstart]._color == current_color: 133 | fields_dic[tkstart]['bg'] = 'lightblue' 134 | if fields_dic[tkstart]['text'] != '': 135 | display_moves(tkstart) 136 | 137 | 138 | 139 | if tkstart != () and tkend != (): 140 | endbutton_color = fields_dic[tkend]['bg'] 141 | 142 | if not check_mate: 143 | play_game() 144 | if error == 0: 145 | undo_coloring() 146 | figure = fields_dic[tkstart]['text'] 147 | figure2 = fields_dic[tkend]['text'] 148 | if figure2 != '': 149 | if tkstart in Chess_Board.Fig_Pos: #Can be true if Rochade 150 | if (Chess_Board.Fig_Pos[tkstart]._figure == 'King' and Chess_Board.Fig_Pos[tkend]._figure == 'Rook') or (Chess_Board.Fig_Pos[tkstart]._figure == 'Rook' and Chess_Board.Fig_Pos[tkend]._figure == 'King'): 151 | fields_dic[tkstart]['text'], fields_dic[tkend]['text'] = figure2, figure 152 | elif is_check: 153 | enemy_king_pos = enemy_king_position(current_color) 154 | fields_dic[enemy_king_pos]['text'] = fields_dic[enemy_king_pos]['text'] + '\ncheck!' 155 | fields_dic[tkstart]['text'], fields_dic[tkend]['text'] = '', figure 156 | else: 157 | fields_dic[tkstart]['text'], fields_dic[tkend]['text'] = '', figure 158 | else: 159 | enemy_king_pos = enemy_king_position(current_color) 160 | if is_check: 161 | fields_dic[enemy_king_pos]['text'] = fields_dic[enemy_king_pos]['text'] + '\ncheck!' 162 | fields_dic[tkstart]['text'], fields_dic[tkend]['text'] = '', figure 163 | elif check_mate: 164 | fields_dic[tkstart]['text'], fields_dic[tkend]['text'] = '', figure 165 | fields_dic[enemy_king_pos]['text'] = fields_dic[enemy_king_pos]['text'] + '\ncheck \nmate!' 166 | playing = False 167 | else: 168 | fields_dic[tkstart]['text'], fields_dic[tkend]['text'] = '', figure 169 | 170 | last_movement = (tkstart, tkend) 171 | undo_coloring() 172 | fields_dic[tkstart]['bg'] = '#ccffcc' 173 | fields_dic[tkend]['bg'] = '#ccffcc' 174 | tkstart = () 175 | tkend = () 176 | startbutton_color = '' 177 | endbutton_color = '' 178 | error = -1 179 | display_player_color = "White's turn" if current_color == 1 else "Black's turn" 180 | labelZahl1['text'] = display_player_color 181 | 182 | else: 183 | undo_coloring() 184 | startbutton_color = '' 185 | endbutton_color = '' 186 | tkstart = () 187 | tkend = () 188 | 189 | 190 | 191 | BR = '\u265C' 192 | BN = '\u265E' 193 | BB = '\u265D' 194 | BQ = '\u265B' 195 | BK = '\u265A' 196 | BP = '\u265F' 197 | 198 | WR = '\u2656' 199 | WN = '\u2658' 200 | WP = '\u2659' 201 | WQ = '\u2655' 202 | WK = '\u2654' 203 | WB = '\u2657' 204 | 205 | a8 = Button(tk, text=BR, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((0,0))]) 206 | a8.grid(row=1, column=1, padx=(40,0), pady=(40,0)) 207 | b8 = Button(tk, text=BN, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((0,1))]) 208 | b8.grid(row=1, column=2, padx=(0,0), pady=(40,0)) 209 | c8 = Button(tk, text=BB, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((0,2))]) 210 | c8.grid(row=1, column=3, padx=(0,0), pady=(40,0)) 211 | d8 = Button(tk, text=BQ, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((0,3))]) 212 | d8.grid(row=1, column=4, padx=(0,0), pady=(40,0)) 213 | e8 = Button(tk, text=BK, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((0,4))]) 214 | e8.grid(row=1, column=5, padx=(0,0), pady=(40,0)) 215 | f8 = Button(tk, text=BB, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((0,5))]) 216 | f8.grid(row=1, column=6, padx=(0,0), pady=(40,0)) 217 | g8 = Button(tk, text=BN, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((0,6))]) 218 | g8.grid(row=1, column=7, padx=(0,0), pady=(40,0)) 219 | h8 = Button(tk, text=BR, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((0,7))]) 220 | h8.grid(row=1, column=8, padx=(0,40), pady=(40,0)) 221 | a7 = Button(tk, text=BP, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((1,0))]) 222 | a7.grid(row=2, column=1, padx=(40,0), pady=(0,0)) 223 | b7 = Button(tk, text=BP, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((1,1))]) 224 | b7.grid(row=2, column=2) 225 | c7 = Button(tk, text=BP, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((1,2))]) 226 | c7.grid(row=2, column=3) 227 | d7 = Button(tk, text=BP, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((1,3))]) 228 | d7.grid(row=2, column=4) 229 | e7 = Button(tk, text=BP, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((1,4))]) 230 | e7.grid(row=2, column=5) 231 | f7 = Button(tk, text=BP, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((1,5))]) 232 | f7.grid(row=2, column=6) 233 | g7 = Button(tk, text=BP, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((1,6))]) 234 | g7.grid(row=2, column=7) 235 | h7 = Button(tk, text=BP, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((1,7))]) 236 | h7.grid(row=2, column=8, padx=(0,40), pady=(0,0)) 237 | a6 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((2,0))]) 238 | a6.grid(row=3, column=1, padx=(40,0), pady=(0,0)) 239 | b6 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((2,1))]) 240 | b6.grid(row=3, column=2) 241 | c6 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((2,2))]) 242 | c6.grid(row=3, column=3) 243 | d6 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((2,3))]) 244 | d6.grid(row=3, column=4) 245 | e6 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((2,4))]) 246 | e6.grid(row=3, column=5) 247 | f6 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((2,5))]) 248 | f6.grid(row=3, column=6) 249 | g6 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((2,6))]) 250 | g6.grid(row=3, column=7) 251 | h6 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((2,7))]) 252 | h6.grid(row=3, column=8, padx=(0,40), pady=(0,0)) 253 | a5 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((3,0))]) 254 | a5.grid(row=4, column=1, padx=(40,0), pady=(0,0)) 255 | b5 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((3,1))]) 256 | b5.grid(row=4, column=2) 257 | c5 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((3,2))]) 258 | c5.grid(row=4, column=3) 259 | d5 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((3,3))]) 260 | d5.grid(row=4, column=4) 261 | e5 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((3,4))]) 262 | e5.grid(row=4, column=5) 263 | f5 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((3,5))]) 264 | f5.grid(row=4, column=6) 265 | g5 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((3,6))]) 266 | g5.grid(row=4, column=7) 267 | h5 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((3,7))]) 268 | h5.grid(row=4, column=8, padx=(0,40), pady=(0,0)) 269 | a4 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((4,0))]) 270 | a4.grid(row=5, column=1, padx=(40,0), pady=(0,0)) 271 | b4 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((4,1))]) 272 | b4.grid(row=5, column=2) 273 | c4 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((4,2))]) 274 | c4.grid(row=5, column=3) 275 | d4 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((4,3))]) 276 | d4.grid(row=5, column=4) 277 | e4 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((4,4))]) 278 | e4.grid(row=5, column=5) 279 | f4 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((4,5))]) 280 | f4.grid(row=5, column=6) 281 | g4 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((4,6))]) 282 | g4.grid(row=5, column=7) 283 | h4 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((4,7))]) 284 | h4.grid(row=5, column=8, padx=(0,40), pady=(0,0)) 285 | a3 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((5,0))]) 286 | a3.grid(row=6, column=1, padx=(40,0), pady=(0,0)) 287 | b3 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((5,1))]) 288 | b3.grid(row=6, column=2) 289 | c3 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((5,2))]) 290 | c3.grid(row=6, column=3) 291 | d3 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((5,3))]) 292 | d3.grid(row=6, column=4) 293 | e3 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((5,4))]) 294 | e3.grid(row=6, column=5) 295 | f3 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((5,5))]) 296 | f3.grid(row=6, column=6) 297 | g3 = Button(tk, text='', font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((5,6))]) 298 | g3.grid(row=6, column=7) 299 | h3 = Button(tk, text='', font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((5,7))]) 300 | h3.grid(row=6, column=8, padx=(0,40), pady=(0,0)) 301 | a2 = Button(tk, text=WP, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((6,0))]) 302 | a2.grid(row=7, column=1, padx=(40,0), pady=(0,0)) 303 | b2 = Button(tk, text=WP, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((6,1))]) 304 | b2.grid(row=7, column=2) 305 | c2 = Button(tk, text=WP, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((6,2))]) 306 | c2.grid(row=7, column=3) 307 | d2 = Button(tk, text=WP, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((6,3))]) 308 | d2.grid(row=7, column=4) 309 | e2 = Button(tk, text=WP, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((6,4))]) 310 | e2.grid(row=7, column=5) 311 | f2 = Button(tk, text=WP, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((6,5))]) 312 | f2.grid(row=7, column=6) 313 | g2 = Button(tk, text=WP, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((6,6))]) 314 | g2.grid(row=7, column=7) 315 | h2 = Button(tk, text=WP, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((6,7))]) 316 | h2.grid(row=7, column=8, padx=(0,40), pady=(0,0)) 317 | a1 = Button(tk, text=WR, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((7,0))]) 318 | a1.grid(row=8, column=1, padx=(40,0), pady=(0,40)) 319 | b1 = Button(tk, text=WN, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((7,1))]) 320 | b1.grid(row=8, column=2, padx=(0,0), pady=(0,40)) 321 | c1 = Button(tk, text=WB, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((7,2))]) 322 | c1.grid(row=8, column=3, padx=(0,0), pady=(0,40)) 323 | d1 = Button(tk, text=WQ, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((7,3))]) 324 | d1.grid(row=8, column=4, padx=(0,0), pady=(0,40)) 325 | e1 = Button(tk, text=WK, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((7,4))]) 326 | e1.grid(row=8, column=5, padx=(0,0), pady=(0,40)) 327 | f1 = Button(tk, text=WB, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((7,5))]) 328 | f1.grid(row=8, column=6, padx=(0,0), pady=(0,40)) 329 | g1 = Button(tk, text=WN, font='Times 20 bold', bg='grey', height=2, width=5, command=lambda: [btnClick((7,6))]) 330 | g1.grid(row=8, column=7, padx=(0,0), pady=(0,40)) 331 | h1 = Button(tk, text=WR, font='Times 20 bold', bg='white', height=2, width=5, command=lambda: [btnClick((7,7))]) 332 | h1.grid(row=8, column=8, padx=(0,40), pady=(0,40)) 333 | 334 | labelZahl1 = Label(tk, text="White's turn", bg = 'white', width = 30) 335 | labelZahl1.config(font=(10)) 336 | labelZahl1.grid(row=1, column=9, sticky='w') 337 | labelZahl2 = Label(tk, text='Game History', bg = 'white', width = 30) 338 | labelZahl2.config(font=(10)) 339 | labelZahl2.grid(row=3, column=9, sticky='w') 340 | 341 | history_gui = Text(tk, height=10, width=30, wrap = 'none', pady = 5, padx = 5) 342 | scroll = Scrollbar(tk, command=history_gui.yview) 343 | history_gui.configure(yscrollcommand=scroll.set, state='disabled') 344 | history_gui.grid(row = 4, column = 9, rowspan = 2) 345 | 346 | restartgame_button = Button(tk, text='Restart Game', bg='white', height=1, width=10, command=lambda: [restart_game()]) 347 | restartgame_button.config(font=(10), pady = 5, padx = 10) 348 | restartgame_button.grid(row=8, column=9) 349 | 350 | 351 | 352 | 353 | 354 | 355 | ###------------------------------------------------------------ ENGINE ------------------------------------------------------------### 356 | 357 | 358 | class Board(object): 359 | def __init__(self): 360 | self.Fig_Pos = { 361 | (0,0) : Rook(0, (0,0), []), 362 | (0,1) : Knight(0, (0,1), []), 363 | (0,2) : Bishop(0, (0,2), []), 364 | (0,3) : Queen(0, (0,3), []), 365 | (0,4) : King(0, (0,4), []), 366 | (0,5) : Bishop(0, (0,5), []), 367 | (0,6) : Knight(0, (0,6), []), 368 | (0,7) : Rook(0, (0,7), []), 369 | (1,0) : Pawn(0, (1,0), []), 370 | (1,1) : Pawn(0, (1,1), []), 371 | (1,2) : Pawn(0, (1,2), []), 372 | (1,3) : Pawn(0, (1,3), []), 373 | (1,4) : Pawn(0, (1,4), []), 374 | (1,5) : Pawn(0, (1,5), []), 375 | (1,6) : Pawn(0, (1,6), []), 376 | (1,7) : Pawn(0, (1,7), []), 377 | (7,0) : Rook(1, (7,0), []), 378 | (7,1) : Knight(1, (7,1), []), 379 | (7,2) : Bishop(1, (7,2), []), 380 | (7,3) : Queen(1, (7,3), []), 381 | (7,4) : King(1, (7,4), []), 382 | (7,5) : Bishop(1, (7,5), []), 383 | (7,6) : Knight(1, (7,6), []), 384 | (7,7) : Rook(1, (7,7), []), 385 | (6,0) : Pawn(1, (6,0), []), 386 | (6,1) : Pawn(1, (6,1), []), 387 | (6,2) : Pawn(1, (6,2), []), 388 | (6,3) : Pawn(1, (6,3), []), 389 | (6,4) : Pawn(1, (6,4), []), 390 | (6,5) : Pawn(1, (6,5), []), 391 | (6,6) : Pawn(1, (6,6), []), 392 | (6,7) : Pawn(1, (6,7), []) 393 | } 394 | 395 | def draw_Board(self): 396 | board_side = [' 8 \u2502',' 7 \u2502',' 6 \u2502',' 5 \u2502',' 4 \u2502',' 3 \u2502',' 2 \u2502',' 1 \u2502'] 397 | print('\n\n a b c d e f g h' + '\n \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n \u2502 \u2502') 398 | k = 8 399 | for i in range(8): 400 | print(board_side[i], end = ' ') 401 | for j in range(8): 402 | if (i,j) in self.Fig_Pos: 403 | if self.Fig_Pos[(i,j)]._figure == 'Knight': 404 | print('N' + str(self.Fig_Pos[(i,j)]._color), end = ' ') 405 | else: 406 | print(self.Fig_Pos[(i,j)]._figure[0] + str(self.Fig_Pos[(i,j)]._color), end = ' ') 407 | else: 408 | print(' \u2022 ', end = ' ') 409 | print(' \u2502 ' + str(k) + '\n \u2502 \u2502') 410 | k -= 1 411 | print(' \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n a b c d e f g h' + '\n') 412 | 413 | def get_positions(self): 414 | return self.Fig_Pos 415 | 416 | def update_positions(self, old_position, new_position): 417 | self.Fig_Pos[new_position] = self.Fig_Pos[old_position] 418 | del self.Fig_Pos[old_position] 419 | 420 | def update_position_rochade(self, old_position, new_position): 421 | p1 = self.Fig_Pos[old_position] 422 | p2 = self.Fig_Pos[new_position] 423 | 424 | self.Fig_Pos[old_position] = p2 425 | self.Fig_Pos[new_position] = p1 426 | 427 | 428 | 429 | class Figure(object): 430 | def __init__(self, color, position, poss_moves): 431 | self._color = color 432 | self.position = position 433 | self.poss_moves = poss_moves 434 | 435 | def get_poss_moves(self): 436 | return self.poss_moves 437 | 438 | def get_position(self): 439 | return self.position 440 | 441 | def get_color(self): 442 | return self._color 443 | 444 | 445 | class King(Figure): 446 | def __init__(self, color, position, poss_moves, figure = 'King'): 447 | Figure.__init__(self, color, position, poss_moves) 448 | self._figure = figure 449 | self.already_moved = False 450 | 451 | def get_sourrounding(self): 452 | i,j = self.position[0], self.position[1] 453 | sur = [(i-1, j-1), (i-1, j), (i-1, j+1), (i, j-1), (i, j+1), (i+1, j-1), (i+1, j), (i+1, j+1)] 454 | sur = [(i[0], i[1]) for i in sur if -1 < i[0] < 8 and -1 < i[1] < 8] 455 | return sur 456 | 457 | def check_move(self, end_position): 458 | if not self.poss_moves: 459 | return False 460 | 461 | cur_position = self.get_position() 462 | cur_Board = Chess_Board.get_positions() 463 | sur = self.get_sourrounding() 464 | 465 | if end_position not in sur: 466 | return False 467 | 468 | if end_position not in cur_Board: 469 | return True 470 | 471 | if end_position in cur_Board and cur_Board[end_position]._color != self._color: 472 | return True 473 | 474 | return False 475 | 476 | def update_current_pos(self, new_position): 477 | self.position = new_position 478 | 479 | def update_poss_moves(self): 480 | sur = self.get_sourrounding() 481 | cur_Board = Chess_Board.get_positions() 482 | self.poss_moves = [(i[0], i[1]) for i in sur if i not in cur_Board or Chess_Board.Fig_Pos[(i[0], i[1])]._color != self._color] 483 | 484 | 485 | class Queen(Figure): 486 | def __init__(self, color, position, poss_moves, figure = 'Queen'): 487 | Figure.__init__(self, color, position, poss_moves) 488 | self._figure = figure 489 | 490 | def get_sourrounding(self): 491 | cur_Board = Chess_Board.get_positions() 492 | i,j = self.position[0], self.position[1] 493 | sur = [] 494 | for count, m in enumerate([(i+1, 8, 1), (i-1, -1, -1), (j+1, 8, 1), (j-1, -1, -1)]): 495 | for k in range(m[0], m[1], m[2]): 496 | udrl = (k,j) if count < 2 else (i,k) 497 | if udrl not in cur_Board: 498 | sur.append(udrl) 499 | elif udrl in cur_Board and Chess_Board.Fig_Pos[udrl]._color != self._color: 500 | sur.append(udrl) 501 | break 502 | else: 503 | break 504 | 505 | for pos in ([1, 1], [1, -1], [-1, 1], [-1, -1]): 506 | i_m = pos[0] 507 | j_m = pos[1] 508 | while -1 < i+i_m < 8 and -1 < j+j_m < 8: 509 | if (i+i_m, j+j_m) in cur_Board and Chess_Board.Fig_Pos[(i+i_m, j+j_m)]._color != self._color: 510 | sur.append((i+i_m, j+j_m)) 511 | break 512 | elif (i+i_m, j+j_m) in cur_Board and Chess_Board.Fig_Pos[(i+i_m, j+j_m)]._color == self._color: 513 | break 514 | else: 515 | sur.append((i+i_m, j+j_m)) 516 | i_m += pos[0] 517 | j_m += pos[1] 518 | return sur 519 | 520 | def update_poss_moves(self): 521 | self.poss_moves = self.get_sourrounding() 522 | 523 | 524 | class Bishop(Figure): 525 | def __init__(self, color, position, poss_moves, figure = 'Bishop'): 526 | Figure.__init__(self, color, position, poss_moves) 527 | self._figure = figure 528 | 529 | def get_sourrounding(self): 530 | cur_Board = Chess_Board.get_positions() 531 | i,j = self.position[0], self.position[1] 532 | sur = [] 533 | for pos in ([1, 1], [1, -1], [-1, 1], [-1, -1]): 534 | i_m = pos[0] 535 | j_m = pos[1] 536 | while -1 < i+i_m < 8 and -1 < j+j_m < 8: 537 | if (i+i_m, j+j_m) in cur_Board and Chess_Board.Fig_Pos[(i+i_m, j+j_m)]._color != self._color: 538 | sur.append((i+i_m, j+j_m)) 539 | break 540 | elif (i+i_m, j+j_m) in cur_Board and Chess_Board.Fig_Pos[(i+i_m, j+j_m)]._color == self._color: 541 | break 542 | else: 543 | sur.append((i+i_m, j+j_m)) 544 | i_m += pos[0] 545 | j_m += pos[1] 546 | return sur 547 | 548 | def update_poss_moves(self): 549 | self.poss_moves = self.get_sourrounding() 550 | 551 | 552 | class Rook(Figure): 553 | def __init__(self, color, position, poss_moves, figure = 'Rook'): 554 | Figure.__init__(self, color, position, poss_moves) 555 | self._figure = figure 556 | self.already_moved = False 557 | 558 | def get_sourrounding(self): 559 | cur_Board = Chess_Board.get_positions() 560 | i,j = self.position[0], self.position[1] 561 | sur = [] 562 | for count, m in enumerate([(i+1, 8, 1), (i-1, -1, -1), (j+1, 8, 1), (j-1, -1, -1)]): 563 | for k in range(m[0], m[1], m[2]): 564 | udrl = (k,j) if count < 2 else (i,k) 565 | if udrl not in cur_Board: 566 | sur.append(udrl) 567 | elif udrl in cur_Board and Chess_Board.Fig_Pos[udrl]._color != self._color: 568 | sur.append(udrl) 569 | break 570 | else: 571 | break 572 | return sur 573 | 574 | def update_poss_moves(self): 575 | self.poss_moves = self.get_sourrounding() 576 | 577 | 578 | class Knight(Figure): 579 | def __init__(self, color, position, poss_moves, figure = 'Knight'): 580 | Figure.__init__(self, color, position, poss_moves) 581 | self._figure = figure 582 | 583 | def get_sourrounding(self): 584 | sur = [] 585 | cur_Board = Chess_Board.get_positions() 586 | i = self.position[0] 587 | j = self.position[1] 588 | 589 | for posspos in [(i-2, j-1), (i-2, j+1), (i-1, j-2), (i-1, j+2), (i+2, j-1), (i+2, j+1), (i+1, j-2), (i+1, j+2)]: 590 | i_k = posspos[0] 591 | j_k = posspos[1] 592 | if 0 <= i_k < 8 and 0 <= j_k < 8 and (i_k, j_k) not in cur_Board: 593 | sur.append((i_k, j_k)) 594 | if 0 <= i_k < 8 and 0 <= j_k < 8 and (i_k, j_k) in cur_Board and Chess_Board.Fig_Pos[(i_k, j_k)]._color != self._color: 595 | sur.append((i_k, j_k)) 596 | return sur 597 | 598 | def update_poss_moves(self): 599 | self.poss_moves = self.get_sourrounding() 600 | 601 | 602 | class Pawn(Figure): 603 | def __init__(self, color, position, poss_moves, figure = 'Pawn'): 604 | Figure.__init__(self, color, position, poss_moves) 605 | self._figure = figure 606 | 607 | def get_sourrounding(self): 608 | sur = [] 609 | cur_Board = Chess_Board.get_positions() 610 | i = self.position[0] 611 | j = self.position[1] 612 | 613 | if self._color == 0: 614 | if (i+1, j) not in cur_Board: 615 | sur.append((i+1, j)) 616 | if i == 1 and (i+2, j) not in cur_Board: 617 | sur.append((i+2, j)) 618 | if (i+1, j+1) in cur_Board and cur_Board[(i+1, j+1)]._color != 0: 619 | sur.append((i+1, j+1)) 620 | if (i+1, j-1) in cur_Board and cur_Board[(i+1, j-1)]._color != 0: 621 | sur.append((i+1, j-1)) 622 | else: 623 | if (i-1, j) not in cur_Board: 624 | sur.append((i-1, j)) 625 | if i == 6 and (i-2, j) not in cur_Board: 626 | sur.append((i-2, j)) 627 | if (i-1, j+1) in cur_Board and cur_Board[(i-1, j+1)]._color != 1: 628 | sur.append((i-1, j+1)) 629 | if (i-1, j-1) in cur_Board and cur_Board[(i-1, j-1)]._color != 1: 630 | sur.append((i-1, j-1)) 631 | return sur 632 | 633 | def update_poss_moves(self): 634 | self.poss_moves = self.get_sourrounding() 635 | 636 | 637 | def check_if_move_is_possible(start_position, end_position): 638 | cur_Board = Chess_Board.get_positions() 639 | if end_position in Chess_Board.Fig_Pos[(start_position)].poss_moves: 640 | return True 641 | else: 642 | return False 643 | 644 | 645 | def check_if_own_king_in_danger(start_position, end_position): 646 | current_color = Chess_Board.Fig_Pos[start_position]._color 647 | position_own_king = () 648 | 649 | Chess_Board.Fig_Pos[start_position].position = end_position 650 | Chess_Board.update_positions(start_position, end_position) 651 | 652 | for to_update in Chess_Board.Fig_Pos: 653 | Chess_Board.Fig_Pos[to_update].update_poss_moves() 654 | if Chess_Board.Fig_Pos[to_update]._figure == 'King' and Chess_Board.Fig_Pos[to_update]._color == current_color: 655 | position_own_king = Chess_Board.Fig_Pos[to_update].position 656 | 657 | for figures in Chess_Board.Fig_Pos: 658 | if Chess_Board.Fig_Pos[figures]._color != current_color and position_own_king in Chess_Board.Fig_Pos[figures].poss_moves: 659 | print('Move not possible. Own King would be in chess.') 660 | Chess_Board.Fig_Pos[end_position].position = start_position 661 | Chess_Board.update_positions(end_position, start_position) 662 | for to_update in Chess_Board.Fig_Pos: 663 | Chess_Board.Fig_Pos[to_update].update_poss_moves() 664 | return False 665 | return True 666 | 667 | 668 | def check_if_checkmate(current_color, start_position, end_position): 669 | global is_check, check_mate, playing 670 | is_check = False 671 | check_mate = False 672 | enemy_king_pos = () 673 | 674 | for find_king in Chess_Board.Fig_Pos: 675 | if Chess_Board.Fig_Pos[find_king]._figure == 'King' and Chess_Board.Fig_Pos[find_king]._color != current_color: 676 | enemy_king_pos = Chess_Board.Fig_Pos[find_king].position 677 | 678 | who_attacks = [] 679 | attackers_moves = [] 680 | 681 | for find_attacker in Chess_Board.Fig_Pos: 682 | if Chess_Board.Fig_Pos[find_attacker]._color == current_color and enemy_king_pos in Chess_Board.Fig_Pos[find_attacker].poss_moves: 683 | who_attacks.append(Chess_Board.Fig_Pos[find_attacker].position) 684 | attackers_moves.extend(Chess_Board.Fig_Pos[find_attacker].poss_moves) 685 | 686 | if not who_attacks: 687 | return True 688 | 689 | for king_move in Chess_Board.Fig_Pos[enemy_king_pos].poss_moves: 690 | if king_move not in attackers_moves: 691 | print('check!') 692 | is_check = True 693 | return True 694 | 695 | for find_helpers in Chess_Board.Fig_Pos: 696 | if Chess_Board.Fig_Pos[find_helpers]._color != current_color and Chess_Board.Fig_Pos[find_helpers]._figure != 'King': 697 | for helpers_poss_move in Chess_Board.Fig_Pos[find_helpers].poss_moves: 698 | if helpers_poss_move in attackers_moves: 699 | old_position = Chess_Board.Fig_Pos[find_helpers].position 700 | new_position = helpers_poss_move 701 | 702 | Chess_Board.Fig_Pos[old_position].position = new_position 703 | Chess_Board.update_positions(old_position, new_position) 704 | 705 | for to_update in Chess_Board.Fig_Pos: 706 | Chess_Board.Fig_Pos[to_update].update_poss_moves() 707 | if enemy_king_pos not in Chess_Board.Fig_Pos[end_position].poss_moves: 708 | print('check!') 709 | is_check = True 710 | Chess_Board.Fig_Pos[new_position].position = old_position 711 | Chess_Board.update_positions(new_position, old_position) 712 | 713 | for to_update in Chess_Board.Fig_Pos: 714 | Chess_Board.Fig_Pos[to_update].update_poss_moves() 715 | return True 716 | 717 | Chess_Board.Fig_Pos[new_position].position = old_position 718 | Chess_Board.update_positions(new_position, old_position) 719 | 720 | for to_update in Chess_Board.Fig_Pos: 721 | Chess_Board.Fig_Pos[to_update].update_poss_moves() 722 | 723 | print('check mate!') 724 | check_mate = True 725 | return True 726 | 727 | 728 | def check_rochade(current_color, start_position, end_position): 729 | cur_Board = Chess_Board.get_positions() 730 | if start_position not in cur_Board or end_position not in cur_Board: 731 | return False 732 | p1 = cur_Board[start_position] 733 | p2 = cur_Board[end_position] 734 | p1_position = p1.position 735 | p2_position = p2.position 736 | 737 | if p1._color != p2._color or p1._figure not in ('King', 'Rook') or p2._figure not in ('King', 'Rook'): 738 | return False 739 | 740 | if p1.already_moved or p2.already_moved: 741 | return False 742 | 743 | pos_between = [] 744 | if p1.position == (0,0) or p2.position == (0,0): 745 | pos_between = [(0,1), (0,2), (0,3)] 746 | elif p1.position == (0,7) or p2.position == (0,7): 747 | pos_between = [(0,5), (0,6)] 748 | elif p1.position == (7,0) or p2.position == (7,0): 749 | pos_between = [(7,1), (7,2), (7,3)] 750 | elif p1.position == (7,7) or p2.position == (7,7): 751 | pos_between = [(7,5), (7,6)] 752 | 753 | for check_poss_attack in Chess_Board.Fig_Pos: 754 | if Chess_Board.Fig_Pos[check_poss_attack].position in pos_between: 755 | return False 756 | if Chess_Board.Fig_Pos[check_poss_attack]._color != current_color: 757 | for positions_between in pos_between: 758 | if positions_between in Chess_Board.Fig_Pos[check_poss_attack].poss_moves: 759 | return False 760 | 761 | Chess_Board.Fig_Pos[start_position].position = end_position 762 | Chess_Board.Fig_Pos[end_position].position = start_position 763 | Chess_Board.update_position_rochade(start_position, end_position) 764 | 765 | for to_update in Chess_Board.Fig_Pos: 766 | Chess_Board.Fig_Pos[to_update].update_poss_moves() 767 | 768 | position_own_king = () 769 | for to_update in Chess_Board.Fig_Pos: 770 | if Chess_Board.Fig_Pos[to_update]._figure == 'King' and Chess_Board.Fig_Pos[to_update]._color == current_color: 771 | position_own_king = Chess_Board.Fig_Pos[to_update].position 772 | 773 | for figures in Chess_Board.Fig_Pos: 774 | if Chess_Board.Fig_Pos[figures]._color != current_color and position_own_king in Chess_Board.Fig_Pos[figures].poss_moves: 775 | Chess_Board.Fig_Pos[end_position].position = start_position 776 | Chess_Board.Fig_Pos[start_position].position = end_position 777 | Chess_Board.update_position_rochade(end_position, start_position) 778 | 779 | for to_update in Chess_Board.Fig_Pos: 780 | Chess_Board.Fig_Pos[to_update].update_poss_moves() 781 | return False 782 | p1.already_moved = True 783 | p2.already_moved = True 784 | return True 785 | 786 | def process_input(current_color, start_position, end_position): 787 | global history_infos_temp 788 | 789 | colors_turn = 'white' if current_color == 1 else 'black' 790 | history_infos_temp = [colors_turn, Chess_Board.Fig_Pos[start_position]._figure, start_position, end_position] 791 | 792 | cur_Board = Chess_Board.get_positions() 793 | if start_position not in cur_Board: 794 | return False 795 | 796 | if current_color != Chess_Board.Fig_Pos[start_position]._color: 797 | print(f'It´s {colors_turn} turn.') 798 | return False 799 | 800 | if check_rochade(current_color, start_position, end_position): 801 | return True 802 | else: 803 | pass 804 | 805 | if check_if_move_is_possible(start_position, end_position) and check_if_own_king_in_danger(start_position, end_position) and check_if_checkmate(current_color, start_position, end_position): 806 | history_infos_temp.append(is_check) 807 | if Chess_Board.Fig_Pos[(end_position)]._figure in ('King', 'Rook'): 808 | Chess_Board.Fig_Pos[(end_position)].already_moved = True 809 | return True 810 | else: 811 | return False 812 | 813 | 814 | def update_history_gui(history_infos_temp): 815 | global start_count_rounds 816 | 817 | start_count_rounds += 1 818 | history.append(history_infos_temp) 819 | sp = history_infos_temp[2] 820 | se = history_infos_temp[3] 821 | 822 | translate = {(0,0):'a8', (0,1):'b8', (0,2):'c8', (0,3):'d8', (0,4):'e8', (0,5):'f8', (0,6):'g8', (0,7):'h8', 823 | (1,0):'a7', (1,1):'b7', (1,2):'c7', (1,3):'d7', (1,4):'e7', (1,5):'f7', (1,6):'g7', (1,7):'h7', 824 | (2,0):'a6', (2,1):'b6', (2,2):'c6', (2,3):'d6', (2,4):'e6', (2,5):'f6', (2,6):'g6', (2,7):'h6', 825 | (3,0):'a5', (3,1):'b5', (3,2):'c5', (3,3):'d5', (3,4):'e5', (3,5):'f5', (3,6):'g5', (3,7):'h5', 826 | (4,0):'a4', (4,1):'b4', (4,2):'c4', (4,3):'d4', (4,4):'e4', (4,5):'f4', (4,6):'g4', (4,7):'h4', 827 | (5,0):'a3', (5,1):'b3', (5,2):'c3', (5,3):'d3', (5,4):'e3', (5,5):'f3', (5,6):'g3', (5,7):'h3', 828 | (6,0):'a2', (6,1):'b2', (6,2):'c2', (6,3):'d2', (6,4):'e2', (6,5):'f2', (6,6):'g2', (6,7):'h2', 829 | (7,0):'a1', (7,1):'b1', (7,2):'c1', (7,3):'d1', (7,4):'e1', (7,5):'f1', (7,6):'g1', (7,7):'h1'} 830 | 831 | hist_out = str(start_count_rounds) + ' ' + str(history_infos_temp[0]) + ' ' + str(history_infos_temp[1]) + ' ' + translate[sp] + ' ' + translate[se] 832 | 833 | if history_infos_temp[-1]: 834 | hist_out += ' check' 835 | if check_mate: 836 | hist_out += ' check mate' 837 | 838 | history_gui.configure(state='normal') 839 | history_gui.insert(END, hist_out + '\n') 840 | history_gui.configure(state='disabled') 841 | 842 | 843 | def play_game(): 844 | global error, current_color, tkstart, tkend, start_position, end_position, is_check, check_mate, history_infos_temp, history 845 | 846 | is_check = False 847 | check_mate = False 848 | start_position = tkstart 849 | end_position = tkend 850 | 851 | if process_input(current_color, start_position, end_position): 852 | current_color = 0 if current_color == 1 else 1 853 | Chess_Board.draw_Board() 854 | update_history_gui(history_infos_temp) 855 | error = 0 856 | 857 | 858 | def main(): 859 | global Chess_Board, current_color, history, start_count_rounds, check_mate 860 | 861 | Chess_Board = Board() 862 | current_color = 1 863 | start_count_rounds = 0 864 | check_mate = False 865 | history = [] 866 | print('Welcome to this Chess Game. Have fun!') 867 | Chess_Board.draw_Board() 868 | for to_update in Chess_Board.Fig_Pos: 869 | Chess_Board.Fig_Pos[to_update].update_poss_moves() 870 | 871 | tk.mainloop() 872 | -------------------------------------------------------------------------------- /gameGUI.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/j00nas/python-chess-game-GUI/918ff418e79d8b9568e4cad3807e82c22ba607ce/gameGUI.png -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ### Update 01.01.2021 4 | 5 | #### Version 2.0 ---- chess_v2_GUI.py 6 | 7 | I completly revised my code, fixed some bugs and added more functions: 8 | 9 | - It displays the game history 10 | - Button added to restart the Game 11 | - It displays the turn of the player 12 | - The last move is visible through coloring 13 | - Rochade is added 14 | 15 | ![alt text](https://github.com/j00nas/python-chess-game-GUI/blob/master/gameGUI.png?raw=true) 16 | 17 | ### Update 25.04.2020 18 | fixed a bug where you could still move the figures after check mate (in GUI-Version) 19 | 20 | 21 | ### Update 23.04.2020 22 | I implemented a GUI via TKinter 23 | Version used: Python 3.7.4 24 | Requirements: TKinter 25 | 26 | 27 | This is a simple python made chess game to run in the console (chess.py). 28 | You can move by typing eg. "b2 d4". 29 | 30 | Or you can also play via GUI (see chess_GUI.py). 31 | --------------------------------------------------------------------------------