-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess_gui.py
165 lines (148 loc) · 7.03 KB
/
chess_gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import pygame
from spritesheet import BlockSheet
# from time import time
from main import play as play1
from main import play as play2
from main import PIECE_ATTACK_FUNCTIONS
from main import KING_CASTLE_POSITIONS
from main import BOARD_ITERATOR
from main import EMPTY
from main import PIECE_MOVE_FUNCTIONS
from main import find_state
from main import find_type
from main import process_move
from main import update_constants
from main import update_double_pawn
from main import find_castle_directions
from main import castle
from main import pawn_move
# @formatter:off
DEFAULT_BOARD = (
# 0 1 2 3 4 5 6 7
((1, 'r'), (1, 'n'), (1, 'b'), (1, 'q'), (1, 'k'), (1, 'b'), (1, 'n'), (1, 'r'), ), # 0
((1, 'p'), (1, 'p'), (1, 'p'), (1, 'p'), (1, 'p'), (1, 'p'), (1, 'p'), (1, 'p'), ), # 1
((0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), ), # 2
((0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), ), # 3
((0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), ), # 4
((0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), ), # 5
((-1, 'p'), (-1, 'p'), (-1, 'p'), (-1, 'p'), (-1, 'p'), (-1, 'p'), (-1, 'p'), (-1, 'p'), ), # 6
((-1, 'r'), (-1, 'n'), (-1, 'b'), (-1, 'q'), (-1, 'k'), (-1, 'b'), (-1, 'n'), (-1, 'r'), ), # 7
)
# @formatter:on
PIECES = ('p', 'n', 'b', 'r', 'q', 'k')
SCALE_FACTOR = 5
FRAME_RATE = 1
PIECE_SIZE = (7, 14)
TILE_SIZE = 100
TILE_DIMENSIONS = (TILE_SIZE, TILE_SIZE)
TILE_COLORS = (None, (99, 114, 124), (160, 160, 160))
NUMBER_SCALE_FACTOR = 3
NUMBER_GAP = 25
SHOW_NUMBERS = False
PLAYER_TILE_COLOR = (174, 126, 126)
COMPUTER_SIDES = (1,)
# COMPUTER_SIDES = ()
COMPUTERS = (None, play1, play2)
CONFIRM_TURN = False
DISPLAY = pygame.display.set_mode([TILE_SIZE * 8 for _ in range(2)])
SPRITE_SHEET = BlockSheet("spritesheet.png", SCALE_FACTOR, PIECE_SIZE)
PIECE_SPRITES = {}
for side in (1, -1):
temp_sprites = SPRITE_SHEET.get_blocks(len(PIECES))
PIECE_SPRITES[side] = {piece: temp_sprites[i] for i, piece in enumerate(PIECES)}
NUMBER_SPRITES = SPRITE_SHEET.get_custom_blocks((7, 7), 10, scale=NUMBER_SCALE_FACTOR)
def find_center(dimensions_1, dimensions_2, coordinates=(0, 0)):
return [(coordinates[i] + (dimensions_1[i] / 2 - dimensions_2[i] / 2)) for i in range(2)]
def draw_board(display, piece_sprites, number_sprites, board, piece_tile):
for y in BOARD_ITERATOR:
for x in BOARD_ITERATOR:
tile_state = find_state((x, y), board)
coordinates = (x * TILE_SIZE, y * TILE_SIZE)
if (x, y) == piece_tile:
color = PLAYER_TILE_COLOR
else:
if (x - y) % 2 == 0:
color = TILE_COLORS[-1]
else:
color = TILE_COLORS[1]
pygame.draw.rect(display, color, (coordinates, TILE_DIMENSIONS))
if SHOW_NUMBERS:
display.blit(number_sprites[x], coordinates)
display.blit(number_sprites[y], (coordinates[0] + NUMBER_GAP, coordinates[1]))
if tile_state != EMPTY:
# print(x, y, board[y][x])
sprite = piece_sprites[tile_state][find_type((x, y), board)]
display.blit(sprite, find_center(TILE_DIMENSIONS, sprite.get_size(), coordinates))
def run(board, side, turn, double_pawn):
# current_time = time()
new_board, double_pawn = COMPUTERS[side](board, side, double_pawn)
if board == new_board:
stalemate = True
else:
stalemate = False
# print(turn, side, time() - current_time)
# print(time() - current_time)
# print("##############################################")
turn, side = update(turn, side)
return new_board, side, turn, double_pawn, stalemate
def update(turn, side):
return turn + 1, side * -1
def convert_to_grid(coordinates):
return [int(coordinate / TILE_SIZE) for coordinate in coordinates]
def main():
board = list(DEFAULT_BOARD)
side = -1
turn = 1
piece = None
double_pawn = None
while True:
draw_board(DISPLAY, PIECE_SPRITES, NUMBER_SPRITES, board, piece)
pygame.display.update()
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
quit()
if side:
if side in COMPUTER_SIDES:
if not CONFIRM_TURN or pygame.key.get_pressed()[pygame.K_SPACE]:
board, side, turn, double_pawn, stalemate = run(board, side, turn, double_pawn)
if stalemate:
side = False
else:
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
pos = tuple(convert_to_grid(event.pos))
if find_state(pos, board) == side:
piece = pos
elif event.type == pygame.MOUSEBUTTONUP:
if piece:
move = tuple(convert_to_grid(event.pos))
piece_type = find_type(piece, board)
if piece_type == 'p':
moves, passant_moves = pawn_move(piece, board, side, double_pawn)
else:
moves = PIECE_MOVE_FUNCTIONS[piece_type](piece, board, side)
passant_moves = []
if move in moves:
update_constants(board, side, piece)
board = process_move(board, piece, move, side, move in passant_moves)
double_pawn = update_double_pawn(piece, move, piece_type)
turn, side = update(turn, side)
elif piece_type == 'k' and move in KING_CASTLE_POSITIONS[side]:
enemy_attacks = {}
for y in BOARD_ITERATOR:
for x in BOARD_ITERATOR:
if find_state((x, y), board) == side * -1:
attacks = PIECE_ATTACK_FUNCTIONS[piece_type]((x, y), board, side * -1)
if attacks:
for attack in attacks:
enemy_attacks.setdefault(attack, []).append((x, y))
castle_directions = find_castle_directions(board, side, enemy_attacks)
for direction in castle_directions:
if KING_CASTLE_POSITIONS[side][direction] == move:
board = castle(board, side, direction)
turn, side = update(turn, side)
break
piece = None
if __name__ == '__main__':
main()