-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathboard.py
170 lines (147 loc) · 5.01 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
"""
This module contains functions relating to the creation and access
of the game board.
"""
import pieces
from square import Square
import globVar
import utils
grid = []
def populate():
place = 0
global grid
grid = []
# initialize global variables
resetGlobal()
# fill grid with empty Squares
for i in range(16):
grid.append([])
for j in range(16):
grid[i].append(Square(False, "b", pieces.Piece("none", "none"), i, j))
# alternate colors
for i in range(8):
if (i % 2 == 0):
place = 0
for j in range(8):
grid[i][place].pieceStatus = False
grid[i][place].color = "white"
grid[i][place].row = i
grid[i][place].col = place
grid[i][place + 1].pieceStatus = False
grid[i][place + 1].color = "black"
grid[i][place + 1].row = i
grid[i][place + 1].col = (place + 1)
place += 2
else:
place = 0
for k in range(8):
grid[i][place].pieceStatus = False
grid[i][place].color = "black"
grid[i][place].row = i
grid[i][place].col = place
grid[i][place + 1].pieceStatus = False
grid[i][place + 1].color = "white"
grid[i][place + 1].row = i
grid[i][place + 1].col = (place + 1)
place += 2
# Fill board with pieces
# black
plr = "b"
Grid(0,0).piece = pieces.Rook(plr, "rook")
Grid(0,1).piece = pieces.Knight(plr, "knight")
Grid(0,2).piece = pieces.Bishop(plr, "bishop")
Grid(0,3).piece = pieces.Queen(plr, "queen")
Grid(0,4).piece = pieces.King(plr, "king")
Grid(0,5).piece = pieces.Bishop(plr, "bishop")
Grid(0,6).piece = pieces.Knight(plr, "knight")
Grid(0,7).piece = pieces.Rook(plr, "rook")
for i in range(8):
Grid(1,i).piece = pieces.Pawn(plr, "pawn")
Grid(1,i).piece.firstMove = True
globVar.firstPawns.append(Grid(1,i).piece)
# set pieceStatus and piece_ID for black pieces
piece_ID = 0
for i in range(2):
for j in range(8):
Grid(i,j).pieceStatus = True
Grid(i,j).piece.label = piece_ID
piece_ID += 1
globVar.b_pieces.append(Grid(i,j).piece)
# white
plr = "W"
Grid(7,0).piece = pieces.Rook(plr, "rook")
Grid(7,1).piece = pieces.Knight(plr, "knight")
Grid(7,2).piece = pieces.Bishop(plr, "bishop")
Grid(7,3).piece = pieces.Queen(plr, "queen")
Grid(7,4).piece = pieces.King(plr, "king")
Grid(7,5).piece = pieces.Bishop(plr, "bishop")
Grid(7,6).piece = pieces.Knight(plr, "knight")
Grid(7,7).piece = pieces.Rook(plr, "rook")
for i in range(8):
Grid(6,i).piece = pieces.Pawn(plr, "pawn")
Grid(6,i).piece.firstMove = True
globVar.firstPawns.append(Grid(6,i).piece)
# set pieceStatus and assign ID to white pieces
place = 6
piece_ID = 0
for i in range(2):
i = place
for j in range(8):
Grid(i,j).pieceStatus = True
Grid(i,j).piece.label = piece_ID
piece_ID += 1
globVar.w_pieces.append(Grid(i,j).piece)
place += 1
# set pieceStatus to false for the rest
place = 2
for i in range(4):
i = place
for j in range(8):
Grid(i,j).pieceStatus = False
# copy square coordinates to pieces
for i in range(8):
for j in range(8):
if Grid(i,j).pieceStatus:
Grid(i,j).piece.row = Grid(i,j).row
Grid(i,j).piece.col = Grid(i,j).col
# Clip board to 8 x 8
while len(grid) > 8:
grid.pop()
for i in range(8):
while len(grid[i]) > 8:
grid[i].pop()
# return reference to grid
def Grid(row, col):
return grid[row][col]
def resetGlobal():
globVar.player = "W"
globVar.playerCount = 0
globVar.w_NumPieces = 16
globVar.b_NumPieces = 16
globVar.r_w_NumPieces = 1
globVar.r_b_NumPieces = 1
globVar.w_check = False
globVar.b_check = False
globVar.removed = False
globVar.removed_label = -1
globVar.removed_color = "none"
globVar.last_row = -1
globVar.last_col = -1
globVar.scanning = False
globVar.r_avail_Num = 1
globVar.r_w_pieces = [pieces.Pawn("none", "none")]
globVar.r_b_pieces = [pieces.Pawn("none", "none")]
globVar.r_avail = [Square(False, "none", pieces.Pawn("none","none"), -1, -1)]
globVar.p_w_Moves = []
globVar.p_b_Moves = []
globVar.p_w_Num = -1
globVar.p_b_Num = -1
globVar.w_pieces = []
globVar.b_pieces = []
globVar.firstPawns = []
# update grid with toSqr
def uGrid(pc):
global grid
# grid[toSqr.row][toSqr.col] = toSqr
grid[pc.row][pc.col].pieceStatus = True
grid[pc.row][pc.col].piece = pc