-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
398 lines (320 loc) · 13.5 KB
/
main.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# Imports modules to control raspberry pi GPIO
import RPi.GPIO as GPIO
import time
import charlieplexing
# Sets up Stockfish engine
from stockfish import Stockfish
stockfish = Stockfish("/home/pi/Downloads/a-stockf")
stockfish = Stockfish(parameters={"Threads": 2, "Minimum Thinking Time": 30})
stockfish.set_skill_level(1)
# 97 is the ASCII code for a, using this we can reduce this down to a numeric form
asciiA = 97
# Class to store piece attributes like colour and type
class Piece:
def __init__(self, colour, pieceType, icon):
self.colour = colour
self.pieceType = pieceType
self.icon = icon
# Class to group together piece initialisation
class Pieces:
def initialisePieces(self):
# Initalises the piece objects and passes their attributes
self.wp = Piece("white", "pawn", "♙")
self.wr = Piece("white", "rook", "♖")
self.wkn = Piece("white", "knight", "♞")
self.wb = Piece("white", "bishop", "♗")
self.wq = Piece("white", "queen", "♕")
self.wk = Piece("white", "king", "♔")
self.bp = Piece("black", "pawn", "♟")
self.br = Piece("black", "rook", "♜")
self.bkn = Piece("black", "knight", "♞")
self.bb = Piece("black", "bishop", "♝")
self.bq = Piece("black", "queen", "♛")
self.bk = Piece("black", "king", "♚")
# Class to store the current state and last known legal state of the board
class BoardRecord:
def __init__(self, p):
# Stack to store previous move
self.previousMoves = []
# Dictionaries linking the name of a piece with its object
self.whitePieceObjects = {"q": p.wq, "k": p.wk, "r": p.wr, "n": p.wkn, "b": p.wb, "p": p.wp}
self.blackPieceObjects = {"q": p.bq, "k": p.bk, "r": p.br, "n": p.bkn, "b": p.bb, "p": p.bp}
# Creates a 2D array to store piece locations
self.board = [[p.wr,p.wkn,p.wb,p.wq,p.wk,p.wb,p.wkn,p.wr],
[p.wp,p.wp,p.wp,p.wp,p.wp,p.wp,p.wp,p.wp],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[p.bp,p.bp,p.bp,p.bp,p.bp,p.bp,p.bp,p.bp],
[p.br,p.bkn,p.bb,p.bq,p.bk,p.bb,p.bkn,p.br]]
# Getter for board variable
def getBoard(self):
# Prints each row and converts objects into their assigned icons
for row in range(7, -1, -1):
rowToPrint = []
for square in self.board[row]:
if square != 0:
rowToPrint.append(square.icon)
else:
rowToPrint.append("0 ")
print("".join(rowToPrint))
return self.board
# Setter for board variable
# Takes two coordinates and moves a piece to it's new position
def setBoard(self, pieceFrom, pieceTo, move):
self.board[pieceTo[1]][pieceTo[0]] = self.board[pieceFrom[1]][pieceFrom[0]]
self.board[pieceFrom[1]][pieceFrom[0]] = 0
# Adds move made to the previousMoves array
if move != "castle":
self.previousMoves.append(move)
# Updates the moves made for the engine
stockfish.set_position(self.previousMoves)
# Function to change a piece type on the board
def promotePiece(self, position, colour, promoteTo):
if colour == "w":
self.board[position[1]][position[0]] = self.whitePieceObjects[promoteTo]
elif colour == "b":
self.board[position[1]][position[0]] = self.blackPieceObjects[promoteTo]
# Stores the pin numbers used for the rows and columns
# Index + 1 is the row number
rowPins = [23, 18, 11, 9, 10, 22, 27, 17]
columnPins = [21, 20, 16, 12, 7, 8, 25, 24]
# Sets the pins to be inputs or outputs
def pinSetup():
GPIO.setmode(GPIO.BCM)
for pin in rowPins:
GPIO.setup(pin, GPIO.OUT)
for pin in columnPins:
GPIO.setup(pin, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
# Function to turn off all the row pins to make sure none are accidently left on
def resetRowPins():
for pin in rowPins:
GPIO.output(pin, 0)
# Function to return the position of pieces in a 2D array
def scanCurrentBoard():
board = []
# Provides power to rows one by one and checks columns for input
# Stores the output to board array
for row in range(8):
board.append([])
GPIO.output(rowPins[row], 1)
for column in range(8):
board[row].append(GPIO.input(columnPins[column]))
GPIO.output(rowPins[row], 0)
return board
'''
Used for development purposes, delete in final
'''
for i in range(7, -1, -1):
print(" ".join(map(str,board[i])))
print("\n\n")
# Takes the current state of the board and repeatedly checks
# for a change. Once found will return the position of the change
# Takes an optional paramater if we are checking for movement to a square
# to prevent the piece being detected twice
"""
Could optimise with wait for edge?
"""
def reportChange(moveFrom = None):
resetRowPins()
oldBoard = scanCurrentBoard()
while True:
newBoard = scanCurrentBoard()
if newBoard != oldBoard:
for row in range(8):
if newBoard[row] != oldBoard[row]:
for column in range(8):
if newBoard[row][column] != oldBoard[row][column]:
if moveFrom != [column, row]:
resetRowPins()
return [column, row]
# Function that waits for a square to turn from on to off
def detectFallingAtPosition(coordinates):
GPIO.output(rowPins[coordinates[1]], 1)
GPIO.add_event_detect(columnPins[coordinates[0]], GPIO.FALLING)
numberRows = [8, 7, 6, 5, 4, 3, 2, 1]
numberCols = [9, 10, 11, 12, 13, 14, 15, 16]
while True:
# Lights up position
charlieplexing.turnOn(numberRows[coordinates[1]])
charlieplexing.turnOn(numberCols[coordinates[0]])
# Breaks loop once piece is picked up
if GPIO.event_detected(columnPins[coordinates[0]]):
GPIO.remove_event_detect(columnPins[coordinates[0]])
charlieplexing.turnOn(0)
break
#GPIO.wait_for_edge(columnPins[coordinates[0]], GPIO.FALLING)
GPIO.output(rowPins[coordinates[1]], 0)
# Function that waits for a square to turn from off to on
def detectRisingAtPosition(coordinates):
GPIO.output(rowPins[coordinates[1]], 1)
GPIO.add_event_detect(columnPins[coordinates[0]], GPIO.RISING)
numberRows = [8, 7, 6, 5, 4, 3, 2, 1]
numberCols = [9, 10, 11, 12, 13, 14, 15, 16]
while True:
# Lights up position
charlieplexing.turnOn(numberRows[coordinates[1]])
charlieplexing.turnOn(numberCols[coordinates[0]])
# Breaks loop once piece is placed down
if GPIO.event_detected(columnPins[coordinates[0]]):
GPIO.remove_event_detect(columnPins[coordinates[0]])
charlieplexing.turnOn(0)
break
#GPIO.wait_for_edge(columnPins[coordinates[0]], GPIO.FALLING)
GPIO.output(rowPins[coordinates[1]], 0)
# Will return True if there is currently a piece at the given coordinates
def isOccupied(coordinates):
board = currentBoard.getBoard()
if board[coordinates[1]][coordinates[0]] != 0:
return True
else:
return False
# Function to take a move generated by the computer
# and update the board if there has been a promotion
def checkForPromotion(move, toMove):
if len(move) == 5:
currentBoard.promotePiece(toMove, computerColour, move[-1])
print(f"Piece is now {move[-1]}")
# Function to determine if a player can promote a piece, if true then
# the player is asked to which piece he would like to promote
# Input is then returned
def checkForPromotionOpportunity(moveTo, moveFrom):
board = currentBoard.getBoard()
promoteTo = ""
# Checks for white pawns in black back line
if moveTo[1] == 7 and board[moveFrom[1]][moveFrom[0]].pieceType == "pawn":
promoteTo = input("What would you like to promote to: ")
# Checks for black pawns in white back line
if moveTo[1] == 0 and board[moveFrom[1]][moveFrom[0]].pieceType == "pawn":
promoteTo = input("What would you like to promote to: ")
# If promotion is not available then "" is returned
return promoteTo
# Function to detect if AI or player has castled and tells the user to move the castle
def checkForCastle(move, fromMove):
board = currentBoard.getBoard()
if board[fromMove[1]][fromMove[0]].pieceType == "king":
# Detects which castling move has been made and moves castle
if move == "e1c1":
currentBoard.setBoard([0, 0], [3, 0], "castle")
moveComputerPieces([0, 0], [3, 0], "a1c1")
elif move == "e1g1":
currentBoard.setBoard([7, 0], [5, 0], "castle")
moveComputerPieces([7, 0], [5, 0], "h1f1")
elif move == "e8c8":
currentBoard.setBoard([0, 7], [3, 7], "castle")
moveComputerPieces([0, 7], [3, 7], "a8c8")
elif move == "e8g8":
currentBoard.setBoard([7, 7], [5, 7], "castle")
moveComputerPieces([7, 7], [5, 7], "h8f8")
# Function to make sure that the pieces are where they're meant to be
# Recursively calls itself until all pieces are in the correct position
def checkBoardIsLegal():
board = currentBoard.getBoard()
for row in range(8):
resetRowPins()
GPIO.output(rowPins[row], 1)
for column in range(8):
# First checks for pieces where no pieces should be
if board[row][column] == 0 and GPIO.input(columnPins[column]) == 1:
print(f"Remove piece from: {convertToChessNotation([column, row])}")
detectFallingAtPosition([column, row])
resetRowPins()
checkBoardIsLegal()
return
# Then checks for empty spaces where pieces should be
elif board[row][column] != 0 and GPIO.input(columnPins[column]) == 0:
print(f"Place {board[row][column].colour.capitalize()} {board[row][column].pieceType.capitalize()} on {convertToChessNotation([column, row])}")
detectRisingAtPosition([column, row])
resetRowPins()
checkBoardIsLegal()
return
resetRowPins()
# Function to tell player where to move pieces
def moveComputerPieces(moveFrom, moveTo, move):
# Tells user which piece to pick up, checks for piece removal
print(f"Move piece from {move[0:2]}")
detectFallingAtPosition(moveFrom)
# Checks if moveTo position is already occupied, if so tells user to remove it
if isOccupied(moveTo):
print(f"Remove piece from {move[2:]}")
detectFallingAtPosition(moveTo)
# Tells user where to move piece
print(f"Move piece to {move[2:]}")
detectRisingAtPosition(moveTo)
print("Thank you!")
resetRowPins()
# Function to convert move to chess notation (for Stockfish)
# Example: "a2"
def convertToChessNotation(move):
return chr(ord('a') + move[0]) + str(move[1] + 1)
# Function to convert chess notation to move
# Example: [1, 1]
def convertToMove(chessNotation):
return [ord(chessNotation[0]) - asciiA, int(chessNotation[1]) - 1]
# Function to obtain the move a player makes
def getPlayerMove():
moveFrom = reportChange()
print(f"From: {moveFrom}")
moveTo = reportChange(moveFrom)
print(f"To {moveTo}")
# Checks if a pawn can be promoted
promotion = checkForPromotionOpportunity(moveTo, moveFrom)
if isOccupied(moveTo):
print("Place down piece")
detectRisingAtPosition(moveTo)
# Adds the from move, to move and promotion if available so the AI can understand it
move = convertToChessNotation(moveFrom) + convertToChessNotation(moveTo) + promotion
# Checks if player has castled
checkForCastle(move, moveFrom)
if stockfish.is_move_correct(move):
print("legal shmegle")
currentBoard.setBoard(moveFrom, moveTo, move)
# If player has promoted a piece then board is updated
if promotion != "":
promotePiece(moveTo, playerColour, promotion)
currentBoard.getBoard()
else:
print("Not a legal move")
charlieplexing.allFlash()
checkBoardIsLegal()
getPlayerMove()
return
# Function to get the AI to generate a move
def generateMove():
move = stockfish.get_best_move()
# Splits move into where it's moving from and where it's moving to
# Converts letters into their corresponding alphabet
# Both arrays have: column then row
fromMove = convertToMove(move[0:2])
toMove = convertToMove(move[2:4])
moveComputerPieces(fromMove, toMove, move)
# Checks if player has castled
checkForCastle(move, fromMove)
# Updates board
currentBoard.setBoard(fromMove, toMove, move)
checkForPromotion(move, toMove)
p = Pieces()
p.initialisePieces()
currentBoard = BoardRecord(p)
computerColour = "b"
playerColour = "w"
pinSetup()
m = input("")
resetRowPins()
checkBoardIsLegal()
while True:
getPlayerMove()
checkBoardIsLegal()
evaluation = stockfish.get_evaluation()
print(evaluation)
if evaluation["type"] == 'mate' and evaluation["value"] == 0:
while True:
charlieplexing.slide("fast")
generateMove()
checkBoardIsLegal()
evaluation = stockfish.get_evaluation()
print(evaluation)
if evaluation["type"] == 'mate' and evaluation["value"] == 0:
while True:
charlieplexing.slide("fast")