-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.py
170 lines (147 loc) · 5.99 KB
/
board.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
166
167
168
169
170
import constants
import pygame
import tile
from Pieces import piece
class gameboard:
board_array = [
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
]
check = None
@classmethod
def draw_pieces(cls, display):
for x in range(len(cls.board_array)):
for y in range(len(cls.board_array[x])):
if cls.board_array[x][y] != piece.empty:
cls.board_array[x][y].draw(display)
@classmethod
def draw_background(cls, display):
tile_1 = pygame.image.load(constants.TILE_1_PATH)
tile_2 = pygame.image.load(constants.TILE_2_PATH)
y_loc = 0
for c in range(len(cls.board_array)):
x_loc = 0
for r in range(len(cls.board_array[c])):
if c % 2 == 0: # even
if r % 2 == 0: # even
display.blit(tile_1, (x_loc, y_loc))
else:
display.blit(tile_2, (x_loc, y_loc))
else:
if r % 2 == 0: # even
display.blit(tile_2, (x_loc, y_loc))
else:
display.blit(tile_1, (x_loc, y_loc))
tile_info = tile.tile(x_loc, y_loc)
x_loc += constants.TILE_SIZE
y_loc += constants.TILE_SIZE
@classmethod
def highlight(cls, x, y, display):
x *= constants.TILE_SIZE
y *= constants.TILE_SIZE
highlight_img = pygame.image.load(constants.POSSIBLE_MOVE_EFFECT_PATH)
display.blit(highlight_img, (x, y))
@classmethod
def move_to(cls, pc, x_to, y_to):
if cls.board_array[x_to][y_to].team != "empty":
cls.board_array[x_to][y_to].kill()
cls.board_array[pc.x][pc.y] = piece.empty
pc.x = x_to # pieces new x
pc.y = y_to # pieces new y
cls.board_array[x_to][y_to] = pc # updated position
if pc.name == "pawn":
pc.moved_once = True
@classmethod
def make_all_empty(cls):
EMPTY = piece.empty
for x in range(len(cls.board_array)):
for y in range(len(cls.board_array[x])):
cls.board_array[x][y] = EMPTY
@classmethod
def set_index(cls, x, y, as_piece):
cls.board_array[x][y] = as_piece
@classmethod
def peek_index(cls, x, y):
return cls.board_array[x][y]
@classmethod
def is_empty(cls, x, y):
if cls.board_array[x][y] == piece.empty:
return True
return False
@classmethod
def has(cls, piece_type):
"""
:type piece_type: str
:param: piece_type: the type of piece being searched for
:returns: True if the type of piece has been found on the board, otherwise returns False
"""
for x in range(len(cls.board_array)):
for y in range(len(cls.board_array[x])):
if cls.board_array[x][y] is not None and cls.board_array[x][y].name == piece_type:
return True
return False
@classmethod
def team_has(cls, piece_type, team):
"""
:type piece_type: str
:param piece_type: the type of piece being searched for
:param team: the team that is being searched for (with the piece)
:return: True if a piece as such has been found, else return False
"""
for x in range(len(cls.board_array)):
for y in range(len(cls.board_array[x])):
if cls.board_array[x][y] is not None and cls.board_array[x][y].name == piece_type and \
cls.board_array[x][y].team == team:
return cls.board_array[x][y]
return False
@classmethod
def show(cls):
output = ""
for c in range(len(cls.board_array)):
for r in range(len(cls.board_array[c])):
if cls.board_array[r][c].name != "king":
output += cls.board_array[r][c].name[0] + " "
else:
output += cls.board_array[r][c].name[0].capitalize() + " "
output += '\n'
print(output)
@classmethod
def start(cls):
cls.make_all_empty()
def place(y_major_line, team_name):
"""
:param y_major_line: the 'y' position (int) of the major line (queen, king, bishop, knight, rook...)
:param team_name: the name of the team being placed
"""
piece.rook(0, y_major_line, team_name)
piece.knight(1, y_major_line, team_name)
piece.bishop(2, y_major_line, team_name)
piece.queen(3, y_major_line, team_name)
piece.king(4, y_major_line, team_name)
piece.bishop(5, y_major_line, team_name)
piece.knight(6, y_major_line, team_name)
piece.rook(7, y_major_line, team_name)
place(7, constants.TEAM_NAME_1)
place(0, constants.TEAM_NAME_2)
# for in range of board array length
for x in range(len(gameboard.board_array[0])):
piece.pawn(x, 1, constants.TEAM_NAME_2)
piece.pawn(x, 6, constants.TEAM_NAME_1)
@classmethod
def in_check(cls, team):
pc = gameboard.team_has('king', team) # get the piece
if pc:
pos = pc.x, pc.y # position of king
for x in range(len(gameboard.board_array)):
for y in range(len(gameboard.board_array[x])):
search = gameboard.board_array[x][y]
if search != piece.empty and search.team != team:
if pos in search.possible_movements():
return True
return False