Reputation:
I want to program a chess GUI in Java. The user can enter a move and then I want the program to make a move. Therefore I'll use an UCI (Universal Chess Interface). An UCI is a terminal/command line application (I'm using Mac OS X and Terminal) that calculates the best move in a position. Now what I need to do is write and read to this terminal application.
For example:
I want the best move for a certain position, so I type:
"go
" (calculates the best move)
Let's say I get this answer:
"e2e4
" (means moving the Pawn (that's a piece in chess) from the e2-square to the e4-square)
Now I need to read that "e2e4" and then ask the user for his next move. So i kinda need to loop through these steps all the time until there is a checkmate: 1. Ask for a move 2. Calculate best response
I've already seen a lot of other StackOverflow questions asking the same question: How to run a command and get its output in Command Line/Terminal. But all answers only use one command, as for instance runtime.exec("ls");
But that's only one command. What I want is to enter a command, get the response, execute another command and so on, so basically I need to communicate with Mac OSX' Terminal app (alternating input and output). How do I accomplish that in Java?
Upvotes: 2
Views: 1738
Reputation: 372
Just look up the return type of Runtime.exec(). It returns a java.lang.Process.
It offers two methods for input and output streaming: getOutputStream() and getInputStream()
You can read and write to the opened Process for ex. with BufferedReaders / BufferedWriters.
For Example: (see also https://stackoverflow.com/a/14745245/2358582)
import java.io.*;
public class c {
public static void main(String[] args) throws Exception{
Process p = Runtime.getRuntime().exec("cmd");
BufferedReader inp = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
//communication
setUpStreamGobbler(p.getInputStream(), System.out);
out.write("cmd\n");
out.flush();
}
public static void setUpStreamGobbler(final InputStream is, final PrintStream ps) {
final InputStreamReader streamReader = new InputStreamReader(is);
new Thread(new Runnable() {
public void run() {
BufferedReader br = new BufferedReader(streamReader);
String line = null;
try {
while ((line = br.readLine()) != null) {
ps.println("process stream: " + line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
Outputs:
process stream: Microsoft Windows [Version 6.1.7601]
process stream: Copyright (c) 2009 Microsoft Corporation. Alle Rechte vorbehalte
n.
process stream:
process stream: P:\Java\ExcecuteFile>cmd
process stream: Microsoft Windows [Version 6.1.7601]
process stream: Copyright (c) 2009 Microsoft Corporation. Alle Rechte vorbehalte
n.
process stream:
Upvotes: 1
Reputation: 2399
I believe that this question might help you: I asked something similar, I hope that this will answer your question
Upvotes: 0
Reputation: 5647
You could execute the command that runs the chess program, and then check the command's output for the best move (using a Process
).
For example:
String move = null;
Process process = Runtime.getRuntime().exec("go");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getOutputStream()));
move = reader.readLine();
// 'move' now holds a reference to the chess program's output, or null if there was none.
Upvotes: 1