-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchessboard.py
More file actions
176 lines (149 loc) · 7.13 KB
/
chessboard.py
File metadata and controls
176 lines (149 loc) · 7.13 KB
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
171
172
173
174
175
176
from MoveableUnitsandSprites import Bishop
from MoveableUnitsandSprites import King
from MoveableUnitsandSprites import Queen
from MoveableUnitsandSprites import Pawn
from MoveableUnitsandSprites import Knight
from MoveableUnitsandSprites import Rook
from MoveableUnitsandSprites import ChessPieces as x
class Chessboard:
def __init__(self, rows, columns):
self.rows = rows
self.columns = columns
self.board = [[0 for _ in range(8)]for x in range(8)]
# Positions of each piece on the board for black
self.board[0][0] = Rook(0,0, "Black")
self.board[0][1] = Knight(0,1, "Black")
self.board[0][2] = Bishop(0,2, "Black")
self.board[0][3] = Queen(0,3, "Black")
self.board[0][4] = King(0,4, "Black")
self.board[0][5] = Bishop(0,5, "Black")
self.board[0][6] = Knight(0,6, "Black")
self.board[0][7] = Rook(0,7, "Black")
for _ in range(8):
self.board[1][_] = Pawn(1, _, "Black")
# for white
self.board[7][0] = Rook(7, 0, "White")
self.board[7][1] = Knight(7, 1, "White")
self.board[7][2] = Bishop(7, 2, "White")
self.board[7][3] = Queen(7, 3, "White")
self.board[7][4] = King(7, 4, "White")
self.board[7][5] = Bishop(7, 5, "White")
self.board[7][6] = Knight(7, 6, "White")
self.board[7][7] = Rook(7, 7, "White")
for _ in range(8):
self.board[6][_] = Pawn(6 , _, "White")
def get_board(self):
return self.board
def draw(self, win, board):
for _ in range(self.rows):
for i in range(self.columns):
if self.board[_][i] != 0:
self.board[_][i].border(win, board )
# for determining the border select and selecting only one at a time
def selectinboard(self, columns, rows):
for _ in range(self.rows):
for i in range(self.columns):
if self.board[_][i] != 0:
# turns all positions in board as unselected
self.board[_][i].selected_piece = False
if self.board[rows][columns] != 0:
# turns singular piece in column and rows as selected
self.board[rows][columns].selected_piece = True
# updates the possible moves depending on the new position of the piece
def updater(self):
for i in range(self.rows):
for j in range(self.columns):
if self.board[i][j] != 0:
self.board[i][j].possible_moves(self.board)
# resets the state of selected piece on all other objects in the board
def reset(self):
for i in range(self.rows):
for j in range(self.columns):
if self.board[i][j] != 0:
self.board[i][j].selected_piece = False
#returns the positions of the two kings on a list
def return_king_pos(self):
Dboard = self.board[:]
king_pos = []
for x in range(self.rows):
for _ in range(self.columns):
try:
if Dboard[x][_].king:
king_pos.append((_,x))
except:
continue
return king_pos
#main function to move the chess pieces
def do_move_chesspiece(self, org, end, turn, color, win):
self.updater()
# condition is used to tell us the amount of kings left in the board which if 0 means only one king is left and it will end the game soon after
condition = 1
cBoard = self.board[:]
try:
king = 0
# 4 conditions need to be met in order for this to accept
# It needs to be odd, the color needs to be the same as the piece color, the ending position is in the move list and not the same as the original position
if turn % 2 != 0 and color == x.get_color(self.board[org[0]][org[1]]):
if (end[1],end[0]) in self.board[org[0]][org[1]].all_possible_movements_list and (org[0], org[1]) != (end[0], end[1]):
print('White Moved')
if cBoard[org[0]][org[1]].pawn:
cBoard[org[0]][org[1]].first_movement = False
if (end[1],end[0]) in self.board[org[0]][org[1]].all_possible_movements_list:
trueend = end
cBoard[org[0]][org[1]].move_pos((end[0], end[1]))
cBoard[end[0]][end[1]] = cBoard[org[0]][org[1]]
cBoard[org[0]][org[1]] = 0
self.updater()
king_pos = self.return_king_pos()
try:
if king_pos[0] in self.board[trueend[0]][trueend[1]].all_possible_movements_list or king_pos[1] in self.board[trueend[0]][trueend[1]].all_possible_movements_list:
# king is defined as 1 to tell me if there is a king in the current move list or not.
king = 1
else:
king = 0
except:
condition = 0
if len(king_pos) < 2:
condition = 0
self.board = cBoard
else:
self.reset()
self.updater()
turn += 1
color = "Black"
return turn, color, king, condition
elif turn % 2 == 0 and color == x.get_color(self.board[org[0]][org[1]]):
if (end[1],end[0]) in self.board[org[0]][org[1]].all_possible_movements_list and (org[0], org[1]) != (end[0], end[1]):
print('Black Moved')
if cBoard[org[0]][org[1]].pawn:
cBoard[org[0]][org[1]].first_movement = False
if (end[1],end[0]) in self.board[org[0]][org[1]].all_possible_movements_list:
trueend = end
cBoard[org[0]][org[1]].move_pos((end[0], end[1]))
cBoard[end[0]][end[1]] = cBoard[org[0]][org[1]]
cBoard[org[0]][org[1]] = 0
self.updater()
king_pos = self.return_king_pos()
try:
if king_pos[0] in self.board[trueend[0]][trueend[1]].all_possible_movements_list or king_pos[1] in self.board[trueend[0]][trueend[1]].all_possible_movements_list:
king = 1
else:
king = 0
except:
condition = 0
if len(king_pos) < 2:
condition = 0
self.board = cBoard
else:
self.reset()
self.updater()
turn += 1
color = "White"
return turn, color, king, condition
else:
pass
return turn, color, king, condition
except AttributeError:
pass
except TypeError:
pass