Untitled
unknown
plain_text
3 years ago
6.1 kB
10
Indexable
#Smart chess board project for SähköpajaK23.
#Code reads board change and compares it with saved board state and uses the change to show legal moves
import serial
import time
import chess
connected = False
while not connected:
try: #START serial communication with arduino
#arduinoData=serial.Serial('com6',115200,timeout=1)
connected = True
except Exception as e:
print("Connection error: "+str(e))
time.sleep(2)
def fenlistcreate(fenstring): # create a list of piece positions from fen string
fenlist = fenstring.split("/") #Splits FEN string into 8 pieces
fenlistout = [[] for _ in range(len(fenlist))] #Create a nested list
b = 0
a = 0
for n in fenlist:
for m in n:
try: #Tests if part of FEN string is a number or not.
num = int(m)
for empty in range(num):
fenlistout[a].append("0") #Input "0" into line on list for every empty space on board
b += 1
except:
fenlistout[a].append("1") #Input piece type into line on list.
b += 1
a += 1
return fenlistout #Returns 8x8 list of board used to check for change in board state.
fenstring = "rnbqkbnr/ppppppp1/7p/8/8/7P/PPPPPPP1/RNBQKBNR" # starting fen string
board = chess.Board(fenstring) #starts board engine
#board = chess.pgn.Game(fenstring)
coordlines = "abcdefgh" # used to create chess board coordinates from num coordinates
oldcoords = ""
n = 0
moved = False
while connected:
# data = arduinoData.readline() #reads arduino board state input Gets a string of 64 0/1
# data = data.strip()
# data = data.decode() #converts from bytes to string
if n < 2:
data = "1,1,1,1,1,1,1,1/1,1,1,1,1,1,1,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,0/1,1,1,1,1,1,1,1/1,1,1,1,1,1,1,1"
n += 1
else:
data = "1,1,1,1,1,1,1,1/1,1,1,1,1,1,1,0/0,0,0,0,0,0,0,1/0,0,0,0,0,0,0,0/0,0,0,0,0,0,0,0/0,0,0,0,0,0,0,0/1,1,1,1,1,1,1,1/1,1,1,1,1,1,1,1"
print(n)
olddata = data
ledmatrix = [[0 for j in range(8)] for i in range(8)]
if data != "":
boardfen = board.epd()
fenstring = boardfen.split()[0]
print(fenstring)
numlist = data.split("/")
sublist = [line.split(",") for line in numlist]
print(sublist)
fencomparelist = fenlistcreate(fenstring)
coord1 = 0
for line in sublist:
coord2 = 0
for tile in line:
if ((tile == fencomparelist[coord1][coord2])):
#DO NOTHING
placeholder = 0
else:
#CHANGE IN CHESSBOARD STATE:
coords = (coordlines[coord2]+str(coord1+1))
legallistcomparison = []
legalmoves = []
legmovesdict = {}
legalmoveslist = list(board.legal_moves) #get list of legal moves at the board state
for mov in legalmoveslist:
legalmoves.append(board.uci(mov))
for mov in legalmoves:
address = mov[:2]
value = mov[2:]
if address in legmovesdict: # create a dictionary of the legalmoves
legmovesdict[address] += "," + str(value)
else:
legmovesdict[address] = str(value)
print(legmovesdict)
try:
movelist = legmovesdict[oldcoords].split(",")
except:
movelist = []
if coords in legmovesdict: #test if change in board state is legal
row = int(coords[1]) - 1
col = ord(coords[0]) - 97
ledmatrix[row][col] = "L"
coordlegalmoves = legmovesdict[coords]
coordlegalmoves = coordlegalmoves.split(",")
for i in coordlegalmoves:
row = int(i[1]) - 1
col = ord(i[0]) - 97
ledmatrix[row][col] = "M" #put "M" into the ouput matrix where the piece can be moves to legally.
print("Legal move: " + coords) #For debugging
oldcoords = coords
#TBD output legal move list to Arduino and display the moves on the board.
elif (coords in movelist):
move_to_make = chess.Move.from_uci(oldcoords + coords)
if move_to_make in legalmoveslist:
board.push(move_to_make)
print("Move made: " + move_to_make.uci())
else:
print("Error: Move not in legal moves list")
else:
print("Illegal move: " + coords) #For debugging
row = int(coords[1]) - 1
col = ord(coords[0]) - 97
ledmatrix[row][col] = "I"
print(coords[1]+coords[0]+"I")
#TBD output illegal move action to Arduino. Show that move is illegal.
coord2 += 1
coord1 += 1
for row in ledmatrix[::-1]:
print(row)
output_string = ""
for row in ledmatrix:
row_string = ""
for val in row:
row_string += str(val) + ","
output_string += row_string
output_string = output_string[:-1]
#print(output_string)
# print the resulting output string
print(board) #For debugging
time.sleep(1) #Not required. Slows code down for debuggingEditor is loading...