-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess.py
176 lines (161 loc) · 4.62 KB
/
chess.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
171
172
173
174
175
176
goodBoard = {
'1h': 'bking',
'6c': 'wqueen',
'2g': 'bbishop',
'5h': 'bqueen',
'3e': 'wking'}
badBoardPrefix = {'1h': 'bking', '6c': 'wqueen',
'2g': 'bbishop', '5h': 'queen', '3e': 'wking'}
badBoardPiece = {'1h': 'bking', '6c': 'wqueen',
'2g': 'bbishop', '5h': 'wace', '3e': 'wking'}
badBoardPawnCount = {'1h': 'bking',
'6c': 'wqueen',
'2g': 'bbishop',
'5h': 'wace',
'3e': 'wking',
'2a': 'wpawn',
'2b': 'wpawn',
'2c': 'wpawn',
'2d': 'wpawn',
'2e': 'wpawn',
'2f': 'wpawn',
'3g': 'wpawn',
'2h': 'wpawn',
'3h': 'wpawn'}
badBoardGridNumber = {'1h': 'bking',
'6c': 'wqueen',
'2g': 'bbishop',
'9h': 'wpawn',
'3e': 'wking'}
badBoardGridLetter = {'1h': 'bking',
'6c': 'wqueen',
'2g': 'bbishop',
'3j': 'wpawn',
'3e': 'wking'}
badBoardKings = {
'1h': 'bking',
'2h': 'bking',
'6c': 'wqueen',
'2g': 'bbishop',
'5h': 'bqueen',
'3e': 'wking'}
goodBoardStartingPositions = {
'1a': 'wrook',
'1b': 'wknight',
'1c': 'wbishop',
'1d': 'wqueen',
'1e': 'wking',
'1f': 'wbishop',
'1g': 'wknight',
'1h': 'wrook',
'2a': 'wpawn',
'2b': 'wpawn',
'2c': 'wpawn',
'2d': 'wpawn',
'2e': 'wpawn',
'2f': 'wpawn',
'2g': 'wpawn',
'2h': 'wpawn',
'8a': 'brook',
'8b': 'bknight',
'8c': 'bbishop',
'8d': 'bqueen',
'8e': 'bking',
'8f': 'bbishop',
'8g': 'bknight',
'8h': 'brook',
'7a': 'bpawn',
'7b': 'bpawn',
'7c': 'bpawn',
'7d': 'bpawn',
'7e': 'bpawn',
'7f': 'bpawn',
'7g': 'bpawn',
'7h': 'bpawn',
}
badBoardStartingPositionsExtra = {
'1a': 'wrook',
'3a': 'wrook',
'1b': 'wknight',
'1c': 'wbishop',
'1d': 'wqueen',
'1e': 'wking',
'1f': 'wbishop',
'1g': 'wknight',
'1h': 'wrook',
'2a': 'wpawn',
'2b': 'wpawn',
'2c': 'wpawn',
'2d': 'wpawn',
'2e': 'wpawn',
'2f': 'wpawn',
'2g': 'wpawn',
'2h': 'wpawn',
'8a': 'brook',
'8b': 'bknight',
'8c': 'bbishop',
'8d': 'bqueen',
'8e': 'bking',
'8f': 'bbishop',
'8g': 'bknight',
'8h': 'brook',
'7a': 'bpawn',
'7b': 'bpawn',
'7c': 'bpawn',
'7d': 'bpawn',
'7e': 'bpawn',
'7f': 'bpawn',
'7g': 'bpawn',
'7h': 'bpawn',
}
def isValidChessBoard(board):
isValid = True
validNumbers = ['1', '2', '3', '4', '5', '6', '7', '8']
validLetters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
pieceCount = {}
for coord, value in board.items():
color = value[0]
piece = value[1:]
pieceCount.setdefault(color, {})
pieceCount[color].setdefault(piece, 0)
pieceCount[color][piece] = pieceCount[color][piece] + 1
coordNumber = coord[0]
coordLetter = coord[1]
# all pieces must be on a valid space from 1a to 8h
if coordNumber not in validNumbers:
isValid = False
if coordLetter not in validLetters:
isValid = False
validColorPrefix = ['b', 'w']
validPieces = ['pawn', 'knight', 'bishop', 'rook', 'queen', 'king']
totals = {}
for key, pieces in pieceCount.items():
# limit of 1 bking and 1 wking
if pieces.get('king', 0) > 1:
isValid = False
# at most 8 pawns
if pieces.get('pawn', 0) > 8:
isValid = False
# each piece name begins with a w or b, followed by 'pawn', 'knight', 'bishop', 'rook', 'queen', 'king'
if key not in validColorPrefix:
isValid = False
for piece, count in pieces.items():
if piece not in validPieces:
isValid = False
totals.setdefault(key, 0)
totals[key] = totals[key] + count
for total in totals.values():
# at most 16 pieces per player
if total > 16:
isValid = False
# must return boolean
return isValid
print(isValidChessBoard(goodBoard))
print(isValidChessBoard(badBoardPrefix))
print(isValidChessBoard(badBoardPiece))
print(isValidChessBoard(badBoardGridNumber))
print(isValidChessBoard(badBoardGridLetter))
print(isValidChessBoard(badBoardKings))
print(isValidChessBoard(badBoardPawnCount))
print(isValidChessBoard(goodBoardStartingPositions))
print(isValidChessBoard(badBoardStartingPositionsExtra))