问题
I'm looking for a method to communicate with a chess engine with uci protocol using matlab. The chess engine is rybka and its an exe file. When I run the rybka.exe, I can communicate via dos command prompt but I want do that via matlab. I think I have to use streampipe and stdin and stdout but I don't know how use it.
I found this code in Python and it works fine but I'm looking for a matlab version:
import subprocess, time
engine = subprocess.Popen(
'a.exe',
universal_newlines=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)
def put(command):
print('\nyou:\n\t'+command)
engine.stdin.write(command+'\n')
def get():
# using the 'isready' command (engine has to answer 'readyok')
# to indicate current last line of stdout
engine.stdin.write('isready\n')
print('\nengine:')
while True:
text = engine.stdout.readline().strip()
if text == 'readyok':
break
if text !='':
print('\t'+text)
回答1:
If it's just a case of using the exe file and capturing the output you can use the system
command to capture the output. For example I can run the system's dir
command in the following way:
>> [~, output] = system('dir')
output =
ant ant.cmd antRun.bat antenv.cmd envset.cmd runant.pl
ant.bat antRun antRun.pl complete-ant-cmd.pl lcp.bat runant.py
Documentation: http://www.mathworks.com/help/matlab/ref/system.html
See also: Running C program's executable from Matlab and getting the output
来源:https://stackoverflow.com/questions/16460383/how-to-communicate-with-uci-protocol-using-matlab