I have had it in my head for a while to build a robotic chess board, a chess board where I play against a computer that controls a robotic arm of some sort that moves real pieces on a real board. The "thinking" will be done on a Raspberry Pi computer, which I bought this week as a graduate gift to myself for having finished my PhD program. The arm will be controlled by an Arduino microcontroller. I have already learned how to get Arduino and Raspberry Pi to communicate with each other by reading Simon Monk's nice book Programming the Raspberry Pi: Getting Started with Python. The Arduino and Raspberry Pi communicate nicely though the serial port using the PySerial package. I have tested this out and it works wonderfully. I must write the Arduino code on my Mac though because the Arduino software released for the Linux Debian operating system is a very old version.
For the chess engine, I have decided to use Stockfish, which uses the Universal Chess Interface. It is consistently rate one of the strongest chess engines. The creators of Stockfish have not provided any documentation for their engine, and the Universal Chess Interface was nontrivial to decipher. Therefore, it took me some time to figure out how to communicate with Stockfish through the terminal.
Tonight, I was able to write a proof-of-concept Python program which let me play chess with Stockfish through the Python shell. This is the first step in writing the software for a USB chessboard. I have included the Python code below. Of course, the Stockfish engine must be installed first.
For the chess engine, I have decided to use Stockfish, which uses the Universal Chess Interface. It is consistently rate one of the strongest chess engines. The creators of Stockfish have not provided any documentation for their engine, and the Universal Chess Interface was nontrivial to decipher. Therefore, it took me some time to figure out how to communicate with Stockfish through the terminal.
Tonight, I was able to write a proof-of-concept Python program which let me play chess with Stockfish through the Python shell. This is the first step in writing the software for a USB chessboard. I have included the Python code below. Of course, the Stockfish engine must be installed first.
Python Code
#This code talks to the Stockfish Chess Engine. Entries are to be made in algebraic
#chess notation (eg. e2e4). Stockfish is set to think for a maximum of 1 sec.
import sys
chess = r'/Applications/stockfish-4-mac/Mac/stockfish-4-64'.split()['linux' in sys.platform]
import subprocess as S
getprompt = 'isready'
done= 'readyok'
proc= S.Popen(chess, stdin=S.PIPE, stdout=S.PIPE, bufsize=1, universal_newlines=True)
print(proc.stdout.readline().strip())
proc.stdin.write('uci\n')
while True :
text = proc.stdout.readline().strip()
print(text)
if text == "uciok":
break
print('Choose skill level (0-20):')
skillLevel=input()
proc.stdin.write('setoption name Skill Level value '+skillLevel+'\n')
proc.stdin.write('ucinewgame\n')
moveList='position startpos moves '
checkmate=False
while checkmate==False:
print('Enter move:')
move=input()
moveList=moveList+move+' '
proc.stdin.write(moveList+'\n')
proc.stdin.write('go movetime 1000\n')
print('Computer moves:')
while True :
text = proc.stdout.readline().strip()
if text[0:8] == 'bestmove':
cpuMove=text[9:13]
print(cpuMove)
moveList=moveList+cpuMove+' '
break
#This code talks to the Stockfish Chess Engine. Entries are to be made in algebraic
#chess notation (eg. e2e4). Stockfish is set to think for a maximum of 1 sec.
import sys
chess = r'/Applications/stockfish-4-mac/Mac/stockfish-4-64'.split()['linux' in sys.platform]
import subprocess as S
getprompt = 'isready'
done= 'readyok'
proc= S.Popen(chess, stdin=S.PIPE, stdout=S.PIPE, bufsize=1, universal_newlines=True)
print(proc.stdout.readline().strip())
proc.stdin.write('uci\n')
while True :
text = proc.stdout.readline().strip()
print(text)
if text == "uciok":
break
print('Choose skill level (0-20):')
skillLevel=input()
proc.stdin.write('setoption name Skill Level value '+skillLevel+'\n')
proc.stdin.write('ucinewgame\n')
moveList='position startpos moves '
checkmate=False
while checkmate==False:
print('Enter move:')
move=input()
moveList=moveList+move+' '
proc.stdin.write(moveList+'\n')
proc.stdin.write('go movetime 1000\n')
print('Computer moves:')
while True :
text = proc.stdout.readline().strip()
if text[0:8] == 'bestmove':
cpuMove=text[9:13]
print(cpuMove)
moveList=moveList+cpuMove+' '
break